Re format pe

From dslsdzc/rev-skills · View interactive version

Re format pe is an AI agent skill from the GitHub repository dslsdzc/rev-skills. PE 格式解析:DOS/NT 头、节表、导入导出、TLS 回调、Rich Header、证书表。 触发词:PE格式、解析exe、section表、Delphi、VB6、vmt、MSVBVM60、RTTI、老程序

How to use this skill

Skill file

PE 格式解析

何时使用 / 何时不用

工具准备

参考 [[re-analyze/platform-tips]] 平台分支——Windows 目标可静态分析免沙箱,动态执行一律沙箱。

objdump(binutils)

pefile(Python,推荐主力)

CFF Explorer(Windows GUI)

操作步骤

  1. e_lfanew 定位 PE 头:

```sh

objdump -p sample.exe | head -40 # 看 Magic: 0x10b (PE32) / 0x20b (PE32+) 与入口点

```

```python

import pefile

pe = pefile.PE('sample.exe')

print("e_lfanew offset:", hex(pe.DOS_HEADER.e_lfanew))

print("PE32" if pe.OPTIONAL_HEADER.Magic == 0x10b else "PE32+")

print("EntryPoint RVA:", hex(pe.OPTIONAL_HEADER.AddressOfEntryPoint))

```

  1. 节表与 RVA/文件偏移映射:

```sh

objdump -h sample.exe # 节名/VMA/文件偏移/大小

```

```python

import pefile

pe = pefile.PE('sample.exe')

for s in pe.sections:

print(s.Name, "VA:", hex(s.VirtualAddress), "raw:", hex(s.PointerToRawData),

hex(s.SizeOfRawData), "chars:", hex(s.Characteristics))

print("RVA->offset:", hex(pe.get_offset_from_rva(0x1000))) # RVA 转文件偏移

```

记住: 节内存中的 VirtualAddress 是 RVA,需加 ImageBase 才是 VA;文件偏移由 PointerToRawData 给出。

  1. 导入表(IAT 大小异常 = 壳特征):

```sh

objdump -p sample.exe | grep -A3 -i 'DLL Name'

```

```python

import pefile

pe = pefile.PE('sample.exe')

for entry in pe.DIRECTORY_ENTRY_IMPORT:

names = [i.name for i in entry.imports if i.name]

print(entry.dll, "imports:", len(names), names[:5])

```

  1. TLS 回调检查(比入口点更早执行):

```python

import pefile

pe = pefile.PE('sample.exe')

if hasattr(pe, 'DIRECTORY_ENTRY_TLS') and pe.DIRECTORY_ENTRY_TLS:

# AddressOfCallBacks 按 VA 存放:VA - ImageBase = RVA(只减一次)

cb_rva = pe.DIRECTORY_ENTRY_TLS.struct.AddressOfCallBacks - pe.OPTIONAL_HEADER.ImageBase

ptr_size = 8 if pe.OPTIONAL_HEADER.Magic == 0x20b else 4 # PE32+(0x20b)/PE32(0x10b) 才是位宽判据;Machine==0x8664 只说明 x64,ARM64(0xAA64) 同样是 PE32+

while True: # 回调数组以 0 结尾

ptr = int.from_bytes(pe.get_data(cb_rva, ptr_size), 'little')

if ptr == 0:

break

print('TLS callback VA:', hex(ptr))

cb_rva += ptr_size

```

简单方法: objdump -p sample.exe 输出中查看 TLS 目录表存在与否;存在则用 pefile 打印回调地址列表,逐条看反汇编。回调在入口点(AddressOfEntryPoint)之前执行——恶意样本常在此放解密/反调试代码。

  1. Rich Header 识别编译器:

```python

import pefile

pe = pefile.PE('sample.exe')

rh = pe.parse_rich_header()

if rh:

vals = rh['values'] # 扁平整数数组 [compid, count, compid, count, ...](pefile 已按 key 解密)

for cid, count in zip(vals[0::2], vals[1::2]):

# compid 高 16 位是工具 Product ID、低 16 位是 build(如 0x10b=VS2015 系)

print(f"compid={cid:#x} tool={cid>>16:#x} build={cid & 0xffff} count={count}")

```

Rich Header 位于 DOS stub 之后、e_lfanew 之前,标识编译工具链版本;被移除/伪造本身也是加壳或手工修改特征。

  1. 证书表:SECURITY_DIRECTORY(目录项 4)的 VirtualAddress 是文件偏移而非 RVA——直接按 RVA 解析会读错位置:

```python

import pefile

pe = pefile.PE('sample.exe')

sec = pe.OPTIONAL_HEADER.DATA_DIRECTORY[4]

print("cert table file offset:", hex(sec.VirtualAddress), "size:", hex(sec.Size))

```

老运行时识别(Delphi / VB6)

老 PE 样本里两类遗留运行时——Delphi(VCL)与 VB6——结构特征鲜明,不必等反编译,格式解析阶段(节表/资源/导入)即可判定。

Delphi(VCL)特征与定位

```python

import pefile

pe = pefile.PE('sample.exe')

if hasattr(pe, 'DIRECTORY_ENTRY_RESOURCE'):

for entry in pe.DIRECTORY_ENTRY_RESOURCE.entries:

name = entry.name.string if entry.name else None

print(name, hex(entry.struct.Type))

```

VB6 特征与入口定位

老壳注意

识别先行原则

常见坑

跨域联合

常见坑与陷阱

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.