Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_PanoptesDLL.cpp
Go to the documentation of this file.
1#include <Windows.h>
2#include <cstdio>
3#include <cstring>
4
5#ifdef _WIN64
6#define HOOK_ARCH "x64"
7#else
8#define HOOK_ARCH "x86"
9#endif
10
11#define HOOK_DLL_NAME "PanoptesDLL" HOOK_ARCH ".dll"
12
14static const char* g_hookedFunctions[] = {
15 "NtWriteVirtualMemory",
16 "NtMapViewOfSection",
17 "NtMapViewOfSectionEx",
18 "NtUnmapViewOfSection",
19 "NtUnmapViewOfSectionEx",
20 "NtProtectVirtualMemory",
21 "NtAllocateVirtualMemory",
22 "NtAllocateVirtualMemoryEx",
23 "NtReadVirtualMemory",
24 "NtCreateThreadEx",
25 "NtQueueApcThread",
26 "NtQueueApcThreadEx2",
27 "NtSetInformationProcess",
28};
29
34static BOOL IsInModule(HMODULE hModule, BYTE* address) {
35 if (hModule == NULL) {
36 return FALSE;
37 }
38
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;
42}
43
49static BYTE* FollowJumps(BYTE* address, int* hops) {
50 *hops = 0;
51 for (int i = 0; i < 8; i++) {
52 if (address[0] == 0xE9) {
53 // jmp rel32
54 address = address + 5 + *(INT32*)(address + 1);
55 }
56 else if (address[0] == 0xFF && address[1] == 0x25) {
57#ifdef _WIN64
58 // jmp [rip+disp32]
59 address = *(BYTE**)(address + 6 + *(INT32*)(address + 2));
60#else
61 // jmp [disp32]
62 address = **(BYTE***)(address + 2);
63#endif
64 }
65 else {
66 break;
67 }
68 (*hops)++;
69 }
70 return address;
71}
72
79static int CheckHooks(const char* phase, HMODULE hHook, BOOL expectHooked) {
80 HMODULE hNtdll = GetModuleHandleA("ntdll.dll");
81 int mismatches = 0;
82
83 printf("\n[%s] expecting %s\n", phase, expectHooked ? "trampolines" : "clean functions");
84
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);
89 continue;
90 }
91
92 printf(" %-26s %p:", name, (void*)function);
93 for (int i = 0; i < 8; i++) {
94 printf(" %02x", function[i]);
95 }
96
97 int hops = 0;
98 BYTE* destination = FollowJumps(function, &hops);
99 BOOL hooked = hops > 0 && IsInModule(hHook, destination);
100
101 if (hops == 0) {
102 printf(" clean");
103 }
104 else if (hooked) {
105 printf(" trampoline -> %p in %s (%d jumps)", (void*)destination, HOOK_DLL_NAME, hops);
106 }
107 else {
108 printf(" trampoline -> %p outside %s (%d jumps)", (void*)destination, HOOK_DLL_NAME, hops);
109 }
110
111 if (hooked != expectHooked) {
112 printf(" <-- UNEXPECTED");
113 mismatches++;
114 }
115 printf("\n");
116 }
117
118 return mismatches;
119}
120
124int main() {
125 printf("Hello World from the %s hook loader\n", HOOK_ARCH);
126
127 char dllPath[MAX_PATH];
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());
131 return 1;
132 }
133
134 // Replace the executable name with ..<dll name>
135 char* lastSlash = strrchr(dllPath, '\\');
136 if (lastSlash == NULL) {
137 printf("Unexpected executable path: %s\n", dllPath);
138 return 1;
139 }
140 *(lastSlash + 1) = '\0';
141 strcat_s(dllPath, MAX_PATH, "..\\" HOOK_DLL_NAME);
142
143 int failures = CheckHooks("before load", NULL, FALSE);
144
145 HMODULE hModule = LoadLibraryA(dllPath);
146 if (hModule == NULL) {
147 printf("Failed to load %s (error %lu)\n", dllPath, GetLastError());
148 return 1;
149 }
150
151 printf("\nLoaded %s at %p\n", dllPath, (void*)hModule);
152
153 failures += CheckHooks("after load", hModule, TRUE);
154
155 FreeLibrary(hModule);
156 printf("\nUnloaded %s\n", HOOK_DLL_NAME);
157
158 failures += CheckHooks("after unload", NULL, FALSE);
159
160 printf("\n%s\n", failures == 0 ? "PASS" : "FAIL");
161 return failures == 0 ? 0 : 1;
162}
int main()
Loads the hook DLL for this architecture from bin/<config>, one directory above the tests folder this...
#define MAX_PATH
Definition callbacks.h:7
#define HOOK_ARCH
#define HOOK_DLL_NAME