PE 文件解析

PE 文件类型

可移植性可执行文件(Portable Executable,缩写为PE)是一种用于可执行文件、目标文件和动态链接库的文件格式,主要使用在32位和64位的Windows操作系统上。PE文件格式封装了Windows操作系统加载可执行程序代码时所必需的一些信息,包括动态链接库、API导入和导出表、资源管理数据和线程局部存储数据。常见的 PE 文件有 EXE、DLL、OCX、SYS、COM。

PE格式是由Unix中的COFF(Common Object File Format,如 .obj)格式修改而来的。在Windows开发环境中,PE格式也称为 PE/COFF 格式,开头为 DOS 头部。

PE 文件格式

pe-format

PE 文件的基本结构,主要由四部分组成:

  • MS-DOS Header,PE文件的第一个字节起始于MS-DOS头部。Magic 为 0x4D5A(“MZ”),大小 64 字节。
  • MS-DOS Stub,整个DOS Stub是一个字节块,其内容随着链接时使用的链接器不同而不同,默认的Stub会打印出消息”This program cannot be run in DOS mode”。
  • PE signature,PE 头的起始标识 Signature,0x50450000(“PE00”),大小 4 字节。
  • File header,PE 的标准头部,记录了PE文件的全局属性,如该PE文件运行的平台、PE文件类型、文件中存在的节的总数等。大小 20 字节。
  • Optional header,包括文件执行时的入口地址、文件被操作系统装入内存后的默认基地址,以及节在磁盘和内存中的对齐单位等信息。(修改此头部内容可能会导致 PE 文件加载或运行失败)
  • Section table,节表,包括多组 Section Header 和 Section Content。其中每个 Section header 大小为 40 字节。

注意:除了 ELF Header,其他节点的顺序都不固定。段(Segment)与节(Section)的区别在于,段是程序执行的必要组成,当多个目标文件链接成一个可执行文件时,会将相同权限的节合并到一个段中。相比而言,节的粒度更小。

PE 头部信息

PE_32_bit_Structure

MS-DOS Header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
IMAGE_DOS_HEADER {
WORD e_magic; // +0000h - EXE标志,“MZ”
WORD e_cblp; // +0002h - 最后(部分)页中的字节数
WORD e_cp; // +0004h - 文件中的全部和部分页数
WORD e_crlc; // +0006h - 重定位表中的指针数
WORD e_cparhdr; // +0008h - 头部尺寸,以段落为单位
WORD e_minalloc; // +000ah - 所需的最小附加段
WORD e_maxalloc; // +000ch - 所需的最大附加段
WORD e_ss; // +000eh - 初始的SS值(相对偏移量)
WORD e_sp; // +0010h - 初始的SP值
WORD e_csum; // +0012h - 补码校验值
WORD e_ip; // +0014h - 初始的IP值
WORD e_cs; // +0016h - 初始的CS值
WORD e_lfarlc; // +0018h - 重定位表的字节偏移量
WORD e_ovno; // +001ah - 覆盖号
WORD e_res[4]; // +001ch - 保留字00
WORD e_oemid; // +0024h - OEM标识符
WORD e_oeminfo; // +0026h - OEM信息
WORD e_res2[10]; // +0028h - 保留字
LONG e_lfanew; // +003ch - PE头相对于文件的偏移地址
}

DOS Header 从文件的起始位置开始,被称作 IMAGE_DOS_HEADER。在其 0x3C 位置,可以找到 e_lfanew,即 PE 头相对文件的偏移地址,即 PE signature(0x50450000)的地址。也可以通过此信息,计算出 DOS Stub 的大小。

NT Header

PE 文件部分紧跟在 DOS Stub 后面,其中 PE signature、 File header、Optional header 被称作 IMAGE_NT_HEADERS。。

1
2
3
4
5
IMAGE_NT_HEADERS {
DWORD Signature; // +0000h - PE文件标识,“PE00”
IMAGE_FILE_HEADER FileHeader; // +0004h - PE标准头
IMAGE_OPTIONAL_HEADER32 OptionalHeader; // +0018h - PE扩展头
}

File Header

PE signature 之后才是标准的 PE 头:File Header,被称作 IMAGE_FILE_HEADER

