Microsoft MVP성태의 닷넷 이야기
Windows: 165. AcLayers의 API 후킹과 FaultTolerantHeap [링크 복사], [링크+제목 복사]
조회: 11282
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)

AcLayers의 API 후킹과 FaultTolerantHeap

FaultTolerantHeap을 살펴보기 위해 일부러 CRT로부터 할당받은 메모리의 헤더를 망가뜨리는 코드를,

Need Sample code for heap corruption in C++
; https://stackoverflow.com/questions/35123189/need-sample-code-for-heap-corruption-in-c

// Visual C++
// ...[생략]...

void ReleaseBuf(char* pBuffer)
{
    delete[] pBuffer;
}

char* AllocBuf()
{
    char* cp = new char[10];
    (*(cp - 5))++;

    return cp;
}

char* _p;

__declspec(dllexport) int __fastcall UseCRTHeap(int value)
{
    if (value == 0)
    {
        _p = AllocBuf();
    }
    else if (value == 1)
    {
        ReleaseBuf(_p);
    }

    return 0;
}

// C#
class Program
{

    [DllImport("Win32Project1.dll")]
    internal static extern int UseCRTHeap(int value);

    static unsafe void Main(string[] args)
    {
        Console.WriteLine("Allocating...");
        UseCRTHeap(0);
        Console.WriteLine("Allocated");

        Console.WriteLine("Releasing...");
        UseCRTHeap(1);
        Console.WriteLine("Released...");
    }
}

사용한 프로그램을 실행하면 최초에는 다음과 같이 3개의 이벤트 로그가 생성됩니다.

Log Name:      Application
Source:        Application Error
Date:          2019-11-14 오전 10:21:10
Event ID:      1000
Task Category: (100)
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      TESTPC
Description:
Faulting application name: ConsoleApplication1.exe, version: 1.0.0.0, time stamp: 0x5dcca494
Faulting module name: ntdll.dll, version: 10.0.18362.418, time stamp: 0x99ca0526
Exception code: 0xc0000374
Fault offset: 0x00000000000f9269
Faulting process id: 0x7200
Faulting application start time: 0x01d59a89cc5d5fd2
Faulting application path: c:\temp\ConsoleApplication1.exe
Faulting module path: C:\WINDOWS\SYSTEM32\ntdll.dll
Report Id: 50e4663e-286d-48dc-aba8-696a11188ac6
Faulting package full name: 
Faulting package-relative application ID: 

Log Name:      Application
Source:        Windows Error Reporting
Date:          2019-11-14 오전 10:21:12
Event ID:      1001
Task Category: None
Level:         Information
Keywords:      Classic
User:          N/A
Computer:      TESTPC
Description:
Fault bucket 2118073145157684012, type 4
Event Name: APPCRASH
Response: Not available
Cab Id: 0

Problem signature:
P1: ConsoleApplication1.exe
P2: 1.0.0.0
P3: 5dcca494
P4: StackHash_b2bc
P5: 10.0.18362.418
P6: 99ca0526
P7: c0000374
P8: PCH_03_FROM_ntdll+0x000000000009CC14
P9: 
P10: 

Attached files:
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER84F4.tmp.mdmp
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER8582.tmp.WERInternalMetadata.xml
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER85A2.tmp.xml
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER85A6.tmp.csv
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER85D6.tmp.txt

These files may be available here:
\\?\C:\ProgramData\Microsoft\Windows\WER\ReportArchive\AppCrash_ConsoleApplicati_75e6a768f43e63769fa883e35983a378d047bd80_6103db0b_e38e88b9-59cf-414d-aaa9-282f7a013f53

Analysis symbol: 
Rechecking for solution: 0
Report Id: 50e4663e-286d-48dc-aba8-696a11188ac6
Report Status: 268435456
Hashed bucket: c8396419a04467d1ed64e84db16b0b2c
Cab Guid: 0

Log Name:      Application
Source:        Windows Error Reporting
Date:          2019-11-14 오전 10:21:14
Event ID:      1001
Task Category: None
Level:         Information
Keywords:      Classic
User:          N/A
Computer:      TESTPC
Description:
Fault bucket 1186916719236790934, type 5
Event Name: FaultTolerantHeap
Response: Not available
Cab Id: 1329775288456208175

