Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_NtMapViewOfSection.cpp
Go to the documentation of this file.
1// Tests the Hooked_NtMapViewOfSection and Hooked_NtMapViewOfSectionEx post-call check: a
2// successful map into another process is reported with the API, the base and size the kernel
3// returned, the requested protection, whether any page of the new view is executable, and
4// whether it is an image. Maps into the current process are not reported.
6
7static const std::vector<HookTestCase> g_Cases = {
8 // NtMapViewOfSection
9
10 {
11 "RWX view mapped into remote process",
12 [](HookTestContext& context) {
13 MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), context.RemoteProcess(), PAGE_EXECUTE_READWRITE);
14 },
15 { "NtMapViewOfSection: 4096-byte view mapped into a remote process at * (protection 0x40, executable)" },
16 },
17 {
18 "RX view mapped into remote process",
19 [](HookTestContext& context) {
20 MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), context.RemoteProcess(), PAGE_EXECUTE_READ);
21 },
22 { "NtMapViewOfSection: 4096-byte view mapped into a remote process at * (protection 0x20, executable)" },
23 },
24 {
25 // The executable flag comes from the view's pages, not the section's maximum protection
26 "RW view of an RWX section mapped into remote process",
27 [](HookTestContext& context) {
28 MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), context.RemoteProcess(), PAGE_READWRITE);
29 },
30 { "NtMapViewOfSection: 4096-byte view mapped into a remote process at * (protection 0x4, not executable)" },
31 },
32 {
33 "RW view mapped into remote process",
34 [](HookTestContext& context) {
35 MapView(CreatePageSection(PAGE_READWRITE), context.RemoteProcess(), PAGE_READWRITE);
36 },
37 { "NtMapViewOfSection: 4096-byte view mapped into a remote process at * (protection 0x4, not executable)" },
38 },
39 {
40 // An image's first page is its read-only header; the code section further in is executable
41 "Image mapped into remote process",
42 [](HookTestContext& context) {
43 MapView(CreateImageSection(), context.RemoteProcess(), PAGE_READONLY);
44 },
45 { "NtMapViewOfSection: *-byte image mapped into a remote process at * (protection 0x2, executable)" },
46 },
47 {
48 "RWX view mapped into current process is not reported",
49 [](HookTestContext&) {
50 MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), GetCurrentProcess(), PAGE_EXECUTE_READWRITE);
51 },
52 {},
53 },
54 {
55 "RWX view mapped through a real handle to self is not reported",
56 [](HookTestContext&) {
57 HANDLE self = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
58 MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), self, PAGE_EXECUTE_READWRITE);
59 CloseHandle(self);
60 },
61 {},
62 },
63 {
64 "Image mapped into current process is not reported",
65 [](HookTestContext&) {
66 MapView(CreateImageSection(), GetCurrentProcess(), PAGE_READONLY);
67 },
68 {},
69 },
70 {
71 // The view asks for more than the section allows, so the map fails
72 "Failed map into remote process is not reported",
73 [](HookTestContext& context) {
74 MapView(CreatePageSection(PAGE_READWRITE), context.RemoteProcess(), PAGE_EXECUTE_READWRITE);
75 },
76 {},
77 },
78 {
79 "Map through a remote handle without PROCESS_VM_OPERATION is not reported",
80 [](HookTestContext& context) {
81 HANDLE remote = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetProcessId(context.RemoteProcess()));
82 MapView(CreatePageSection(PAGE_EXECUTE_READWRITE), remote, PAGE_EXECUTE_READWRITE);
83 CloseHandle(remote);
84 },
85 {},
86 },
87
88 // NtMapViewOfSectionEx
89
90 {
91 "Ex: RWX view mapped into remote process",
92 [](HookTestContext& context) {
93 MapViewEx(CreatePageSection(PAGE_EXECUTE_READWRITE), context.RemoteProcess(), PAGE_EXECUTE_READWRITE);
94 },
95 { "NtMapViewOfSectionEx: 4096-byte view mapped into a remote process at * (protection 0x40, executable)" },
96 },
97 {
98 "Ex: image mapped into remote process",
99 [](HookTestContext& context) {
100 MapViewEx(CreateImageSection(), context.RemoteProcess(), PAGE_READONLY);
101 },
102 { "NtMapViewOfSectionEx: *-byte image mapped into a remote process at * (protection 0x2, executable)" },
103 },
104 {
105 "Ex: RWX view mapped into current process is not reported",
106 [](HookTestContext&) {
107 MapViewEx(CreatePageSection(PAGE_EXECUTE_READWRITE), GetCurrentProcess(), PAGE_EXECUTE_READWRITE);
108 },
109 {},
110 },
111 {
112 "Ex: failed map into remote process is not reported",
113 [](HookTestContext& context) {
114 MapViewEx(CreatePageSection(PAGE_READWRITE), context.RemoteProcess(), PAGE_EXECUTE_READWRITE);
115 },
116 {},
117 },
118};
119
120int main(int argc, char** argv) {
121 return RunHookTests(argc, argv, "NtMapViewOfSection", g_Cases);
122}
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...
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.
HANDLE CreateImageSection()
Creates an image section from System32.
PVOID MapViewEx(HANDLE section, HANDLE process, ULONG protection)
Maps a view of a section through NtMapViewOfSectionEx. Returns NULL if the call fails.
HANDLE CreatePageSection(DWORD protection)
Creates a one-page, pagefile-backed section with the given maximum protection.