1
2
3
4
5
6
7
8
9
IMAGE_FILE_HEADER {
WORD Machine; // +0004h - 运行平台
WORD NumberOfSections; // +0006h - PE中节的数量
DWORD TimeDateStamp; // +0008h - 文件创建日期和时间
DWORD PointerToSymbolTable; // +000ch - 指向符号表
DWORD NumberOfSymbols; // +0010h - 符号表中的符号数量
WORD SizeOfOptionalHeader; // +0014h - 扩展头结构的长度
WORD Characteristics; // +0016h - 文件属性
}
  1. 0x0004h 位置的 Machine 标记 PE 文件的运行平台。(常见的平台:0x8664,AMD64/X64平台。0x14c,I386)
  2. 0x0006h 位置的 NumberOfSections 记录了 PE 文件中 Section 的个数。
  3. 0x000ch 位置的 PointerToSymbolTable 记录了符号表的偏移位置。
  4. 0x000ch 位置的 Characteristics 记录了文件相关属性,每种属性占一个标记位。(x0002,可执行标记,表示文件有效可运行。0x2000,标记文件为 DLL 动态库)

Optional Header

每个 PE 文件都有一个向加载程序提供信息的可选标头,被称作 IMAGE_OPTIONAL_HEADER32。其大小在 File header 中指定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
IMAGE_OPTIONAL_HEADER {
WORD Magic; // +0018h - 魔术字
BYTE MajorLinkerVersion; // +001ah - 链接器版本号
BYTE MinorLinkerVersion; // +001bh -
DWORD SizeOfCode; // +001ch - 所有含代码的节的总大小
DWORD SizeOfInitializedData; // +0020h - 所有含已初始化数据的节的总大小
DWORD SizeOfUninitializedData; // +0024h - 所有含未初始化数据的节的大小
DWORD AddressOfEntryPoint; // +0028h - 程序执行入口RVA
DWORD BaseOfCode; // +002ch - 代码的节的起始RVA
DWORD BaseOfData; // +0030h - 数据的节的起始RVA
DWORD ImageBase; // +0034h - 程序的建议装载地址
DWORD SectionAlignment; // +0038h - 内存中的节的对齐粒度
DWORD FileAlignment; // +003ch - 文件中的节的对齐粒度
WORD MajorOperatingSystemVersion; // +0040h - 操作系统版本号
WORD MinorOperatingSystemVersion; // +0042h -
WORD MajorImageVersion; // +0044h - 该PE的版本号
WORD MinorImageVersion; // +0046h -
WORD MajorSubsystemVersion; // +0048h - 所需子系统的版本号
WORD MinorSubsystemVersion; // +004ah -
DWORD Win32VersionValue; // +004ch - 未用
DWORD SizeOfImage; // +0050h - 内存中的整个PE映象尺寸
DWORD SizeOfHeaders; // +0054h - 所有头+节表的大小
DWORD CheckSum; // +0058h - 校验和
WORD Subsystem; // +005ch - 文件的子系统
WORD DllCharacteristics; // +005eh - DLL文件特性
DWORD SizeOfStackReserve; // +0060h - 初始化时的栈大小
DWORD SizeOfStackCommit; // +0064h - 初始化时实际提交的栈大小
DWORD SizeOfHeapReserve; // +0068h - 初始化时保留的堆大小
DWORD SizeOfHeapCommit; // +006ch - 初始化时实际提交的堆大小
DWORD LoaderFlags; // +0070h - 与调试有关
DWORD NumberOfRvaAndSizes; // +0074h - 下面的数据目录结构的项目数量
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; // 0078h - 数据目录
}
  1. 0x0018 位置的 Magic 标记了可执行文件类型,。(0x10b:PE32,0x20b:PE32+,即 PE64,允许 64 位地址空间,同时将 Image 大小限制为 2 GB)
  2. 0x0028 位置的 AddressOfEntryPoint,记录了程序执行入口地址 RVA(Relative virtual address)。
  3. 0x0034 位置的 ImageBase,记录了加载到内存中的图像的第一个字节的首选地址,必须是 64 K 的倍数。DLL 的默认值为0x10000000。,RVA + ImageBase 才是真正的地址 VA(virtual address)。
  4. 0x0074 位置的 NumberOfRvaAndSizes 和 DataDirectory,定义了 PE 文件中出现的所有不同类型的数据的目录信息,从Windows NT 3.1操作系统开始到现在,该数据目录中定义的数据类型一直是 16 种。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
