Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_NtUnmapViewOfSection.cpp
Go to the documentation of this file.
1// Tests the Hooked_NtUnmapViewOfSection and Hooked_NtUnmapViewOfSectionEx checks. The view is
2// queried before the call (pre-check), because once it is unmapped its protection is gone, and
3// reported only if the unmap then succeeds (post-check). A view with any executable page is
4// reported with the API, whether the target is the current process, the address passed in, and
5// whether it was an image. The whole view is walked, so an image unmapped by its base, whose
6// first page is the read-only PE header, still counts as executable.
8#include <winternl.h>
9
10static const std::vector<HookTestCase> g_Cases = {
11 // NtUnmapViewOfSection
12
13 {
14 "RWX view unmapped from current process",
15 [](HookTestContext&) {
16 UnmapView(GetCurrentProcess(), MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), GetCurrentProcess(), PAGE_EXECUTE_READWRITE));
17 },
18 { "NtUnmapViewOfSection: executable view at * unmapped from the current process" },
19 },
20 {
21 "RX view unmapped from current process",
22 [](HookTestContext&) {
23 UnmapView(GetCurrentProcess(), MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), GetCurrentProcess(), PAGE_EXECUTE_READ));
24 },
25 { "NtUnmapViewOfSection: executable view at * unmapped from the current process" },
26 },
27 {
28 "Image unmapped from current process by its base",
29 [](HookTestContext&) {
30 UnmapView(GetCurrentProcess(), MapView(CreateImageSection(), GetCurrentProcess(), PAGE_READONLY));
31 },
32 { "NtUnmapViewOfSection: executable image at * unmapped from the current process" },
33 },
34 {
35 // Any address inside a view unmaps the whole view
36 "Image unmapped from current process by an address inside it",
37 [](HookTestContext&) {
38 BYTE* base = (BYTE*)MapView(CreateImageSection(), GetCurrentProcess(), PAGE_READONLY);
39 UnmapView(GetCurrentProcess(), base + 0x1000);
40 },
41 { "NtUnmapViewOfSection: executable image at * unmapped from the current process" },
42 },
43 {
44 "RWX view unmapped through a real handle to self",
45 [](HookTestContext&) {
46 HANDLE self = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
47 UnmapView(self, MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), self, PAGE_EXECUTE_READWRITE));
48 CloseHandle(self);
49 },
50 { "NtUnmapViewOfSection: executable view at * unmapped from the current process" },
51 },
52 {
53 // The pre-check queries the view with the caller's handle, so the handle needs query access
54 "RWX view unmapped through a handle with only limited query access",
55 [](HookTestContext&) {
56 PVOID view = MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), GetCurrentProcess(), PAGE_EXECUTE_READWRITE);
57 HANDLE self = OpenProcess(PROCESS_VM_OPERATION | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
58 UnmapView(self, view);
59 CloseHandle(self);
60 },
61 { "NtUnmapViewOfSection: executable view at * unmapped from the current process" },
62 },
63 {
64 // Mapping into the target is reported by the map hook first
65 "Image unmapped from remote process",
66 [](HookTestContext& context) {
67 HANDLE remote = context.RemoteProcess();
68 UnmapView(remote, MapView(CreateImageSection(), remote, PAGE_READONLY));
69 },
70 {
71 "NtMapViewOfSection: *-byte image mapped into a remote process at * (protection 0x2, executable)",
72 "NtUnmapViewOfSection: executable image at * unmapped from the remote process",
73 },
74 },
75 {
76 "RW view unmapped from current process is not reported",
77 [](HookTestContext&) {
78 UnmapView(GetCurrentProcess(), MapView(CreatePageSection(PAGE_READWRITE), GetCurrentProcess(), PAGE_READWRITE));
79 },
80 {},
81 },
82 {
83 "RW view of an RWX section unmapped from current process is not reported",
84 [](HookTestContext&) {
85 UnmapView(GetCurrentProcess(), MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), GetCurrentProcess(), PAGE_READWRITE));
86 },
87 {},
88 },
89 {
90 // The pre-check sees executable memory, but private memory is not a view, so the unmap
91 // fails and nothing is reported. The allocation itself is reported by the allocate hook
92 "Failed unmap of executable private memory is not reported",
93 [](HookTestContext&) {
94 UnmapView(GetCurrentProcess(), VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE));
95 },
96 { "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)" },
97 },
98 {
99 "Failed unmap through a handle without PROCESS_VM_OPERATION is not reported",
100 [](HookTestContext&) {
101 PVOID view = MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), GetCurrentProcess(), PAGE_EXECUTE_READWRITE);
102 HANDLE self = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
103 UnmapView(self, view);
104 CloseHandle(self);
105 },
106 {},
107 },
108
109 // NtUnmapViewOfSectionEx
110
111 {
112 "Ex: RWX view unmapped from current process",
113 [](HookTestContext&) {
114 UnmapViewEx(GetCurrentProcess(), MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), GetCurrentProcess(), PAGE_EXECUTE_READWRITE));
115 },
116 { "NtUnmapViewOfSectionEx: executable view at * unmapped from the current process" },
117 },
118 {
119 "Ex: image unmapped from remote process",
120 [](HookTestContext& context) {
121 HANDLE remote = context.RemoteProcess();
122 UnmapViewEx(remote, MapView(CreateImageSection(), remote, PAGE_READONLY));
123 },
124 {
125 "NtMapViewOfSection: *-byte image mapped into a remote process at * (protection 0x2, executable)",
126 "NtUnmapViewOfSectionEx: executable image at * unmapped from the remote process",
127 },
128 },
129 {
130 "Ex: RW view unmapped from current process is not reported",
131 [](HookTestContext&) {
132 UnmapViewEx(GetCurrentProcess(), MapView(CreatePageSection(PAGE_READWRITE), GetCurrentProcess(), PAGE_READWRITE));
133 },
134 {},
135 },
136 {
137 "Ex: failed unmap of executable private memory is not reported",
138 [](HookTestContext&) {
139 UnmapViewEx(GetCurrentProcess(), VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE));
140 },
141 { "NtAllocateVirtualMemory: 4096 executable bytes allocated at * in the current process (protection 0x40)" },
142 },
143
144 // Last, because it unmaps the remote target's own image
145
146 {
147 // The first step of process hollowing: find the target's image base in its PEB, then
148 // unmap it. Reading the PEB is reported by the read hook
149 "Remote process's main image unmapped (process hollowing)",
150 [](HookTestContext& context) {
151 HANDLE remote = context.RemoteProcess();
152 PROCESS_BASIC_INFORMATION info{};
153 NtdllFunction<decltype(&NtQueryInformationProcess)>("NtQueryInformationProcess")(
154 remote, ProcessBasicInformation, &info, sizeof(info), NULL);
155 PVOID imageBase = NULL;
156 SIZE_T bytesRead;
157 ReadProcessMemory(remote, (BYTE*)info.PebBaseAddress + 2 * sizeof(void*), &imageBase, sizeof(imageBase), &bytesRead);
158 UnmapView(remote, imageBase);
159 },
160 {
161 std::string("NtReadVirtualMemory: ") + (sizeof(void*) == 8 ? "8" : "4") +
162 " bytes read from the PEB of the remote process at offset " + (sizeof(void*) == 8 ? "0x10" : "0x8"),
163 "NtUnmapViewOfSection: executable image at * unmapped from the remote process",
164 },
165 },
166};
167
168int main(int argc, char** argv) {
169 return RunHookTests(argc, argv, "NtUnmapViewOfSection", g_Cases);
170}
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.
PVOID MapView(HANDLE section, HANDLE process, ULONG protection)
Maps a view of a section through NtMapViewOfSection. Returns NULL if the call fails.
NTSTATUS UnmapView(HANDLE process, PVOID address)
HANDLE CreateImageSection()
Creates an image section from System32.
NTSTATUS UnmapViewEx(HANDLE process, PVOID address)
HANDLE CreatePageSection(DWORD protection)
Creates a one-page, pagefile-backed section with the given maximum protection.