Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
hook_test_sections.hpp
Go to the documentation of this file.
1#pragma once
2// Section helpers for the map and unmap hook tests. The views are mapped and unmapped by calling
3// the ntdll exports directly, which go through the hooks, so each test controls exactly which
4// export (plain or Ex) is exercised.
6
7#define TEST_NT_SUCCESS(status) ((NTSTATUS)(status) >= 0)
8
10#define TEST_VIEW_UNMAP 2
11
12typedef NTSTATUS(NTAPI* NtMapViewOfSection_t)(HANDLE SectionHandle, HANDLE ProcessHandle,
13 PVOID* BaseAddress, ULONG_PTR ZeroBits, SIZE_T CommitSize, PLARGE_INTEGER SectionOffset,
14 PSIZE_T ViewSize, DWORD InheritDisposition, ULONG AllocationType, ULONG Win32Protect);
15typedef NTSTATUS(NTAPI* NtMapViewOfSectionEx_t)(HANDLE SectionHandle, HANDLE ProcessHandle,
16 PVOID* BaseAddress, PLARGE_INTEGER SectionOffset, PSIZE_T ViewSize, ULONG AllocationType,
17 ULONG PageProtection, PMEM_EXTENDED_PARAMETER ExtendedParameters, ULONG ExtendedParameterCount);
18typedef NTSTATUS(NTAPI* NtUnmapViewOfSection_t)(HANDLE ProcessHandle, PVOID BaseAddress);
19typedef NTSTATUS(NTAPI* NtUnmapViewOfSectionEx_t)(HANDLE ProcessHandle, PVOID BaseAddress, ULONG Flags);
20
22inline HANDLE CreatePageSection(DWORD protection) {
23 return CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, protection | SEC_COMMIT, 0, 0x1000, NULL);
24}
25
29inline HANDLE CreateImageSection() {
30 char path[MAX_PATH];
31 GetSystemDirectoryA(path, MAX_PATH);
32 strcat_s(path, "\\version.dll");
33 HANDLE file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
34 if (file == INVALID_HANDLE_VALUE) {
35 fprintf(stderr, " failed to open %s (error %lu)\n", path, GetLastError());
36 return NULL;
37 }
38 HANDLE section = CreateFileMappingA(file, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL);
39 CloseHandle(file);
40 return section;
41}
42
44inline PVOID MapView(HANDLE section, HANDLE process, ULONG protection) {
45 auto map = NtdllFunction<NtMapViewOfSection_t>("NtMapViewOfSection");
46 PVOID base = NULL;
47 SIZE_T size = 0;
48 NTSTATUS status = map(section, process, &base, 0, 0, NULL, &size, TEST_VIEW_UNMAP, 0, protection);
49 return TEST_NT_SUCCESS(status) ? base : NULL;
50}
51
53inline PVOID MapViewEx(HANDLE section, HANDLE process, ULONG protection) {
54 auto map = NtdllFunction<NtMapViewOfSectionEx_t>("NtMapViewOfSectionEx");
55 if (map == NULL) {
56 fprintf(stderr, " NtMapViewOfSectionEx is not exported by this ntdll\n");
57 return NULL;
58 }
59 PVOID base = NULL;
60 SIZE_T size = 0;
61 NTSTATUS status = map(section, process, &base, NULL, &size, 0, protection, NULL, 0);
62 return TEST_NT_SUCCESS(status) ? base : NULL;
63}
64
65inline NTSTATUS UnmapView(HANDLE process, PVOID address) {
66 return NtdllFunction<NtUnmapViewOfSection_t>("NtUnmapViewOfSection")(process, address);
67}
68
69inline NTSTATUS UnmapViewEx(HANDLE process, PVOID address) {
70 auto unmap = NtdllFunction<NtUnmapViewOfSectionEx_t>("NtUnmapViewOfSectionEx");
71 if (unmap == NULL) {
72 fprintf(stderr, " NtUnmapViewOfSectionEx is not exported by this ntdll\n");
73 return (NTSTATUS)0xC0000002; // STATUS_NOT_IMPLEMENTED
74 }
75 return unmap(process, address, 0);
76}
#define MAX_PATH
Definition callbacks.h:7
T NtdllFunction(const char *name)
Looks up an ntdll export. Calls through the result go through the hooks, because the hooks patch the ...
#define TEST_NT_SUCCESS(status)
NTSTATUS(NTAPI * NtMapViewOfSectionEx_t)(HANDLE SectionHandle, HANDLE ProcessHandle, PVOID *BaseAddress, PLARGE_INTEGER SectionOffset, PSIZE_T ViewSize, ULONG AllocationType, ULONG PageProtection, PMEM_EXTENDED_PARAMETER ExtendedParameters, ULONG ExtendedParameterCount)
NTSTATUS(NTAPI * NtUnmapViewOfSectionEx_t)(HANDLE ProcessHandle, PVOID BaseAddress, ULONG Flags)
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)
NTSTATUS(NTAPI * NtMapViewOfSection_t)(HANDLE SectionHandle, HANDLE ProcessHandle, PVOID *BaseAddress, ULONG_PTR ZeroBits, SIZE_T CommitSize, PLARGE_INTEGER SectionOffset, PSIZE_T ViewSize, DWORD InheritDisposition, ULONG AllocationType, ULONG Win32Protect)
PVOID MapViewEx(HANDLE section, HANDLE process, ULONG protection)
Maps a view of a section through NtMapViewOfSectionEx. Returns NULL if the call fails.
#define TEST_VIEW_UNMAP
SECTION_INHERIT::ViewUnmap: the view is not mapped into child processes.
HANDLE CreatePageSection(DWORD protection)
Creates a one-page, pagefile-backed section with the given maximum protection.
NTSTATUS(NTAPI * NtUnmapViewOfSection_t)(HANDLE ProcessHandle, PVOID BaseAddress)