学破解 *一* PE格式之MS-DOS MZ header
4880 点击·0 回帖
![]() | ![]() | |
![]() | 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(;myDOSheader,sizeof(myDOSheader),1,p); 吧文件指针移动到e_lfanew所指的相对偏移,即PE文件头 [cpp] view plaincopy fseek(p,myDOSheader.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((myDOSheader.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 myDOSheader; unsigned long sig; p = fopen("test1.exe","r+b"); if(p == NULL)return -1; fread(;myDOSheader,sizeof(myDOSheader),1,p); fseek(p,myDOSheader.e_lfanew,SEEK_SET); fread(;sig,4,1,p); fclose(p); printf("IMAGE_DOS_HEADER dump:/n"); printf("e_magic : %04x/n",myDOSheader.e_magic); printf("e_cblp : %04x/n",myDOSheader.e_cblp); printf("e_cp : %04x/n",myDOSheader.e_cp); printf("e_crlc : %04x/n",myDOSheader.e_crlc); printf("e_cparhdr : %04x/n",myDOSheader.e_cparhdr); printf("e_minalloc: %04x/n",myDOSheader.e_minalloc); printf("e_maxalloc: %04x/n",myDOSheader.e_maxalloc); printf("e_ss : %04x/n",myDOSheader.e_ss); printf("e_sp : %04x/n",myDOSheader.e_sp); printf("e_csum : %04x/n",myDOSheader.e_csum); printf("e_ip : %04x/n",myDOSheader.e_ip); printf("e_cs : %04x/n",myDOSheader.e_cs); printf("e_lfarlc : %04x/n",myDOSheader.e_lfarlc); printf("e_ovno : %04x/n",myDOSheader.e_ovno); printf("e_res[0] : %04x/n",myDOSheader.e_res[0]); printf("e_oemid : %04x/n",myDOSheader.e_oemid); printf("e_oeminfo : %04x/n",myDOSheader.e_oeminfo); printf("res2[0] : %04x/n",myDOSheader.e_res2[0]); printf("lfanew : %08x/n",myDOSheader.e_lfanew); if((myDOSheader.e_magic ==IMAGE_DOS_SIGNATURE) ;; (sig == IMAGE_NT_SIGNATURE)) printf("有效的PE文件/n"); else printf("无效的PE文件/n"); return 0; } | |
![]() | ![]() |