9 PSIZE_T RegionSize, ULONG AllocationType, ULONG PageProtection,
10 PMEM_EXTENDED_PARAMETER ExtendedParameters, ULONG ExtendedParameterCount);
13static PVOID AllocEx(HANDLE process, SIZE_T size, ULONG allocationType, ULONG protection) {
16 if (allocate == NULL) {
17 fprintf(stderr,
" NtAllocateVirtualMemoryEx is not exported by this ntdll\n");
20 allocate(process, &base, &size, allocationType, protection, NULL, 0);
27static HMODULE LoadCopy(
const char* folderName) {
32 if (first == NULL || GetModuleFileNameA(first, source,
MAX_PATH) == 0 || GetTempPathA(
MAX_PATH, folder) == 0) {
36 strcat_s(folder, folderName);
37 CreateDirectoryA(folder, NULL);
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());
47static const std::vector<HookTestCase> g_Cases = {
52 "Second copy of the DLL reports each call once",
54 HMODULE second = LoadCopy(
"PanoptesSecondCopy");
55 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
59 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
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)",
72 "Three copies of the DLL report each call once",
74 HMODULE second = LoadCopy(
"PanoptesSecondCopy");
75 HMODULE third = LoadCopy(
"PanoptesThirdCopy");
76 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
80 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
84 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
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)",
98 "RWX in current process",
100 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
102 {
"NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)" },
105 "RX in current process",
107 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READ);
109 {
"NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x20)" },
112 "Execute-only in current process",
114 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE);
116 {
"NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x10)" },
120 "Size is reported rounded up to whole pages",
122 VirtualAlloc(NULL, 0x1001, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
124 {
"NtAllocateVirtualMemory: 8192 executable bytes allocated at * in the current process (protection 0x40)" },
129 "Reserve-only RWX in current process",
131 VirtualAlloc(NULL, 0x1000, MEM_RESERVE, PAGE_EXECUTE_READWRITE);
133 {
"NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)" },
136 "RWX in current process through a real handle to self",
138 HANDLE self = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
139 VirtualAllocEx(self, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
142 {
"NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)" },
145 "RWX in remote process",
147 VirtualAllocEx(context.RemoteProcess(), NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
149 {
"NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the remote process (protection 0x40)" },
152 "RX in remote process",
154 VirtualAllocEx(context.RemoteProcess(), NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READ);
156 {
"NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the remote process (protection 0x20)" },
159 "RW allocation is not reported",
161 VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
166 "Remote RW allocation is not reported",
168 VirtualAllocEx(context.RemoteProcess(), NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
174 "Failed RWX allocation is not reported",
176 VirtualAlloc(GetModuleHandleA(NULL), 0x1000, MEM_RESERVE, PAGE_EXECUTE_READWRITE);
181 "RWX allocation through a handle without PROCESS_VM_OPERATION is not reported",
183 HANDLE self = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
184 VirtualAllocEx(self, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
193 "Ex: RWX in current process",
195 AllocEx(GetCurrentProcess(), 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
197 {
"NtAllocateVirtualMemoryEx: 4096 executable bytes allocated at * in the current process (protection 0x40)" },
200 "Ex: RX in remote process",
202 AllocEx(context.RemoteProcess(), 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READ);
204 {
"NtAllocateVirtualMemoryEx: 4096 executable bytes allocated at * in the remote process (protection 0x20)" },
207 "Ex: RW allocation is not reported",
209 AllocEx(GetCurrentProcess(), 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
215 "Ex: failed RWX allocation is not reported",
217 AllocEx(GetCurrentProcess(), 0x1000, 0, PAGE_EXECUTE_READWRITE);
223int main(
int argc,
char** argv) {
224 return RunHookTests(argc, argv,
"NtAllocateVirtualMemory", g_Cases);
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...
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.