IMAGE_DATA_DIRECTORY {
DWORD VirtualAddress; // +0000h - 数据的起始RVA
DWORD Size; // +0004h - 数据块的长度
}

#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 // Export Directory
#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 // Import Directory
#define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 // Resource Directory
#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 // Exception Directory
#define IMAGE_DIRECTORY_ENTRY_SECURITY 4 // Security Directory
#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 // Base Relocation Table
#define IMAGE_DIRECTORY_ENTRY_DEBUG 6 // Debug Directory
#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 // Architecture Specific Data
#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 // RVA of GP
#define IMAGE_DIRECTORY_ENTRY_TLS 9 // TLS Directory
#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 // Load Configuration Directory
#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 // Bound Import Directory in headers
#define IMAGE_DIRECTORY_ENTRY_IAT 12 // Import Address Table
#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 // Delay Load Import Descriptors
#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 // COM Runtime descriptor

Section Header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
IMAGE_SECTION_HEADER {
BYTE Name[IMAGE_SIZEOF_SHORT_NAME]; // +0000h - 8个字节节名
union {
DWORD PhysicalAddress;
DWORD VirtualSize;
} Misc; // +0008h - 节区的尺寸
DWORD VirtualAddress; // +000ch - 节区的RVA地址(相对虚拟地址)
DWORD SizeOfRawData; // +0010h - 在文件中对齐后的尺寸
DWORD PointerToRawData; // +0014h - 在文件中的偏移(文件偏移地址)
DWORD PointerToRelocations; // +0018h - 在OBJ文件中使用
DWORD PointerToLinenumbers; // +001ch - 行号表的位置(供调试用)
WORD NumberOfRelocations; // +0020h - 在OBJ文件中使用
WORD NumberOfLinenumbers; // +0022h - 行号表中行号的数量
DWORD Characteristics; // +0024h - 节的属性
}

//节的属性 Characteristics 比较常见的定义如下,更多信息见 winnt.h 的定义
#define IMAGE_SCN_CNT_CODE 0x00000020 // 此节包含代码
#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 // 此节包含已初始化数据
#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 // 此节包含未初始化数据
#define IMAGE_SCN_LNK_INFO 0x00000200 // 此节包含注释或其他类型信息
#define IMAGE_SCN_LNK_REMOVE 0x00000800 // 此节不会成为映像的一部分
#define IMAGE_SCN_LNK_COMDAT 0x00001000 // 此节包含COM数据
#define IMAGE_SCN_NO_DEFER_SPEC_EXC 0x00004000 // 在此节的TLB项中重置异常控制位
#define IMAGE_SCN_GPREL 0x00008000 // 此节可以访问GP相关内容
#define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 // 此节包含扩展重定位信息
#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 // 此节可以被丢弃(比如重定位.reloc节)
#define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 // 此节不可以缓存
#define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 // 此节不可以分页
#define IMAGE_SCN_MEM_SHARED 0x10000000 // 此节是共享的
#define IMAGE_SCN_MEM_EXECUTE 0x20000000 // 此节是可执行的
#define IMAGE_SCN_MEM_READ 0x40000000 // 此节是可读的
#define IMAGE_SCN_MEM_WRITE 0x80000000 // 此节是可写的

节的名称可以随意定义,但是通常有一些约定俗成的名称,如下:

