| Sulaiman Chang Personal Website |
|
Windows PE File Format Walkthrough II Sulaiman Chang September 14, 2004 IMAGE_NT_HEADERS Now, we are on the way to understand the IMAGE_NT_HEADERS structure. This structure is declared in WINNT.H file.
typedef struct _IMAGE_NT_HEADERS {
DWORD Signature;
IMAGE_FILE_HEADER FileHeader;
IMAGE_OPTIONAL_HEADER32 OptionalHeader;
} IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;
we got so many things to understand now! nevermind, we just move on step by step and hopefully in the end, we would be able to make sense out of it.
The IMAGE_NT_HEADERS structure represents the PE header format. The Signature should contained the value 50 45 00 00 or (ascii) PE 0 0 to be identified as a valid PE image. IMAGE_FILE_HEADER The IMAGE_FILE_HEADER is a 20 bytes structure that included in the structure of IMAGE_NT_HEADERS.
typedef struct _IMAGE_FILE_HEADER {
WORD Machine;
WORD NumberOfSections;
DWORD TimeDateStamp;
DWORD PointerToSymbolTable;
DWORD NumberOfSymbols;
WORD SizeOfOptionalHeader;
WORD Characteristics;
} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
now, we got some information how to code our IMAGE_NT_HEADERS with IMAGE_FILE_HEADER. Below is what we could form using the above information.
IMAGE_NT_HEADERS: ;start : 80 (128) to 1EF (495)
.Signature db 'PE',0,0 ;128 131
IMAGE_FILE_HEADER: ;start : 84 (132) to 97 (151)
.Machine dw 0x014C ;132 133 for intel 386
.NumberOfSection dw 0x0003 ;134 135
.TimeDateStamp dd %t ;136 139
.PointerToSymbolTable dd 0 ;140 143
.NumberOfSymbols dd 0 ;144 147
.SizeOfOptionalHeader dw 0x00E0 ;148 149
.Characteristic dw 0x818F ;150 151
db 0x0B,0x01,0x01,0x37,0x00,0x00,0x00,0x00,\
..... to the end
our characteristic value is 0x818F which is equal to
+----------------------------------------------+
| Characteristic value for general PE EXE file |
+----------------------------------------------+
IMAGE_FILE_RELOCS_STRIPPED 0x0001
IMAGE_FILE_EXECUTABLE_IMAGE 0x0002 + = 0x0003
IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004 + = 0x0007
IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008 + = 0x000F
IMAGE_FILE_BYTES_REVERSED_LO 0x0080 + = 0x008F
IMAGE_FILE_32BIT_MACHINE 0x0100 + = 0x018F
IMAGE_FILE_BYTES_REVERSED_HI 0x8000 + = 0x818F <-- our characteristic value
now, we got some new idea, how our PE file format actually looks like.
PE File Format (so far)
=======================
0x00 .... 0x3F ------------------------------- IMAGE_DOS_HEADER
0x40 .... 0x7F ------------------------------- DOS 2.0 Stub Program
0x80 .... ? ------------------------------- IMAGE_NT_HEADERS
0x84 .... 0x97 --------------------- IMAGE_FILE_HEADER
Our NumberOfSection value is 0x0003 because we got 3 section for our PE, which are, ".data", ".code" and ".idata".
typedef struct _IMAGE_OPTIONAL_HEADER {
WORD Magic;
BYTE MajorLinkerVersion;
BYTE MinorLinkerVersion;
DWORD SizeOfCode;
DWORD SizeOfInitializedData;
DWORD SizeOfUninitializedData;
DWORD AddressOfEntryPoint;
DWORD BaseOfCode;
DWORD BaseOfData;
DWORD ImageBase;
DWORD SectionAlignment;
DWORD FileAlignment;
WORD MajorOperatingSystemVersion;
WORD MinorOperatingSystemVersion;
WORD MajorImageVersion;
WORD MinorImageVersion;
WORD MajorSubsystemVersion;
WORD MinorSubsystemVersion;
DWORD Win32VersionValue;
DWORD SizeOfImage;
DWORD SizeOfHeaders;
DWORD CheckSum;
WORD Subsystem;
WORD DllCharacteristics;
DWORD SizeOfStackReserve;
DWORD SizeOfStackCommit;
DWORD SizeOfHeapReserve;
DWORD SizeOfHeapCommit;
DWORD LoaderFlags;
DWORD NumberOfRvaAndSizes;
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
} IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;
ok, i guess we got some information to code our IMAGE_OPTIONAL_HEADER
PE File Format (so far)
=======================
0x00 .... 0x3F ------------------------------- IMAGE_DOS_HEADER
0x40 .... 0x7F ------------------------------- DOS 2.0 Stub Program
0x80 .... ? ------------------------------- IMAGE_NT_HEADERS
0x84 .... 0x97 --------------------- IMAGE_FILE_HEADER
0x98 .... 0xF7 --------------------- IMAGE_OPTIONAL_HEADER
0xF8 .... 0x177 ---------- IMAGE_DATA_DIRECTORY
IMAGE_OPTIONAL_HEADER: ;start : 98 (152) to F7 (247) * till IMAGE_DATA_DIRECTORY
;offset
.Magic dw 0x010B ;152 153
.MajorLinkerVersion db 0x01 ;154
.MinorLinkerVersion db 0x37 ;155
.SizeOfCode dd 0 ;156 159
.SizeOfInitializedData dd 0 ;160 163
.SizeOfUninitializedData dd 0 ;164 167
.AddressOfEntryPoint dd 0x2000 ;168 171
;base + 2000 = 402000 (.code section)
.BaseOfCode dd 0 ;172 175
.BaseOfData dd 0 ;176 179
.ImageBase dd 0x00400000 ;180 183 (default)
.SectionAlignment dd 0x00001000 ;184 187 4096 bytes
.FileAlignment dd 0x00000200 ;188 191 512 bytes (default)
.MajorOperatingSystemVersion dw 1 ;192 193
.MinorOperatingSystemVersion dw 0 ;194 195
.MajorImageVersion dw 0 ;196 197
.MinorImageVersion dw 0 ;198 199
.MajorSubsystemVersion dw 4 ;200 201
.MinorSubsystemVersion dw 0 ;202 203
.Win32VersionValue dd 0 ;204 207
.SizeOfImage dd 0x00004000 ;208 211
.SizeOfHeaders dd 0x00000200 ;212 215
.CheckSum dd 0x0000EF20 ;216 219
.Subsystem dw 2 ;220 221 IMAGE_SUBSYSTEM_WINDOWS_GUI
.DllCharacteristics dw 0 ;222 223
.SizeOfStackReserve dd 0x00001000 ;224 227 4096 bytes
.SizeOfStackCommit dd 0x00001000 ;228 231 4096 bytes
.SizeOfHeapReserve dd 0x00100000 ;232 235 1048576 bytes
.SizeOfHeapCommit dd 0 ;236 239
.LoaderFlags dd 0 ;240 243
.NumberOfRvaAndSizes dd 0x10 ;244 247 16 decimal
IMAGE_DATA_DIRECTORY: ;start : F8 (248) to 177 (375) * till IMAGE_SECTION_TABLE
rq 1 ;248 255
.ImportTableVA dd 0x00003000 ;256 263
.ImportTableSize dd 0x00000090
rq 14 ;we don't need them also ;263 + 112 = 375
db 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2E,0x64,0x61,0x74,0x61,0x00,0x00,0x00,\
.... to the end
Once we finish with IMAGE_DATA_DIRECTORY, we will need to present the system with IMAGE_SECTION_HEADER. Since we got 3 sections as defined above, we would need 3 structures of IMAGE_SECTION_TABLE
#define IMAGE_SIZEOF_SHORT_NAME 8
typedef struct _IMAGE_SECTION_HEADER {
BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
union {
DWORD PhysicalAddress;
DWORD VirtualSize;
} Misc;
DWORD VirtualAddress;
DWORD SizeOfRawData;
DWORD PointerToRawData;
DWORD PointerToRelocations;
DWORD PointerToLinenumbers;
WORD NumberOfRelocations;
WORD NumberOfLinenumbers;
DWORD Characteristics;
} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
Continue to Windows PE File Format Walkthrough III |
| Copyright © 2004 Sulaiman Chang. All Rights Reserved. |