Dos头标志

    1. #define IMAGE_DOS_SIGNATURE 0x4D5A // MZ

    Dos_MZ头

    1. typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header
    2. +00h WORD e_magic; // Magic number(需要被设置值0x5A4D,ASCII值为“MZ”)
    3. WORD e_cblp; // Bytes on last page of file
    4. WORD e_cp; // Pages in file
    5. WORD e_crlc; // Relocations
    6. WORD e_cparhdr; // Size of header in paragraphs
    7. WORD e_minalloc; // Minimum extra paragraphs needed
    8. WORD e_maxalloc; // Maximum extra paragraphs needed
    9. WORD e_ss; // Initial (relative) SS value
    10. WORD e_sp; // Initial SP value
    11. WORD e_csum; // Checksum
    12. WORD e_ip; // Initial IP value
    13. WORD e_cs; // Initial (relative) CS value
    14. WORD e_lfarlc; // File address of relocation table
    15. WORD e_ovno; // Overlay number
    16. WORD e_res[4]; // Reserved words
    17. WORD e_oemid; // OEM identifier (for e_oeminfo)
    18. WORD e_oeminfo; // OEM information; e_oemid specific
    19. WORD e_res2[10]; // Reserved words
    20. +3Ch LONG e_lfanew; // File address of new exe header(指出PE头的文件偏移)
    21. } IMAGE_DOS_HEADER, * PIMAGE_DOS_HEADER;

    NT头

    1. typedef struct _IMAGE_NT_HEADERS {
    2. +00h DWORD Signature; //此字段被设置为0x00004550,ASCII码是“PE00”
    3. +04h IMAGE_FILE_HEADER FileHeader; //一个IMAGE_FILE_HEADER结构
    4. +18h IMAGE_OPTIONAL_HEADER32 OptionalHeader; //一个IMAGE_OPTIONAL_HEADER 结构
    5. } IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;

    NT头标志

    1. #define IMAGE_NT_SIGNATURE 0x00004550 // PE00

    文件头

    1. typedef struct _IMAGE_FILE_HEADER {
    2. +04h WORD Machine;
    3. +06h WORD NumberOfSections; //区块的数量
    4. +08h DWORD TimeDateStamp;
    5. +0Ch DWORD PointerToSymbolTable;
    6. +10h DWORD NumberOfSymbols;
    7. +14h WORD SizeOfOptionalHeader; //跟在此结构后面的数据的大小,即IMAGE_OPTIONAL_HEADER的大小
    8. +18h WORD Characteristics;
    9. } IMAGE_FILE_HEADER, * PIMAGE_FILE_HEADER;

    扩展头

    1. typedef struct _IMAGE_OPTIONAL_HEADER {
    2. WORD Magic; //32位PE文件0x010B,64位PE文件0x020B
    3. BYTE MajorLinkerVersion;
    4. BYTE MinorLinkerVersion;
    5. DWORD SizeOfCode;
    6. DWORD SizeOfInitializedData;
    7. DWORD SizeOfUninitializedData;
    8. +28h DWORD AddressOfEntryPoint; //程序执行入口RVA (入口点,也称OEP)
    9. DWORD BaseOfCode; //代码区块的起始RVA
    10. DWORD BaseOfData; //数据区块的起始RVA
    11. +34h DWORD ImageBase; //默认加载基址(如果没有加载到这个地址,会发生重定位)
    12. +38h DWORD SectionAlignment; //块对齐数,映射到内存中的区段对齐,一般为0x1000
    13. +3Ch DWORD FileAlignment; //文件对齐数,一般是0x200
    14. WORD MajorOperatingSystemVersion;
    15. WORD MinorOperatingSystemVersion;
    16. WORD MajorImageVersion;
    17. WORD MinorImageVersion;
    18. WORD MajorSubsystemVersion;
    19. WORD MinorSubsystemVersion;
    20. DWORD Win32VersionValue;
    21. +50h DWORD SizeOfImage; //映像装入内存后的总大小
    22. +54h DWORD SizeOfHeaders; //MS-DOS头部+PE+区块表的总大小(一般为0x400)5ch
    23. DWORD CheckSum; //映像的校验和
    24. WORD Subsystem;
    25. +5Eh WORD DllCharacteristics; //Dll特征的标志(其含有控制随机基址字段)
    26. DWORD SizeOfStackReserve;
    27. DWORD SizeOfStackCommit;
    28. DWORD SizeOfHeapReserve;
    29. DWORD SizeOfHeapCommit;
    30. DWORD LoaderFlags;
    31. DWORD NumberOfRvaAndSizes; //数据目录表的个数
    32. +78h IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; //数据目录表
    33. } IMAGE_OPTIONAL_HEADER32, * PIMAGE_OPTIONAL_HEADER32;

    数据目录表

    1. #define IMAGE_DIRECTORY_ENTRY_EXPORT 0 // 导出表
    2. #define IMAGE_DIRECTORY_ENTRY_IMPORT 1 // 导入表
    3. #define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 // 资源
    4. #define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 // 异常
    5. #define IMAGE_DIRECTORY_ENTRY_SECURITY 4 // 安全
    6. #define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 // 重定位表
    7. #define IMAGE_DIRECTORY_ENTRY_DEBUG 6 // 调试信息
    8. // IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 // (X86 usage)
    9. #define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 // 版权信息
    10. #define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 // RVA of GP
    11. #define IMAGE_DIRECTORY_ENTRY_TLS 9 // TLS Directory
    12. #define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 // Load Configuration Directory
    13. #define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 // Bound Import Directory in headers
    14. #define IMAGE_DIRECTORY_ENTRY_IAT 12 // 导入函数地址表
    15. #define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 // Delay Load Import Descriptors
    16. #define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 // COM Runtime descriptor
    1. typedef struct _IMAGE_DATA_DIRECTORY {
    2. DWORD VirtualAddress; //数据的RVA
    3. DWORD Size; //数据的大小
    4. } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;

    导入表

    1. typedef struct _IMAGE_IMPORT_DESCRIPTOR {
    2. union {
    3. DWORD Characteristics; // 0 for terminating null import descriptor
    4. DWORD OriginalFirstThunk; // RVA to original unbound IAT (PIMAGE_THUNK_DATA)
    5. } DUMMYUNIONNAME;
    6. DWORD TimeDateStamp; // 0 if not bound,
    7. // -1 if bound, and real date\time stamp
    8. // in IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT (new BIND)
    9. // O.W. date/time stamp of DLL bound to (Old BIND)
    10. DWORD ForwarderChain; // -1 if no forwarders
    11. DWORD Name;
    12. DWORD FirstThunk; // RVA to IAT (if bound this IAT has actual addresses)
    13. } IMAGE_IMPORT_DESCRIPTOR;

    导出表

    1. typedef struct _IMAGE_EXPORT_DIRECTORY {
    2. DWORD Characteristics;
    3. DWORD TimeDateStamp;
    4. WORD MajorVersion;
    5. WORD MinorVersion;
    6. DWORD Name;
    7. DWORD Base;
    8. DWORD NumberOfFunctions;
    9. DWORD NumberOfNames;
    10. DWORD AddressOfFunctions; // RVA from base of image
    11. DWORD AddressOfNames; // RVA from base of image
    12. DWORD AddressOfNameOrdinals; // RVA from base of image
    13. } IMAGE_EXPORT_DIRECTORY, *PIMAGE_EXPORT_DIRECTORY;

    资源表

    1. typedef struct _IMAGE_RESOURCE_DIRECTORY {
    2. DWORD Characteristics;
    3. DWORD TimeDateStamp;
    4. WORD MajorVersion;
    5. WORD MinorVersion;
    6. WORD NumberOfNamedEntries;
    7. WORD NumberOfIdEntries;
    8. // IMAGE_RESOURCE_DIRECTORY_ENTRY DirectoryEntries[];
    9. } IMAGE_RESOURCE_DIRECTORY, *PIMAGE_RESOURCE_DIRECTORY;

    重定位表

    1. 0000 0000 ...

    调试表

    1. typedef struct _IMAGE_DEBUG_DIRECTORY {
    2. DWORD Characteristics;
    3. DWORD TimeDateStamp;
    4. WORD MajorVersion;
    5. WORD MinorVersion;
    6. DWORD Type;
    7. DWORD SizeOfData;
    8. DWORD AddressOfRawData;
    9. DWORD PointerToRawData;
    10. } IMAGE_DEBUG_DIRECTORY, *PIMAGE_DEBUG_DIRECTORY;

    区段表

    1. typedef struct _IMAGE_SECTION_HEADER {
    2. BYTE Name[IMAGE_SIZEOF_SHORT_NAME]; //块名
    3. union {
    4. DWORD PhysicalAddress; //始终为NULL
    5. DWORD VirtualSize; //指出实际的、被使用的区块的大小
    6. } Misc; //(也就是区块数据没有对齐处理前的实际大小)
    7. DWORD VirtualAddress; //该块装载到内存中的RVA
    8. DWORD SizeOfRawData; //该块在磁盘文件中所占的大小
    9. DWORD PointerToRawData; //该块在磁盘文件中的偏移
    10. DWORD PointerToRelocations;
    11. DWORD PointerToLinenumbers;
    12. WORD NumberOfRelocations; //由PointerToRelocations指向的重定位的数目
    13. WORD NumberOfLinenumbers;
    14. DWORD Characteristics; //块属性
    15. } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;