Problem signature:
P1: ConsoleApplication1.exe
P2: 1.0.0.0
P3: 5DCCA494
P4: ffffbaad
P5: 
P6: 
P7: 
P8: 
P9: 
P10: 

Attached files:
\\?\C:\WINDOWS\SERVIC~1\LOCALS~1\AppData\Local\Temp\FTH84D5.tmp\fthempty.txt
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER84D6.tmp.WERInternalMetadata.xml
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER84D7.tmp.xml
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER84D9.tmp.csv
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER84F9.tmp.txt
\\?\C:\Windows\ServiceProfiles\LocalService\AppData\Local\Temp\WER88CF.tmp.WERDataCollectionStatus.txt

These files may be available here:

Analysis symbol: 
Rechecking for solution: 0
Report Id: e1740ff6-55b8-4d78-80c0-b80185bc9d28
Report Status: 268435464
Hashed bucket: 4c18d2a648f924301078c677b414ba96
Cab Guid: 94e535c5-8c62-4522-9274-4f9453865f2f

주의 깊게 보면, 3번째 로그의 "Event Name"이 "FaultTolerantHeap"인 것을 알 수 있습니다. 그런데, 이후 실행하게 되면 FaultTolerantHeap에 대한 이벤트 로그가 더 이상 생성이 안 됩니다. 재미있는 것은, 이렇게 몇 번 "FaultTolerantHeap"에 해당하는 비정상 종료 프로그램을 실행하다 보면 윈도우는 해당 프로그램을 다음의 레지스트리 경로에 등록하게 됩니다.

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
    Value name: c:\temp\ConsoleApplication1.exe
    Value type: REG_SZ
    Value data: FaultTolerantHeap

(또는, HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers)

만약, 운(?)이 따른다면 이후에는 비정상 종료를 하지 않을 가능성도 있습니다. 왜냐하면, 윈도우는 이런 유의 비정상 종료를 완화하기 위해 후킹을 제공하기 때문입니다. 실제로, AppCompatFlags에 등록되기 전에는 비정상 종료 시 메모리 덤프를 뜨게 되면 다음과 같은 식의 call stack을 보여줍니다.

ntdll!NtWaitForMultipleObjects+14 
KERNELBASE!WaitForMultipleObjectsEx+107 
KERNELBASE!WaitForMultipleObjects+e 
kernel32!WerpReportFaultInternal+51b 
kernel32!WerpReportFault+ac 
KERNELBASE!UnhandledExceptionFilter+3b8 
ntdll!RtlUserThreadStart$filt$0+a2 
ntdll!_C_specific_handler+96 
ntdll!RtlpExecuteHandlerForException+f 
ntdll!RtlDispatchException+219 
ntdll!KiUserExceptionDispatch+2e 
ntdll!RtlpFreeHeapInternal+65ed4 
ntdll!RtlFreeHeap+51 
ucrtbase!_free_base+1b 
Win32Project1!ExternC_FAST_Func_By_DEF+34 
DomainBoundILStubClass.IL_STUB_PInvoke(Int32)+76 
Program.Main(System.String[])+8b 
clr!CallDescrWorkerInternal+83 
clr!CallDescrWorkerWithHandler+4e 
clr!MethodDescCallSite::CallTargetWorker+102 
clr!RunMain+25f 
clr!Assembly::ExecuteMainMethod+b7 
clr!SystemDomain::ExecuteMainMethod+643 
clr!ExecuteEXE+3f 
clr!_CorExeMainInternal+b2 
clr!CorExeMain+14 
mscoreei!CorExeMain+112 
mscoree!CorExeMain_Exported+6c 
kernel32!BaseThreadInitThunk+14 
ntdll!RtlUserThreadStart+21 

반면, 일단 윈도우가 FaultTolerantHeap 유형임을 알고 개입하기 시작하면 다음과 같이 AcLayers.dll의 함수가 중간에 들어가는 것을 볼 수 있습니다.

