Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_NtProtectVirtualMemory.cpp
Go to the documentation of this file.
1// Tests the Hooked_NtProtectVirtualMemory post-call check: a successful change to any executable
2// protection (PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE or PAGE_EXECUTE_WRITECOPY,
3// with or without modifiers) is reported with whether the target is the current process and the
4// old and new protection. The region's contents are not checked, so a region without a PE image
5// is reported too, under the event's "PE image" wording.
7
10static BYTE* AllocWithPE(const HookTestContext& context) {
11 BYTE* page = (BYTE*)VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
12 memcpy(page, context.PEImage(), context.PESize);
13 return page;
14}
15
16static const std::vector<HookTestCase> g_Cases = {
17 {
18 "PE in current process made RX",
19 [](HookTestContext& context) {
20 DWORD old;
21 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_EXECUTE_READ, &old);
22 },
23 { "NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x20)" },
24 },
25 {
26 "PE in current process made RWX",
27 [](HookTestContext& context) {
28 DWORD old;
29 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_EXECUTE_READWRITE, &old);
30 },
31 { "NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x40)" },
32 },
33 {
34 "PE in current process made execute-only",
35 [](HookTestContext& context) {
36 DWORD old;
37 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_EXECUTE, &old);
38 },
39 { "NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x10)" },
40 },
41 {
42 // PAGE_GUARD is a modifier on top of the base protection, which is still executable
43 "PE in current process made RX with PAGE_GUARD",
44 [](HookTestContext& context) {
45 DWORD old;
46 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_EXECUTE_READ | PAGE_GUARD, &old);
47 },
48 { "NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x120)" },
49 },
50 {
51 "PE in current process made RX through a real handle to self",
52 [](HookTestContext& context) {
53 HANDLE self = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
54 DWORD old;
55 VirtualProtectEx(self, AllocWithPE(context), 0x1000, PAGE_EXECUTE_READ, &old);
56 CloseHandle(self);
57 },
58 { "NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x20)" },
59 },
60 {
61 // Writing the PE goes through WriteProcessMemory, so the write hook reports it first
62 "PE in remote process made RX",
63 [](HookTestContext& context) {
64 HANDLE remote = context.RemoteProcess();
65 BYTE* page = (BYTE*)VirtualAllocEx(remote, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
66 SIZE_T written;
67 WriteProcessMemory(remote, page, context.PEImage(), context.PESize, &written);
68 DWORD old;
69 VirtualProtectEx(remote, page, 0x1000, PAGE_EXECUTE_READ, &old);
70 },
71 {
72 "NtWriteVirtualMemory: PE image written to * in the remote process, destination not executable",
73 "NtProtectVirtualMemory: PE image at * in the remote process made executable (0x4 -> 0x20)",
74 },
75 },
76 {
77 "Region without a PE made RX is reported",
78 [](HookTestContext&) {
79 BYTE* page = (BYTE*)VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
80 memset(page, 0x90, 0x1000);
81 DWORD old;
82 VirtualProtect(page, 0x1000, PAGE_EXECUTE_READ, &old);
83 },
84 { "NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x20)" },
85 },
86 {
87 "Remote region without a PE made RX is reported",
88 [](HookTestContext& context) {
89 HANDLE remote = context.RemoteProcess();
90 BYTE* page = (BYTE*)VirtualAllocEx(remote, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
91 DWORD old;
92 VirtualProtectEx(remote, page, 0x1000, PAGE_EXECUTE_READ, &old);
93 },
94 { "NtProtectVirtualMemory: PE image at * in the remote process made executable (0x4 -> 0x20)" },
95 },
96 {
97 "PE made non-executable is not reported",
98 [](HookTestContext& context) {
99 DWORD old;
100 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_READONLY, &old);
101 },
102 {},
103 },
104 {
105 "Executable region made non-executable is not reported",
106 [](HookTestContext& context) {
107 BYTE* page = AllocWithPE(context);
108 DWORD old;
109 VirtualProtect(page, 0x1000, PAGE_EXECUTE_READ, &old);
110 VirtualProtect(page, 0x1000, PAGE_READWRITE, &old);
111 },
112 { "NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x20)" },
113 },
114 {
115 "Failed protection change is not reported",
116 [](HookTestContext&) {
117 // Reserved but not committed, so the protection change fails
118 BYTE* reserved = (BYTE*)VirtualAlloc(NULL, 0x1000, MEM_RESERVE, PAGE_READWRITE);
119 DWORD old;
120 VirtualProtect(reserved, 0x1000, PAGE_EXECUTE_READ, &old);
121 },
122 {},
123 },
124 {
125 // Private memory cannot be made copy-on-write, so the call fails
126 "Failed change to PAGE_EXECUTE_WRITECOPY is not reported",
127 [](HookTestContext& context) {
128 DWORD old;
129 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_EXECUTE_WRITECOPY, &old);
130 },
131 {},
132 },
133 {
134 "Protection change through a handle without PROCESS_VM_OPERATION is not reported",
135 [](HookTestContext& context) {
136 HANDLE self = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
137 DWORD old;
138 VirtualProtectEx(self, AllocWithPE(context), 0x1000, PAGE_EXECUTE_READ, &old);
139 CloseHandle(self);
140 },
141 {},
142 },
143};
144
145int main(int argc, char** argv) {
146 return RunHookTests(argc, argv, "NtProtectVirtualMemory", g_Cases);
147}
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...
static constexpr SIZE_T PESize
The number of PEImage() bytes the cases copy, one page.
const BYTE * PEImage() const
A buffer holding a real PE image: this executable's own mapped headers.
HANDLE RemoteProcess() const
A suspended cmd.exe of the same architecture to use as a remote target.
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.