MCPcopy Create free account
hub / github.com/changeofpace/Self-Remapping-Code / ValidateSectionAlignment

Function ValidateSectionAlignment

Self-Remapping Code/main.cpp:13–47  ·  view source on GitHub ↗

Verify that .rdata and .data's base addresses are a multiple of the system allocation granularity.

Source from the content-addressed store, hash-verified

11// Verify that .rdata and .data's base addresses are a multiple of the system
12// allocation granularity.
13static bool ValidateSectionAlignment(const PVOID ImageBase)
14{
15 PE_HEADER pe;
16 if (!FillPEHeader(SIZE_T(ImageBase), pe))
17 return false;
18
19 const PIMAGE_SECTION_HEADER text = GetPeSectionByName(pe, ".text");
20 const PIMAGE_SECTION_HEADER rdata = GetPeSectionByName(pe, ".rdata");
21 const PIMAGE_SECTION_HEADER data = GetPeSectionByName(pe, ".data");
22 if (!(text && text < rdata && rdata < data))
23 return false;
24
25 auto printSectionSummary = [&ImageBase](const PIMAGE_SECTION_HEADER Section)
26 {
27 printf("%-8.8s %p +%016X %16X\n",
28 Section->Name,
29 SIZE_T(ImageBase) + Section->VirtualAddress,
30 Section->VirtualAddress,
31 Section->Misc.VirtualSize);
32 };
33
34 printSectionSummary(text);
35 printSectionSummary(rdata);
36 printSectionSummary(data);
37
38 SYSTEM_INFO si;
39 GetSystemInfo(&si);
40
41 auto isAligned = [&si](const PIMAGE_SECTION_HEADER Section)
42 {
43 return Section->VirtualAddress % si.dwAllocationGranularity == 0;
44 };
45
46 return isAligned(rdata) && isAligned(data);
47}
48
49// Verify that we're unable set .text or .rdata's page protection via NtProtectVirtualMemory.
50// e.g., changing .text from PAGE_EXECUTE_READ to PAGE_EXECUTE_READWRITE should

Callers 1

mainFunction · 0.85

Calls 2

FillPEHeaderFunction · 0.85
GetPeSectionByNameFunction · 0.85

Tested by

no test coverage detected