名称 描述
.text 代码段,Borland C++编译器代码段为code
.data 可读写的数据段,存放全局变量或静态变量
.rdata 只读数据段,存放常量信息
.idata 导入数据段,存放导入表信息
.edata 导出数据段,存放导出表信息
.rsrc 资源段,存放图标、菜单等资源信息
.bss 未初始化数据段
.crt 存放用于支持C++运行时库(CRT)所添加的数据
.tls 存放用于支持通过_declspec(thread)声明的线程局部存储数据
.reloc 存放重定位信息
.sdata 存放可被全局指针定位的可读写数据
.srdata 存放可被全局指针定位的只读数据
.pdata 存放异常表,结构体为IMAGE_RUNTIME_FUNTCION_ENTRY
.debug$S 存放OBJ文件中Codeview格式符号
.debug$T 存放OBJ文件中Codeview格式类型符号
.debug$P 存放使用预编译头时的一些信息
.drectve 存放编译时的一些链接命令
.didat 存放延迟装入的数据
  1. 字符串一般位于 .data 和 .rdata 数据段。
  2. 虚拟内存地址(Virtual Address,VA)指PE文件被装入内存之后的地址;相对虚拟地址(Relative Virtual Address,RVA)指相对于映像基址的内存地址偏移。计算公式为:虚拟内存地址(VA) = 映像基址(Image Base) + 相对虚拟地址(RVA)。
  3. 文件(偏移)地址指代码在原始 PE 文件中的地址。由于无论在文件还是内存中,代码相对代码段(Section)起始地址的偏移是固定的,因此:代码文件偏移 - 代码所在节的起始文件偏移 = 虚拟内存地址 - 映像基址 - 代码所在节的起始虚拟内存偏移。

常用命令

PE 文件分析,通常使用 dumpbin 命令。

dumpbin 命令

visual studio 内置的 dumpbin 可用来查看可执行程序和动态库文件。可以通过 vs 提供的开发者命令提示工具打开。

获取 PE 文件的所有头信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# [CMD]
dumpbin /headers {YourDLL}

# [RESULT]
PE signature found

File Type: DLL

FILE HEADER VALUES
8664 machine (x64)
6 number of sections
63EB2369 time date stamp Tue Feb 14 14:00:09 2023
0 file pointer to symbol table
0 number of symbols
F0 size of optional header
2022 characteristics
Executable
Application can handle large (>2GB) addresses
DLL

OPTIONAL HEADER VALUES
20B magic # (PE32+)
14.29 linker version
13E400 size of code
95000 size of initialized data
0 size of uninitialized data
1382E8 entry point (00000001801382E8) _DllMainCRTStartup
1000 base of code
180000000 image base (0000000180000000 to 00000001801D7FFF)
1000 section alignment
200 file alignment
6.00 operating system version
0.00 image version
6.00 subsystem version
0 Win32 version
1D8000 size of image
400 size of headers
0 checksum
2 subsystem (Windows GUI)
160 DLL characteristics
High Entropy Virtual Addresses
Dynamic base
NX compatible
100000 size of stack reserve
1000 size of stack commit
100000 size of heap reserve
1000 size of heap commit
0 loader flags
10 number of directories
1AC7D0 [ D628] RVA [size] of Export Directory
1B9DF8 [ 2A8] RVA [size] of Import Directory
0 [ 0] RVA [size] of Resource Directory
1C4000 [ E328] RVA [size] of Exception Directory
0 [ 0] RVA [size] of Certificates Directory
1D4000 [ 3C94] RVA [size] of Base Relocation Directory
190060 [ 70] RVA [size] of Debug Directory
0 [ 0] RVA [size] of Architecture Directory
0 [ 0] RVA [size] of Global Pointer Directory
190280 [ 28] RVA [size] of Thread Storage Directory
1900D0 [ 138] RVA [size] of Load Configuration Directory
0 [ 0] RVA [size] of Bound Import Directory
140000 [ 8E0] RVA [size] of Import Address Table Directory
0 [ 0] RVA [size] of Delay Import Directory
0 [ 0] RVA [size] of COM Descriptor Directory
0 [ 0] RVA [size] of Reserved Directory


SECTION HEADER #1
.text name
13E25F virtual size
1000 virtual address (0000000180001000 to 000000018013F25E)
13E400 size of raw data
400 file pointer to raw data (00000400 to 0013E7FF)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
60000020 flags
Code
Execute Read

SECTION HEADER #2
.rdata name
7C576 virtual size
140000 virtual address (0000000180140000 to 00000001801BC575)
7C600 size of raw data
13E800 file pointer to raw data (0013E800 to 001BADFF)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
40000040 flags
Initialized Data
Read Only

Debug Directories

