Microsoft MVP성태의 닷넷 이야기
Windows: 165. AcLayers의 API 후킹과 FaultTolerantHeap [링크 복사], [링크+제목 복사]
조회: 11268
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




... 16  17  18  19  20  21  22  23  24  25  26  27  [28]  29  30  ...
NoWriterDateCnt.TitleFile(s)
12919정성태1/13/20226218Windows: 199. Host Network Service (HNS)에 의해서 점유되는 포트
12918정성태1/13/20226409Linux: 47. WSL - shell script에서 설정한 환경 변수가 스크립트 실행 후 반영되지 않는 문제
12917정성태1/12/20225654오류 유형: 785. C# - The type or namespace name '...' could not be found (are you missing a using directive or an assembly reference?)
12916정성태1/12/20225386오류 유형: 784. TFS - One or more source control bindings for this solution are not valid and are listed below.
12915정성태1/11/20225664오류 유형: 783. Visual Studio - We didn't find any interpreters
12914정성태1/11/20227559VS.NET IDE: 172. 비주얼 스튜디오 2022의 파이선 개발 환경 지원
12913정성태1/11/20228081.NET Framework: 1133. C# - byte * (바이트 포인터)를 FileStream으로 쓰는 방법 [1]
12912정성태1/11/20228720개발 환경 구성: 623. ffmpeg.exe를 사용해 비디오 파일의 이미지를 PGM(Portable Gray Map) 파일 포맷으로 출력하는 방법 [1]
12911정성태1/11/20226046VS.NET IDE: 171. 비주얼 스튜디오 - 더 이상 만들 수 없는 "ASP.NET Core 3.1 Web Application (.NET Framework)" 프로젝트
12910정성태1/10/20226529제니퍼 .NET: 30. 제니퍼 닷넷 적용 사례 (8) - CPU high와 DB 쿼리 성능에 문제가 함께 있는 사이트
12909정성태1/10/20227922오류 유형: 782. Visual Studio 2022 설치 시 "Couldn't install Microsoft.VisualCpp.Redist.14.Latest"
12908정성태1/10/20225749.NET Framework: 1132. C# - ref/out 매개변수의 IL 코드 처리
12907정성태1/9/20226221오류 유형: 781. (youtube-dl.exe) 실행 시 "This app can't run on your PC" / "Access is denied." 오류 발생
12906정성태1/9/20226815.NET Framework: 1131. C# - 네임스페이스까지 동일한 타입을 2개의 DLL에서 제공하는 경우 충돌을 우회하는 방법 [1]파일 다운로드1
12905정성태1/8/20226475오류 유형: 780. Could not load file or assembly 'Microsoft.VisualStudio.TextTemplating.VSHost.15.0, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
12904정성태1/8/20228519개발 환경 구성: 623. Visual Studio 2022 빌드 환경을 위한 github Actions 설정 [1]
12903정성태1/7/20227120.NET Framework: 1130. C# - ELEMENT_TYPE_INTERNAL 유형의 사용 예
12902정성태1/7/20227162오류 유형: 779. SQL 서버 로그인 에러 - provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.
12901정성태1/5/20227263오류 유형: 778. C# - .NET 5+에서 warning CA1416: This call site is reachable on all platforms. '...' is only supported on: 'windows' 경고 발생
12900정성태1/5/20228909개발 환경 구성: 622. vcpkg로 ffmpeg를 빌드하는 경우 생성될 구성 요소 제어하는 방법
12899정성태1/3/20228387개발 환경 구성: 621. windbg에서 python 스크립트 실행하는 방법 - pykd (2)
12898정성태1/2/20228939.NET Framework: 1129. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 비디오 인코딩 예제(encode_video.c) [1]파일 다운로드1
12897정성태1/2/20227813.NET Framework: 1128. C# - 화면 캡처한 이미지를 ffmpeg(FFmpeg.AutoGen)로 동영상 처리 [4]파일 다운로드1
12896정성태1/1/202210678.NET Framework: 1127. C# - FFmpeg.AutoGen 라이브러리를 이용한 기본 프로젝트 구성파일 다운로드1
12895정성태12/31/20219170.NET Framework: 1126. C# - snagit처럼 화면 캡처를 연속으로 수행해 동영상 제작 [1]파일 다운로드1
12894정성태12/30/20217160.NET Framework: 1125. C# - DefaultObjectPool<T>의 IDisposable 개체에 대한 풀링 문제 [3]파일 다운로드1
... 16  17  18  19  20  21  22  23  24  25  26  27  [28]  29  30  ...