PE FORMAT] -4- NT Header (DOS Sign, IMAGE_FILE_HEADER, IMAGE_OPTIONAL_HEADER)

 


参考:

docs.microsoft.com

 

NT Headerの構造体はIMAGE_NT_HEADERS.

 

f:id:velup:20190422014304p:plain

typedef struct _IMAGE_NT_HEADERS {

 DWORD Signature; // PE sign (50 45 00 00)

 IMAGE_FILE_HEADER FileHeader;

 IMAGE_OPTIONAL_HEADER32 OptionalHeader;

} IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;

 size : F8

f:id:velup:20190420210008p:plain

IMAGE_NT_HEADERS (Notepad.exe)

 

<member>

 [DWORD Signature]

f:id:velup:20190420203424p:plain

PE sign

 [IMAGE_FILE_HEADER FileHeader;]

 

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;

f:id:velup:20190420210449p:plain

IMAGE_FILE_HEADER (notepad.exe)

f:id:velup:20190420210511p:plain

IMAGE_FILE_HEADER PE View (notepad.exe)

Machine

The architecture type of the computer. An image file can only be run on the specified computer or a system that emulates the specified computer. This member can be one of the following values.

Value Meaning
                                    
IMAGE_FILE_MACHINE_I386
0x014c
x86
                                    
IMAGE_FILE_MACHINE_IA64
0x0200
Intel Itanium
                                    
IMAGE_FILE_MACHINE_AMD64
0x8664
x64

 

NumberOfSections

The number of sections. This indicates the size of the section table, which immediately follows the headers. Note that the Windows loader limits the number of sections to 96.

 

SizeOfOptionalHeader

The size of the optional header, in bytes. This value should be 0 for object files.

 

Characteristics

The characteristics of the image. This member can be one or more of the following values.

Value Meaning
                        
IMAGE_FILE_RELOCS_STRIPPED
0x0001
Relocation information was stripped from the file. The file must be loaded at its preferred base address. If the base address is not available, the loader reports an error.
                        
IMAGE_FILE_EXECUTABLE_IMAGE
0x0002
The file is executable (there are no unresolved external references).
                        
IMAGE_FILE_LINE_NUMS_STRIPPED
0x0004
COFF line numbers were stripped from the file.
                        
IMAGE_FILE_LOCAL_SYMS_STRIPPED
0x0008
COFF symbol table entries were stripped from file.
                        
IMAGE_FILE_AGGRESIVE_WS_TRIM
0x0010
Aggressively trim the working set. This value is obsolete.
                        
IMAGE_FILE_LARGE_ADDRESS_AWARE
0x0020
The application can handle addresses larger than 2 GB.
                        
IMAGE_FILE_BYTES_REVERSED_LO
0x0080
The bytes of the word are reversed. This flag is obsolete.
                        
IMAGE_FILE_32BIT_MACHINE
0x0100
The computer supports 32-bit words.
                        
IMAGE_FILE_DEBUG_STRIPPED
0x0200
Debugging information was removed and stored separately in another file.
                        
IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP
0x0400
If the image is on removable media, copy it to and run it from the swap file.
                        
IMAGE_FILE_NET_RUN_FROM_SWAP
0x0800
If the image is on the network, copy it to and run it from the swap file.
                        
IMAGE_FILE_SYSTEM
0x1000
The image is a system file.
                        
IMAGE_FILE_DLL
0x2000
The image is a DLL file. While it is an executable file, it cannot be run directly.
                        
IMAGE_FILE_UP_SYSTEM_ONLY
0x4000
The file should be run only on a uniprocessor computer.
                        
IMAGE_FILE_BYTES_REVERSED_HI
0x8000
The bytes of the word are reversed. This flag is obsolete.
 
 
[IMAGE_OPTIONAL_HEADER32 OptionalHeader;]
 
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;
 

f:id:velup:20190420212214p:plain

IMAGE_OPTIONAL_HEADER PE View (notepad.exe)

Magic

The state of the image file. This member can be one of the following values.

