Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Navigating PE File Structure

Recommended resources

Hello! Welcome to your first lesson. This course will take you deep into the world of reverse engineering, starting from the very foundations. Given your background in C and Assembly, you've spent plenty of time creating executables. Now, we're going to peel back the layers and see what's actually inside them.

This first lesson focuses on the Portable Executable (PE) file format, which is the standard for executables, object code, and DLLs on Windows. By the end of this session, you will be able to identify the core components of a PE file—its headers, sections, and the all-important entry point. Understanding this structure is the first essential step before analyzing, modifying, or reversing any Windows program.

Understanding the PE Format's Purpose

When a compiler transforms your C or Assembly source code into an .exe file, it doesn't just dump the machine instructions onto the disk. It organizes them within a highly structured format that the Windows operating system can understand and execute. This format is the Portable Executable. Think of it as a blueprint or a container that tells the Windows loader everything it needs to know: what code to execute, what data it needs, where in memory to place everything, and what other libraries to load.

Let's begin by looking at a high-level overview of the PE file structure.

This diagram shows the basic layout of a PE file, starting with legacy DOS components, followed by the main PE headers, a table describing the sections, and finally the sections themselves which contain the program's code and data.

As you can see, a PE file is composed of several key parts, which we will explore one by one:

  • DOS Header & DOS Stub: Relics from the past for backward compatibility.
  • NT Headers: The modern and most important header block, containing metadata about the file.
  • Section Headers (Section Table): An index describing each of the sections that follow.
  • Sections: The actual content of the program, such as executable code, data, and resources.

To get a solid grasp of these concepts, I recommend reading two introductory articles. The first provides a great narrative overview, while the second offers a more structured, reference-style breakdown.

Anatomy of the Portable Executable (PE) Format - Deep Hacking

This article from Deep Hacking provides a very detailed and structured look at the PE format. It's a great reference to have.

Start by reading the introduction to understand the PE format's origins. Then, review the Glossary of Terms. Given your CSE background, many of these terms like Offset and Mapping will be familiar, but it's good to see them defined in the specific context of PE files.

Malware Analysis Fundamentals: A Guide to the PE File Format

This guide provides a more practical, malware-analysis-oriented perspective on the PE format.

Read the introduction and the section What is a PE file? (up to "Structure of a PE file"). This will reinforce the purpose of the format and introduce the role of the Windows loader.


The Headers: Blueprints for the Loader

The headers are the most critical part of the PE file for the operating system. They contain the metadata that directs the loader on how to map the file into memory and prepare it for execution. Let's start by looking at a raw binary file in a hex editor.

A hex editor view of a PE file. Note the "MZ" characters at the very beginning (offset 0x00) and the "PE" characters further down (offset 0xE0 in this example). These are the signatures for the DOS header and the NT header, respectively.

1. The DOS Header and Stub

As you see in the hex editor, every PE file begins with the hexadecimal values 4D 5A, which is ASCII for "MZ". This is the "magic number" identifying the file as MS-DOS compatible. It's named after Mark Zbikowski, one of the original architects of MS-DOS.

The DOS header is largely a legacy structure. For modern Windows, its most important field is a 4-byte value at offset 0x3c called e_lfanew. This field contains the file offset to the real header, the NT Header. Following the DOS header is the DOS Stub, a small program that typically just prints "This program cannot be run in DOS mode." and exits.

This structure allows a PE file to be gracefully handled by older DOS systems while providing a clear pointer for modern Windows loaders to find the information they need.

The following reading demonstrates how to find these fields using a hex editor. This is a fundamental skill in manual analysis.

Malware Analysis Fundamentals: A Guide to the PE File Format

This section walks through the practical steps of identifying a PE file and navigating its initial headers.

Read the section titled A first look inside malware.exe. Pay close attention to how the author finds the e_lfanew value at offset 0x3c and uses it to jump to the location of the NT header. The explanation of little-endian byte order is also crucial.

2. The NT Headers

The e_lfanew field points to the IMAGE_NT_HEADERS structure, the heart of the PE file's metadata. This structure is itself composed of three parts:

  1. Signature: A 4-byte signature that is always 50 45 00 00 (ASCII for "PE\0\0"). This confirms we're at the right place.
  2. File Header (IMAGE_FILE_HEADER): This structure provides basic information about the file itself. Key fields include:
    • Machine: Specifies the target architecture (e.g., x86, x64, ARM).
    • NumberOfSections: Tells the loader how many sections follow the headers.
    • TimeDateStamp: The file's compilation time.
    • Characteristics: Flags defining the file's attributes (e.g., is it an executable, a DLL, a system file?).
  3. Optional Header (IMAGE_OPTIONAL_HEADER): Despite its name, this header is mandatory for executable files. It contains the most critical information for the loader. Key fields include:
    • AddressOfEntryPoint: An offset to the first instruction to be executed.
    • ImageBase: The preferred virtual address where the file should be loaded into memory.
    • SectionAlignment and FileAlignment: Specify how sections should be aligned in memory and in the file on disk, respectively.
    • SizeOfImage: The total size of the image in memory after it's loaded.
    • DataDirectory: An array of structures pointing to other important data, like the import/export tables. We will explore these in future lessons.

