9static BYTE* Alloc(DWORD protection) {
10 return (BYTE*)VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, protection);
14static BYTE* QueryPeb(HANDLE process,
bool wow64 =
false) {
18 query(process, ProcessWow64Information, &peb32,
sizeof(peb32), NULL);
21 PROCESS_BASIC_INFORMATION info{};
22 query(process, ProcessBasicInformation, &info,
sizeof(info), NULL);
23 return (BYTE*)info.PebBaseAddress;
27 PVOID Buffer, SIZE_T NumberOfBytes, PSIZE_T NumberOfBytesTransferred);
33static void RewriteInPlace(HANDLE process, BYTE* address, SIZE_T size) {
41#define PEB_BEING_DEBUGGED_OFFSET 0x2
43#define PEB_IMAGE_BASE_OFFSET 0x10
44#define PEB_IMAGE_BASE_OFFSET_TEXT "0x10"
46#define PEB_IMAGE_BASE_OFFSET 0x8
47#define PEB_IMAGE_BASE_OFFSET_TEXT "0x8"
49#define POINTER_SIZE_TEXT (sizeof(void*) == 8 ? "8" : "4")
52#define REMOTE_IMAGE_BASE_READ (std::string("NtReadVirtualMemory: ") + POINTER_SIZE_TEXT + \
53 " bytes read from the PEB of the remote process at offset " PEB_IMAGE_BASE_OFFSET_TEXT)
64static MainImage MainImageAt(HANDLE process, BYTE* base) {
67 IMAGE_DOS_HEADER dos{};
68 read(process, base, &dos,
sizeof(dos), &transferred);
69 IMAGE_NT_HEADERS nt{};
70 read(process, base + dos.e_lfanew, &nt,
sizeof(nt), &transferred);
71 return { base, base + nt.OptionalHeader.AddressOfEntryPoint,
72 base + dos.e_lfanew + offsetof(IMAGE_NT_HEADERS, OptionalHeader.AddressOfEntryPoint) };
77static MainImage RemoteMainImage(HANDLE process) {
82 return MainImageAt(process, base);
87static void RewriteProtected(HANDLE process, BYTE* address, SIZE_T size, DWORD protection) {
89 VirtualProtectEx(process, address, size, protection, &old);
90 RewriteInPlace(process, address, size);
91 VirtualProtectEx(process, address, size, old, &old);
94static const std::vector<HookTestCase> g_Cases = {
98 "PE into current process RW memory (pseudo-handle)",
101 WriteProcessMemory(GetCurrentProcess(), Alloc(PAGE_READWRITE), context.PEImage(), context.PESize, &written);
103 {
"NtWriteVirtualMemory: PE image written to * in the current process, destination not executable" },
106 "PE into current process RW memory (real handle to self)",
108 HANDLE self = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
110 WriteProcessMemory(self, Alloc(PAGE_READWRITE), context.PEImage(), context.PESize, &written);
113 {
"NtWriteVirtualMemory: PE image written to * in the current process, destination not executable" },
116 "PE into current process RWX memory",
119 WriteProcessMemory(GetCurrentProcess(), Alloc(PAGE_EXECUTE_READWRITE), context.PEImage(), context.PESize, &written);
122 "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)",
123 "NtWriteVirtualMemory: PE image written to * in the current process, destination executable",
129 "PE into current process RX memory",
132 WriteProcessMemory(GetCurrentProcess(), Alloc(PAGE_EXECUTE_READ), context.PEImage(), context.PESize, &written);
135 "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x20)",
136 "NtProtectVirtualMemory: PE image at * in the current process made executable (0x20 -> *)",
137 "NtWriteVirtualMemory: PE image written to * in the current process, destination executable",
138 "NtProtectVirtualMemory: PE image at * in the current process made executable (0x40 -> *)",
142 "PE into remote process RW memory",
144 HANDLE remote = context.RemoteProcess();
145 BYTE* destination = (BYTE*)VirtualAllocEx(remote, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
147 WriteProcessMemory(remote, destination, context.PEImage(), context.PESize, &written);
149 {
"NtWriteVirtualMemory: PE image written to * in the remote process, destination not executable" },
152 "PE into remote process RWX memory",
154 HANDLE remote = context.RemoteProcess();
155 BYTE* destination = (BYTE*)VirtualAllocEx(remote, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
157 WriteProcessMemory(remote, destination, context.PEImage(), context.PESize, &written);
160 "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the remote process (protection 0x40)",
161 "NtWriteVirtualMemory: PE image written to * in the remote process, destination executable",
166 "MZ without a PE signature is reported",
168 BYTE buffer[0x1000] = {
'M',
'Z' };
170 WriteProcessMemory(GetCurrentProcess(), Alloc(PAGE_READWRITE), buffer,
sizeof(buffer), &written);
172 {
"NtWriteVirtualMemory: PE image written to * in the current process, destination not executable" },
175 "Just the two MZ bytes are reported",
178 WriteProcessMemory(GetCurrentProcess(), Alloc(PAGE_READWRITE), context.PEImage(), 2, &written);
180 {
"NtWriteVirtualMemory: PE image written to * in the current process, destination not executable" },
183 "A single M byte is not reported",
186 WriteProcessMemory(GetCurrentProcess(), Alloc(PAGE_READWRITE), context.PEImage(), 1, &written);
191 "ZM (byte-swapped magic) is not reported",
193 BYTE buffer[0x1000] = {
'Z',
'M' };
195 WriteProcessMemory(GetCurrentProcess(), Alloc(PAGE_READWRITE), buffer,
sizeof(buffer), &written);
200 "Non-PE data is not reported",
203 memset(buffer, 0x90,
sizeof(buffer));
205 WriteProcessMemory(GetCurrentProcess(), Alloc(PAGE_READWRITE), buffer,
sizeof(buffer), &written);
210 "Failed write of a PE is not reported",
213 BYTE* reserved = (BYTE*)VirtualAlloc(NULL, 0x1000, MEM_RESERVE, PAGE_READWRITE);
215 WriteProcessMemory(GetCurrentProcess(), reserved, context.PEImage(), context.PESize, &written);
223 "Write to current process PEB (pseudo-handle)",
225 BYTE* peb = (BYTE*)NtCurrentTeb()->ProcessEnvironmentBlock;
229 "NtReadVirtualMemory: 1 bytes read from the PEB of the current process at offset 0x2",
230 "NtWriteVirtualMemory: 1 bytes written to the PEB of the current process at offset 0x2",
234 "Write to current process PEB (real handle to self)",
236 HANDLE self = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
241 "NtReadVirtualMemory: 1 bytes read from the PEB of the current process at offset 0x2",
242 "NtWriteVirtualMemory: 1 bytes written to the PEB of the current process at offset 0x2",
247 "Write to the last byte of the PEB page",
249 BYTE* peb = (BYTE*)NtCurrentTeb()->ProcessEnvironmentBlock;
250 RewriteInPlace(GetCurrentProcess(), peb + 0xFFF, 1);
253 "NtReadVirtualMemory: 1 bytes read from the PEB of the current process at offset 0xfff",
254 "NtWriteVirtualMemory: 1 bytes written to the PEB of the current process at offset 0xfff",
259 "Write to remote process PEB ImageBaseAddress",
261 HANDLE remote = context.RemoteProcess();
273 "Write to remote WOW64 process 32-bit PEB",
275 HANDLE remote = context.Wow64RemoteProcess();
279 "NtReadVirtualMemory: 1 bytes read from the PEB of the remote process at offset 0x2",
280 "NtWriteVirtualMemory: 1 bytes written to the PEB of the remote process at offset 0x2",
285 "Write next to the PEB is not reported",
288 BYTE* teb = (BYTE*)NtCurrentTeb();
289 RewriteInPlace(GetCurrentProcess(), teb + 0x100, 1);
295 "Failed write to the PEB is not reported",
297 HANDLE self = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
301 {
"NtReadVirtualMemory: 1 bytes read from the PEB of the current process at offset 0x2" },
309 "Write to remote process entry point code",
311 HANDLE remote = context.RemoteProcess();
312 RewriteProtected(remote, RemoteMainImage(remote).EntryPoint, 1, PAGE_EXECUTE_READWRITE);
316 "NtProtectVirtualMemory: PE image at * in the remote process made executable (0x20 -> 0x40)",
317 "NtWriteVirtualMemory: 1 bytes written over the main image's entry point code in the remote process, entry point *",
318 "NtProtectVirtualMemory: PE image at * in the remote process made executable (* -> 0x20)",
322 "Write to remote process AddressOfEntryPoint field",
324 HANDLE remote = context.RemoteProcess();
325 RewriteProtected(remote, RemoteMainImage(remote).EntryPointField,
sizeof(DWORD), PAGE_READWRITE);
329 "NtWriteVirtualMemory: 4 bytes written over the main image's AddressOfEntryPoint field in the remote process, entry point *",
333 "Write to current process entry point code",
335 MainImage image = MainImageAt(GetCurrentProcess(), (BYTE*)GetModuleHandle(NULL));
336 RewriteProtected(GetCurrentProcess(), image.
EntryPoint, 1, PAGE_EXECUTE_READWRITE);
339 "NtProtectVirtualMemory: PE image at * in the current process made executable (0x20 -> 0x40)",
340 "NtWriteVirtualMemory: 1 bytes written over the main image's entry point code in the current process, entry point *",
341 "NtProtectVirtualMemory: PE image at * in the current process made executable (* -> 0x20)",
345 "Write elsewhere in the remote main image's headers is not reported",
347 HANDLE remote = context.RemoteProcess();
349 RewriteProtected(remote, RemoteMainImage(remote).Base + 2, 1, PAGE_READWRITE);
355int main(
int argc,
char** argv) {
356 return RunHookTests(argc, argv,
"NtWriteVirtualMemory", g_Cases);
#define POINTER_SIZE_TEXT
#define PEB_IMAGE_BASE_OFFSET
#define PEB_IMAGE_BASE_OFFSET_TEXT
NTSTATUS(NTAPI * NtReadWriteVirtualMemory_t)(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytes, PSIZE_T NumberOfBytesTransferred)
#define PEB_BEING_DEBUGGED_OFFSET
#define REMOTE_IMAGE_BASE_READ
The read hook's report of the PEB.ImageBaseAddress read in RemoteMainImage.
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.
A process's main image: its base, entry point and the address of the AddressOfEntryPoint header field...