Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 723. C# / Visual C++ - Control Flow Guard (CFG) 활성화 [링크 복사], [링크+제목 복사],
조회: 6748
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일

C# / Visual C++ - Control Flow Guard (CFG) 활성화

CFG라는 기술이 있군요. ^^

Control Flow Guard for platform security
; https://learn.microsoft.com/en-us/windows/win32/secbp/control-flow-guard

기존의 /GS (Buffer Security Check), Data Execution Prevention (DEP), Address Space Layout Randomization (ASLR)로도, 시중에서 흔하게 구할 수 있는 해킹 서적의 예제들이 실습이 안 될 정도로 보안이 강화됐는데요, 그런데 이제는 Indirect Call, (쉬운 예로) 코드상 함수 포인터를 이용한 호출까지도 대상 주소가 적법한지를 체크하는 기능이 추가됐습니다.

이를 구현하기 위해, 컴파일러와 운영체제 양측의 지원이 필요한데요, Visual C++ 컴파일러의 경우 "C/C++ > Code Generation > Control Flow Guard" 옵션을 활성화하면 됩니다. 유의해야 할 점은, Release 모드에서만 켜는 것이 좋은데, Debug 모드에서 키게 되면 다른 옵션(Program Database for Edit And Continue)과의 충돌로 빌드가 안 됩니다.

cl : command line  error D8016: '/ZI' and '/guard:cf' command-line options are incompatible

(EnC 옵션이 필요하지 않다면 Debug에서도 /Zi + /guard:cf 옵션을 사용하면 됩니다.)

구체적으로 어떻게 동작하는지에 대해서는, 문서에 있는 한 장의 그림으로 잘 설명하고 있습니다.

cfg_1.jpg

이로 인해 발생하는 뚜렷한 차이점이라면, cfg가 켜져 있는 경우 대상 함수를 호출하기 전에 미리 (fail-fast) 실패해버린다는 점입니다.




실제로 Visual C++ 컴파일러가 어떻게 대응하는지를 간단하게 살펴보겠습니다. ^^

#include <Windows.h>

typedef BOOL(WINAPI* BEEPPROC)(DWORD, DWORD);

int main()
{
    BEEPPROC pfn = (BEEPPROC)GetProcAddress(LoadLibrary(L"kernel32.dll"), "Beep");
    pfn(1000, 1000);
}

위의 예제를 cfg 옵션 없이 컴파일하면 기계어로 다음과 같은 코드가 생성됩니다.

    11:     pfn(1000, 1000);
00007FF6A3D114E4 BA E8 03 00 00       mov         edx,3E8h  
00007FF6A3D114E9 B9 E8 03 00 00       mov         ecx,3E8h  
00007FF6A3D114EE FF 54 24 20          call        qword ptr [pfn]  
00007FF6A3D114F2 90                   nop  

call [pfn], 즉 indirect call을 그대로 호출하고 있는데요, 이를 cfg 옵션을 켜고 컴파일하면 다음과 같이 변경됩니다.

    11:     pfn(1000, 1000);
00007FF62CC11EC4 48 8B 44 24 20       mov         rax,qword ptr [pfn]  
00007FF62CC11EC9 48 89 44 24 28       mov         qword ptr [rsp+28h],rax  
00007FF62CC11ECE BA E8 03 00 00       mov         edx,3E8h  
00007FF62CC11ED3 B9 E8 03 00 00       mov         ecx,3E8h  
00007FF62CC11ED8 48 8B 44 24 28       mov         rax,qword ptr [rsp+28h]  
00007FF62CC11EDD FF 15 2D 01 01 00    call        qword ptr [__guard_dispatch_icall_fptr (07FF62CC22010h)]  
00007FF62CC11EE3 90                   nop  

(아하... 어쩌다 windbg로 역어셈블 코드를 보면 나오던 __guard_dispatch_icall_fptr이 바로 cfg 옵션으로 추가된 코드였었던 것입니다.)

문제를 재현하기 위해 코드를 다음과 같이 바꾸고 실행하면,

BEEPPROC pfn = (BEEPPROC)GetProcAddress(LoadLibrary(L"kernel32.dll"), "Beep");
pfn = (BEEPPROC)0x00007FF091AD03A0;
pfn(1000, 1000);

cfg가 활성화된 경우 LdrpDispatchUserCallTarget에서 예외가 발생합니다.

    ntdll.dll!LdrpDispatchUserCallTarget()  Unknown
>    ConsoleApplication1.exe!main() Line 12  C++
    ConsoleApplication1.exe!invoke_main() Line 79   C++
...[생략]...

이때의 이벤트 로그를 보면, "Exception code: 0xc0000409 (STATUS_FAIL_FAST_EXCEPTION)"으로 나옵니다. 반면 cfg가 꺼진 상태라면 call stack이 이렇게 나오는데요,

    00007ff091ad03a0()  Unknown
>    ConsoleApplication1.exe!main() Line 12  C++
    ConsoleApplication1.exe!invoke_main() Line 79   C++

