11 BYTE* page = (BYTE*)VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
16static const std::vector<HookTestCase> g_Cases = {
18 "PE in current process made RX",
21 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_EXECUTE_READ, &old);
23 {
"NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x20)" },
26 "PE in current process made RWX",
29 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_EXECUTE_READWRITE, &old);
31 {
"NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x40)" },
34 "PE in current process made execute-only",
37 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_EXECUTE, &old);
39 {
"NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x10)" },
43 "PE in current process made RX with PAGE_GUARD",
46 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_EXECUTE_READ | PAGE_GUARD, &old);
48 {
"NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x120)" },
51 "PE in current process made RX through a real handle to self",
53 HANDLE self = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
55 VirtualProtectEx(self, AllocWithPE(context), 0x1000, PAGE_EXECUTE_READ, &old);
58 {
"NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x20)" },
62 "PE in remote process made RX",
65 BYTE* page = (BYTE*)VirtualAllocEx(remote, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
67 WriteProcessMemory(remote, page, context.
PEImage(), context.
PESize, &written);
69 VirtualProtectEx(remote, page, 0x1000, PAGE_EXECUTE_READ, &old);
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)",
77 "Region without a PE made RX is reported",
79 BYTE* page = (BYTE*)VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
80 memset(page, 0x90, 0x1000);
82 VirtualProtect(page, 0x1000, PAGE_EXECUTE_READ, &old);
84 {
"NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x20)" },
87 "Remote region without a PE made RX is reported",
90 BYTE* page = (BYTE*)VirtualAllocEx(remote, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
92 VirtualProtectEx(remote, page, 0x1000, PAGE_EXECUTE_READ, &old);
94 {
"NtProtectVirtualMemory: PE image at * in the remote process made executable (0x4 -> 0x20)" },
97 "PE made non-executable is not reported",
100 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_READONLY, &old);
105 "Executable region made non-executable is not reported",
107 BYTE* page = AllocWithPE(context);
109 VirtualProtect(page, 0x1000, PAGE_EXECUTE_READ, &old);
110 VirtualProtect(page, 0x1000, PAGE_READWRITE, &old);
112 {
"NtProtectVirtualMemory: PE image at * in the current process made executable (0x4 -> 0x20)" },
115 "Failed protection change is not reported",
118 BYTE* reserved = (BYTE*)VirtualAlloc(NULL, 0x1000, MEM_RESERVE, PAGE_READWRITE);
120 VirtualProtect(reserved, 0x1000, PAGE_EXECUTE_READ, &old);
126 "Failed change to PAGE_EXECUTE_WRITECOPY is not reported",
129 VirtualProtect(AllocWithPE(context), 0x1000, PAGE_EXECUTE_WRITECOPY, &old);
134 "Protection change through a handle without PROCESS_VM_OPERATION is not reported",
136 HANDLE self = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
138 VirtualProtectEx(self, AllocWithPE(context), 0x1000, PAGE_EXECUTE_READ, &old);
145int main(
int argc,
char** argv) {
146 return RunHookTests(argc, argv,
"NtProtectVirtualMemory", g_Cases);
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.