Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_NtAllocateVirtualMemory.cpp
Go to the documentation of this file.
1// Tests the Hooked_NtAllocateVirtualMemory and Hooked_NtAllocateVirtualMemoryEx post-call check:
2// a successful allocation with an executable protection is reported with the API, the base and
3// size the kernel returned, whether the target is the current process, and the protection.
4// The Ex cases call ntdll directly, because which export the Win32 wrappers use varies between
5// Windows versions.
7
8typedef NTSTATUS(NTAPI* NtAllocateVirtualMemoryEx_t)(HANDLE ProcessHandle, PVOID* BaseAddress,
9 PSIZE_T RegionSize, ULONG AllocationType, ULONG PageProtection,
10 PMEM_EXTENDED_PARAMETER ExtendedParameters, ULONG ExtendedParameterCount);
11
13static PVOID AllocEx(HANDLE process, SIZE_T size, ULONG allocationType, ULONG protection) {
14 auto allocate = NtdllFunction<NtAllocateVirtualMemoryEx_t>("NtAllocateVirtualMemoryEx");
15 PVOID base = NULL;
16 if (allocate == NULL) {
17 fprintf(stderr, " NtAllocateVirtualMemoryEx is not exported by this ntdll\n");
18 return NULL;
19 }
20 allocate(process, &base, &size, allocationType, protection, NULL, 0);
21 return base;
22}
23
27static HMODULE LoadCopy(const char* folderName) {
28 char source[MAX_PATH];
29 char folder[MAX_PATH];
30 char destination[MAX_PATH];
31 HMODULE first = GetModuleHandleA(HOOK_DLL_NAME);
32 if (first == NULL || GetModuleFileNameA(first, source, MAX_PATH) == 0 || GetTempPathA(MAX_PATH, folder) == 0) {
33 fprintf(stderr, " cannot locate %s\n", HOOK_DLL_NAME);
34 return NULL;
35 }
36 strcat_s(folder, folderName);
37 CreateDirectoryA(folder, NULL);
38 sprintf_s(destination, "%s\\%s", folder, HOOK_DLL_NAME);
39 HMODULE second = CopyFileA(source, destination, FALSE) ? LoadLibraryA(destination) : NULL;
40 if (second == NULL || second == first) {
41 fprintf(stderr, " failed to load a second copy from %s (error %lu)\n", destination, GetLastError());
42 return NULL;
43 }
44 return second;
45}
46
47static const std::vector<HookTestCase> g_Cases = {
48 // A second copy of the DLL wraps the first copy's hooks; it silences the first copy so each
49 // call is reported once, and the first copy reports again once the second unloads. Unloading
50 // the copy unmaps an executable image, which the first copy reports.
51 {
52 "Second copy of the DLL reports each call once",
53 [](HookTestContext&) {
54 HMODULE second = LoadCopy("PanoptesSecondCopy");
55 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
56 if (second != NULL) {
57 FreeLibrary(second);
58 }
59 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
60 },
61 {
62 "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)",
63 "NtUnmapViewOfSection: executable image at * unmapped from the current process",
64 "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)",
65 },
66 },
67 {
68 // Three copies, as on a machine where the driver injected the installed DLL and this test
69 // loaded its own build: only the newest reports, and each unload hands reporting to the
70 // next newest. Copies are unloaded newest first, because Detours hooks have to be removed
71 // in the reverse of the order they were placed
72 "Three copies of the DLL report each call once",
73 [](HookTestContext&) {
74 HMODULE second = LoadCopy("PanoptesSecondCopy");
75 HMODULE third = LoadCopy("PanoptesThirdCopy");
76 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
77 if (third != NULL) {
78 FreeLibrary(third);
79 }
80 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
81 if (second != NULL) {
82 FreeLibrary(second);
83 }
84 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
85 },
86 {
87 "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)",
88 "NtUnmapViewOfSection: executable image at * unmapped from the current process",
89 "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)",
90 "NtUnmapViewOfSection: executable image at * unmapped from the current process",
91 "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)",
92 },
93 },
94
95 // NtAllocateVirtualMemory
96
97 {
98 "RWX in current process",
99 [](HookTestContext&) {
100 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
101 },
102 { "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)" },
103 },
104 {
105 "RX in current process",
106 [](HookTestContext&) {
107 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READ);
108 },
109 { "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x20)" },
110 },
111 {
112 "Execute-only in current process",
113 [](HookTestContext&) {
114 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE);
115 },
116 { "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x10)" },
117 },
118 {
119 // The kernel rounds the size up to whole pages, and the event reports the rounded size
120 "Size is reported rounded up to whole pages",
121 [](HookTestContext&) {
122 VirtualAlloc(NULL, 0x1001, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
123 },
124 { "NtAllocateVirtualMemory: 8192 executable bytes allocated at * in the current process (protection 0x40)" },
125 },
126 {
127 // Reserving with an executable protection succeeds, so it is reported though nothing is
128 // committed yet
129 "Reserve-only RWX in current process",
130 [](HookTestContext&) {
131 VirtualAlloc(NULL, 0x1000, MEM_RESERVE, PAGE_EXECUTE_READWRITE);
132 },
133 { "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)" },
134 },
135 {
136 "RWX in current process through a real handle to self",
137 [](HookTestContext&) {
138 HANDLE self = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
139 VirtualAllocEx(self, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
140 CloseHandle(self);
141 },
142 { "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)" },
143 },
144 {
145 "RWX in remote process",
146 [](HookTestContext& context) {
147 VirtualAllocEx(context.RemoteProcess(), NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
148 },
149 { "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the remote process (protection 0x40)" },
150 },
151 {
152 "RX in remote process",
153 [](HookTestContext& context) {
154 VirtualAllocEx(context.RemoteProcess(), NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READ);
155 },
156 { "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the remote process (protection 0x20)" },
157 },
158 {
159 "RW allocation is not reported",
160 [](HookTestContext&) {
161 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
162 },
163 {},
164 },
165 {
166 "Remote RW allocation is not reported",
167 [](HookTestContext& context) {
168 VirtualAllocEx(context.RemoteProcess(), NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
169 },
170 {},
171 },
172 {
173 // This executable's image already occupies the requested address
174 "Failed RWX allocation is not reported",
175 [](HookTestContext&) {
176 VirtualAlloc(GetModuleHandleA(NULL), 0x1000, MEM_RESERVE, PAGE_EXECUTE_READWRITE);
177 },
178 {},
179 },
180 {
181 "RWX allocation through a handle without PROCESS_VM_OPERATION is not reported",
182 [](HookTestContext&) {
183 HANDLE self = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
184 VirtualAllocEx(self, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
185 CloseHandle(self);
186 },
187 {},
188 },
189
190 // NtAllocateVirtualMemoryEx
191
192 {
193 "Ex: RWX in current process",
194 [](HookTestContext&) {
195 AllocEx(GetCurrentProcess(), 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
196 },
197 { "NtAllocateVirtualMemoryEx: 4096 executable bytes allocated at * in the current process (protection 0x40)" },
198 },
199 {
200 "Ex: RX in remote process",
201 [](HookTestContext& context) {
202 AllocEx(context.RemoteProcess(), 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READ);
203 },
204 { "NtAllocateVirtualMemoryEx: 4096 executable bytes allocated at * in the remote process (protection 0x20)" },
205 },
206 {
207 "Ex: RW allocation is not reported",
208 [](HookTestContext&) {
209 AllocEx(GetCurrentProcess(), 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
210 },
211 {},
212 },
213 {
214 // An allocation type of 0 is invalid
215 "Ex: failed RWX allocation is not reported",
216 [](HookTestContext&) {
217 AllocEx(GetCurrentProcess(), 0x1000, 0, PAGE_EXECUTE_READWRITE);
218 },
219 {},
220 },
221};
222
223int main(int argc, char** argv) {
224 return RunHookTests(argc, argv, "NtAllocateVirtualMemory", g_Cases);
225}
NTSTATUS(NTAPI * NtAllocateVirtualMemoryEx_t)(HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG AllocationType, ULONG PageProtection, PMEM_EXTENDED_PARAMETER ExtendedParameters, ULONG ExtendedParameterCount)
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
Resources shared by the cases of one test run, created in the child process. The remote processes are...
T NtdllFunction(const char *name)
Looks up an ntdll export. Calls through the result go through the hooks, because the hooks patch the ...
int RunHookTests(int argc, char **argv, const char *apiName, const std::vector< HookTestCase > &cases)
Runs the cases and prints a PASS/FAIL line for each. Call from main.
#define HOOK_DLL_NAME