PE File Structure
PE format is actually a data structure that tells Windows OS loader what information is required in order to manage the wrapped executable code.
This includes dynamic library references for linking, API export, and import tables, resource management data, and TLS data. The data structures on disk are the same data structures used in the memory and if you know how to find something in a PE file, it will help while analyzing any Windows malware samples.

DOS Header.
DOS Header occupies the first 64 bytes of the file. DOS Header there because DOS can recognize it as a valid executable and can run it in the DOS stub mode.
As we can see we have a list of structures that came under the DOS header. We will not discuss everything as it is beyond our scope; we will discuss important ones that are required, such as e_magic and e_lfanew structure.
e_magic: Determine whether a file is a PE file. A list of file signatures can be found Here
e_lfanew: Offset relative to the beginning of the file, used to find the PE header.

As shown in the above figure e_magic value is 4D 5A (MZ) and e_lfanew is 0x00000108 (PE File header address)
DOS Stub.
A stub is a tiny program or a piece of code that is run by default when the execution of an application starts. This stub prints out the message “This program cannot be run in DOS mode” when the program is not compatible with Windows.

PE File Header.
The PE header is located by looking at the e_lfanew field of the MS-DOS Header. The e_lfanew field gives the offset of the PE header location.

The main PE Header is a structure of type IMAGE_NT_HEADERS and mainly contains PE signature, IMAGE_FILE_HEADER, and IMAGE_OPTIONAL_HEADER.
Signature is 50 45 00 00 (PE)
Standard PE header (_IMAGE_FILE_HEADER)
The Standard PE header is the next 20 bytes of the PE file and contains only the most basic information about the layout of the file.

Optional PE header (_IMAGE_OPTIONAL_HEADER)

In the Example the first member (Magic, 2Byte): the magic number 020B, which means that the file is a 64-bit PE.
The optional PE header is followed by the standard PE header, and its size is 32-bit default E0H, 64-bit default F0H bytes. The optional header contains most of the meaningful information about the executable image, such as initial stack size, program entry point location, preferred base address, operating system version, section alignment information.
Data Directories (_IMAGE_DATA_DIRECTORY)
It is the last entry of the Optional Header. The data directory indicates where to find other important components of executable information in the file. It is really nothing more than an array of IMAGE_DATA_DIRECTORY structures that are located at the end of the optional header structure. The current PE file format defines 16 possible data directories, 11 of which are now being used.
Each data directory entry specifies the size and relative virtual address of the directory. To locate a particular directory, you determine the relative address from the data directory array in the optional header.
Then use the virtual address to determine which section the directory is in. Once you determine which section contains the directory, the section header for that section is then used to find the exact file offset location of the data directory.
Section Header Table
Section Header Table is an array of IMAGE_SECTION_HEADER structures and contains information related to the various sections available in the image of an executable file. The sections in the image are sorted by the RVAs rather than alphabetically.
Sections Headers Table contains the following important fields:
Name
Virtual Size
Virtual Address
Raw Size
Raw Address
Reloc Address
Linenumbers
Relocations Number
Linenumbers Number
Characteristics
Sections
PE section headers also specify the section name using using a simple character array field, called as Name. Below are the various common sections names available from an executable file:
.text: This is normally the first section and contains the executable code for the application. Inside this section is also an entry point of the application: the address of the first application instruction that will be executed. An application can have more than one section with the executable code.
.data: This section contains an initialized data of an application such as strings.
.rdata or .idata: Usually these section names are used for the sections where the import table is located. This is the table that lists the Windows API used by the application (along with the names of their associated DLLs). Using this, the Windows loader knows the API to find, in which system DLL, in order to retrieve its address.
.reloc: contains relocation information.
.rsrc: This is the common name for the resource-container section, which contains things like images used for the application’s UI.
.debug: contains debug information.

Overview, Important things to keep in mind.

Last updated
Was this helpful?