Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Static Analysis

The PE File Format

Understand the layout of Windows PE files (DOS header, PE signature, COFF and optional headers, sections) and parse a synthetic PE yourself with Python's struct module.

Most malware you will meet targets Windows, and Windows programs (.exe, .dll, .sys) use the Portable Executable (PE) format. Learning the layout of a PE file is the single most valuable static-analysis skill, because the headers answer many questions in seconds: is it 32-bit or 64-bit, a program or a library, when was it compiled, where does execution start, which sections does it have?

You will not need any real malware. The examples below build a tiny, harmless synthetic PE in memory (it contains no executable code) and parse it with the same logic real tools use.

The big picture

flowchart TB
    A["DOS header (MZ) and DOS stub"] --> B["PE signature 'PE' 0 0"]
    B --> C["COFF file header"]
    C --> D["Optional header (with data directories)"]
    D --> E["Section table"]
    E --> F[".text (code)"]
    E --> G[".data / .rdata (data)"]
    E --> H[".rsrc (resources)"]

Everything after the DOS header is found by following offsets.

1. DOS header and stub

The file begins with MZ. The DOS header is 64 bytes long, and its last field, e_lfanew at offset 0x3C, holds the file offset of the real PE header. The stub that follows prints "This program cannot be run in DOS mode." on ancient DOS and is otherwise ignored.

2. PE signature

At e_lfanew, four bytes 50 45 00 00 (PE\0\0). If they are missing, the file is not a valid PE.

3. COFF file header (20 bytes)

Field Meaning
Machine CPU type: 0x14c = x86 (32-bit), 0x8664 = x64
NumberOfSections How many section headers follow
TimeDateStamp Compile time in Unix seconds (easy to forge)
SizeOfOptionalHeader Length of the next header
Characteristics Flags: executable image, DLL, and so on

4. Optional header

Despite its name it is required for programs. Key fields: Magic (0x10b for 32-bit PE32, 0x20b for PE32+), AddressOfEntryPoint (where code starts, as an RVA), ImageBase (preferred load address), SectionAlignment, FileAlignment, SizeOfImage, Subsystem (GUI or console) and the data directories (imports, exports, resources and more).

5. Section table

Each 40-byte entry describes one section: name, virtual size and address, raw size and file offset, and flags (readable, writable, executable).

An RVA (relative virtual address) is an address relative to where the image is loaded in memory. Files on disk and images in memory have different layouts, so tools convert RVAs into file offsets using the section table. Imports, Exports and Sections uses this conversion.

Build a harmless synthetic PE

The function below assembles a minimal PE32 header with two sections and fake section contents. It cannot run: the "code" section holds 0x90 bytes (x86 no-ops) and the entry point is only a number. Run it and read the hex dump of its header.

Python Assemble a tiny, inert PE

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The 0x80 in the code reflects that the PE header starts 128 bytes into the file, after the 64-byte DOS header and the 64-byte stub.

Parse the headers

Here is the other half: a parser that does what PE viewers do. It reads only the fields we wrote and prints a summary. Run it as is; it contains its own copy of the builder so it works on its own.

Python Parse PE headers with struct

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check the result against what the builder wrote: x86, console program, entry point RVA 0x1000 (the start of .text), compile time from the timestamp.

What analysts read from the headers

  • Compile timestamp: hints at when the sample was built. Authors can forge it, so treat it as a clue, not a fact. A timestamp of zero or in the future is itself suspicious.
  • Entry point location: normally inside the first code section. An entry point in the last section often means a packer or an infector appended code.
  • Subsystem: GUI programs may hide windows; console programs may be launched by scripts.
  • DLL flag: DLLs are loaded by other programs, which matters for injection and side-loading.
  • Section anomalies: odd names, writable-and-executable sections, huge size differences between virtual and raw sizes. The next two lessons explain them.
  • Checksum and signature: an invalid checksum or missing digital signature is not proof of malice but adds up with other facts.

Real tools

In practice you use a PE viewer or the Python library pefile: pefile.PE("file.exe") gives every field above as an attribute. The browser runner cannot install it, so we parse by hand, which teaches exactly what the library does.

Common mistakes

  • Trusting the compile timestamp.
  • Mixing up RVAs and file offsets.
  • Assuming 32-bit samples are old and harmless.
  • Forgetting that .NET programs are also PE files, with managed code inside.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does a PE file store the offset of the real PE header?

    e_lfanew in the DOS header points to the PE signature.
  2. 2. Which machine value marks a 64-bit x64 PE file?

    0x14C is x86; 0x8664 is x64. 0x10B is the optional-header magic for PE32.
  3. 3. How reliable is the compile timestamp?

    Authors can edit or randomise the timestamp, and some build tools set it deliberately.
  4. 4. An entry point inside the LAST section of a program often suggests what?

    Compilers start execution in the code section; a different location points to a packer or infection.

Summary

  • Windows executables use the PE format: DOS header, PE signature, COFF header, optional header, section table, sections.
  • e_lfanew leads to the PE header; the COFF header gives machine, section count and timestamp.
  • The optional header holds the entry point, image base, subsystem and data directories.
  • Headers answer many triage questions quickly, but timestamps and names can be forged.
  • You can parse them with struct or the pefile library.

Next: Imports, Exports and Sections