-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsystemhooks.cpp
575 lines (472 loc) · 20.8 KB
/
systemhooks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#define PHNT_VERSION PHNT_WIN10_22H2
#include <phnt_windows.h>
#include <phnt.h>
#include <ntexapi.h>
#include <ntpsapi.h>
#include <minidumpapiset.h>
#include <TlHelp32.h>
#include <mmeapi.h>
#include <filesystem>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <intrin.h>
#include <string>
#include <string_view>
#include <iostream>
#include <filesystem>
#include <asmjit/core/jitruntime.h>
#include <asmjit/x86/x86assembler.h>
#include "libs/patterns/Hooking.Patterns.h"
#include "libs/minhook/include/MinHook.h"
#include "restorentdll.h"
#include "utils.h"
#include "systemhooks.h"
#include "exceptions.h"
#include "arxan.h"
#include "paths.h"
#include "pluginloader.h"
bool weAreDebugging = false;
DWORD inputThreadId = -1;
HANDLE inputHandle = nullptr;
void* RtlRestoreContextAddr;
// ntdll hooks
NtUserQueryWindow_t NtUserQueryWindowOrig;
NtUserGetForegroundWindow_t NtUserGetForegroundWindowOrig;
NtUserBuildHwndList_t NtUserBuildHwndListOrig;
NtSetInformationProcess_t NtSetInformationProcessOrig;
NtUserFindWindowEx_t NtUserFindWindowExOrig;
NtUserWindowFromPoint_t NtUserWindowFromPointOrig;
NtAllocateVirtualMemory_t NtAllocateVirtualMemoryOrig;
NtMapViewOfSection_t NtMapViewOfSectionOrig;
NtSetInformationJobObject_t NtSetInformationJobObjectOrig;
NtUserGetClassName_t NtUserGetClassNameOrig;
NtUserInternalGetWindowText_t NtUserInternalGetWindowTextOrig;
// kernel32 hooks
// kernelbase hooks
GetWindowText_t GetWindowTextOrig;
EnumWindowsOrig_t EnumWindowsOrig;
GetThreadContext_t GetThreadContextOrig;
CreateThread_t CreateThreadOrig;
AddVectoredExceptionHandler_t AddVectoredExceptionHandlerOrig;
SetUnhandledExceptionFilter_t SetUnhandledExceptionFilterOrig;
CheckRemoteDebuggerPresent_t CheckRemoteDebuggerPresentOrig;
CreateMutexEx_t CreateMutexExOrig;
SetWindowsHookEx_t SetWindowsHookExOrig;
SetThreadContext_t SetThreadContextOrig;
CreateWindowEx_t CreateWindowExOrig;
RtlRestoreContext_t RtlRestoreContextOrig;
FreeLibrary_t FreeLibraryOrig;
FreeLibraryAndExitThread_t FreeLibraryAndExitThreadOrig;
GetWindowThreadProcessId_t GetWindowThreadProcessIdOrig;
GetClassName_t GetClassNameOrig;
EnumChildWindows_t EnumChildWindowsOrig;
GetMenu_t GetMenuOrig;
GetMenuString_t GetMenuStringOrig;
GetSubMenu_t GetSubMenuOrig;
std::vector<PVOID> VectoredExceptions;
CONTEXT context = {};
uint64_t ntdllSize = 0;
bool GetThreadContextFunc(HANDLE thread, CONTEXT context)
{
uint64_t baseAddr = reinterpret_cast<uint64_t>(GetModuleHandle(nullptr));
auto* source = _ReturnAddress();
HMODULE module;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, static_cast<LPCSTR>(source), &module);
if ((baseAddr == (uint64_t)module) && !weAreDebugging)
return 0;
else
return GetThreadContextOrig(thread, &context);
}
bool SetThreadContextFunc(HANDLE thread, CONTEXT* context)
{
HMODULE module;
uint64_t baseAddr = reinterpret_cast<uint64_t>(GetModuleHandle(nullptr));
auto* source = _ReturnAddress();
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, static_cast<LPCSTR>(source), &module);
if ((baseAddr == (uint64_t)module) && !weAreDebugging)
return 0;
else
return SetThreadContextOrig(thread, context);
}
HHOOK SetWindowsHookExFunc(int idHook, HOOKPROC lpfn, HINSTANCE hmod, DWORD dwThreadId)
{
return 0;
}
void RtlRestoreContextFunc(PCONTEXT ContextRecord, _EXCEPTION_RECORD* ExceptionRecord)
{
// After recovering from an exception, the main thread's hw Dr0-3 registers get set.
// Clears hw breakpoints
ContextRecord->Dr0 = 0;
ContextRecord->Dr1 = 0;
ContextRecord->Dr2 = 0;
ContextRecord->Dr3 = 0;
RtlRestoreContextOrig(ContextRecord, ExceptionRecord);
MH_RemoveHook(RtlRestoreContextAddr);
}
NTSTATUS NtAllocateVirtualMemoryFunc(HANDLE ProcessHandle,
PVOID* BaseAddress,
ULONG_PTR ZeroBits,
SIZE_T RegionSize,
ULONG AllocationType,
ULONG Protect)
{
NTSTATUS result = NtAllocateVirtualMemoryOrig(ProcessHandle,BaseAddress,ZeroBits,RegionSize,AllocationType,Protect);
static bool bInit = false;
if (Protect & PAGE_EXECUTE_READWRITE && *(SIZE_T*)RegionSize == ntdllSize && !bInit)
{
static int counter = 0;
counter++;
/* checksum counts for latest build supported by donetsk defcon
p 57
p 41
p 30
*/
static bool firstTime = true;
if (firstTime)
{
clock_t start_time = clock();
CreateInlineAsmStub();
CreateChecksumHealingStub();
double elapsed_time = (double)(clock() - start_time) / CLOCKS_PER_SEC;
printf("creating inline hooks for checksums took: %f seconds\n", elapsed_time);
printf("done hooking\n");
firstTime = false;
}
// Arxan does a startup checksum check routine that I didn't bother bypassing,
// doesn't matter anyways since iirc none of the game's functions gets called anyways.
// 6 is just an arbitary number so that we create gameplay related hooks a little bit later.
if (counter == 6)
{
DisableTlsCallbacks();
DisableKiUserApcDispatcherHook();
RestoreKernel32ThreadInitThunkFunction();
RemoveNtdllChecksumChecks();
RestoreNtdllDbgFunctions();
InitializePluginLoader();
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)DbgRemove, NULL, NULL, NULL);
bInit = true;
}
}
return result;
}
HWND DialogButton = 0;
DWORD WINAPI testThread()
{
while (true)
{
SendMessage(DialogButton, BM_CLICK, 0, 0);
if (!IsWindowVisible(DialogButton))
return 0;
}
}
HWND CreateWindowExFunc(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle,
int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
HWND result = CreateWindowExOrig(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
// style for buttons, hardcoded since I didn't bother reading the flags for when a button is made
if (dwStyle == 0x50000000)
{
static int counter = 0;
counter++;
// sliders get included too as "buttons", 3rd counter is the recommended options
if (counter == 5)
{
DialogButton = result;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)testThread, NULL, 0, NULL);
}
}
return result;
}
HWND NtUserFindWindowExFunc(HWND hwndParent, HWND hwndChildAfter, PUNICODE_STRING ucClassName, PUNICODE_STRING ucWindowName)
{
printf("ntuserfindwindowexfunc got called\n");
return 0;
}
HWND NtUserWindowFromPointFunc(LONG X,LONG Y)
{
printf("window from point got called\n");
return 0x0;
}
int GetWindowTextFunc(HWND hWnd, LPSTR lpString, int nMaxCount)
{
return 0;
}
// game closes itself if we return false
BOOL EnumWindowsFunc(WNDENUMPROC lpEnumFunc, LPARAM lParam)
{
return EnumWindowsOrig(lpEnumFunc, lParam);
}
HANDLE WINAPI CreateThreadFunc(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
{
if (weAreDebugging)
{
HANDLE thread = CreateThreadOrig(lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags, lpThreadId);
return thread;
}
// Loop each thread and attach breakpoints
HANDLE snapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, GetCurrentProcessId());
THREADENTRY32 entry = {};
entry.dwSize = sizeof(THREADENTRY32);
Thread32First(snapshotHandle, &entry);
do {
if (entry.th32OwnerProcessID == GetCurrentProcessId())
{
HANDLE currentThread = OpenThread(THREAD_ALL_ACCESS, false, entry.th32ThreadID);
bool setThreadResult = 1;
if (!suspendNewThreads)
setThreadResult = SetThreadContextOrig(currentThread, &context);
}
} while (Thread32Next(snapshotHandle, &entry));
HANDLE thread = CreateThreadOrig(lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags, lpThreadId);
if (suspendNewThreads)
SuspendThread(thread);
return thread;
}
HANDLE CreateMutexExFunc(const LPSECURITY_ATTRIBUTES attributes, const LPCSTR name, const DWORD flags, const DWORD access)
{
int compare1 = strcmp(name, "$ IDA trusted_idbs");
int compare2 = strcmp(name, "$ IDA registry mutex $");
if (compare1 == 0)
return CreateMutexExOrig(attributes, "blablabla", flags, access);
if (compare2 == 0)
return CreateMutexExOrig(attributes, "blablablabla", flags, access);
return CreateMutexExOrig(attributes, name, flags, access);
}
// TODO: maybe causes a startup crash now? tries to read memory from 0x0 inside crashdump
HANDLE NtUserQueryWindowFunc(HWND hwnd, WINDOWINFOCLASS WindowInfo)
{
//if ((WindowInfo == WindowProcess || WindowInfo == WindowThread) && IsWindowBad(hwnd))
if (WindowInfo == WindowProcess || WindowInfo == WindowThread)
{
if (WindowInfo == WindowProcess)
return NtCurrentTeb()->ClientId.UniqueProcess;
if (WindowInfo == WindowThread)
return NtCurrentTeb()->ClientId.UniqueThread;
}
return NtUserQueryWindowOrig(hwnd, WindowInfo);
}
HWND NtUserGetForegroundWindowFunc()
{
/*
HWND result = NtUserGetForegroundWindowOrig();
if (result != nullptr && IsWindowBad(result))
result = NULL;
return result;
*/
HWND result = NtUserGetForegroundWindowOrig();
DWORD processId = 0;
GetWindowThreadProcessIdOrig(result, &processId);
if (processId == GetCurrentProcessId())
return result;
else
return 0x0;
}
NTSTATUS NtUserBuildHwndListFunc(HDESK hDesk, HWND hWndNext, BOOL EnumChildren, BOOL RemoveImmersive, DWORD ThreadID, UINT Max, HWND* List, PULONG Cnt)
{
NTSTATUS result = NtUserBuildHwndListOrig(hDesk, hWndNext, 0, RemoveImmersive, ThreadID, Max, List, Cnt);
if (NT_SUCCESS(result) && List != nullptr && Cnt != nullptr)
FilterHwndList(List, Cnt);
return result;
}
DWORD GetWindowThreadProcessIdFunc(HWND hWnd, LPDWORD lpdwProcessId)
{
//return GetWindowThreadProcessIdOrig(hWnd, lpdwProcessId);
return 0x0;
}
int GetClassNameFunc(HWND hWnd, LPSTR lpClassName, int nMaxCount)
{
//return GetClassNameOrig(hWnd, lpClassName, nMaxCount);
return 0x0;
}
BOOL EnumChildWindowsFunc(HWND hWndParent, WNDENUMPROC lpEnumFunc, LPARAM lParam)
{
//return EnumChildWindowsOrig(hWndParent, lpEnumFunc, lParam);
return false;
}
HMENU GetMenuFunc(HWND hWnd)
{
//return GetMenuOrig(hWnd);
return 0x0;
}
int GetMenuStringFunc(HMENU hMenu,UINT uIDItem,LPSTR lpString, int cchMax,UINT flags)
{
//return GetMenuStringOrig(hMenu, uIDItem, lpString, cchMax, flags);
return 0x0;
}
HMENU GetSubMenuFunc(HMENU hMenu, int nPos)
{
//return GetSubMenuOrig(hMenu, nPos);
return 0x0;
}
BOOL CheckRemoteDebuggerPresentFunc(HANDLE hProcess, PBOOL pbDebuggerPresent)
{
*(BOOL*)pbDebuggerPresent = false;
return true;
}
PVOID AddVectoredExceptionHandlerFunc(ULONG First, PVECTORED_EXCEPTION_HANDLER Handler)
{
PVOID handler = AddVectoredExceptionHandlerOrig(First, Handler);
VectoredExceptions.push_back(handler);
return handler;
}
LPTOP_LEVEL_EXCEPTION_FILTER SetUnhandledExceptionFilterFunc(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
{
return 0;
}
void generalTlsCallbackFunction()
{
return;
}
void DisableKiUserApcDispatcherHook()
{
void* KiUserApcDispatcherAddr = (void*)GetProcAddress(GetModuleHandle("ntdll.dll"), "KiUserApcDispatcher");
// TODO: pull the bytes before arxan decides to hook the apc dispatcher
// else we might cause crashes for other ntdll variants
uint8_t bytes[14] = {0x48, 0x8b, 0x4c, 0x24, 0x18, 0x48, 0x8b, 0xc1, 0x4c, 0x8b, 0xcc, 0x48, 0xc1, 0xf9};
DWORD old_protect{};
VirtualProtect(KiUserApcDispatcherAddr, sizeof(uint8_t) * 14, PAGE_EXECUTE_READWRITE, &old_protect);
memcpy(KiUserApcDispatcherAddr, bytes, sizeof(uint8_t) * 14);
VirtualProtect(KiUserApcDispatcherAddr, sizeof(uint8_t) * 14, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), KiUserApcDispatcherAddr, sizeof(uint8_t) * 14);
}
void RestoreKernel32ThreadInitThunkFunction()
{
// cold war removes the function ptr from ntdll Kernel32ThreadInitThunkFunction to its own, redirecting createremotethread
// does rdtsc checks which in turn makes it so that if the process is completely suspended, will crash on created threads
void* BaseInitThread = (void*)GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlUserThreadStart");
void* BaseThreadInitThunk = (void*)GetProcAddress(GetModuleHandle("kernel32.dll"), "BaseThreadInitThunk");
PVOID RtlUserThreadStart = (PVOID)((DWORD64)BaseInitThread + 0x7);
DWORD64 RtlUserThreadStartFuncOffset = (UINT64)((PUCHAR)RtlUserThreadStart + *(PULONG)((PUCHAR)RtlUserThreadStart + 0x3) + 0x7);
uint64_t* basethreadinitptr = (uint64_t*)RtlUserThreadStartFuncOffset;
memcpy(basethreadinitptr, &BaseThreadInitThunk, sizeof(uint64_t));
}
void DisableTlsCallbacks()
{
uint64_t baseAddr = reinterpret_cast<uint64_t>(GetModuleHandle(nullptr));
char* tlscallback_1 = reinterpret_cast<char*>(baseAddr + 0x6b9220);
char* tlscallback_2 = reinterpret_cast<char*>(baseAddr + 0x6d7110);
char* tlscallback_3 = reinterpret_cast<char*>(baseAddr + 0x6e8480);
char* tlscallback_4 = reinterpret_cast<char*>(baseAddr + 0x6e9a90);
printf("tls1: %llx\n", tlscallback_1);
printf("tls2: %llx\n", tlscallback_2);
printf("tls3: %llx\n", tlscallback_3);
printf("tls4: %llx\n", tlscallback_4);
if (MH_CreateHook(tlscallback_1, &generalTlsCallbackFunction, NULL) != MH_OK) { printf("hook didn't work\n"); }
if (MH_EnableHook(tlscallback_1) != MH_OK) { printf("hook didn't work\n"); }
if (MH_CreateHook(tlscallback_2, &generalTlsCallbackFunction, NULL) != MH_OK) { printf("hook didn't work\n"); }
if (MH_EnableHook(tlscallback_2) != MH_OK) { printf("hook didn't work\n"); }
if (MH_CreateHook(tlscallback_3, &generalTlsCallbackFunction, NULL) != MH_OK) { printf("hook didn't work\n"); }
if (MH_EnableHook(tlscallback_3) != MH_OK) { printf("hook didn't work\n"); }
if (MH_CreateHook(tlscallback_4, &generalTlsCallbackFunction, NULL) != MH_OK) { printf("hook didn't work\n"); }
if (MH_EnableHook(tlscallback_4) != MH_OK) { printf("hook didn't work\n"); }
printf("disabled tls callbacks\n");
}
DWORD WINAPI ConsoleInput(LPVOID lpReserved)
{
bool setHWBP = false;
while (true)
{
std::string input;
getline(std::cin, input);
if (setHWBP)
{
uint64_t baseAddr = reinterpret_cast<uint64_t>(GetModuleHandle(nullptr));
uint64_t hwbpAddress = strtoll(input.c_str(), NULL, 16);
char* bpAddr1 = reinterpret_cast<char*>(baseAddr + hwbpAddress - StartOfBinary);
placeHardwareBP(bpAddr1, 3, Condition::Write);
setHWBP = false;
}
if (strcmp(input.c_str(), "b") == 0)
{
printf("set breakpoint\n");
setHWBP = true;
}
}
return 0;
}
void InitializeSystemHooks()
{
void* GetThreadContextAddr = (void*)GetProcAddress(GetModuleHandle("kernelbase.dll"), "GetThreadContext");
void* SetThreadContextAddr = (void*)GetProcAddress(GetModuleHandle("kernelbase.dll"), "SetThreadContext");
void* CheckRemoteDebuggerPresentAddr = (void*)GetProcAddress(GetModuleHandle("kernelbase.dll"), "CheckRemoteDebuggerPresent");
void* OutputDebugStringAddr = (void*)GetProcAddress(GetModuleHandle("kernelbase.dll"), "OutputDebugStringA");
void* SetUnhandledExceptionFilterAddr = (void*)GetProcAddress(GetModuleHandle("kernelbase.dll"), "SetUnhandledExceptionFilter");
void* FreeLibraryAddr = (void*)GetProcAddress(GetModuleHandle("kernelbase.dll"), "FreeLibrary");
void* FreeLibraryAndExitThreadAddr = (void*)GetProcAddress(GetModuleHandle("kernelbase.dll"), "FreeLibraryAndExitThread");
void* AddVectoredExceptionHandlerAddr = (void*)GetProcAddress(GetModuleHandle("kernel32.dll"), "AddVectoredExceptionHandler");
void* CreateThreadAddr = (void*)GetProcAddress(GetModuleHandle("kernel32.dll"), "CreateThread");
void* CreateMutexExAddr = (void*)GetProcAddress(GetModuleHandle("kernel32.dll"), "CreateMutexExA");
// we need this hook for hwbp syscall stuff to work
void* NtAllocateVirtualMemoryAddr = (void*)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtAllocateVirtualMemory");
void* EnumWindowsAddr = (void*)GetProcAddress(GetModuleHandle("user32.dll"), "EnumWindows");
void* GetWindowThreadProcessIdAddr = (void*)GetProcAddress(GetModuleHandle("user32.dll"), "GetWindowThreadProcessId");
void* GetClassNameAddr = (void*)GetProcAddress(GetModuleHandle("user32.dll"), "GetClassNameA");
void* GetWindowTextAddr = (void*)GetProcAddress(GetModuleHandle("user32.dll"), "GetWindowTextA");
void* EnumChildWindowsAddr = (void*)GetProcAddress(GetModuleHandle("user32.dll"), "EnumChildWindows");
void* GetMenuAddr = (void*)GetProcAddress(GetModuleHandle("user32.dll"), "GetMenu");
void* GetMenuStringAddr = (void*)GetProcAddress(GetModuleHandle("user32.dll"), "GetMenuStringA");
void* GetSubMenuAddr = (void*)GetProcAddress(GetModuleHandle("user32.dll"), "GetSubMenu");
void* SetWindowsHookExAddr = (void*)GetProcAddress(GetModuleHandle("user32.dll"), "SetWindowsHookExW");
void* CreateWindowExAddr = (void*)GetProcAddress(GetModuleHandle("user32.dll"), "CreateWindowExW");
void* NtUserQueryWindowAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserQueryWindow");
void* NtUserGetForegroundWindowAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserGetForegroundWindow");
void* NtUserBuildHwndListAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserBuildHwndList");
void* NtUserFindWindowExAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserFindWindowEx");
void* NtUserWindowFromPointAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserWindowFromPoint");
void* NtUserInternalGetWindowTextAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserInternalGetWindowText");
void* NtUserGetWindowProcessHandleAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserGetWindowProcessHandle");
void* NtUserGetTopLevelWindowAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserGetTopLevelWindow");
void* NtUserChildWindowFromPointExAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserChildWindowFromPointEx");
void* NtUserInternalGetWindowIconAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserInternalGetWindowIcon");
void* NtUserRealChildWindowFromPointAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserRealChildWindowFromPoint");
void* NtUserWindowFromDCAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserWindowFromDC");
void* NtUserGetClassNameAddr = (void*)GetProcAddress(GetModuleHandle("win32u.dll"), "NtUserGetClassName");
struct hook_t
{
void* addr;
void* ourFunction;
void** originalFunction;
};
hook_t hooks[] {
{SetThreadContextAddr, &SetThreadContextFunc, (LPVOID*)(&SetThreadContextOrig)},
{NtUserQueryWindowAddr, &NtUserQueryWindowFunc, (LPVOID*)(&NtUserQueryWindowOrig)},
{NtUserInternalGetWindowTextAddr, &GetMenuFunc, (LPVOID*)(&NtUserInternalGetWindowTextOrig)},
{NtUserGetClassNameAddr, &GetMenuFunc, (LPVOID*)(&NtUserGetClassNameOrig)},
{NtUserFindWindowExAddr, &NtUserFindWindowExFunc, (LPVOID*)(&NtUserFindWindowExOrig)},
{NtUserWindowFromPointAddr, &NtUserWindowFromPointFunc, (LPVOID*)(&NtUserWindowFromPointOrig)},
{CreateWindowExAddr, &CreateWindowExFunc, (LPVOID*)(&CreateWindowExOrig)},
{GetWindowTextAddr, &GetWindowTextFunc, (LPVOID*)(&GetWindowTextOrig)},
{EnumWindowsAddr, &EnumWindowsFunc, (LPVOID*)(&EnumWindowsOrig)},
{GetThreadContextAddr, &GetThreadContextFunc, (LPVOID*)(&GetThreadContextOrig)},
{CreateThreadAddr, &CreateThreadFunc, (LPVOID*)(&CreateThreadOrig)},
{SetWindowsHookExAddr, &SetWindowsHookExFunc, (LPVOID*)(&SetWindowsHookExOrig)},
{CreateMutexExAddr, &CreateMutexExFunc, (LPVOID*)(&CreateMutexExOrig)},
{CheckRemoteDebuggerPresentAddr, &CheckRemoteDebuggerPresentFunc, (LPVOID*)(&CheckRemoteDebuggerPresentOrig)},
{AddVectoredExceptionHandlerAddr, &AddVectoredExceptionHandlerFunc, (LPVOID*)(&AddVectoredExceptionHandlerOrig)},
{SetUnhandledExceptionFilterAddr, &SetUnhandledExceptionFilterFunc, (LPVOID*)(&SetUnhandledExceptionFilterOrig)},
{NtUserGetForegroundWindowAddr, &NtUserGetForegroundWindowFunc, (LPVOID*)(&NtUserGetForegroundWindowOrig)},
{NtUserBuildHwndListAddr, &NtUserBuildHwndListFunc, (LPVOID*)(&NtUserBuildHwndListOrig)},
{GetWindowThreadProcessIdAddr, &GetWindowThreadProcessIdFunc, (LPVOID*)(&GetWindowThreadProcessIdOrig)},
{GetClassNameAddr, &GetClassNameFunc, (LPVOID*)(&GetClassNameOrig)},
{EnumChildWindowsAddr, &EnumChildWindowsFunc, (LPVOID*)(&EnumChildWindowsOrig)},
{GetMenuAddr, &GetMenuFunc, (LPVOID*)(&GetMenuOrig)},
{GetMenuStringAddr, &GetMenuStringFunc, (LPVOID*)(&GetMenuStringOrig)},
{GetSubMenuAddr, &GetSubMenuFunc, (LPVOID*)(&GetSubMenuOrig)},
{NtUserGetWindowProcessHandleAddr, &GetMenuFunc, NULL},
{NtUserGetTopLevelWindowAddr, &GetMenuFunc, NULL},
{NtUserChildWindowFromPointExAddr, &GetMenuFunc, NULL},
{NtUserInternalGetWindowIconAddr, &GetMenuFunc, NULL},
{NtUserRealChildWindowFromPointAddr, &GetMenuFunc, NULL},
{NtUserWindowFromDCAddr, &GetMenuFunc, NULL},
{NtAllocateVirtualMemoryAddr, &NtAllocateVirtualMemoryFunc, (LPVOID*)(&NtAllocateVirtualMemoryOrig)},
};
size_t amountHooks = sizeof(hooks) / sizeof(hook_t);
for (int i=0; i < amountHooks; i++)
{
if (MH_CreateHook(hooks[i].addr, hooks[i].ourFunction, hooks[i].originalFunction) != MH_OK)
printf("system hook %d didn't work\n", i);
if (MH_EnableHook(hooks[i].addr) != MH_OK)
printf("system hook %d didn't work\n", i);
}
}