您的位置:首页 > 其它

学破解 <一> PE格式之MS-DOS MZ header

2012-04-18 23:27 190 查看
from:http://www.2cto.com/Article/201203/123125.html

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 的结构是这样的

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;

下面是完整的程序

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: