2y ago · Aug 11, 2024 11:25 AM
Every 64-bit Windows dynamic link library (.dll) or executable (.exe) in memory follows the Portable Executable (PE) format.
Parsing Structure Hierarchy:
This is the exact method used to locate the section boundaries when scanning for function signatures!
Parsing Structure Hierarchy:
CPP
#include <windows.h>
#include <iostream>
void InspectPE(uintptr_t moduleBase) {
// 1. DOS Header
auto dosHeader = reinterpret_cast<PIMAGE_DOS_HEADER>(moduleBase);
if (dosHeader->e_magic != IMAGE_DOS_SIGNATURE) return; // 'MZ'
// 2. NT Headers (x64)
auto ntHeaders = reinterpret_cast<PIMAGE_NT_HEADERS64>(moduleBase + dosHeader->e_lfanew);
if (ntHeaders->Signature != IMAGE_NT_SIGNATURE) return; // 'PE '
std::cout << "Entry Point RVA: 0x" << std::hex << ntHeaders->OptionalHeader.AddressOfEntryPoint << std::endl;
std::cout << "Image Size: 0x" << ntHeaders->OptionalHeader.SizeOfImage << " bytes" << std::endl;
// 3. Section Headers
auto section = IMAGE_FIRST_SECTION(ntHeaders);
for (WORD i = 0; i < ntHeaders->FileHeader.NumberOfSections; ++i, ++section) {
char name[9] = { 0 };
memcpy(name, section->Name, 8);
std::cout << "Section: " << name
<< " | RVA: 0x" << section->VirtualAddress
<< " | Size: 0x" << section->Misc.VirtualSize << std::endl;
}
}This is the exact method used to locate the
CODE
.text
PEHeader | IMAGE_NT_HEADERS & Section Parsing
The following users thanked PEHeader for this post: