Microsoft MVP성태의 닷넷 이야기
Windows: 165. AcLayers의 API 후킹과 FaultTolerantHeap [링크 복사], [링크+제목 복사]
조회: 11278
글쓴 사람
정성태 (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)
13373정성태6/19/20234398오류 유형: 865. 파이썬 - pymssql 설치 관련 오류 정리
13372정성태6/15/20233111개발 환경 구성: 682. SQL Server TLS 통신을 위해 사용되는 키 길이 확인 방법
13371정성태6/15/20233132개발 환경 구성: 681. openssl - 인증서 버전(V1 / V3)
13370정성태6/14/20233296개발 환경 구성: 680. C# - Ubuntu + Microsoft.Data.SqlClient + SQL Server 2008 R2 연결 방법 - TLS 1.2 지원
13369정성태6/13/20233094개발 환경 구성: 679. PyCharm(을 비롯해 JetBrains에 속한 여타) IDE에서 내부 Window들의 탭이 없어진 경우
13368정성태6/13/20233225개발 환경 구성: 678. openssl로 생성한 인증서를 SQL Server의 암호화 인증서로 설정하는 방법
13367정성태6/10/20233333오류 유형: 864. openssl로 만든 pfx 인증서를 Windows Server 2016 이하에서 등록 시 "The password you entered is incorrect" 오류 발생
13366정성태6/10/20233130.NET Framework: 2128. C# - 윈도우 시스템에서 지원하는 암호화 목록(Cipher Suites) 나열파일 다운로드1
13365정성태6/8/20232896오류 유형: 863. MODIFY FILE encountered operating system error 112(failed to retrieve text for this error. Reason: 15105)
13364정성태6/8/20233678.NET Framework: 2127. C# - Ubuntu + Microsoft.Data.SqlClient + SQL Server 2008 R2 연결 방법 [1]
13363정성태6/7/20233242스크립트: 49. 파이썬 - "Transformers (신경망 언어모델 라이브러리) 강좌" - 1장 2절 코드 실행 결과
13362정성태6/1/20233164.NET Framework: 2126. C# - 서버 측의 요청 제어 (Microsoft.AspNetCore.RateLimiting)파일 다운로드1
13361정성태5/31/20233638오류 유형: 862. Facebook - ASP.NET/WebClient 사용 시 graph.facebook.com/me 호출에 대해 403 Forbidden 오류
13360정성태5/31/20233037오류 유형: 861. WSL/docker - failed to start shim: start failed: io.containerd.runc.v2: create new shim socket
13359정성태5/19/20233354오류 유형: 860. Docker Desktop - k8s 초기화 무한 반복한다면?
13358정성태5/17/20233660.NET Framework: 2125. C# - Semantic Kernel의 Semantic Memory 사용 예제 [1]파일 다운로드1
13357정성태5/16/20233464.NET Framework: 2124. C# - Semantic Kernel의 Planner 사용 예제파일 다운로드1
13356정성태5/15/20233769DDK: 10. Device Driver 테스트 설치 관련 오류 (Code 37, Code 31) 및 인증서 관련 정리
13355정성태5/12/20233685.NET Framework: 2123. C# - Semantic Kernel의 ChatGPT 대화 구현 [1]파일 다운로드1
13354정성태5/12/20233958.NET Framework: 2122. C# - "Use Unicode UTF-8 for worldwide language support" 설정을 한 경우, 한글 입력이 '\0' 문자로 처리
13352정성태5/12/20233569.NET Framework: 2121. C# - Semantic Kernel의 대화 문맥 유지파일 다운로드1
13351정성태5/11/20234072VS.NET IDE: 185. Visual Studio - 원격 Docker container 내에 실행 중인 응용 프로그램에 대한 디버깅 [1]
13350정성태5/11/20233323오류 유형: 859. Windows Date and Time - Unable to continue. You do not have permission to perform this task
13349정성태5/11/20233661.NET Framework: 2120. C# - Semantic Kernel의 Skill과 Function 사용 예제파일 다운로드1
13348정성태5/10/20233570.NET Framework: 2119. C# - Semantic Kernel의 "Basic Loading of the Kernel" 예제
13347정성태5/10/20233933.NET Framework: 2118. C# - Semantic Kernel의 Prompt chaining 예제파일 다운로드1
1  2  3  4  5  6  7  8  9  [10]  11  12  13  14  15  ...