그러니까, 명시된 주소로 call을 했고 그 내부의 기계어 코드가 정상적이지 않을 것이므로 AV(Access Violation) 예외가 발생하게 됩니다. 따라서 이벤트 로그에도 "Exception code: 0xc0000005"로 나옵니다.

위의 경우에는 임의의 주소를 지정했지만, 실제로는 (dumpbin으로 확인할 수 있는) Guard CF Function Table에 등록돼 있지 않은 주소는 모두 invalid 주소로 판정하고 실패하는 것으로 보입니다. 보다 구체적인 정보는 blackhat 문서를 참고하세요.)




참고로 .NET App은 어떻게 하고 있을까요? ^^

우선, dotnet.exe 및 관련 바이너리(clrjt.dll)을 보면 CFG 컴파일러 스위치가 적용된 것을 확인할 수 있습니다.

C:temp> dumpbin /headers /loadconfig "C:\Program Files\dotnet\dotnet.exe"
Microsoft (R) COFF/PE Dumper Version 14.40.33811.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file C:\Program Files\dotnet\dotnet.exe

PE signature found

File Type: EXECUTABLE IMAGE

...[생략]...

OPTIONAL HEADER VALUES
             20B magic # (PE32+)
           14.40 linker version
            DA00 size of code
           14200 size of initialized data
               0 size of uninitialized data
            9790 entry point (0000000140009790)
            1000 base of code
       140000000 image base (0000000140000000 to 0000000140024FFF)
            1000 section alignment
             200 file alignment
            6.00 operating system version
            0.00 image version
            6.00 subsystem version
               0 Win32 version
           25000 size of image
             400 size of headers
           2671E checksum
               3 subsystem (Windows CUI)
            C160 DLL characteristics
                   High Entropy Virtual Addresses
                   Dynamic base
                   NX compatible
                   Control Flow Guard
                   Terminal Server Aware
          180000 size of stack reserve
            1000 size of stack commit
          100000 size of heap reserve
            ...[생략]...

  Section contains the following load config:

            00000140 size
                   0 time date stamp
                0.00 Version
            ...[생략]...
    000000014000F328 Guard CF address of check-function pointer
    000000014000F338 Guard CF address of dispatch-function pointer
    000000014000F420 Guard CF function table
                  1E Guard CF function count
            10417500 Guard Flags
                       CF instrumented
                       FID table present
                       Protect delayload IAT
                       Delayload IAT in its own section
                       Export suppression info present
                       Long jump target table present
                       EH Continuation table present
                0000 Code Integrity Flags
                0000 Code Integrity Catalog
            00000000 Code Integrity Catalog Offset
            00000000 Code Integrity Reserved
    0000000000000000 Guard CF address taken IAT entry table
                   0 Guard CF address taken IAT entry count
    0000000000000000 Guard CF long jump target table
                   0 Guard CF long jump target count
    0000000000000000 Dynamic value relocation table
    0000000000000000 Hybrid metadata pointer
    0000000000000000 Guard RF address of failure-function
    0000000000000000 Guard RF address of failure-function pointer
            00000000 Dynamic value relocation table offset
                0000 Dynamic value relocation table section
                0000 Reserved2
    0000000000000000 Guard RF address of stack pointer verification function pointer
            00000000 Hot patching table offset
                0000 Reserved3
    0000000000000000 Enclave configuration pointer
    0000000140013034 Volatile metadata pointer
    000000014000F3F0 Guard EH continuation table
                   9 Guard EH continuation count
    000000014000F330 Guard XFG address of check-function pointer
    000000014000F340 Guard XFG address of dispatch-function pointer
    000000014000F348 Guard XFG address of dispatch-table-function pointer
    000000014000F350 CastGuard OS determined failure mode
    000000014000F358 Guard memcpy function pointer

            ...[생략]...

c:\temp> dumpbin /headers /loadconfig "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\clrjit.dll"
Microsoft (R) COFF/PE Dumper Version 14.40.33811.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\clrjit.dll

PE signature found

File Type: DLL

            ...[생략]...

OPTIONAL HEADER VALUES
             20B magic # (PE32+)
            ...[생략]...
               2 subsystem (Windows GUI)
            4160 DLL characteristics
                   High Entropy Virtual Addresses
                   Dynamic base
                   NX compatible
                   Control Flow Guard
          100000 size of stack reserve
            ...[생략]...

        00000001 extended DLL characteristics
                   CET compatible

  Section contains the following load config:

            00000140 size
            ...[생략]...
    0000000000000000 Edit List
    000000018019F040 Security Cookie
    0000000180166470 Guard CF address of check-function pointer
    0000000180166480 Guard CF address of dispatch-function pointer
    0000000180166550 Guard CF function table
                  E7 Guard CF function count
            10417500 Guard Flags
                       CF instrumented
                       FID table present
                       Protect delayload IAT
                       Delayload IAT in its own section
                       Export suppression info present
                       Long jump target table present
                       EH Continuation table present
                0000 Code Integrity Flags
                0000 Code Integrity Catalog
            ...[생략]...

