ImageVerifierCode 换一换
格式:DOCX , 页数:22 ,大小:187.31KB ,
资源ID:8330400      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8330400.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(PE文件结构详解.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

PE文件结构详解.docx

1、PE文件结构详解PE文件结构详解1 摘要Windows NT 3.1引入了一种名为PE文件格式的新可执行文件格式。PE文件格式的规范包含在了MSDN的CD中(Specs and Strategy, Specifications, Windows NT File Format Specifications),但是它非常之晦涩。 然而这一的文档并未提供足够的信息,所以开发者们无法很好地弄懂PE格式。本文旨在解决这一问题,它会对整个的PE文件格式作一个十分彻底的解释,另外,本文中还带有对所有必需结构的描述以及示范如何使用这些信息的源码示例。为了获得PE文件中所包含的重要信息,我编写了一个名为PEFI

2、LE.DLL的动态链接库,本文中所有出现的源码示例亦均摘自于此。这个DLL和它的源代码都作为PEFile示例程序的一部分包含在了CD中(译注:示例程序请在MSDN中寻找,本站恕不提供),你可以在你自己的应用程序中使用这个DLL;同样,你亦可以依你所愿地使用并构建它的源码。在本文末尾,你会找到PEFILE.DLL的函数导出列表和一个如何使用它们的说明。我觉得你会发现这些函数会让你从容应付PE文件格式的。2 介绍Windows操作系统家族最近增加的Windows NT为开发环境和应用程序本身带来了很大的改变,这之中一个最为重大的当属PE文件格式了。新的PE文件格式主要来自于UNIX操作系统所通用的

3、COFF规范,同时为了保证与旧版本MS-DOS及Windows操作系统的兼容,PE文件格式也保留了MS-DOS中那熟悉的MZ头部。在本文之中,PE文件格式是以自顶而下的顺序解释的。在你从头开始研究文件内容的过程之中,本文会详细讨论PE文件的每一个组成部分。很多解决PE文件格式的工作和直接观看数据有关。例如,要弄懂导入地址名称表是如何构成的,我就得同时查看.idata段头部、导入映像数据目录、可选头部以及当前的.idata段实体,而EXEVIEW.EXE就是查看这些信息的最佳示例。 在针对PE文件的有关编程中,你可能用到以下一些数据结构:IMAGE_DOS_HEADER IMAGE_IMPORT

4、_DESCRIPTORIMAGE_NT_HEADERS IMAGE_SECTION_HEADER IMAGE_OPTIONAL_HEADERIMAGE_DATA_DIRECTORYIMAGE_FILE_HEADER3 PE文件结构图图1 PE文件结构从MS-DOS文件头结构开始,我将按照PE文件格式各成分的出现顺序依次对其进行讨论,并且讨论的大部分是以示例代码为基础来示范如何获得文件的信息的。3.1 MS-DOS头部/实模式头部PE文件格式的第一个组成部分是MS-DOS头部。在PE文件格式中,它并非一个新概念,因为它与MS-DOS 2.0以来就已有的MS-DOS头部是完全一样的。保留这个相同结

5、构的最主要原因是,当你尝试在Windows 3.1以下或MS-DOS 2.0以上的系统下装载一个文件的时候,操作系统能够读取这个文件并明白它是和当前系统不相兼容的。换句话说,当你在MS-DOS 6.0下运行一个Windows NT可执行文件时,你会得到这样一条消息:“This program cannot be run in DOS mode.”如果MS-DOS头部不是作为PE文件格式的第一部分的话,操作系统装载文件的时候就会失败,并提供一些完全没用的信息,例如:“The name specified is not recognized as an internal or external c

6、ommand, operable program or batch file.”MS-DOS头部占据了PE文件的头64个字节,描述它内容的结构,即图1中的(1)部分的定义如下:typedef struct _IMAGE_DOS_HEADER / DOS的.EXE头部 USHORT e_magic; / 魔术数字 USHORT e_cblp; / 文件最后页的字节数 USHORT e_cp; / 文件页数 USHORT e_crlc; / 重定义元素个数 USHORT e_cparhdr; / 头部尺寸,以段落为单位 USHORT e_minalloc; / 所需的最小附加段 USHORT e_

7、maxalloc; / 所需的最大附加段 USHORT e_ss; / 初始的SS值(相对偏移量) USHORT e_sp; / 初始的SP值 USHORT e_csum; / 校验和 USHORT e_ip; / 初始的IP值 USHORT e_cs; / 初始的CS值(相对偏移量) USHORT e_lfarlc; / 重分配表文件地址 USHORT e_ovno; / 覆盖号 USHORT e_res4; / 保留字 USHORT e_oemid; / OEM标识符(相对e_oeminfo) USHORT e_oeminfo; / OEM信息 USHORT e_res210; / 保留字

8、 LONG e_lfanew; / 新exe头部的文件地址IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;第一个域e_magic,被称为魔术数字,它被用于表示一个MS-DOS兼容的文件类型。所有MS-DOS兼容的可执行文件都将这个值设为0x5A4D,表示ASCII字符MZ。MS-DOS头部之所以有的时候被称为MZ头部,就是这个缘故。还有许多其它的域对于MS-DOS操作系统来说都有用,但是对于Windows NT来说,这个结构中只有一个有用的域最后一个域e_lfnew,一个4字节的文件偏移量,PE文件头部就是由它定位的。且MS_DOS头部(dos_head)的地址即为文

9、件映像后的基地址,对于Windows NT的PE文件来说,PE文件头部是紧跟在MS-DOS头部和实模式程序残余之后的。.操作打开文件:hFile = CreateFile(lpFileName, );MS-DOS头部地址:dos_head =(IMAGE_DOS_HEADER *)basepointer;打印MS-DOS头部信息:3.2 实模式残余程序实模式残余程序是一个在装载时能够被MS-DOS运行的实际程序。对于一个MS-DOS的可执行映像文件,应用程序就是从这里执行的。对于Windows、OS/2、Windows NT这些操作系统来说,MS-DOS残余程序就代替了主程序的位置被放在这里。

10、这种残余程序通常什么也不做,而只是输出一行文本,例如:“This program requires Microsoft Windows v3.1 or greater.”当然,用户可以在此放入任何的残余程序,这就意味着你可能经常看到像这样的东西:“You cant run a Windows NT application on OS/2, its simply not possible.” 当为Windows 3.1构建一个应用程序的时候,链接器将向你的可执行文件中链接一个名为WINSTUB.EXE的默认残余程序。你可以用一个基于MS-DOS的有效程序取代WINSTUB,并且用STUB模块定义

11、语句指示链接器,这样就能够取代链接器的默认行为。为Windows NT开发的应用程序可以通过使用-STUB:链接器选项来实现。不同的文件,其大小不一样,即图1中(2)部分的大小由MS-DOS头的域e_lfnew来确定。3.3 PE文件头部与标志PE文件头部的地址(peheader)是由MS-DOS头部的e_lfanew域定位的,这个域只是给出了文件的偏移量,所以要确定PE头部的实际内存映射地址,就需要添加文件的内存映射基地址。Peheader = dos_head + dos_head-e_lfanew。PE文件头部的定义,即图1中(3)部分的定义如下:The IMAGE_NT_HEADERS

12、 structure represents the PE header format.typedef struct _IMAGE_NT_HEADERS DWORD Signature; IMAGE_FILE_HEADER FileHeader; IMAGE_OPTIONAL_HEADER OptionalHeader; IMAGE_NT_HEADERS, *PIMAGE_NT_HEADERS;其中,文件头FileHeader的结构体定义如下:The IMAGE_FILE_HEADER structure represents the COFF header format.typedef str

13、uct _IMAGE_FILE_HEADER WORD Machine; WORD NumberOfSections; DWORD TimeDateStamp; DWORD PointerToSymbolTable; DWORD NumberOfSymbols; WORD SizeOfOptionalHeader; WORD Characteristics; IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;这个文件头结构中一个有用的入口是NumberOfSections域,它表示如果你要方便地提取文件信息的话,就需要了解多少个段,更明确一点来说,有多少个段头部和多

14、少个段实体。每一个段头部和段实体都在文件中连续地排列着,所以要决定段头部和段实体在哪里结束的话,段的数目是必需的。以下的语句从PE文件头中提取了段的数目:numberofsection = peHeader-FileHeader.NumberOfSections; 3.4 PE可选头部PE可执行文件中接下来的224个字节组成了PE可选头部。虽然它的名字是“可选头部”,但是请确信:这个头部并非“可选”,而是“必需”的。可选头部的偏移量即为:offset = dos_head-e_lfanew + SIZE_OF_NT_SIGNATURE(即:4) + sizeof(IMAGE_FILE_HEAD

15、ER)。可选头部包含了很多关于可执行映像的重要信息,例如初始的堆栈大小、程序入口点的位置、首选基地址、操作系统版本、段对齐的信息等等。IMAGE_OPTIONAL_HEADER结构如下: The IMAGE_OPTIONAL_HEADER structure represents the optional header format.typedef struct _IMAGE_OPTIONAL_HEADER WORD Magic; BYTE MajorLinkerVersion; BYTE MinorLinkerVersion; DWORD SizeOfCode; / Size of the

16、code section, in bytes, or the sum of all such sections /if there are multiple code sections. DWORD SizeOfInitializedData;/ Size of the initialized data section, in bytes, or /the sum of all such sections if there are multiple initialized data sections. DWORD SizeOfUninitializedData;/ Size of the un

17、initialized data section, in bytes, or the sum of all such sections if there are multiple uninitialized data sections. DWORD AddressOfEntryPoint;/ Pointer to the entry point function, relative to the /image base address. The entry point function is optional for DLLs. When no entry /point is present,

18、 this member is zero. DWORD BaseOfCode;/ Pointer to the beginning of the code section, relative to the /image base. DWORD BaseOfData;/ Pointer to the beginning of the data section, relative to the /image base. DWORD ImageBase;/ Preferred address of the first byte of the image when it is loaded /in m

19、emory. This value is a multiple of 64K bytes. The default value for DLLs is /0x10000000. The default value for applications is 0x00400000. DWORD SectionAlignment;/ Alignment of sections loaded in memory, in bytes. This value /must be greater than or equal to the FileAlignment member. The default val

20、ue is the /page size for the system. DWORD FileAlignment; WORD MajorOperatingSystemVersion; WORD MinorOperatingSystemVersion; WORD MajorImageVersion; WORD MinorImageVersion; WORD MajorSubsystemVersion; WORD MinorSubsystemVersion; DWORD Win32VersionValue; DWORD SizeOfImage;/ Size of the image, in byt

21、es, including all headers. Must be a /multiple of SectionAlignment. DWORD SizeOfHeaders;/ Combined size of the MS-DOS stub, the PE header, and the section /headers, rounded to a multiple of the value specified in the FileAlignment member. DWORD CheckSum; WORD Subsystem; WORD DllCharacteristics; DWOR

22、D SizeOfStackReserve; DWORD SizeOfStackCommit; DWORD SizeOfHeapReserve; DWORD SizeOfHeapCommit; DWORD LoaderFlags; DWORD NumberOfRvaAndSizes; IMAGE_DATA_DIRECTORY DataDirectoryIMAGE_NUMBEROF_DIRECTORY_ENTRIES; IMAGE_OPTIONAL_HEADER, *PIMAGE_OPTIONAL_HEADER;其中NumberOfRvaAndSizes这个域标识了接下来的DataDirector

23、y数组。请注意它被用来标识这个数组,而不是数组中的各个入口数字,这一点非常重要。 DataDirectory。数据目录表示文件中其它可执行信息重要组成部分的位置。它事实上就是一个IMAGE_DATA_DIRECTORY结构的数组,位于可选头部结构的末尾。The IMAGE_DATA_DIRECTORY structure represents the data directory.typedef struct _IMAGE_DATA_DIRECTORY DWORD VirtualAddress; DWORD Size; IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DI

24、RECTORY;当前的PE文件格式定义了16种可能的数据目录,这之中的11种现在在使用中。 数据目录的各个元素依次如下所示:The following is a list of the data directories:OffsetDescription96Export table address and size104Import table address and size /输入表(引入表)112Resource table address and size120Exception table address and size128Certificate table address an

25、d size136Base relocation table address and size /重定位信息144Debugging information starting address and size152Architecture-specific data address and size160Global pointer register relative virtual address168Thread local storage (TLS) table address and size176Load configuration table address and size184

26、Bound import table address and size /绑定输入表(引入表)192Import address table address and size200Delay import descriptor address and size208Reserved由上表可知,有如下定义# define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16。4 PE文件节表(段头部)PE文件规范由目前为止定义的那些头部以及一个名为“段”的一般对象组成。段包含了文件的内容,包括代码、数据、资源以及其它可执行信息,每个段都有一个头部和一个实体(原始数据)。我将在下

27、面描述段头部的有关信息,但是段实体则缺少一个严格的文件结构。因此,它们几乎可以被链接器按任何的方法组织,只要它的头部填充了足够能够解释数据的信息。段头部定义如下:The IMAGE_SECTION_HEADER structure represents the image section header format.typedef struct _IMAGE_SECTION_HEADER BYTE NameIMAGE_SIZEOF_SHORT_NAME; union DWORD PhysicalAddress;/ File address.DWORD VirtualSize;/ Total s

28、ize of the section when loaded into memory, in bytes. /if this value is greater than the SizeOfRawData member, the section is filled with /zeroes. Misc; DWORD VirtualAddress;/ Address of the first byte of the section when loaded into memory, /relative to the image base.DWORD SizeOfRawData;/ Size of

29、the initialized data on disk, in bytes. This value /must be a multiple of the FileAlignment member of the IMAGE_OPTIONAL_HEADER structure. /If this value is less than the VirtualSize member, the remainder of the section is /filled with zeroes. If the section contains only uninitialized data, the mem

30、ber is /zero.DWORD PointerToRawData;/ File pointer to the first page within the COFF file. This value must /be a multiple of the FileAlignment member of the IMAGE_OPTIONAL_HEADER structure. If /a section contains only uninitialized data, this member is zero./本节在文件中的偏移量DWORD PointerToRelocations;/ Fi

31、le pointer to the beginning of the relocation entries /for the section. If there are no relocations, this value is zero.DWORD PointerToLinenumbers;/ File pointer to the beginning of the line-number /entries for the section. If there are no COFF line numbers, this value is zero.WORD NumberOfRelocations;/ Number of relocation entries for the section. This value /is zero for executable images.WORD NumberOfLinenumbers;/ Number of line-number entries for the section.DWORD Characteristics; IMAGE_SECTION_HEADER, *PIMAGE_SECTIO

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1