Archived Having A Look At Portable Executables
Post
Cancel

Having A Look At Portable Executables

Examining Windows EXEs and DLLs

Tools:

This is the source code i used to create an example executable.

I used Visual Studio 2019s default compiler and C++

Microsoft PE. docs.microsoft.com

The other important Source i used is this PDF:

Goppit Portable Executable. stondecoder.ord

There are small differences between x86 and x64 i will focus on x86.

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio>

static void sayHello(char* name)
{
	printf("Hello %s",name);
}

int main(int argc, char* argv[])
{
	char* name = argv[1];
	sayHello(name);
}

This code will simply take the first argument we pass to our Exe and print “Hello “+ your argument.

Portable Executable

DOS Header 64 bytes

OffsetValueMeaning
0x000x4D5A or MZE-magic: Stands for Mark Zbikowsky who created the PE format
0x3c0x0100E-lfanew: The offset to the start of the Pe Header

Dos Header

PE Header

OffsetValueMeaning
0x01000x50450000Signature: “PE” folloewd by 2x 0x00
0x010420 bytesImage File Header
0x0118224 bytesOptional Header

File Header

PE File Header

The location of the Header will depend on the E-lfanew value in the Dos Header

OffsetValueMeaning
0x01040x4c01Machine: For example i386
0x01060x0500Number of sections: .text.rdata.data.rsrc.reloc
0x01080x20663C5ETime of creation: 2020/02/06 19:16:48
0x010c0x00000000Pointer to symbol table: 0 here because not debug Version
0x01100x00000000Number of symbols
0x01140xE000Size of Optional Header
0x01160x0201Characteristics: see below

Characteristics

Characteristics 0x0201 1. MAGE_FILE_RELOCS_STRIPPED 0x0001. Image only, Windows CE, and Microsoft Windows NT and later. This indicates that the file does not contain base relocations and must therefore be loaded at its preferred base address. If the base address is not available, the reports an error. The default behavior of the linker is to strip base relocations from executable (EXE) files. 2. IMAGE_FILE_DEBUG_STRIPPED 0x0200.Debugging information is removed from the image file.

Optional Header 224 bytes

Optional Header

The Last 128 bytes contain the Data Directory

Optional Header. docs.microsoft.com

OffsetValueMeaning
0x01180x0b01Magic Number: either 0x10b or 0x20b
0x011a0x0E18Linker Version
0x011c0x000E0000Size of Code Section
0x01200x00140000Size of Initialized Data
0x01240x00000000Size of Unitialized Data
0x01280xB2120000Address of Entrypoint 1
0x012c0x00100000Base of Code
0x01300x00200000Base of Data
0x01340x00004000Image Base 2
0x01380x00100000Section Alignment 3
0x013c0x00020000File Alignment
0x0140+16 bytesversionsA bunch of version Stuff
0x01500x00600000Size of Image
0x01540x00400000Size of Headers
0x01580x00000000CheckSum
0x015c0x0300Subsystem
0x015e0x4081DllCharacteristics !!Important
0x01600x00001000Size of Stack Reserve
0x01640x00100000Size of Stack Commit
0x01680x00001000Size of Heap Reserve
0x016c0x00100000Size of Heap Commit
0x01700x00000000Loader Flags 4
0x01740x10000000Number of directory entries 5
0x01780x00000000Virtual address of Data Dir
0x017c0x00000000Size of Data directory

An example how this looks mapped to memory in the debugger. Immunity :

Pe Header. Immunity

Data Directory

Pe Data Directory

In our case we have 16 Data Directory Entrys

OffsetValueMeaning
0x01780x00000000Export Table Address=0 6
0x017c0x00000000Export Table Size=0
0x01800x4c250000Import Table Address 7
0x01840xA0000000Import Table Size=10
0x01880x00400000Resource Table Address
0x018c0xE0010000Resource Table Size
0x01900x00000000Exeption Table Address
0x01940x00000000Exception Table Size
0x01980x00000000Certificate Table Address
0x019c0x00000000Certificate Table Size
0x01a00x00500000Relocation Table Address
0x01a40x54010000Relocation Table Size
0x01a80x20210000Debug Data Address
0x01ac0x70000000Debug Data Size
0x01b00x00000000Architecture Data Address
0x01b40x00000000Architecture Data Size
0x01b80x00000000Global Pointer Address
0x01bc0x00000000Must be 0
0x01c00x00000000Thread Local Storage Address
0x01c40x00000000Thread Local Storage Size
0x01c80x90210000Load Config Table Address
0x01cc0x40000000Load Config Table Size
0x01d00x00000000Bound Import Table Address
0x01d40x00000000Bound Import Table Size
0x01d80x00200000Import Address Table Adress
0x01dc0xC4000000Import Address Table Size
0x01e00x00000000Delay-Load Import Table Addr
0x01e40x00000000Delay-Load Import Table Size
0x01e80x00000000CLR Header Address
0x01f00x00000000CLR Header Size
0x01f40x00000000Reserved
0x01f80x00000000Reserved

Section Headers

Pe Section Header

I will only list the .text section for this one.
The name of a Section are kept in the first 8 bytes of the Header
OffsetValueMeaning
0x01F80x2E74657874000000Name of Section (here .text)
0x02000xFC0C0000Virtual Size
0x02080x00100000Virtual Address
0x020C0x000E0000Size of Raw Data
0x02100x00040000Pointer to raw Data
0x02140x00000000Pointer to Relocations
0x02180x00000000Pointer to Linenumbers
0x02190x0000Number of Relocations
0x021B0x0000Number of Linenumbers
0x021D0x20000006Characteristics /permissions

Pe Sections. Immunity

Understanding imports. sandsprite.com

Footnotes:

  1. When you attach a debugger the Entry Point will be the location in Virtual Address Space where EIP will first be located. 

  2. The Image Base is the point in the Virtual Address Space where you will find the first byte of your Image. 

  3. The Section alignment has to be greater than or equal to the File alignment. This variable determines how memory is mapped from your disk to random access memory. 

  4. Certain debuggers cant handle corrupted flags + number of RVA and sizes and will run the exe without debugging. Pretty interesting for malware. 

  5. Each one of the Data Directories has 2 fields. The first one is a pointer to the Directory and the second one determines the Size. In our example there are 16 Data Directories. These Directories contain stuff like import and export table, so they are definitely important for reverse engineering. 

  6. When a table has no entries the data directory will contain 0x00000000 

  7. Might want to read up on the Import directory table 

This post is licensed under CC BY 4.0 by the author.

-

-

Trending Tags