하지만, JIT 컴파일이 된 코드에는 CFG가 적용되지 않은 듯합니다.

namespace ConsoleApp1;

internal class Program
{
    static unsafe void Main(string[] args)
    {
        delegate* unmanaged[Stdcall]<int, bool, int> sleepExFunc = (delegate* unmanaged[Stdcall]<int, bool, int>)0x00007FF091AD03A0;
        sleepExFunc(2000, false);
    }
}

위의 코드를 실행해 보면, 0x00007FF091AD03A0 주소에서 AV(0xc0000005) 예외가 발생합니다.

물론, 닷넷 코드에서 저렇게 native 주소를 직접 호출하는 코드를 넣는 경우는 거의 없을 것이므로, CFG 여부에 따른 차이가 크진 않을 것입니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 9/19/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2024-09-17 07시42분
The case of the fail-fast crashes coming from the power management system
; https://devblogs.microsoft.com/oldnewthing/20240913-00/?p=110257

---------------------------

A quick introduction to return address protection technologies (Intel Control-flow Enforcement Technology (CET))
; https://devblogs.microsoft.com/oldnewthing/20241015-00/?p=110374

Effects of classic return address tricks on hardware-assisted return address protection
; https://devblogs.microsoft.com/oldnewthing/20241016-00/?p=110378
정성태

... 76  77  78  [79]  80  81  82  83  84  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
11956정성태6/24/201921303Linux: 17. CentOS 7에서 .NET Core Web App 실행 환경 구성 [1]
11955정성태6/20/201919679Math: 60. C# - 로지스틱 회귀를 이용한 분류파일 다운로드1
11954정성태6/20/201918440오류 유형: 550. scp - sudo: no tty present and no askpass program specified
11953정성태6/20/201916580오류 유형: 549. The library 'libhostpolicy.so' required to execute the application was not found in '...'
11952정성태6/20/201917285Linux: 16. 우분투, Centos의 Netbios 호스트 이름 풀이 방법
11951정성태6/20/201920532오류 유형: 548. scp 연결 시 "Permission denied" 오류 및 "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!" 경고
11950정성태6/18/201920578.NET Framework: 845. C# - 윈도우 작업 관리자와 리소스 모니터의 메모리 값을 구하는 방법
11949정성태6/18/201915887오류 유형: 547. CoreCLR Profiler 예제 프로젝트 빌드 시 컴파일 오류 유형
11948정성태6/17/201918377Linux: 15. 리눅스 환경의 Visual Studio Code에서 TFS 서버 연동
11947정성태6/17/201920174Linux: 14. 리눅스 환경에서 TFS 서버 연동
11946정성태6/17/201921141개발 환경 구성: 445. C# - MathNet으로 정규 분포를 따르는 데이터를 생성, PLplot으로 Histogram 표현파일 다운로드1
11945정성태6/17/201918826Linux: 13. node.js에서 syslog로 출력하는 방법
11944정성태6/16/201925223Linux: 12. Ubuntu 16.04/18.04에서 node.js 최신 버전 설치 방법
11943정성태6/15/201918466.NET Framework: 844. C# - 박싱과 언박싱 [1]
11942정성태6/13/201924770개발 환경 구성: 444. 로컬의 Visual Studio Code로 원격 리눅스 머신에 접속해 개발하는 방법 [1]
11941정성태6/13/201917430오류 유형: 546. "message NETSDK1057: You are using a preview version of .NET Core" 빌드 경고 없애는 방법
11940정성태6/13/201917639개발 환경 구성: 443. Visual Studio의 Connection Manager 기능(Remote SSH 관리)을 위한 명령행 도구파일 다운로드1
11939정성태6/13/201916465오류 유형: 545. Managed Debugging Assistant 'FatalExecutionEngineError'
11938정성태6/12/201918966Math: 59. C# - 웨이트 벡터 갱신식을 이용한 퍼셉트론 분류파일 다운로드1
11937정성태6/11/201925345개발 환경 구성: 442. .NET Core 3.0 preview 5를 이용해 Windows Forms/WPF 응용 프로그램 개발 [1]
11936정성태6/10/201918257Math: 58. C# - 최소 자승법의 1차, 2차 수렴 그래프 변화 확인 [2]파일 다운로드1
11935정성태6/9/201919795.NET Framework: 843. C# - PLplot 출력을 파일이 아닌 Window 화면으로 변경
11934정성태6/7/201921141VC++: 133. typedef struct와 타입 전방 선언으로 인한 C2371 오류파일 다운로드1
11933정성태6/7/201919503VC++: 132. enum 정의를 C++11의 enum class로 바꿀 때 유의할 사항파일 다운로드1
11932정성태6/7/201918659오류 유형: 544. C++ - fatal error C1017: invalid integer constant expression파일 다운로드1
11931정성태6/6/201919200개발 환경 구성: 441. C# - CairoSharp/GtkSharp 사용을 위한 프로젝트 구성 방법
... 76  77  78  [79]  80  81  82  83  84  85  86  87  88  89  90  ...