c - PE部分数据

标签 c winapi

我有一个可移植可执行 (.exe) 文件,我想检索它的部分。我喜欢这样:

IMAGE_DOS_HEADER* pDOSHeader = (IMAGE_DOS_HEADER*) buffer; // DOS Header
IMAGE_NT_HEADERS* pNTHeaders = (IMAGE_NT_HEADERS*) ((BYTE *) pDOSHeader + pDOSHeader->e_lfanew); // PE Header   

到目前为止一切都很好。我发现了这个:

The section table: This follows immediately after the PE header. It is an array of IMAGE_SECTION_HEADER structures, each containing the information about one section in the PE file such as its attribute and virtual offset. Remember the number of sections is the second member of FileHeader (6 bytes from the start of the PE header). If there are 8 sections in the PE file, there will be 8 duplicates of this structure in the table. Each header structure is 40 bytes apiece and there is no "padding" between them.

现在,当我这样做时:

IMAGE_SECTION_HEADER* pSection = (IMAGE_SECTION_HEADER*) (pNTHeaders->FileHeader.PointerToSymbolTable);

或:

IMAGE_SECTION_HEADER* pSection = (IMAGE_SECTION_HEADER*) ((BYTE *) pNTHeaders + sizeof(IMAGE_NT_HEADERS));

pSection 的地址为 NULL (0)。

我必须提到 buffer 变量是我读取 PE 数据的地方。

最佳答案

您需要使用 SizeOfOptionalHeader 值来跳过可选 header 。不要依赖它的固定大小。 winnt.h 有一个你应该使用的宏:

#define IMAGE_FIRST_SECTION( ntheader ) ((PIMAGE_SECTION_HEADER)  \
    ((ULONG_PTR)(ntheader) +                                      \
     FIELD_OFFSET( IMAGE_NT_HEADERS, OptionalHeader ) +           \
     ((ntheader))->FileHeader.SizeOfOptionalHeader   \
    ))

关于c - PE部分数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23498583/

相关文章:

c - 在 C 中的内联函数中循环展开

c - http 服务器 (c) - 发送命令不起作用(来自服务器的空回复)

如果文件存在,调用 open() 创建文件失败

security - LsaEnumerateAccountRights 总是返回 "File not found"

c - 内存分配C

objective-c - 如何在没有解析器生成器的情况下用 C 或 Objective-C 编写解析器?

c# - 在 C#/.NET 中访问超出 MAX_PATH 的文件

windows - Windows SDK 里面有什么?

delphi - 使用 WM_SETFOCUS 和 WM_KILLFOCUS

delphi - LsaAddAccountRights 不适合我