Quick notes about PE file format

image

Table of Contents

IMAGE_DOS_HEADER

OffsetTypeFieldDescription
00WORDe_magicMZ
02WORDe_cblpBytes on last page of file
04WORDe_cpPages in file
06WORDe_crlcRelocations
08WORDe_cparhdrSize of header in paragraphs
0AWORDe_minallocMinimum extra paragraphs needed
0CWORDe_maxallocMaximum extra paragraphs needed
0EWORDe_ssInitial (relative) SS value
10WORDe_spInitial SP value
12WORDe_csumChecksum
14WORDe_ipInitial IP value
16WORDe_csInitial (relative) CS value
18WORDe_lfarlcFile address of relocation table
1AWORDe_ovnoOverlay number
1CWORD[4]e_resReserved words
24WORDe_oemidOEM identifier (for e_oeminfo)
26WORDe_oeminfoOEM information; e_oemid specific
28WORD[10]e_res2Reserved words
3CDWORDe_lfanewFile address of new exe header

Note: Get the address of IMAGE_NT_HEADERS by adding the base address to e_lfanew

IMAGE_NT_HEADERS

TypeField
DWORDSignature
IMAGE_FILE_HEADERFileHeader
IMAGE_OPTIONAL_HEADEROptionalHeader

IMAGE_FILE_HEADER (The COFF File Header)

OffsetTypeField
00DWORDSignature
04WORDMachine (0x8864 : AMD64, 0x14c : i386)
06WORDNumber of Sections
08DWORDTimeDateStamp
0CDWORDPointerToSymbolTable
10DWORDNumberOfSymbols
14WORDSizeOfOptionalHeader
16WORDCharacteristics

IMAGE_OPTIONAL_HEADER

There are 2 versions of the Optional Header, 32-bit and 64-bit. Their differences are:

  • The number of members defined within the struct: 32-bit has an additional member, BaseOfData (hold the RVA of the data section)
  • The data type of some of members

32-bit

Standard Fields
18WORDMagic
1ABYTEMajorLinkerVersion
1BBYTEMinorLinkerVersion
1CDWORDSizeOfCode (size of the code section)
20DWORDSizeOfInitializedData
24DWORDSizeOfUnitializedData
28DWORDAddressOfEntryPoint
2CDWORDBaseOfCode
30DWORDBaseOfData
NT additional fields
34DWORDImageBase (The preferred address to be loaded)
38DWORDSectionAlignment
3CDWORDFileAlignment
40WORDMajorOperatingSystemVersion
42WORDMinorOperatingSystemVersion
44WORDMajorImageVersion
46WORDMinorImageVersion
48WORDMajorSubsystemVersion
4AWORDMinorSubsystemVersion
4CDWORDReserved1
50DWORDSizeOfImage
54DWORDSizeOfHeaders
58DWORDCheckSum (The algorithm for computing the checksum is incorporated into IMAGHELP.DLL)
5CWORDSubsystem
5EWORDDllCharacteristics
60DWORDSizeOfStackReserve
64DWORDSizeOfStackCommit
68DWORDSizeOFHeapReserve
6CDWORDSizeOfHeapCommit
70DWORDLoaderFlags
74DWORDNumberOfRvaAndSizes
78DWORDExportDirectory VA
7CDWORDExportDirectory Size
80DWORDImportDirectory VA
84DWORDImportDirectory Size
88DWORDResourceDirectory VA
8CDWORDResourceDirectory Size
90DWORDExceptionDirectory VA
94DWORDExceptionDirectory Size
98DWORDSecurityDirectory VA
9CDWORDSecurityDirectory Size
A0DWORDBaseRelocationTable VA
A4DWORDBaseRelocationTable Size
A8DWORDDebugDirectory VA
ACDWORDDebugDirectory Size
B0DWORDArchitectureSpecificData VA
B4DWORDArchitectureSpecificData Size
B8DWORDRVAofGP VA
BCDWORDRVAofGP Size
C0DWORDTLSDirectory VA
C4DWORDTLSDirectory Size
C8DWORDLoadConfigurationDirectory VA
CCDWORDLoadConfigurationDirectory Size
D0DWORDBoundImportDirectoryinheaders VA
D4DWORDBoundImportDirectoryinheaders Size
D8DWORDImportAddressTable VA
DCDWORDImportAddressTable Size
E0DWORDDelayLoadImportDescriptors VA
E4DWORDDelayLoadImportDescriptors Size
E8DWORDCOMRuntimedescriptor VA
ECDWORDCOMRuntimedescriptor Size
F0DWORD0
F4DWORD0

IMAGE_DATA_DIRECTORY

typedef struct _IMAGE_DATA_DIRECTORY {
    DWORD   VirtualAddress;
    DWORD   Size;
} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
  • VirtualAddress: the RVA point to the start of the Data Directory
  • Size: the size of the Data Directory

List of Data Directories defined in winnt.h

// Directory Entries

#define IMAGE_DIRECTORY_ENTRY_EXPORT          0   // Export Directory
#define IMAGE_DIRECTORY_ENTRY_IMPORT          1   // Import Directory
#define IMAGE_DIRECTORY_ENTRY_RESOURCE        2   // Resource Directory
#define IMAGE_DIRECTORY_ENTRY_EXCEPTION       3   // Exception Directory
#define IMAGE_DIRECTORY_ENTRY_SECURITY        4   // Security Directory
#define IMAGE_DIRECTORY_ENTRY_BASERELOC       5   // Base Relocation Table
#define IMAGE_DIRECTORY_ENTRY_DEBUG           6   // Debug Directory
//      IMAGE_DIRECTORY_ENTRY_COPYRIGHT       7   // (X86 usage)
#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE    7   // Architecture Specific Data
#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR       8   // RVA of GP
#define IMAGE_DIRECTORY_ENTRY_TLS             9   // TLS Directory
#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG    10   // Load Configuration Directory
#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT   11   // Bound Import Directory in headers
#define IMAGE_DIRECTORY_ENTRY_IAT            12   // Import Address Table
#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT   13   // Delay Load Import Descriptors
#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14   // COM Runtime descriptor

IMAGE_EXPORT_DIRECTORY

OffsetTypeField
00DWORDCharacteristics
04DWORDTimeDateStamp
08WORDMajorVersion
0AWORDMinorVersion
0CDWORDName
10DWORDBase
14DWORDNumberOfFunctions
18DWORDNumberOfNames
1CDWORDAddressOfFunctions
20DWORDAddressOfNames
24DWORDAddressOfNameOrdinals

Sections

Section NameDescription
.textContains the executable code of the program
.dataContains the initialized data
.bssContains the uninitialized data
.rdataContains Read-Only initialized data
.edataContains the Export tables
.idataContains the Import tables
.relocContains image relocation information
.rsrcContains resources used by the program
.tlsProvides storage for every executing thread of the program

IMAGE_SECTION_HEADER

OffsetTypeField
00BYTE[8]Name
08DWORDPhysicalAddress / VirtualSize
0CDWORDVirtualAddress
10DWORDSizeOfRawData
14DWORDPointerToRawData
18DWORDPointerToRelocations
1CDWORDPointerToLineNumbers
20WORDNumberOfRelocations
22WORDNumberOfLineNumbers
24DWORDCharacteristics

The SizeOfRawData and VirtualSize can be different based on:

  • The file alignment as SizeOfRawData must be a multiple of IMAGE_OPTIONAL_HEADER.FileAlignment.
  • The VirtualSize is depend on how the section expand to reserve memory space for the uninitialized data.

References
kienmanowar | 0xRick | microsoft