dbghelp.h 中有很多文档化的 Helper 函数可以完全解析 Pe 文件
如果您不想使用 dbghelp.h,几乎所有这些函数在 ntdll.dll 中都有一个 Rtl 等效函数,您可以动态调用它(LoadLibrary .GetProcAddres)
例如,此函数ImageNtHeader 在 ntdll.dll 中有一个等效的 RtlImageNtHeader,您可以将其与 GetprocAddress() 一起使用
下面是一个示例代码,它使用 dbghelp.h 转储给定模块中所有部分的一些相关细节,并与 msvc 2017 社区在 win 10 64 机器上用于 x64 架构 x86 您可能需要使用相关的 32 位结构
#include <windows.h>
#include <dbghelp.h>
#include <stdio.h>
#pragma comment(lib, "dbghelp.lib")
int main(void)
{
HMODULE hMod = GetModuleHandleA("kernelbase.dll");
if (hMod)
{
PIMAGE_NT_HEADERS64 NtHeader = ImageNtHeader(hMod);
WORD NumSections = NtHeader->FileHeader.NumberOfSections;
PIMAGE_SECTION_HEADER Section = IMAGE_FIRST_SECTION(NtHeader);
for (WORD i = 0; i < NumSections; i++)
{
printf("%-8s\t%x\t%x\t%x\n", Section->Name, Section->VirtualAddress,
Section->PointerToRawData, Section->SizeOfRawData);
Section++;
}
}
return 0;
}
编译并链接并执行
cl /Zi /W4 /nologo /analyze /EHsc /Od %1 /link /release
TextSectAddr.exe
======================================
Name VA Raw Size
======================================
.text 1000 400 102600
.rdata 104000 102a00 155e00
.data 25a000 258800 1600
.pdata 25f000 259e00 e800
.didat 26e000 268600 800
.rsrc 26f000 268e00 600
.reloc 270000 269400 22400