ntdll!NtWaitForMultipleObjects+14 
KERNELBASE!WaitForMultipleObjectsEx+107 
KERNELBASE!WaitForMultipleObjects+e 
kernel32!WerpReportFaultInternal+51b 
kernel32!WerpReportFault+ac 
KERNELBASE!UnhandledExceptionFilter+3b8 
ntdll!RtlUserThreadStart$filt$0+a2 
ntdll!_C_specific_handler+96 
ntdll!RtlpExecuteHandlerForException+f 
ntdll!RtlDispatchException+219 
ntdll!KiUserExceptionDispatch+2e 
ntdll!RtlSizeHeap+1af 
AcLayers!NS_FaultTolerantHeap::APIHook_RtlFreeHeap+146 
ucrtbase!_free_base+1b 
Win32Project1!ExternC_FAST_Func_By_DEF+34 
DomainBoundILStubClass.IL_STUB_PInvoke(Int32)+76 
Program.Main(System.String[])+8b 
clr!CallDescrWorkerInternal+83 
clr!CallDescrWorkerWithHandler+4e 
clr!MethodDescCallSite::CallTargetWorker+102 
clr!RunMain+25f 
clr!Assembly::ExecuteMainMethod+b7 
clr!SystemDomain::ExecuteMainMethod+643 
clr!ExecuteEXE+3f 
clr!_CorExeMainInternal+b2 
clr!CorExeMain+14 
mscoreei!CorExeMain+112 
mscoree!CorExeMain_Exported+6c 
kernel32!BaseThreadInitThunk+14 
ntdll!RtlUserThreadStart+21 

APIHook_RtlFreeHeap에서 할당된 Heap 크기를 알기 위해 RtlSizeHeap을 호출하는데, 아쉽게도 이번 오류는 Header 영역이 깨졌기 때문에 그런 시도조차 실패하게 된 것입니다.




그렇다면 다시 AcLayers의 개입을 없앨 수 있을까요? 제가 해본 바로는 "AppCompatFlags\Layers" 레지스트리에 등록된 것을 지운다고 해결되지는 않았습니다. 대신 해당 EXE를 다른 경로에 복사해 실행하면 다시 "FaultTolerantHeap" 이벤트 로그가 생성되었고 이후 마찬가지로 몇 번 실행하면 "AppCompatFlags\Layers" 레지스트리에 등록이 되었습니다. (물론, 다시 그 EXE의 실행에는 AcLayers가 개입합니다.)

검색해 보면, 아예 FTH 기능을 끄는 방법이 나옵니다.

Disabling a Shim
; https://learn.microsoft.com/en-us/archive/blogs/maartenb/disabling-a-shim

You can disable the whole shim engine through this setting from GPEdit.msc:

Administrative Templates \ Windows Components \ Application Compatibility \ Turn off Application Compatibility Engine

Setting it to enabled means that the Shim engine is off. Reboot and there you have it. No output in DebugView.

또한 다음의 글을 보면,

Fault Tolerant Heap
; https://learn.microsoft.com/en-us/windows/win32/win7appqual/fault-tolerant-heap

HKLM\Software\Microsoft\FTH 경로의 Enabled 값을 0으로 설정해도 FTH 기능을 시스템 전역적으로 끌 수 있다고 나옵니다.

참고로, 소개된 이벤트 로그 경로(Event Viewer > Applications and Services Logs > Microsoft > Windows > Fault-Tolerant-Heap)를 보면 FTH에 대해 "service started" / "service stopped" 이벤트들을 볼 수 있는데 NT Service처럼 내부적으로 다뤄지고 있는 듯한데 실제로 이를 제어할 수 있는 방법은 gpedit.msc와 레지스트리 설정 이외에는 없는 것 같습니다.

그리고 "Application Compatibility"에 등록된 항목들을 전부 초기화(reset) 할 수 있는 명령어를 소개하는데,

Rundll32.exe fthsvc.dll,FthSysprepSpecialize

