Windows系统下的可执行文件,是基于Microsoft设计的一种新的文件结构,此结构被称之为PE结构。PE的意思是Portable Executable(可移植的执行体),所有Win32执行体都是用PE文件格式,其中包括SYS、DLL、EXE、COM、OCX等。
不管是学习逆向、破解还是安全,了解PE文件格式都是非常必要的。
PE文件的第一个部分是IMAGE_DOS_HEADER,大小为64B,这里面有两个重要的数据成员。第一个为e_magic,这个必须为MZ,即0x5A4D。当然,0x5A4D这是典型的小端格式(Little Endian);另一个重要的数据成员是最后一个成员e_lfanew,这个成员的值为IMAGE_NT_HEADERS的偏移。
在IMAGE_DOS_HEADER和IMAGE_NT_HEADERS之间一个DOS Stub,这段程序用于在DOS环境中显示一个字符串,“This program cannot be run in DOS mode”,现在DOS早已灭绝,这段数据由编译器生成,可以用0填充或者删除(删除的话要移动很多数据)。
IMAGE_DOS_HEADER的定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header WORD e_magic; // Magic number WORD e_cblp; // Bytes on last page of file WORD e_cp; // Pages in file WORD e_crlc; // Relocations WORD e_cparhdr; // Size of header in paragraphs WORD e_minalloc; // Minimum extra paragraphs needed WORD e_maxalloc; // Maximum extra paragraphs needed WORD e_ss; // Initial (relative) SS value WORD e_sp; // Initial SP value WORD e_csum; // Checksum WORD e_ip; // Initial IP value WORD e_cs; // Initial (relative) CS value WORD e_lfarlc; // File address of relocation table WORD e_ovno; // Overlay number WORD e_res[4]; // Reserved words WORD e_oemid; // OEM identifier (for e_oeminfo) WORD e_oeminfo; // OEM information; e_oemid specific WORD e_res2[10]; // Reserved words LONG e_lfanew; // File address of new exe header } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER; |