当前位置:
文档之家› WindowsPE文件格式资料
WindowsPE文件格式资料
DOS头
MZ文件头 :DOS MZ HEADER DOS插桩程序 :DOS Stub PE文件标志 :“ PE\0\0”
文件头
PE文件头
映像文件头 :IMAGE_FILE_HEADER 可选映像头 :IMAGE_OPTIONAL_HEADER32 数据目录表 :IMAGE_DATA_DIRECTORY IMAGE_SECTION_HEADER
第三章 Windows PE格式
内容
PE文件
相关术语
PE文件格式详解 Windows病毒原理
1. PE文件
PE(Portable Executable)是Win32可执行文 件的标准格式,如*.EXE、*.DLL、*.OCX等, 都是PE格式 病毒籍EXE文件被执行时传播,Win32病毒 感染文件时,基本上将EXE文件作为目标
DOS头数据结构
typedef struct _IMAGE_DOS_HEADER { // DOS的.EXE头部 USHORT e_magic; // 魔术数字 “MZ” USHORT e_cblp; // 文件最后页的字节数 USHORT e_cp; // 文件页数 USHORT e_crlc; // 重定义元素个数 USHORT e_cparhdr; // 头部尺寸,以段落为单位 USHORT e_minalloc; // 所需的最小附加段 USHORT e_maxalloc; // 所需的最大附加段 USHORT e_ss; // 初始的SS值(相对偏移量) USHORT e_sp; // 初始的SP值 USHORT e_csum; // 校验和 USHORT e_ip; // 初始的IP值 USHORT e_cs; // 初始的CS值(相对偏移量) USHORT e_lfarlc; // 重分配表文件地址 USHORT e_ovno; // 覆盖号 USHORT e_res[4]; // 保留字 USHORT e_oemid; // OEM标识符(相对e_oeminfo) USHORT e_oeminfo; // OEM信息 USHORT e_res2[10]; // 保留字 LONG e_lfanew; // 新EXE头部的文件地址 003c } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
节表 (Section Table)
IMAGE_SECTION_HEADER IMAGE_SECTION_HEADER IMAGE_SECTION_HEADER .text .data .edata .reloc ... COFF行号 COFF符号表 Code View调试信息
节 (Section)
Win32病毒运行流程
Win32病毒运行流程
①用户点击或系统自动运行HOST程序; ②装载HOST程序到内存; ③由PE文件的AddressOfEntryPoint加 ImageBase之和,定位第一条语句的位置(程序 入口); ④从第一条语句开始执行(其实是病毒代码); ⑤病毒主体代码执行完毕,将控制权交给HOST 程序原来的入口代码; ⑥HOST程序继续执行。
保护模式下,程序访问存储器所使用的逻辑地
址称为虚拟地址(VA),内存偏移地址(memory offset)
相对虚拟地址(Relative
Virtual Address,RVA)
指内存中相对于PE文件装入地址(基地址) 的偏移量 RVA=VA – imagebase
3. PE文件结构
P E 文 件 总 体 层 次 结 构
术语(续)
入口点(Entry point)
程序执行的第一行代码
基地址(Image base)
文件执行时将被映射到指定内存地址的存初始地址,
由PE文件本身决定。默认,EXE:0x00400000, DLL:0x1000000。用链接程序的/BASE选项改变该值
术语(续)
虚拟地址(Virtual Address,VA)
PE文件加载流程
Major steps in loading a PE file into memory:
When the PE file is run, the PE loader examines the DOS MZ header for the offset of the PE header. If found, it skips to the PE header. The PE loader checks if the PE header is valid. If so, it goes to the end of the PE header. Immediately following the PE header is the section table. The PE header reads information about the sections and maps those sections into memory using file mapping. It also gives each section the attributes as specified in the section table. After the PE file is mapped into memory, the PE loader concerns itself with the logical parts of the PE file, such as the import table.
问题
计算机病毒怎会在HOST程序之前执行?
2. 术语
文件偏移地址(File offset)
PE文件存储在磁盘上,各数据段的地址称为文件偏移
地址或物理地址(raw offset)。文件偏移地址从PE文件的第一个字节Fra bibliotek始计数,起始值为0
映射到内存中 磁盘中的PE文件 文件尾 文 件 偏 移 地 址 文件头 不能映射的数据 .reloc .data .text 节表 PE头 DOS头 DOS头 基地址(ImageBase) .reloc .data .text 某一虚拟地址(VA) 节表 PE头 相对虚拟地址(RVA)
调试信息
文件尾
3.1 DOS头与DOS插桩程序
DOS头与DOS插桩程序
PE结构中紧随MZ文件头之后的DOS插桩程序(DOS
Stub) IMAGE_DOS_HEADER结构识别一个合法的DOS头 该结构的e_lfanew(偏移60,32bits)成员定位PE开始的 标志0x00004550(“PE\0\0”) 病毒通过“MZ”、“PE”这两个标志,初步判断当前程 序是否是目标文件——PE文件。