`
lobin
  • 浏览: 383252 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

关于PE的一些整理

 
阅读更多

PE

PE是Windows下一种可执行程序文件格式,程序格式通常以.exe为后缀,也就是我们在Windows下运行或直接点击就可以直接运行的程序,当然Windows下可执行程序文件并不只是这种直接运行或直接点击就可以直接运行的程序,还包括.lib, .dll, .sys等这些也都是可执行文件,而且Windows下直接运行或直接点击就可以直接运行的程序也不只是exe程序,像.com程序也可以直接运行或直接点击就可以直接运行

 

说到exe程序,除了Windows下的程序格式以.exe为后缀,Windows还兼容以前DOS系统下的程序,DOS系统的可执行程序也是以.exe为后缀。虽然都是以.exe为后缀,但是两种不同的程序格式,PE格式是兼容DOS下的EXE格式的。

 

PE格式的可执行文件和EXE格式的程序文件一样也是以.exe为后缀名。

它也带一个EXE头部,另外它还带一个PE头部。

 

PE头部由一个PE头部标识, COFF文件头部(COFF File Header)以及Optional头部(Optional Header)组成。

 

PE格式的程序是兼容EXE格式的。

 

PE格式

PE即Portable Executable,可移植执行

 

可执行文件

包括EXE文件,镜像文件,库文件包括动态链接库文件(.dll)和静态链接库文件(.lib)

 

概念介绍

ImageBase

程序(image)加载到内存的首选地址,必须是64K的倍数。对于dll动态链接库文件,默认为0x10000000,对于Windows CE下的exe文件默认为0x00010000,对于Windows NT, Windows 2000, Windows XP, Windows 95, Windows 98以及Windows Me下的exe文件默认为0x00400000。

 

所以如果我们以16进制打开程序,找到ImageBase可以看到通常是0x00400000。

 

AddressOfEntryPoint

程序(image)加载到内存后,相对于ImageBase的执行入口地址。对于程序来说,这个是开始执行的地址。对于设备驱动程序,这是初始化函数的地址。 对于DLL来说,入口点是可选的。 当没有入口点时,此字段必须为零。

 

本地Heap空间

即local heap space

 

PE格式大概布局:

MS-DOS 2.0 Compatible EXE Header

unused

OEM Identifier

OEM Information

Offset to PE Header

MS-DOS 2.0 Stub Program and Relocation Table

unused

PE Header (aligned on 8-byte boundary)

Section Headers

Image Pages:

import info

export info

base relocations

resource info

 

其中,PE Header前面的那部分:

MS-DOS 2.0 Compatible EXE Header

unused

OEM Identifier

OEM Information

Offset to PE Header

MS-DOS 2.0 Stub Program and Relocation Table

unused

是MS-DOS 2.0 Section,只用于兼容MS-DOS。

 

PE Header包括4个字节的PE签名标识以及20个字节的COFF File Header。

 

COFF文件头部(COFF File Header)

 

COFF File Header 

20个字节。

 

Offset Size Field Description
Machine  The number that identifies the type of target machine. For more information, see Machine Types
NumberOfSections  The number of sections. This indicates the size of the section table, which immediately follows the headers. 
TimeDateStamp  The low 32 bits of the number of seconds since 00:00 January 1, 1970 (a C run-time time_t value), that indicates when the file was created. 
PointerToSymbolTable The file offset of the COFF symbol table, or zero if no COFF symbol table is present. This value should be zero for an image because COFF debugging information is deprecated. 
12  NumberOfSymbols  The number of entries in the symbol table. This data can be used to locate the string table, which immediately follows the symbol table. This value should be zero for an image because COFF debugging information is deprecated. 
16  SizeOfOptionalHeader The size of the optional header, which is required for executable files but not for object files. This value should be zero for an object file. For a description of the header format, see Optional Header (Image Only).
18  Characteristics  The flags that indicate the attributes of the file. For specific flag values, see Characteristics

 

Optional Header

Optional Header(可选头)的大小不是固定的。

 

Section Table (Section Headers)

Section Table(段表)的每一行实际上是一个段头

Section Table(段表)在可选头后面。如果有可选头的话。

 

Section Header

每个Section Header(段头)大小为40个字节。

 

OffsetSizeField              Description

0       8       Name                  An 8-byte, null-padded UTF-8 encoded string. If the string is exactly 8 characters long, there is no terminating null. 

                                      For longer names, this field contains a slash (/) that is followed by an ASCII representation of a decimal number that is an offset into the string table. Executable images do not use a string table and do not support section names longer than 8 characters. Long names in object files are truncated if they are emitted to an executable file. 

8       4       VirtualSize           The total size of the section when loaded into memory. If this value is greater than SizeOfRawData, the section is zero-padded. 

                                      This field is valid only for executable images and should be set to zero for object files. 

12      4       VirtualAddress        For executable images, the address of the first byte of the section relative to the image base when the section is loaded into memory. 

                                      For object files, this field is the address of the first byte before relocation is applied; for simplicity, compilers should set this to zero. Otherwise, it is an arbitrary value that is subtracted from offsets during relocation. 

16      4       SizeOfRawData         The size of the section (for object files) or the size of the initialized data on disk (for image files). For executable images, 

                                      this must be a multiple of FileAlignment from the optional header. If this is less than VirtualSize, the remainder of the section is zero-filled. Because the SizeOfRawData field is rounded but the VirtualSize field is not, it is possible for SizeOfRawData to be greater than VirtualSize as well. When a section contains only uninitialized data, this field should be zero. 

20      4       PointerToRawData      The file pointer to the first page of the section within the COFF file. For executable images, this must be a multiple of 

                                      FileAlignment from the optional header. For object files, the value should be aligned on a 4-byte boundary for best performance. When a section contains only uninitialized data, this field should be zero. 

24      4       PointerToRelocations  The file pointer to the beginning of relocation entries for the section. This is set to zero for executable images or if there 

                                      are no relocations. 

28      4       PointerToLinenumbers  The file pointer to the beginning of line-number entries for the section. This is set to zero if there are no COFF line numbers. 

                                      This value should be zero for an image because COFF debugging information is deprecated. 

32      2       NumberOfRelocations   The number of relocation entries for the section. This is set to zero for executable images. 

34      2       NumberOfLinenumbers   The number of line-number entries for the section. This value should be zero for an image because COFF debugging information 

                                      is deprecated. 

 

36      4       Characteristics       The flags that describe the characteristics of the section. For more information, see Section Flags.

 

.text

代码段

 

如下代码段的头部:

                         2E 74 65 78 74 00 00 00

DC 41 00 00 00 00 C0 FF  00 42 00 00 00 02 00 00

00 00 00 00 00 00 00 00  00 00 00 00 20 00 30 60

0-7:2E 74 65 78 74 00 00 00,前面8个字节表示段的名称: .text,所以最多8个字符,不足8个字符的后面用0x00填充。

8-11:虚拟大小,DC 41 00 00,表示:0x41dc

12-15:虚拟地址,00 00 C0 FF,表示:0xffc00000

16-19:原始数据大小,00 42 00 00,表示:0x4200

20-23:指向原始数据的指针,00 02 00 00,表示:0x0200

24-27:指向重定位表的指针,00 00 00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

28-31:指向行号表的指针,00 00 00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

32-33:重定位表项的个数,00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

34-35:行号表项的个数,00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

36-39:特性,20 00 30 60,表示:0x60300020,表示:0x20000000+0x40000000+0x00300000+0x00000020,其中0x20000000表示The section can be executed as code.;0x40000000表示The section can be read.;0x00300000表示Align data on a 4-byte boundary. Valid only for object files;0x00000020表示The section contains executable code。

 

 

.data

数据段

 

如下数据段的头部:

2E 64 61 74 61 00 00 00  4C 19 00 00 E0 41 C0 FF

00 1A 00 00 E0 45 00 00  00 00 00 00 00 00 00 00

00 00 00 00 40 00 60 C0  

0-7:2E 64 61 74 61 00 00 00,前面8个字节表示段的名称: .data,所以最多8个字符,不足8个字符的后面用0x00填充。

8-11:虚拟大小,4C 19 00 00,表示:0x194c

12-15:虚拟地址,E0 41 C0 FF,表示:0xffc041e0

16-19:原始数据大小,00 1A 00 00,表示:0x1a00

20-23:指向原始数据的指针,E0 45 00 00,表示:0x45e0

24-27:指向重定位表的指针,00 00 00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

28-31:指向行号表的指针,00 00 00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

32-33:重定位表项的个数,00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

34-35:行号表项的个数,00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

36-39:特性,40 00 60 C0,表示:0xc0600040,表示:0x80000000+0x40000000+0x00600000+0x00000040,其中0x80000000表示The section can be written to.;0x40000000表示The section can be read.;0x00600000表示Align data on a 32-byte boundary. Valid only for object files;0x00000040表示The section contains initialized data。

 

.bss

 

如下.bss段的头部:

2E 62 73 73 00 00 00 00

80 03 00 00 00 60 C0 FF  00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00  00 00 00 00 80 00 60 C0

0-7:2E 62 73 73 00 00 00 00,前面8个字节表示段的名称: .bss,所以最多8个字符,不足8个字符的后面用0x00填充。

8-11:虚拟大小,80 03 00 00,表示:0x0380

12-15:虚拟地址,00 60 C0 FF,表示:0xffc06000

16-19:原始数据大小,00 00 00 00,表示:0x0000

20-23:指向原始数据的指针,00 00 00 00,表示:0x0000

24-27:指向重定位表的指针,00 00 00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

28-31:指向行号表的指针,00 00 00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

32-33:重定位表项的个数,00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

34-35:行号表项的个数,00 00,表示:0x0000,可执行文件或镜像文件应该设置为0

36-39:特性,80 00 60 C0,表示:0xc0600080,表示:0x80000000+0x40000000+0x00600000+0x00000080,其中0x80000000表示The section can be written to.;0x40000000表示The section can be read.;0x00600000表示Align data on a 32-byte boundary. Valid only for object files;0x00000080表示The section contains uninitialized data。

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics