Skip to content

Commit b72afcf

Browse files
committed
Fix hook for 32-bit ntdll!RtlUserThreadStart
1 parent 58379ac commit b72afcf

2 files changed

Lines changed: 44 additions & 26 deletions

File tree

MemoryModule/MmpTls.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ DWORD NTAPI MmpUserThreadStart(LPVOID lpThreadParameter) {
282282
return Context.ThreadStartRoutine(Context.ThreadParameter);
283283
}
284284

285+
#ifdef _WIN64
285286
VOID NTAPI HookRtlUserThreadStart(
286287
_In_ PTHREAD_START_ROUTINE Function,
287288
_In_ PVOID Parameter) {
@@ -291,6 +292,36 @@ VOID NTAPI HookRtlUserThreadStart(
291292

292293
return MmpGlobalDataPtr->MmpTls->Hooks.OriginRtlUserThreadStart(MmpUserThreadStart, &Context);
293294
}
295+
#else
296+
VOID
297+
__declspec(naked)
298+
HookRtlUserThreadStart(
299+
_In_ PTHREAD_START_ROUTINE Function, //eax
300+
_In_ PVOID Parameter) { //ebx
301+
__asm {
302+
// THREAD_CONTEXT Context;
303+
sub esp, 8;
304+
305+
// Context.ThreadStartRoutine = PTHREAD_START_ROUTINE(Function);
306+
mov dword ptr ds : [esp] , eax;
307+
308+
// Context.ThreadParameter = Parameter;
309+
mov dword ptr ds : [esp + 4] , ebx;
310+
311+
mov eax, MmpUserThreadStart;
312+
mov ebx, esp;
313+
314+
// Shadow stack for ntdll!RtlUserThreadStart
315+
sub esp, 8;
316+
317+
// MmpGlobalDataPtr->MmpTls->Hooks.OriginRtlUserThreadStart(MmpUserThreadStart, &Context);
318+
mov ecx, MmpGlobalDataPtr;
319+
mov ecx, dword ptr ds : [ecx + 0x48] ;
320+
mov ecx, dword ptr ds : [ecx + 0x48] ;
321+
call ecx;
322+
}
323+
}
324+
#endif
294325

295326
VOID NTAPI HookLdrShutdownThread(VOID) {
296327

test/test.cpp

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -118,41 +118,28 @@ int test() {
118118
return 0;
119119
}
120120

121-
void test_uef() {
122-
auto buffer = ReadDllFile("a.dll");
121+
void test_cf() {
122+
auto buffer = ReadDllFile("CoreFoundation.dll");
123123

124-
HMODULE hm = LoadLibraryMemory(buffer);
125-
auto pfn = GetProcAddress(hm, "unhandled_exception");
124+
if (buffer) {
125+
HMODULE hm = LoadLibraryMemory(buffer);
126+
delete[]buffer;
126127

127-
auto result = pfn();
128-
if (result == 1234) {
129-
printf("mmpp success\n");
128+
if (hm) {
129+
printf("Load success: %p\n", hm);
130+
FreeLibraryMemory(hm);
131+
}
132+
else {
133+
printf("Load fail: %d\n", GetLastError());
134+
}
130135
}
131136

132137
return;
133138
}
134139

135-
void Tp() {
136-
auto pool = CreateThreadpool(nullptr);
137-
if (pool) {
138-
139-
SetThreadpoolThreadMaximum(pool, 1);
140-
SetThreadpoolThreadMinimum(pool, 1);
141-
142-
Sleep(1000);
143-
144-
CloseThreadpool(pool);
145-
}
146-
}
147-
148140
int main() {
149-
150141
DisplayStatus();
151-
test();
152-
153-
Tp();
154-
155-
WaitForSingleObject(NtCurrentProcess(), INFINITE);
142+
test_cf();
156143

157144
return 0;
158145
}

0 commit comments

Comments
 (0)