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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

PE文件结构详解Word格式文档下载.docx

1、IMAGE_DATA_DIRECTORYIMAGE_FILE_HEADER3 PE文件结构图图1 PE文件结构从MS-DOS文件头结构开始,我将按照PE文件格式各成分的出现顺序依次对其进行讨论,并且讨论的大部分是以示例代码为基础来示范如何获得文件的信息的。3.1 MS-DOS头部/实模式头部PE文件格式的第一个组成部分是MS-DOS头部。在PE文件格式中,它并非一个新概念,因为它与MS-DOS 2.0以来就已有的MS-DOS头部是完全一样的。保留这个相同结构的最主要原因是,当你尝试在Windows 3.1以下或MS-DOS 2.0以上的系统下装载一个文件的时候,操作系统能够读取这个文件并明白它

2、是和当前系统不相兼容的。换句话说,当你在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 command, operable program or batch file.”MS-DOS头部占据了PE文件的头64个字节,描述它内容的结

3、构,即图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_maxalloc; / 所需的最大附加段 USHORT e_ss; / 初始的SS值(相对偏移量) USHORT e_sp; / 初始的SP

4、值 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; LONG e_lfanew; / 新exe头部的文件地址IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;第一个域e_magi

5、c,被称为魔术数字,它被用于表示一个MS-DOS兼容的文件类型。所有MS-DOS兼容的可执行文件都将这个值设为0x5A4D,表示ASCII字符MZ。MS-DOS头部之所以有的时候被称为MZ头部,就是这个缘故。还有许多其它的域对于MS-DOS操作系统来说都有用,但是对于Windows NT来说,这个结构中只有一个有用的域最后一个域e_lfnew,一个4字节的文件偏移量,PE文件头部就是由它定位的。且MS_DOS头部(dos_head)的地址即为文件映像后的基地址,对于Windows NT的PE文件来说,PE文件头部是紧跟在MS-DOS头部和实模式程序残余之后的。.操作打开文件:hFile = C

6、reateFile(lpFileName, );MS-DOS头部地址:dos_head =(IMAGE_DOS_HEADER *)basepointer;打印MS-DOS头部信息:3.2 实模式残余程序实模式残余程序是一个在装载时能够被MS-DOS运行的实际程序。对于一个MS-DOS的可执行映像文件,应用程序就是从这里执行的。对于Windows、OS/2、Windows NT这些操作系统来说,MS-DOS残余程序就代替了主程序的位置被放在这里。这种残余程序通常什么也不做,而只是输出一行文本,例如:“This program requires Microsoft Windows v3.1 or

7、greater.”当然,用户可以在此放入任何的残余程序,这就意味着你可能经常看到像这样的东西:“You cant run a Windows NT application on OS/2, its simply not possible.” 当为Windows 3.1构建一个应用程序的时候,链接器将向你的可执行文件中链接一个名为WINSTUB.EXE的默认残余程序。你可以用一个基于MS-DOS的有效程序取代WINSTUB,并且用STUB模块定义语句指示链接器,这样就能够取代链接器的默认行为。为Windows NT开发的应用程序可以通过使用-STUB:链接器选项来实现。不同的文件,其大小不一样,

8、即图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 structure represents the PE header format.typedef struct _IMAGE_NT_HEADERS

9、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 struct _IMAGE_FILE_HEADER WORD Machine; WORD NumberOfSections; DWORD TimeDateSt

10、amp; DWORD PointerToSymbolTable; DWORD NumberOfSymbols; WORD SizeOfOptionalHeader; WORD Characteristics; IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;这个文件头结构中一个有用的入口是NumberOfSections域,它表示如果你要方便地提取文件信息的话,就需要了解多少个段,更明确一点来说,有多少个段头部和多少个段实体。每一个段头部和段实体都在文件中连续地排列着,所以要决定段头部和段实体在哪里结束的话,段的数目是必需的。以下的语句从PE文件头中提取了段的数目

11、:numberofsection = peHeader-FileHeader.NumberOfSections;3.4 PE可选头部PE可执行文件中接下来的224个字节组成了PE可选头部。虽然它的名字是“可选头部”,但是请确信:这个头部并非“可选”,而是“必需”的。可选头部的偏移量即为:offset = dos_head-e_lfanew + SIZE_OF_NT_SIGNATURE(即:4) + sizeof(IMAGE_FILE_HEADER)。可选头部包含了很多关于可执行映像的重要信息,例如初始的堆栈大小、程序入口点的位置、首选基地址、操作系统版本、段对齐的信息等等。IMAGE_OPTI

12、ONAL_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 code section, in bytes, or the sum of all such sections /if there are multiple

13、 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 uninitialized data section, in bytes, or the sum of all such sections if there a

14、re 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, this member is zero. DWORD BaseOfCode;/ Pointer to the beginning of the code

15、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 memory. This value is a multiple of 64K bytes. The default value for DLLs is /0

16、x10000000. 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 value is the /page size for the system. DWORD FileAlignment; WORD MajorOperatingS

17、ystemVersion; WORD MinorOperatingSystemVersion; WORD MajorImageVersion; WORD MinorImageVersion; WORD MajorSubsystemVersion; WORD MinorSubsystemVersion; DWORD Win32VersionValue; DWORD SizeOfImage;/ Size of the image, in bytes, including all headers. Must be a /multiple of SectionAlignment. DWORD Size

18、OfHeaders;/ 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; DWORD SizeOfStackReserve; DWORD SizeOfStackCommit; DWORD SizeOfHeapReserve; DWORD

19、SizeOfHeapCommit; DWORD LoaderFlags; DWORD NumberOfRvaAndSizes; IMAGE_DATA_DIRECTORY DataDirectoryIMAGE_NUMBEROF_DIRECTORY_ENTRIES; IMAGE_OPTIONAL_HEADER, *PIMAGE_OPTIONAL_HEADER;其中NumberOfRvaAndSizes这个域标识了接下来的DataDirectory数组。请注意它被用来标识这个数组,而不是数组中的各个入口数字,这一点非常重要。DataDirectory。数据目录表示文件中其它可执行信息重要组成部分的位

20、置。它事实上就是一个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_DIRECTORY;当前的PE文件格式定义了16种可能的数据目录,这之中的11种现在在使用中。 数据目录的各个元素依次如下所示:The following is

21、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 and size136Base relocation table address and size /重定位信息144Debugging information

22、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 size184Bound import table address and size /绑定输入表(引入表)192Import address table address

23、and size200Delay import descriptor address and size208Reserved由上表可知,有如下定义# define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16。4 PE文件节表(段头部)PE文件规范由目前为止定义的那些头部以及一个名为“段”的一般对象组成。段包含了文件的内容,包括代码、数据、资源以及其它可执行信息,每个段都有一个头部和一个实体(原始数据)。我将在下面描述段头部的有关信息,但是段实体则缺少一个严格的文件结构。因此,它们几乎可以被链接器按任何的方法组织,只要它的头部填充了足够能够解释数据的信息。段头部定义如

24、下: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 size of the section when loaded into memory, in bytes. /if this value is greater

25、 than the SizeOfRawData member, the section is filled with /zeroes. Misc;/ Address of the first byte of the section when loaded into memory, /relative to the image base.DWORD SizeOfRawData;/ Size of the initialized data on disk, in bytes. This value /must be a multiple of the FileAlignment member of

26、 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 member is /zero.DWORD PointerToRawData;/ File pointer to the first page within the COFF file. This value

27、 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;/ File pointer to the beginning of the relocation entries /for the section. If there are no relocations,

28、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