8static BYTE* QueryPeb(HANDLE process,
bool wow64 =
false) {
12 query(process, ProcessWow64Information, &peb32,
sizeof(peb32), NULL);
15 PROCESS_BASIC_INFORMATION info{};
16 query(process, ProcessBasicInformation, &info,
sizeof(info), NULL);
17 return (BYTE*)info.PebBaseAddress;
20static void Read(HANDLE process,
const BYTE* address, SIZE_T size) {
23 ReadProcessMemory(process, address, buffer, size, &bytesRead);
26static BYTE* CurrentPeb() {
27 return (BYTE*)NtCurrentTeb()->ProcessEnvironmentBlock;
32#define PEB_LDR_OFFSET 0x18
33#define PEB_LDR_OFFSET_TEXT "0x18"
34#define PEB_PARAMETERS_OFFSET 0x20
35#define PEB_PARAMETERS_OFFSET_TEXT "0x20"
37#define PEB_LDR_OFFSET 0xC
38#define PEB_LDR_OFFSET_TEXT "0xc"
39#define PEB_PARAMETERS_OFFSET 0x10
40#define PEB_PARAMETERS_OFFSET_TEXT "0x10"
42#define POINTER_SIZE_TEXT (sizeof(void*) == 8 ? "8" : "4")
44static const std::vector<HookTestCase> g_Cases = {
46 "Read of current process PEB (pseudo-handle)",
48 Read(GetCurrentProcess(), CurrentPeb(), 0x40);
50 {
"NtReadVirtualMemory: 64 bytes read from the PEB of the current process at offset 0x0" },
53 "Read of current process PEB (real handle to self)",
55 HANDLE self = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
64 "Read of remote process PEB ProcessParameters",
66 HANDLE remote = context.RemoteProcess();
73 "Read of the whole remote PEB page",
75 HANDLE remote = context.RemoteProcess();
76 Read(remote, QueryPeb(remote), 0x1000);
78 {
"NtReadVirtualMemory: 4096 bytes read from the PEB of the remote process at offset 0x0" },
81 "Read of the last byte of the PEB page",
83 Read(GetCurrentProcess(), CurrentPeb() + 0xFFF, 1);
85 {
"NtReadVirtualMemory: 1 bytes read from the PEB of the current process at offset 0xfff" },
89 "Read of remote WOW64 process 32-bit PEB",
91 HANDLE remote = context.Wow64RemoteProcess();
92 Read(remote, QueryPeb(remote,
true) + 0x10, 4);
94 {
"NtReadVirtualMemory: 4 bytes read from the PEB of the remote process at offset 0x10" },
97 "Read of remote WOW64 process 64-bit PEB",
99 HANDLE remote = context.Wow64RemoteProcess();
100 Read(remote, QueryPeb(remote) + 0x10, 8);
102 {
"NtReadVirtualMemory: 8 bytes read from the PEB of the remote process at offset 0x10" },
106 "Read of the TEB is not reported",
108 Read(GetCurrentProcess(), (
const BYTE*)NtCurrentTeb(), 0x40);
113 "Read of ordinary memory is not reported",
115 Read(GetCurrentProcess(), context.PEImage(), 0x40);
120 "Read of remote ordinary memory is not reported",
122 HANDLE remote = context.RemoteProcess();
123 BYTE* page = (BYTE*)VirtualAllocEx(remote, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
124 Read(remote, page, 0x40);
129 "Failed read of the PEB is not reported",
132 HANDLE self = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
133 Read(self, QueryPeb(self), 0x40);
140int main(
int argc,
char** argv) {
141 return RunHookTests(argc, argv,
"NtReadVirtualMemory", g_Cases);
#define PEB_PARAMETERS_OFFSET_TEXT
#define PEB_PARAMETERS_OFFSET
#define PEB_LDR_OFFSET_TEXT
#define POINTER_SIZE_TEXT
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.