Portable Execution (PE)

Headers in PE file

PE Headers contains instructions for Windows, that it needs to execute the binary file.

How to read PE headers?

PE Headers can be read in hexeditor, but they are only binary data and that is hard to read. We can use program called pe-tree to analyze PE header.

PE Headers are STRUCT datatype. They are user-defined meaning we need to see the documentation to understand the type for each STRUCT variable.

4D 5A

Each PE file has 4D 5A at the begining meaning MZ named after Mark Zbikowski - Microsoft architects that created MS-DOS file format.

When 4D 5A are present at the start of file, Windows considers it Portable Executable format file.

IMAGE_DOS_HEADER - backwards combability stuff

This header is not very usefull and is there mostly for backwards compatibility reasons. Not much interesting in this header for reverse engineer. Only maybe the first and last field, see below.

First entry of IMAGE_DOS_HEADER is e_magic

This so called e_magic could be either 0x5a4d or 0x4d5a depending, if the computer is running X86 or ARM. Refer to endianness.

Last entry is e_lfanew and it tells us where the next IMAGE_DOS_HEADER begins. for example, it could have value of 0x000000d8.

DOS_STUB - wrong system stuff

The DOS STUB only runs if PE file is incompatible with the system it is being run on-

IMAGE_NT_HEADERS - the important stuff

This header contains most of the viral information of PE file. Also take a look at Microsoft Documentation

NT_HEADERS

signature

First 4 bytes on NT_HEADERS are reserver for signature. For example signature might be 50 45 00 00, PE in ascii. TODO: what is the purpose of signature?

FILE_HEADER

MS DOCUMENTATION

Fields:

OPTIONAL_HEADER

!IMPORTANT information

IMAGE_SECTION_HEADER

code, images, elements etc., are stored in different sections. Such as - .text = executable code - can be read and executed - .data = initialized data - can be read and write - .rdata / .idata = import information of PE file. - .ndata = uninitialized data - .reloc = relocation information - .rsrc = icons, images and other resources for app UI

These sections include: - VirtualAddress = sections relative virtual address (RVA) - VirtualSize = size once loaded into memory - SizeOfRawData = size on disk - Characteristics = permissions of the section such as READ, WRITE, EXECUTE

IMAGE_IMPORT_DESCRIPTOR

Contains information about the different Window APIs that the PE file loads when executed.

Identify packed executables

Malware authors and software developers package their PE-files to avoid piracy or detection.

Packed PE-headers can be analyzed from: - import functions - section headers and their permission, size and randomness

Detection: - High entropy (randomness) - Execute permissions on multiple sections - empty or random section names - Big difference in SizeOfRawData and Misc_VirtualSize of PE sections - few import functions