The following reading provides the detailed C structure definitions for these headers from winnt.h, which should be very familiar to you.

Anatomy of the Portable Executable (PE) Format - Deep Hacking

This article gives a deep dive into the headers with their C struct definitions.

Read the section on the DOS Header to see the full IMAGE_DOS_HEADER struct and a recap of e_magic and e_lfanew. Move to the NT Headers section. Study the IMAGE_NT_HEADERS structure and its components: the PE Signature and the File Header. Finally, read the extensive section on the Optional Header. You don't need to memorize every field, but focus on understanding the purpose of the key fields listed above (AddressOfEntryPoint, ImageBase, etc.).


Sections: The File's Content

Following all the headers is the Section Table, which is an array of IMAGE_SECTION_HEADER structures. The NumberOfSections field in the File Header tells the loader how many of these structures to expect. Each IMAGE_SECTION_HEADER describes one section of the file, giving its name, size, location, and memory permissions.

The sections themselves come after the section table and contain the program's actual code and data. Common section names include:

  • .text: Contains the executable machine code. The loader gives this section read and execute permissions.
  • .data: Contains initialized global and static variables. Has read/write permissions.
  • .rdata: Contains read-only data, such as constant values and strings.
  • .rsrc: Contains resources used by the application, such as icons, images, and menus.

Each section header contains fields that specify both the section's on-disk properties and its in-memory properties. This distinction is fundamental to understanding how a PE file is loaded.

  • PointerToRawData and SizeOfRawData: The file offset and size of the section on disk.
  • VirtualAddress and VirtualSize: The relative memory offset and size of the section once loaded.
  • Characteristics: Flags that define the section's attributes, most importantly memory permissions (Read, Write, Execute).

Anatomy of the Portable Executable (PE) Format - Deep Hacking

This part explains the structure of section headers and the purpose of their key fields.

Read the section on Section Headers. Pay special attention to the IMAGE_SECTION_HEADER structure and the explanation of the difference between the on-disk fields (PointerToRawData) and the in-memory fields (VirtualAddress).


The Entry Point: Where Execution Begins

We've now covered the major structural components. The final piece of the puzzle for this lesson is the entry point. The AddressOfEntryPoint field in the Optional Header gives the loader the starting address for execution.

However, this address is a Relative Virtual Address (RVA). An RVA is an offset from the ImageBase. To find the actual memory address, or Virtual Address (VA), where execution begins, the loader performs a simple calculation:

For example, if the ImageBase is 0x400000 and the AddressOfEntryPoint is 0x1262, the CPU will be directed to start executing instructions at the virtual address 0x401262.

The following video provides an excellent practical demonstration of these concepts, showing how the loader maps the file into memory and how the entry point is calculated and reached.

🔴 Portable Executable Files: Analyzing In-Memory versus On Disk

In this video, Dr. Josh Stroschein provides a clear, side-by-side comparison of a PE file on disk versus in memory. This will tie together everything we've discussed.

Watch the following segments: Memory Layout: Here, Process Hacker is used to show how a PE file is loaded into a process's virtual address space, including its base address. On-Disk Headers: The presenter uses a hex editor to examine the ImageBase specified in the Optional Header and explains why the actual in-memory address might be different due to Address Space Layout Randomization (ASLR). Section Mapping: This segment clearly illustrates the relationship between PointerToRawData (on-disk) and VirtualAddress (in-memory) for the file's sections. The Entry Point: This is a crucial part. It explains how the entry point address is an RVA and demonstrates the calculation needed to find its location on disk versus in memory. Debugging the Entry Point: Finally, see how a debugger (WinDbg) is used to set a breakpoint at the calculated entry point and confirm that this is indeed where execution begins.


Conclusion

In this lesson, we dissected the fundamental structure of a Windows PE file. You now have a conceptual map of how these executables are constructed and interpreted by the operating system.

Here are the key takeaways:

  • PE files are the standard executable format on Windows, containing headers and sections that act as a blueprint for the OS loader.
  • The file begins with a legacy DOS Header ("MZ"), whose primary modern role is to point to the NT Headers via the e_lfanew field.
  • The NT Headers ("PE") are the core of the file's metadata, consisting of a File Header (general info) and an Optional Header (loader-specific info).
  • The Optional Header is critical, defining the ImageBase (preferred load address) and the AddressOfEntryPoint (start of execution).
  • The Section Headers form a table that describes each section's name, size, location on disk (PointerToRawData), location in memory (VirtualAddress), and permissions.
  • The actual entry point's virtual address is calculated by adding the AddressOfEntryPoint (an RVA) to the ImageBase.

In our next lesson, we will move beyond manual inspection with hex editors and explore powerful tools like PE-bear and CFF Explorer, which parse these structures for you and present them in a much more readable format. This will significantly speed up our static analysis process.