1.常见PE文件
可执行文件:EXE,SCR,COM
驱动程序:SYS,VSD
库文件:DLL,OCX,CPL,DRV
对象文件:OBJ
2.PE32(32位)、PE32+或者PE+(64位)
3.PE组成:
DOS头
DOS存根
NT头
节区头(代码段)
节区头(数据段)
节区头(资源段)
节区(代码段)
节区(数据段)
节区(资源段)
4.VA(virtual address:虚拟地址)&RVA(relative virtual address:相对虚拟地址)
VA=RVA+ImageBase(映象基址)
5.DOS头
IMAGE_DOS_HEADER STRUCT
{
+0h WORD e_magic
+2h WORD e_cblp
+4h WORD e_cp
+6h WORD e_crlc
+8h WORD e_cparhdr
+0ah WORD e_minalloc
+0ch WORD e_maxalloc
+0eh WORD e_ss
+10h WORD e_sp
+12h WORD e_csum
+14h WORD e_ip
+16h WORD e_cs
+18h WORD e_lfarlc
+1ah WORD e_ovno
+1ch WORD e_res[4]
+24h WORD e_oemid
+26h WORD e_oeminfo
+29h WORD e_res2[10]
+3ch DWORD e_lfanew
} IMAGE_DOS_HEADER ENDS
6.DOS存根
DOS存根是16位的汇编指令,用以在DOS中运行
7.NT头
typedef struct _IMAGE_NT_HEADERS {
+00h DWORD Signature;
+04h IMAGE_FILE_HEADER FileHeader;
+18h IMAGE_OPTIONAL_HEADER32 OptionalHeader;
} IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;
typedef struct _IMAGE_FILE_HEADER {
+00h WORD Machine;
+02h WORD NumberOfSections;
+04h DWORD TimeDateStamp;
+08h DWORD PointerToSymbolTable;
+0ch DWORD NumberOfSymbols;
+10h WORD SizeOfOptionalHeader;
+12h WORD Characteristics;
} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
typedef struct _IMAGE_OPTIONAL_HEADER {
+00h WORD Magic;
+02h BYTE MajorLinkerVersion;
+03h BYTE MinorLinkerVersion;
+04h DWORD SizeOfCode;
+08h DWORD SizeOfInitializedData;
+0ch DWORD SizeOfUninitializedData;
+10h DWORD AddressOfEntryPoint;
+14h DWORD BaseOfCode;
+18h DWORD BaseOfData;
+1ch DWORD ImageBase;
+20h DWORD SectionAlignment;
+24h DWORD FileAlignment;
+28h WORD MajorOperatingSystemVersion;
+2ah WORD MinorOperatingSystemVersion;
+2ch WORD MajorImageVersion;
+2eh WORD MinorImageVersion;
+30h WORD MajorSubsystemVersion;
+32h WORD MinorSubsystemVersion;
+34h DWORD Win32VersionValue;
+38h DWORD SizeOfImage;
+3ch DWORD SizeOfHeaders;
+40h DWORD CheckSum;
+44h WORD Subsystem;
+46h WORD DllCharacteristics;
+48h DWORD SizeOfStackReserve;
+4ch DWORD SizeOfStackCommit;
+50h DWORD SizeOfHeapReserve;
+54h DWORD SizeOfHeapCommit;
+58h DWORD LoaderFlags;
+5eh DWORD NumberOfRvaAndSizes;
+60h IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
} IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;
typedef struct {
+00h DWORD VirtualAddress;
+04h DWORD Size;
} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
#define IMAGE_DIRECTORY_ENTRY_EXPORT 0
#define IMAGE_DIRECTORY_ENTRY_IMPORT 1
#define IMAGE_DIRECTORY_ENTRY_RESOURCE 2
#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3
#define IMAGE_DIRECTORY_ENTRY_SECURITY 4
#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5
#define IMAGE_DIRECTORY_ENTRY_DEBUG 6
#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7
#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8
#define IMAGE_DIRECTORY_ENTRY_TLS 9
#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10
#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11
#define IMAGE_DIRECTORY_ENTRY_IAT 12
#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13
#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14
8.节区头
typedef struct _IMAGE_SECTION_HEADER {
+00h BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
+08h union {
DWORD PhysicalAddress;
DWORD VirtualSize;
} Misc;
+0ch DWORD VirtualAddress;
+10h DWORD SizeOfRawData;
+14h DWORD PointerToRawData;
+18h DWORD PointerToRelocations;
+1ch DWORD PointerToLinenumbers;
+20h WORD NumberOfRelocations;
+22h WORD NumberOfLinenumbers;
+24h DWORD Characteristics;
} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
RVA与RAW换算(内存地址与文件偏移的转换)
RAW=RVA-VirtualAddress+PointerToRawData
9.IAT(Import Address Table:导入地址表):
INT和IAT是以NULL为结束的长整型数组,保存的是_IMAGE_IMPORT_BY_NAME, IMAGE_THUNK_DATA 的地址
_IMAGE_IMPORT_DESCRIPTOR:结构体中记录着PE文件要导入哪些库文件
typedef struct _IMAGE_IMPORT_DESCRIPTOR {
union {
DWORD Characteristics;
DWORD OriginalFirstThunk;
};
DWORD TimeDateStamp;
DWORD ForwarderChain;
DWORD Name;
DWORD FirstThunk;
} IMAGE_IMPORT_DESCRIPTOR;
typedef struct _IMAGE_IMPORT_BY_NAME {
WORD Hint;
BYTE Name[1];
} `, *PIMAGE_IMPORT_BY_NAME;
typedef struct _IMAGE_THUNK_DATA32 {
union {
DWORD ForwarderString;
DWORD Function;
DWORD Ordinal;
DWORD AddressOfData;
} u1;
} IMAGE_THUNK_DATA32;
IMAGE_THUNK_DATA64与IMAGE_THUNK_DATA32的区别,仅仅是把DWORD换成了64位整数。
10.EAT
typedef struct _IMAGE_EXPORT_DIRECTORY {
DWORD Characteristics;
DWORD TimeDateStamp;
WORD MajorVersion;
WORD MinorVersion;
DWORD Name;
DWORD Base;
DWORD NumberOfFunctions;
DWORD NumberOfNames;
DWORD AddressOfFunctions;
DWORD AddressOfNames;
DWORD AddressOfNameOrdinals;
} IMAGE_EXPORT_DIRECTORY,*PIMAGE_EXPORT_DIRECTORY;