Binalyzer:Phase 3 已完成!
发布: (2026年5月3日 GMT+8 06:42)
3 分钟阅读
原文: Dev.to
Source: Dev.to
概览
比较 ELF 与 PE 的节(section)分析,解释两种格式的差异以及你是如何处理的。
终于,Binalyzer 的第 3 阶段已完成!现在它能够列出 PE 和 ELF 文件的节信息。以下是本次工作的重要要点。
解析 PE 节
ELF 与 PE 本质上是不同的文件格式,因此读取和解析它们的节字段需要采用不同的方法。
- PE 文档 – 查阅官方 PE 规范,以了解节的布局。
- 读取数据 – 使用
read()和struct.unpack()从二进制流中提取字段。 - 字段覆盖 – 确保读取每个节的所有字段;遗漏字段会导致数据错位。
读取节名称
节名称会被空字节(\x00)填充。使用 Python 的 replace() 方法去除填充:
clean_name = raw_name.replace(b'\x00', b'')
处理标志位
每个标志值以十六进制数字存储,某些值是多个标志的组合。解决方案使用按位与 (&) 来检测单个标志:
if characteristics & flag == flag:
flags_dict[flag] = description
这会生成一个字典,键为标志,值为对应的描述。
示例输出
File path: /mnt/c/Windows/system32/cmd.exe
Filetype: PE
Magic number 0x20b
PE Header :
COFF Offset : 248
Signature : b'PE\x00\x00'
File Header :
Machine : x64
NumberOfSections : 8
TimeDateStamp : 2091-09-06 23:01:06+00:00
PointerToSymbolTable : 0
NumberOfSymbols : 0
SizeOfOptionalHeader (bytes) : 240
Characteristics :
0x2 : Executable file
0x20 : Can handle >2GB addresses
Optional Header :
Standard Fields :
Magic : PE32+
MajorLinkerVersion : 14
MinorLinkerVersion : 38
SizeOfCode : 233472
SizeOfInitializedData : 217088
SizeOfUnitizializedData : 0
AddressOfEntryPoint : 162592
BaseOfCode (address) : 4096
Sections :
[0]
Name : .text
VirtualSize : 0x37db6
VirtualAddress : 0x1000
SizeOfRawData : 0x38000
PointerToRawData : 0x1000
PointerToRelocations : 0x0
PointerToLinenumbers : 0x0
NumberOfRelocations : 0x0
NumberOfLinenumbers : 0x0
Characteristics :
0x0 : Reserved for future use
0x20 : Contains executable code
0x20000000 : Can be executed as code
0x40000000 : Can be read
[1]
Name : fothk
VirtualSize : 0x1000
VirtualAddress : 0x39000
SizeOfRawData : 0x1000
PointerToRawData : 0x39000
PointerToRelocations : 0x0
PointerToLinenumbers : 0x0
NumberOfRelocations : 0x0
NumberOfLinenumbers : 0x0
Characteristics :
0x0 : Reserved for future use
0x20 : Contains executable code
0x20000000 : Can be executed as code
0x40000000 : Can be read
[2]
Name : .rdata
VirtualSize : 0x9b38
VirtualAddress : 0x3a000
SizeOfRawData : 0xa000
PointerToRawData : 0x3a000
PointerToRelocations : 0x0
PointerToLinenumbers : 0x0
NumberOfRelocations : 0x0
NumberOfLinenumbers : 0x0
Characteristics :
0x0 : Reserved for future use
0x40 : Contains initialized data
0x40000000 : Can be read
后续计划
想了解此更新的更多细节,请查看 GitHub 仓库的发布页面。第 4 阶段即将启动。再见!