(권장하지는 않지만) 실제로 저 명령을 내리면 "AppCompatFlags\Layers" 레지스트리 설정까지 전부 초기화되는 것을 볼 수 있습니다. 따라서 call stack에 보이던 AcLayers의 개입도 없어집니다.

이번엔 여기까지만! ^^




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/7/2023]

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

비밀번호

댓글 작성자
 




1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13450정성태11/21/20232255닷넷: 2164. C# - Octokit을 이용한 GitHub Issue 검색파일 다운로드1
13449정성태11/21/20232354개발 환경 구성: 688. Azure OpenAI 서비스 신청 방법
13448정성태11/20/20232629닷넷: 2163. .NET 8 - Dynamic PGO를 결합한 성능 향상파일 다운로드1
13447정성태11/16/20232487닷넷: 2162. ASP.NET Core 웹 사이트의 SSL 설정을 코드로 하는 방법
13446정성태11/16/20232419닷넷: 2161. .NET Conf 2023 - Day 1 Blazor 개요 정리
13445정성태11/15/20232704Linux: 62. 리눅스/WSL에서 CA 인증서를 저장하는 방법
13444정성태11/15/20232456닷넷: 2160. C# 12 - Experimental 특성 지원
13443정성태11/14/20232493개발 환경 구성: 687. OpenSSL로 생성한 사용자 인증서를 ASP.NET Core 웹 사이트에 적용하는 방법
13442정성태11/13/20232322개발 환경 구성: 686. 비주얼 스튜디오로 실행한 ASP.NET Core 사이트를 WSL 2 인스턴스에서 https로 접속하는 방법
13441정성태11/12/20232654닷넷: 2159. C# - ASP.NET Core 프로젝트에서 서버 Socket을 직접 생성하는 방법파일 다운로드1
13440정성태11/11/20232354Windows: 253. 소켓 Listen 시 방화벽의 Public/Private 제어 기능이 비활성화된 경우
13439정성태11/10/20232846닷넷: 2158. C# - 소켓 포트를 미리 시스템에 등록/예약해 사용하는 방법(Port Exclusion Ranges)파일 다운로드1
13438정성태11/9/20232462닷넷: 2157. C# - WinRT 기능을 이용해 윈도우에서 실행 중인 Media App 제어
13437정성태11/8/20232661닷넷: 2156. .NET 7 이상의 콘솔 프로그램을 (dockerfile 없이) 로컬 docker에 배포하는 방법
13436정성태11/7/20232895닷넷: 2155. C# - .NET 8 런타임부터 (Reflection 없이) 특성을 이용해 public이 아닌 멤버 호출 가능
13435정성태11/6/20232833닷넷: 2154. C# - 네이티브 자원을 포함한 관리 개체(예: 스레드)의 GC 정리
13434정성태11/1/20232631스크립트: 62. 파이썬 - class의 정적 함수를 동적으로 교체
13433정성태11/1/20232357스크립트: 61. 파이썬 - 함수 오버로딩 미지원
13432정성태10/31/20232408오류 유형: 878. 탐색기의 WSL 디렉터리 접근 시 "Attempt to access invalid address." 오류 발생
13431정성태10/31/20232723스크립트: 60. 파이썬 - 비동기 FastAPI 앱을 gunicorn으로 호스팅
13430정성태10/30/20232614닷넷: 2153. C# - 사용자가 빌드한 ICU dll 파일을 사용하는 방법
13429정성태10/27/20232871닷넷: 2152. Win32 Interop - C/C++ DLL로부터 이중 포인터 버퍼를 C#으로 받는 예제파일 다운로드1
13428정성태10/25/20232926닷넷: 2151. C# 12 - ref readonly 매개변수
13427정성태10/18/20233104닷넷: 2150. C# 12 - 정적 문맥에서 인스턴스 멤버에 대한 nameof 접근 허용(Allow nameof to always access instance members from static context)
13426정성태10/13/20233287스크립트: 59. 파이썬 - 비동기 호출 함수(run_until_complete, run_in_executor, create_task, run_in_threadpool)
13425정성태10/11/20233104닷넷: 2149. C# - PLinq의 Partitioner<T>를 이용한 사용자 정의 분할파일 다운로드1
1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...