struct _IMAGE_DOS_HEADER {0x00 WORD e_magic;//"MZ标记" 用于判断是否为可执行文件. //notepad.exe 5a4dh WORD = U0x02 WORD e_cblp;//0090h0x04 WORD e_cp;//0003h0x06 WORD e_crlc;//0000h0x08 WORD e_cparhdr;//0004h0x0a WORD e_minalloc;//0000h0x0c WORD e_maxalloc;//ffffh0x0e WORD e_ss;//0000h0x10 WORD e_sp;//00b8h0x12 WORD e_csum;//0000h0x14 WORD e_ip;//0000h0x16 WORD e_cs;//0000h0x18 WORD e_lfarlc;//0040h0x1a WORD e_ovno;//0000h0x1c WORD e_res[4];//0000 0000 0000 0000h0x24 WORD e_oemid;//00 00 h0x26 WORD e_oeminfo;//0000h0x28 WORD e_res2[10];0x3c DWORD e_lfanew;//PE头相对于文件的偏移,用于定位PE文件//00 00 00 e8 *};
struct _IMAGE_FILE_HEADER {0x00 WORD Machine;//程序运行的CPU型号:0x0 任何处理器/0x14C 386及后续处理器0x02 WORD NumberOfSections;//文件中存在的节的总数,如果要新增节或者合并节 就要修改这个值0x04 DWORD TimeDateStamp;//时间戳:文件的创建时间(和操作系统的创建时间无关),编译器填写的0x08 DWORD PointerToSymbolTable;0x0c DWORD NumberOfSymbols;0x10 WORD SizeOfOptionalHeader;//可选PE头的大小,32位PE文件默认E0h 64位PE文件默认为F0h 大小可以自定义.0x12 WORD Characteristics;//每个位有不同的含义,可执行文件值为10F 即0 1 2 3 8位置1 };
可选PE头**(很多重要属性)
struct _IMAGE_OPTIONAL_HEADER {0x00 WORD Magic;//说明文件类型:10B 32位下的PE文件 20B 64位下的PE文件0x02 BYTE MajorLinkerVersion;0x03 BYTE MinorLinkerVersion;0x04 DWORD SizeOfCode;//所有代码节的和,必须是FileAlignment的整数倍 编译器填的 没用0x08 DWORD SizeOfInitializedData;//已初始化数据大小的和,必须是FileAlignment的整数倍 编译器填的 没用0x0c DWORD SizeOfUninitializedData;//未初始化数据大小的和,必须是FileAlignment的整数倍 编译器填的 没用0x10 DWORD AddressOfEntryPoint;//程序入口0x14 DWORD BaseOfCode;//代码开始的基址,编译器填的 没用0x18 DWORD BaseOfData;//数据开始的基址,编译器填的 没用0x1c DWORD ImageBase;//内存镜像基址0x20 DWORD SectionAlignment;//内存对齐0x24 DWORD FileAlignment;//文件对齐0x28 WORD MajorOperatingSystemVersion;0x2a WORD MinorOperatingSystemVersion;0x2c WORD MajorImageVersion;0x2e WORD MinorImageVersion;0x30 WORD MajorSubsystemVersion;0x32 WORD MinorSubsystemVersion;0x34 DWORD Win32VersionValue;0x38 DWORD SizeOfImage;//内存中整个PE文件的映射的尺寸,可以比实际的值大,但必须是SectionAlignment的整数倍0x3c DWORD SizeOfHeaders;//所有头+节表按照文件对齐后的大小,否则加载会出错0x40 DWORD CheckSum;//校验和,一些系统文件有要求.用来判断文件是否被修改.0x44 WORD Subsystem;0x46 WORD DllCharacteristics;0x48 DWORD SizeOfStackReserve;//初始化时保留的堆栈大小 0x4c DWORD SizeOfStackCommit;//初始化时实际提交的大小0x50 DWORD SizeOfHeapReserve;//初始化时保留的堆大小 0x54 DWORD SizeOfHeapCommit;//初始化时实践提交的大小 0x58 DWORD LoaderFlags;0x5c DWORD NumberOfRvaAndSizes;//目录项数目0x60 _IMAGE_DATA_DIRECTORY DataDirectory[16];};
节表-节的描述信息,目录;
IMAGE_SECTION_HEADER *pISH =(IMAGE_SECTION_HEADER *)((DWORD)pFileBuf + pIDH->e_lfanew +sizeof(IMAGE_NT_HEADERS));typedefstruct _IMAGE_SECTION_HEADER {// length:0x28;0x00 BYTE Name[IMAGE_SIZEOF_SHORT_NAME];//8个字节 一般情况下是以"\0"结尾的ASCII吗字符串来标识的名称,内容可以自定义.注意:该名称并不遵守必须以"\0"结尾的规律,如果不是以"\0"结尾,系统会截取8个字节的长度进行处理.union{0x08 DWORD PhysicalAddress;0x08 DWORD VirtualSize;//内存中没有对齐前的实际大小,内存中的大小和sizeOfRawData大小不一样} Misc;//双字 是该节在没有对齐前的真实尺寸,该值可以不准确。0x0c DWORD VirtualAddress;//节区在内存中的偏移地址。加上ImageBase才是在内存中的真正地址.0x10 DWORD SizeOfRawData;//节在文件中对齐后的尺寸.0x14 DWORD PointerToRawData;//节区在文件中的偏移.0x18 DWORD PointerToRelocations;//在obj文件中使用 对exe无意义0x1c DWORD PointerToLinenumbers;//行号表的位置 调试的时候使用0x20 WORD NumberOfRelocations;//在obj文件中使用 对exe无意义0x22 WORD NumberOfLinenumbers;//行号表中行号的数量 调试的时候使用0x24 DWORD Characteristics;//节的属性};//节的特征属性中,可执行、可读、可写、包含可执行代码等等。
标志(属性块) 常用特征值对照表:
[值:00000020h][IMAGE_SCN_CNT_CODE // Section contains code.(包含可执行代码)][值:00000040h][IMAGE_SCN_CNT_INITIALIZED_DATA // Section contains initialized data.(该块包含已初始化的数据)][值:00000080h][IMAGE_SCN_CNT_UNINITIALIZED_DATA // Section contains uninitialized data.(该块包含未初始化的数据)][值:00000200h][IMAGE_SCN_LNK_INFO // Section contains comments or some other type of information.][值:00000800h][IMAGE_SCN_LNK_REMOVE // Section contents will not become part of image.][值:00001000h][IMAGE_SCN_LNK_COMDAT // Section contents comdat.][值:00004000h][IMAGE_SCN_NO_DEFER_SPEC_EXC // Reset speculative exceptions handling bits in the TLB entries for this section.][值:00008000h][IMAGE_SCN_GPREL // Section content can be accessed relative to GP.][值:00500000h][IMAGE_SCN_ALIGN_16BYTES // Default alignment if no others are specified.][值:01000000h][IMAGE_SCN_LNK_NRELOC_OVFL // Section contains extended relocations.][值:02000000h][IMAGE_SCN_MEM_DISCARDABLE // Section can be discarded.][值:04000000h][IMAGE_SCN_MEM_NOT_CACHED // Section is not cachable.][值:08000000h][IMAGE_SCN_MEM_NOT_PAGED // Section is not pageable.][值:10000000h][IMAGE_SCN_MEM_SHARED // Section is shareable(该块为共享块).][值:20000000h][IMAGE_SCN_MEM_EXECUTE // Section is executable.(该块可执行)][值:40000000h][IMAGE_SCN_MEM_READ // Section is readable.(该块可读)][值:80000000h][IMAGE_SCN_MEM_WRITE // Section is writeable.(该块可写)]