PE的意思就是这个the Protable Executable (PE) file format
微软搞得那么一套东西,字面意思是可移植的,但是现实使用中没见他多么的可移植,PE格式借鉴了UNIX系统中的COFF (Common Object File Format) 格式。而且PE对MS-
DOS的兼容,保留了MS-
DOS头,在
DOS下打开会提示 “这是win32程序在
DOS下不能跑” 向下兼容,非常的友好。
MS-
DOS MZ header 的结构是这样的
MS-
DOS MZ header
[cpp] view plaincopy
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;
其中比较关键的成员是这个e_lfanew 它指向了PE文件头在PE文件中的相对虚拟地址RAV(Relative Virtual Addresses),e_magic的值应该等于0x5A4D 是MS-
DOS MZ header的标志MZ好像是个程序员名字的缩写 其他成员基本没啥大用,一些加壳
软件会修改它的成员为自己的节腾出空间,或者在添加节形式感染时节表尾部的空隙不够写入一个新的解表结构的时候把IMAGE_
DOS_HEADE 和IMAGE_NT_HEADER 融合。
可以自己写一个小程序来输出一下IMAGE_
DOS_HEADE
IMAGE_
DOS_HEADE这个结构体定义在windows.h中
系统加载PE格式的文件时,会先加载IMAGE_
DOS_HEADE这个结构体,再根据结构体里的e_lfanew提供的相对偏移找到PE文件头。
用
C语言可以直接读出IMAGE_
DOS_HEADE这个结构体,下面开始写。
从文件的开始位置读取IMAGE_
DOS_HEADE结构体
[cpp] view plaincopy
fread(;my
DOSheader,sizeof(my
DOSheader),1,p);
吧文件指针移动到e_lfanew所指的相对偏移,即PE文件头
[cpp] view plaincopy
fseek(p,my
DOSheader.e_lfanew,SEEK_SET);
读取PE文件标志,这个PE Signature是PE\0\0 这样一个值,证明它是PE格式的身份。
[cpp] view plaincopy
fread(;sig,4,1,p);
这个判断中大写的变量都是,windows.h中的常数
IMAGE_NT_SIGNATURE 的值是PE\0\0
IMAGE_
DOS_SIGNATURE 的值是MZ
具体的定义可以自己去windows.h中看
if((my
DOSheader.e_magic ==IMAGE_
DOS_SIGNATURE) ;;
(sig == IMAGE_NT_SIGNATURE))
printf("有效的PE文件/n");
else
printf("无效的PE文件/n");
return 0;
下面是完整的程序
[cpp] view plaincopy
#include "windows.h"
#include "stdio.h"
int main(int argc, char* argv[])
{
FILE *p;
IMAGE_
DOS_HEADER my
DOSheader;
unsigned long sig;
p = fopen("test1.exe","r+b");
if(p == NULL)return -1;
fread(;my
DOSheader,sizeof(my
DOSheader),1,p);
fseek(p,my
DOSheader.e_lfanew,SEEK_SET);
fread(;sig,4,1,p);
fclose(p);
printf("IMAGE_
DOS_HEADER dump:/n");
printf("e_magic : %04x/n",my
DOSheader.e_magic);
printf("e_cblp : %04x/n",my
DOSheader.e_cblp);
printf("e_cp : %04x/n",my
DOSheader.e_cp);
printf("e_crlc : %04x/n",my
DOSheader.e_crlc);
printf("e_cparhdr : %04x/n",my
DOSheader.e_cparhdr);
printf("e_minalloc: %04x/n",my
DOSheader.e_minalloc);
printf("e_maxalloc: %04x/n",my
DOSheader.e_maxalloc);
printf("e_ss : %04x/n",my
DOSheader.e_ss);
printf("e_sp : %04x/n",my
DOSheader.e_sp);
printf("e_csum : %04x/n",my
DOSheader.e_csum);
printf("e_ip : %04x/n",my
DOSheader.e_ip);
printf("e_cs : %04x/n",my
DOSheader.e_cs);
printf("e_lfarlc : %04x/n",my
DOSheader.e_lfarlc);
printf("e_ovno : %04x/n",my
DOSheader.e_ovno);
printf("e_res[0] : %04x/n",my
DOSheader.e_res[0]);
printf("e_oemid : %04x/n",my
DOSheader.e_oemid);
printf("e_oeminfo : %04x/n",my
DOSheader.e_oeminfo);
printf("res2[0] : %04x/n",my
DOSheader.e_res2[0]);
printf("lfanew : %08x/n",my
DOSheader.e_lfanew);
if((my
DOSheader.e_magic ==IMAGE_
DOS_SIGNATURE) ;;
(sig == IMAGE_NT_SIGNATURE))
printf("有效的PE文件/n");
else
printf("无效的PE文件/n");
return 0;
}