Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_NtReadVirtualMemory.cpp
Go to the documentation of this file.
1// Tests the Hooked_NtReadVirtualMemory post-call check: a successful read overlapping the
2// target's PEB page is reported with the size, whether the target is the current process, and
3// the offset into the PEB. From x64, a WOW64 target's 32-bit PEB counts too.
5#include <winternl.h>
6
8static BYTE* QueryPeb(HANDLE process, bool wow64 = false) {
9 auto query = NtdllFunction<decltype(&NtQueryInformationProcess)>("NtQueryInformationProcess");
10 if (wow64) {
11 ULONG_PTR peb32 = 0;
12 query(process, ProcessWow64Information, &peb32, sizeof(peb32), NULL);
13 return (BYTE*)peb32;
14 }
15 PROCESS_BASIC_INFORMATION info{};
16 query(process, ProcessBasicInformation, &info, sizeof(info), NULL);
17 return (BYTE*)info.PebBaseAddress;
18}
19
20static void Read(HANDLE process, const BYTE* address, SIZE_T size) {
21 BYTE buffer[0x1000];
22 SIZE_T bytesRead;
23 ReadProcessMemory(process, address, buffer, size, &bytesRead);
24}
25
26static BYTE* CurrentPeb() {
27 return (BYTE*)NtCurrentTeb()->ProcessEnvironmentBlock;
28}
29
30// Offsets of PEB fields, which differ by architecture
31#ifdef _WIN64
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"
36#else
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"
41#endif
42#define POINTER_SIZE_TEXT (sizeof(void*) == 8 ? "8" : "4")
43
44static const std::vector<HookTestCase> g_Cases = {
45 {
46 "Read of current process PEB (pseudo-handle)",
47 [](HookTestContext&) {
48 Read(GetCurrentProcess(), CurrentPeb(), 0x40);
49 },
50 { "NtReadVirtualMemory: 64 bytes read from the PEB of the current process at offset 0x0" },
51 },
52 {
53 "Read of current process PEB (real handle to self)",
54 [](HookTestContext&) {
55 HANDLE self = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
56 Read(self, QueryPeb(self) + PEB_LDR_OFFSET, sizeof(void*));
57 CloseHandle(self);
58 },
59 { std::string("NtReadVirtualMemory: ") + POINTER_SIZE_TEXT +
60 " bytes read from the PEB of the current process at offset " PEB_LDR_OFFSET_TEXT },
61 },
62 {
63 // How tools find a process's command line: PEB->ProcessParameters
64 "Read of remote process PEB ProcessParameters",
65 [](HookTestContext& context) {
66 HANDLE remote = context.RemoteProcess();
67 Read(remote, QueryPeb(remote) + PEB_PARAMETERS_OFFSET, sizeof(void*));
68 },
69 { std::string("NtReadVirtualMemory: ") + POINTER_SIZE_TEXT +
70 " bytes read from the PEB of the remote process at offset " PEB_PARAMETERS_OFFSET_TEXT },
71 },
72 {
73 "Read of the whole remote PEB page",
74 [](HookTestContext& context) {
75 HANDLE remote = context.RemoteProcess();
76 Read(remote, QueryPeb(remote), 0x1000);
77 },
78 { "NtReadVirtualMemory: 4096 bytes read from the PEB of the remote process at offset 0x0" },
79 },
80 {
81 "Read of the last byte of the PEB page",
82 [](HookTestContext&) {
83 Read(GetCurrentProcess(), CurrentPeb() + 0xFFF, 1);
84 },
85 { "NtReadVirtualMemory: 1 bytes read from the PEB of the current process at offset 0xfff" },
86 },
87#ifdef _WIN64
88 {
89 "Read of remote WOW64 process 32-bit PEB",
90 [](HookTestContext& context) {
91 HANDLE remote = context.Wow64RemoteProcess();
92 Read(remote, QueryPeb(remote, true) + 0x10, 4);
93 },
94 { "NtReadVirtualMemory: 4 bytes read from the PEB of the remote process at offset 0x10" },
95 },
96 {
97 "Read of remote WOW64 process 64-bit PEB",
98 [](HookTestContext& context) {
99 HANDLE remote = context.Wow64RemoteProcess();
100 Read(remote, QueryPeb(remote) + 0x10, 8);
101 },
102 { "NtReadVirtualMemory: 8 bytes read from the PEB of the remote process at offset 0x10" },
103 },
104#endif
105 {
106 "Read of the TEB is not reported",
107 [](HookTestContext&) {
108 Read(GetCurrentProcess(), (const BYTE*)NtCurrentTeb(), 0x40);
109 },
110 {},
111 },
112 {
113 "Read of ordinary memory is not reported",
114 [](HookTestContext& context) {
115 Read(GetCurrentProcess(), context.PEImage(), 0x40);
116 },
117 {},
118 },
119 {
120 "Read of remote ordinary memory is not reported",
121 [](HookTestContext& context) {
122 HANDLE remote = context.RemoteProcess();
123 BYTE* page = (BYTE*)VirtualAllocEx(remote, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
124 Read(remote, page, 0x40);
125 },
126 {},
127 },
128 {
129 "Failed read of the PEB is not reported",
130 [](HookTestContext&) {
131 // The handle can query the PEB's address but not read memory
132 HANDLE self = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
133 Read(self, QueryPeb(self), 0x40);
134 CloseHandle(self);
135 },
136 {},
137 },
138};
139
140int main(int argc, char** argv) {
141 return RunHookTests(argc, argv, "NtReadVirtualMemory", g_Cases);
142}
#define PEB_PARAMETERS_OFFSET_TEXT
#define PEB_PARAMETERS_OFFSET
#define PEB_LDR_OFFSET_TEXT
#define POINTER_SIZE_TEXT
#define PEB_LDR_OFFSET
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.