Value Meaning
            
IMAGE_NT_OPTIONAL_HDR_MAGIC
The file is an executable image. This value is defined as IMAGE_NT_OPTIONAL_HDR32_MAGIC in a 32-bit application and as IMAGE_NT_OPTIONAL_HDR64_MAGIC in a 64-bit application.
            
IMAGE_NT_OPTIONAL_HDR32_MAGIC
0x10b
The file is an executable image.
            
IMAGE_NT_OPTIONAL_HDR64_MAGIC
0x20b
The file is an executable image.
            
IMAGE_ROM_OPTIONAL_HDR_MAGIC
0x107
The file is a ROM image.
 
 

AddressOfEntryPoint

A pointer to the entry point function, relative to the image base address. For executable files, this is the starting address. For device drivers, this is the address of the initialization function. The entry point function is optional for DLLs. When no entry point is present, this member is zero.

(Entry PointのRVA値を持つ)

 

ImageBase

The preferred address of the first byte of the image when it is loaded in memory. This value is a multiple of 64K bytes. The default value for DLLs is 0x10000000. The default value for applications is 0x00400000, except on Windows CE where it is 0x00010000.

(PE FILEがロードされるスタート地点)

 

SectionAlignment

The alignment of sections loaded in memory, in bytes. This value must be greater than or equal to the FileAlignment member. The default value is the page size for the system.

(MEMORYでSECTIONの最小単位)

 

SizeOfImage

The size of the image, in bytes, including all headers. Must be a multiple ofSectionAlignment.

 

SizeOfHeaders

The combined size of the following items, rounded to a multiple of the value specified in the FileAlignment member.

  • e_lfanew member of IMAGE_DOS_HEADER
  • 4 byte signature
  • size of IMAGE_FILE_HEADER
  • size of optional header
  • size of all section headers
 

Subsystem

The subsystem required to run this image. The following values are defined.

Value Meaning
            
IMAGE_SUBSYSTEM_UNKNOWN
0
Unknown subsystem.
            
IMAGE_SUBSYSTEM_NATIVE
1
No subsystem required (device drivers and native system processes).
            
IMAGE_SUBSYSTEM_WINDOWS_GUI
2
Windows graphical user interface (GUI) subsystem.
            
IMAGE_SUBSYSTEM_WINDOWS_CUI
3
Windows character-mode user interface (CUI) subsystem.
            
IMAGE_SUBSYSTEM_OS2_CUI
5
OS/2 CUI subsystem.
            
IMAGE_SUBSYSTEM_POSIX_CUI
7
POSIX CUI subsystem.
            
IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
9
Windows CE system.
            
IMAGE_SUBSYSTEM_EFI_APPLICATION
10
Extensible Firmware Interface (EFI) application.
            
IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
11
EFI driver with boot services.
            
IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
12
EFI driver with run-time services.
            
IMAGE_SUBSYSTEM_EFI_ROM
13
EFI ROM image.
            
IMAGE_SUBSYSTEM_XBOX
14
Xbox system.
            
IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION
16
Boot application.
 

NumberOfRvaAndSizes

The number of directory entries in the remainder of the optional header. Each entry describes a location and size.

 

DataDirectory

The following is a list of the data directories. Offsets are relative to the beginning of the optional header.

Offset (PE/PE32+) Description
96/112 Export table address and size
104/120 Import table address and size
112/128 Resource table address and size
120/136 Exception table address and size
128/144 Certificate table address and size
136/152 Base relocation table address and size
144/160 Debugging information starting address and size
152/168 Architecture-specific data address and size
160/176 Global pointer register relative virtual address
168/184 Thread local storage (TLS) table address and size
176/192 Load configuration table address and size
184/200 Bound import table address and size
192/208 Import address table address and size
200/216 Delay import descriptor address and size
208/224 The CLR header address and size
216/232 Reserved
 
 

f:id:velup:20190420213448p:plain

IMAGE_OPTIONAL_HEADER PE View (notepad.exe)