Skip to content

Commit 3c9a88e

Browse files
committed
Add support for SetUnhandledExceptionFilter
1 parent 5f60d93 commit 3c9a88e

5 files changed

Lines changed: 67 additions & 7 deletions

File tree

MemoryModule/MemoryModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ NTSTATUS MemoryLoadLibrary(
239239
LPVOID(old_header->OptionalHeader.ImageBase),
240240
old_header->OptionalHeader.SizeOfImage,
241241
MEM_RESERVE,
242-
PAGE_READWRITE
242+
PAGE_EXECUTE_READWRITE
243243
);
244244
if (!base) {
245245
if (old_header->OptionalHeader.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) {

MemoryModule/MmpTls.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,11 @@ VOID NTAPI HookLdrShutdownThread(VOID) {
347347
entry = entry->Flink;
348348
}
349349

350-
--MmpGlobalDataPtr->MmpTls->MmpActiveThreadCount;
350+
if (record) {
351+
--MmpGlobalDataPtr->MmpTls->MmpActiveThreadCount;
352+
}
353+
354+
assert(0 < (int)MmpGlobalDataPtr->MmpTls->MmpActiveThreadCount);
351355

352356
LeaveCriticalSection(&MmpGlobalDataPtr->MmpTls->MmpTlspLock);
353357

@@ -371,7 +375,7 @@ VOID NTAPI HookLdrShutdownThread(VOID) {
371375
}
372376
else {
373377
if (MmpGlobalDataPtr->MmpTls->MmpTlsList.Flink != &MmpGlobalDataPtr->MmpTls->MmpTlsList) {
374-
assert(false);
378+
assert(NtCurrentTeb()->ThreadLocalStoragePointer == nullptr);
375379
}
376380
}
377381

a/dllmain.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,45 @@ int thread() {
146146
return -1;
147147
}
148148

149+
DWORD Value;
150+
volatile LPDWORD lpAddr;
151+
152+
LONG WINAPI Filter(_In_ struct _EXCEPTION_POINTERS* ExceptionInfo) {
153+
154+
if (ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION) {
155+
156+
lpAddr = &Value;
157+
158+
// +++++++
159+
// begin compiler specific
160+
// +++++++
161+
162+
//ExceptionInfo->ContextRecord->Rip -= 7;
163+
ExceptionInfo->ContextRecord->Rax = (ULONG_PTR)lpAddr;
164+
165+
// +++++++
166+
// end compiler specific
167+
// +++++++
168+
169+
return EXCEPTION_CONTINUE_EXECUTION;
170+
}
171+
172+
return EXCEPTION_CONTINUE_SEARCH;
173+
}
174+
175+
int unhandled_exception() {
176+
auto filter = SetUnhandledExceptionFilter(Filter);
177+
auto ff = SetUnhandledExceptionFilter(filter);
178+
179+
if (ff != Filter) {
180+
printf("%p\t%p\t%p\nfailed\n", filter, ff, Filter);
181+
return 0;
182+
}
183+
184+
filter = SetUnhandledExceptionFilter(Filter);
185+
lpAddr = nullptr;
186+
*lpAddr = 1;
187+
SetUnhandledExceptionFilter(filter);
188+
189+
return 1234;
190+
}

a/m.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ test = __test__
44
thread
55
Socket = ws2_32.WSASocketW
66
VerifyTruse = wintrust.WinVerifyTrust
7-
test_user32
7+
test_user32
8+
unhandled_exception

test/test.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "../MemoryModule/stdafx.h"
2+
#include "../MemoryModule/LoadDllMemoryApi.h"
23
#include <cstdio>
34

45
static PVOID ReadDllFile(LPCSTR FileName) {
@@ -115,10 +116,22 @@ int test() {
115116
return 0;
116117
}
117118

118-
int main() {
119-
DisplayStatus();
119+
void test_uef() {
120+
auto buffer = ReadDllFile("a.dll");
121+
122+
HMODULE hm = LoadLibraryMemory(buffer);
123+
auto pfn = GetProcAddress(hm, "unhandled_exception");
124+
125+
auto result = pfn();
126+
if (result == 1234) {
127+
printf("mmpp success\n");
128+
}
120129

121-
test();
130+
return;
131+
}
132+
133+
int main() {
134+
test_uef();
122135

123136
return 0;
124137
}

0 commit comments

Comments
 (0)