对于一个PE文件来说,最开始的位置就是一个DOS程序。DOS程序包含一个DOS头和一个DOS程序体。
DOS头部是由IMAGE_DOS_HEADER结构体来定义的。
该结构体定义如下:
- typedef struct _IMAE_DOS_HEADER { //DOS .EXE header 位置
- WORD e_magic; //Magic number; 0x00
- WORD e_cblp; //Bytes on last page of file 0x02
- WORD e_cp; //Pages in file 0x04
- WORD e_crlc; //Relocations 0x06
- WORD e_cparhdr; //Size of header in paragraphs 0x08
- WORD e_minalloc; //Minimum extra paragraphs needed 0x0A
- WORD e_maxalloc; //Maximum extra paragraphs needed 0x0C
- WORD e_ss; //Initial (relative) SS value 0x0E
- WORD e_sp; //Initial SP value 0x10
- WORD e_csum; //Checksum 0x12
- WORD e_ip; //Initial IP value 0x14
- WORD e_cs; //Initial (relative) CS value 0x16
- WORD e_lfarlc; //File address of relocation table 0x18
- WORD e_ovno; //Overlay number 0x1A
- WORD e_res[4]; //Reserved words 0x1C
- WORD e_oemid; //OEM identifier (for e_oeminfo) 0x24
- WORD e_oeminfo; //OEM information; e_oemid specific 0x26
- WORD e_res2[10]; //Reserved words 0x28
- LONG e_lfanew; //File address of new exe header 0x3C
- } IMAGE_DOS-HEADER, *PIMAGE_DOS_HEADER;
该结构体中,有两个字段需要注意,分别是第一个字段 e_magic,和最后一个字段 e_lfanew字段。
e_magic 字段是一个DOS可执行文件的标识符,该字段占用两个字节,该位置保存着的字符是“MZ”。该标识符在Winnt.h头文件中有一个宏定义,定义如下所示:
- #define IMAGE_DOS_SIGNATURE 0x5A4D //MZ