11#define HOOK_DLL_NAME "PanoptesDLL" HOOK_ARCH ".dll"
14static const char* g_hookedFunctions[] = {
15 "NtWriteVirtualMemory",
17 "NtMapViewOfSectionEx",
18 "NtUnmapViewOfSection",
19 "NtUnmapViewOfSectionEx",
20 "NtProtectVirtualMemory",
21 "NtAllocateVirtualMemory",
22 "NtAllocateVirtualMemoryEx",
23 "NtReadVirtualMemory",
26 "NtQueueApcThreadEx2",
27 "NtSetInformationProcess",
34static BOOL IsInModule(HMODULE hModule, BYTE* address) {
35 if (hModule == NULL) {
39 BYTE* base = (BYTE*)hModule;
40 PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER)base)->e_lfanew);
41 return address >= base && address < base + ntHeaders->OptionalHeader.SizeOfImage;
49static BYTE* FollowJumps(BYTE* address,
int* hops) {
51 for (
int i = 0; i < 8; i++) {
52 if (address[0] == 0xE9) {
54 address = address + 5 + *(INT32*)(address + 1);
56 else if (address[0] == 0xFF && address[1] == 0x25) {
59 address = *(BYTE**)(address + 6 + *(INT32*)(address + 2));
62 address = **(BYTE***)(address + 2);
79static int CheckHooks(
const char* phase, HMODULE hHook, BOOL expectHooked) {
80 HMODULE hNtdll = GetModuleHandleA(
"ntdll.dll");
83 printf(
"\n[%s] expecting %s\n", phase, expectHooked ?
"trampolines" :
"clean functions");
85 for (
const char* name : g_hookedFunctions) {
86 BYTE* function = (BYTE*)GetProcAddress(hNtdll, name);
87 if (function == NULL) {
88 printf(
" %-26s not exported by this ntdll, skipping\n", name);
92 printf(
" %-26s %p:", name, (
void*)function);
93 for (
int i = 0; i < 8; i++) {
94 printf(
" %02x", function[i]);
98 BYTE* destination = FollowJumps(function, &hops);
99 BOOL hooked = hops > 0 && IsInModule(hHook, destination);
105 printf(
" trampoline -> %p in %s (%d jumps)", (
void*)destination,
HOOK_DLL_NAME, hops);
108 printf(
" trampoline -> %p outside %s (%d jumps)", (
void*)destination,
HOOK_DLL_NAME, hops);
111 if (hooked != expectHooked) {
112 printf(
" <-- UNEXPECTED");
125 printf(
"Hello World from the %s hook loader\n",
HOOK_ARCH);
128 DWORD length = GetModuleFileNameA(NULL, dllPath,
MAX_PATH);
129 if (length == 0 || length ==
MAX_PATH) {
130 printf(
"Failed to get the executable path (error %lu)\n", GetLastError());
135 char* lastSlash = strrchr(dllPath,
'\\');
136 if (lastSlash == NULL) {
137 printf(
"Unexpected executable path: %s\n", dllPath);
140 *(lastSlash + 1) =
'\0';
143 int failures = CheckHooks(
"before load", NULL, FALSE);
145 HMODULE hModule = LoadLibraryA(dllPath);
146 if (hModule == NULL) {
147 printf(
"Failed to load %s (error %lu)\n", dllPath, GetLastError());
151 printf(
"\nLoaded %s at %p\n", dllPath, (
void*)hModule);
153 failures += CheckHooks(
"after load", hModule, TRUE);
155 FreeLibrary(hModule);
158 failures += CheckHooks(
"after unload", NULL, FALSE);
160 printf(
"\n%s\n", failures == 0 ?
"PASS" :
"FAIL");
161 return failures == 0 ? 0 : 1;
int main()
Loads the hook DLL for this architecture from bin/<config>, one directory above the tests folder this...