Time Type Size RVA Pointer
-------- ------- -------- -------- --------
63EB2369 cv 77 0019754C 195D4C Format: RSDS, {282AC026-D381-4EC0-B2A2-A96E87E4414F}, 1, E:\Gxxx\Release\GCloud.pdb
63EB2369 feat 14 001975C4 195DC4 Counts: Pre-VC++ 11.00=0, C/C++=591, /GS=591, /sdl=0, guardN=38
63EB2369 coffgrp 34C 001975D8 195DD8 4C544347 (LTCG)
63EB2369 iltcg 0 00000000 0

SECTION HEADER #3
.data name
6500 virtual size
1BD000 virtual address (00000001801BD000 to 00000001801C34FF)
4200 size of raw data
1BAE00 file pointer to raw data (001BAE00 to 001BEFFF)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
C0000040 flags
Initialized Data
Read Write

SECTION HEADER #4
.pdata name
E328 virtual size
1C4000 virtual address (00000001801C4000 to 00000001801D2327)
E400 size of raw data
1BF000 file pointer to raw data (001BF000 to 001CD3FF)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
40000040 flags
Initialized Data
Read Only

SECTION HEADER #5
.xbld name
E3 virtual size
1D3000 virtual address (00000001801D3000 to 00000001801D30E2)
200 size of raw data
1CD400 file pointer to raw data (001CD400 to 001CD5FF)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
C2000040 flags
Initialized Data
Discardable
Read Write

SECTION HEADER #6
.reloc name
3C94 virtual size
1D4000 virtual address (00000001801D4000 to 00000001801D7C93)
3E00 size of raw data
1CD600 file pointer to raw data (001CD600 to 001D13FF)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
42000040 flags
Initialized Data
Discardable
Read Only

Summary

7000 .data
F000 .pdata
7D000 .rdata
4000 .reloc
13F000 .text
1000 .xbld

查看 PE 文件的依赖库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# [CMD]
dumpbin /dependents {YourDLL}

# [RESULT]
Dump of file G:\xxxx\GCloud.dll

File Type: DLL

Image has the following dependencies:

api-ms-win-core-synch-l1-1-0.dll
api-ms-win-core-handle-l1-1-0.dll
WS2_32.dll
api-ms-win-core-errorhandling-l1-1-0.dll
api-ms-win-core-sysinfo-l1-1-0.dll
api-ms-win-core-profile-l1-1-0.dll
api-ms-win-core-com-l1-1-0.dll
api-ms-win-core-string-l1-1-0.dll
api-ms-win-core-libraryloader-l1-2-0.dll
api-ms-win-core-file-l1-1-0.dll
api-ms-win-core-synch-l1-2-0.dll
api-ms-win-core-processthreads-l1-1-0.dll
ext-ms-win-rtcore-ntuser-window-l1-1-0.dll
ext-ms-win-rtcore-ntuser-message-l1-1-0.dll
api-ms-win-core-processthreads-l1-1-1.dll
api-ms-win-core-libraryloader-l1-2-1.dll
bcrypt.dll
xmem.dll
MSVCP140.dll
VCRUNTIME140_1.dll
VCRUNTIME140.dll
api-ms-win-crt-heap-l1-1-0.dll
api-ms-win-crt-stdio-l1-1-0.dll
api-ms-win-crt-runtime-l1-1-0.dll
api-ms-win-crt-string-l1-1-0.dll
api-ms-win-crt-utility-l1-1-0.dll
api-ms-win-crt-convert-l1-1-0.dll
api-ms-win-crt-time-l1-1-0.dll
api-ms-win-crt-filesystem-l1-1-0.dll
api-ms-win-crt-environment-l1-1-0.dll
api-ms-win-core-rtlsupport-l1-1-0.dll
api-ms-win-core-debug-l1-1-0.dll
api-ms-win-core-interlocked-l1-1-0.dll

Summary

7000 .data
F000 .pdata
7D000 .rdata
4000 .reloc
13F000 .text
1000 .xbld

常用工具

  1. CFF Explorer,是 Explorer Suite(http://www.ntcore.com/)中的一个工具,用于PE文件的修改。
  2. Dependencies,用于查看 dll 信息。

参考

https://zh.wikipedia.org/zh-cn/%E5%8F%AF%E7%A7%BB%E6%A4%8D%E5%8F%AF%E6%89%A7%E8%A1%8C
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format
https://learn.microsoft.com/en-us/cpp/build/reference/dumpbin-reference?view=msvc-170
https://github.com/NixOS/patchelf