I created this Windows PE from scratch to understand in depth how Windows PE (Portable Executable) files work and how they're built. This helps me for malware analysis.
A Win64 GUI executable, hand-assembled byte-by-byte with NASM — every DOS header field, NT header field, section header, and import table entry written manually, no linker.
It shows a Yes/No MessageBoxA ("Do you want to continue?") and exits with
code 0 (Yes) or 1 (No).
.
├── docs/Nasm_cheatsheet.md # notes kept while building this
├── images/ # crash screenshots referenced below
├── msgbox.asm # the whole PE, hand-written
├── msgbox.exe # assembled output
├── LICENSE
└── README.md
nasm -f bin msgbox.asm -o msgbox.exeRequires NASM. Output should be exactly 2560 bytes (0xA00).
Copy msgbox.exe to a Windows machine or VM and run it:
.\msgbox.exeOr just simply double-click the .exe file and you will see this :
For users running in powershell:
Click Yes/No, then check the exit code:
$LASTEXITCODE # 0 = Yes, 1 = NoIf PowerShell returns immediately without waiting (common for GUI-subsystem processes), use:
Start-Process -FilePath .\msgbox.exe -Wait
$LASTEXITCODEThis is an unsigned, hand-built binary — Windows SmartScreen will flag it as uncommon. Run it in a VM or sandbox you control.
- Wrote
dd x, dd yon one line in the data directories — NASM needs oneddper value. - Forgot the
bits 64directive, so NASM defaulted to 16-bit mode. - Forgot the null-terminator entry at the end of the Import Directory Table (a zeroed 20-byte entry marking the end of the DLL list), which the loader needs to know where the import descriptors stop.
- Got the padding wrong between sections more than once —
.text,.data,.bss, and.idataneed to land at exact file offsets matching their section headers'PointerToRawData, so I added explicittimes (X - ($ - $$)) db 0padding between each one. SizeOfImagedidn't cover.idata's virtual address range.SizeOfInitializedDatadidn't include.idata.- After adding the
.idatasection header,SizeOfHeadersand the other sections'PointerToRawDatavalues were stale and had to be recalculated. - Typo:
call[rel ...], missing a space, blocked assembly entirely. - The big one: used
call [rel user32_iat]to call through the IAT andmov rdx, messageto reference strings in.data, both relying on RIP-relative addressing. NASM's-f binoutput computes that using flat file-offset deltas, not RVA deltas — wrong across sections, sinceSectionAlignmentandFileAlignmentdiverge. Didn't fail to assemble, crashed the binary at runtime instead. Full writeup below.
Full commit history: github.com/RamadhanAdam/raw-pe/commits/main
Symptom: first build that compiled clean, sized correctly, and passed PE-bear inspection produced no dialog on launch. No window, no error.
Checking if it was running:
.\msgbox.exe
$LASTEXITCODENo output. GUI-subsystem processes detach immediately, so $LASTEXITCODE
alone wasn't enough, I switched to Start-Process -Wait to block on it.
Event Viewer: checked Event Viewer -> Windows Logs -> Application. First
entry was a Windows Error Reporting event (ID 1001, type BEX64 — a
generic crash bucket Windows uses when it can't classify the fault more
precisely):
Next to it, the Application Error (Event ID 1000) with the actual
exception details:
Faulting application name: msgbox.exe
Exception code: 0xc0000005 (access violation)
Fault offset: 0x0000000000000000
Faulting module name: unknown
Fault offset 0x0 and module unknown meant the CPU jumped straight to
address 0x0 — consistent with calling through a null or garbage pointer.
Root cause: .text called imported functions like this:
call [rel user32_iat][rel label] is RIP-relative addressing. NASM's -f bin output has no
concept of sections — it computes every relative offset as a flat file byte
delta. Accurate within one section, but sections are laid out differently on
disk (FileAlignment = 0x200) than in memory (SectionAlignment = 0x1000).
A reference from .text (file offset 0x400, RVA 0x1000) to .idata
(file offset 0x800, RVA 0x4000) has a file delta of 0x400 but a
runtime delta of 0x3000. NASM baked in the wrong one, so the call landed
on garbage — in this case, effectively 0x0.
Fix: replaced every cross-section symbolic reference with an absolute
64-bit VA, computed as ImageBase + section_RVA + intra_section_offset,
loaded with mov reg, imm64 and dereferenced with call [reg]:
; before (broken — file-offset delta != runtime RVA delta)
call [rel user32_iat]
; after (correct — absolute VA, computed at assemble time)
mov rax, 0x140000000 + 0x4000 + (user32_iat - idata_start)
call [rax]Intra-section symbolic math (e.g. import_dir - idata_start, both inside
.idata) was safe to leave as-is, since file-offset delta and RVA delta
agree when both labels live in the same section.
Verified:
Dialog appeared, exit code matched Yes/No correctly.
Follow-up intermittent crash: same 0xc0000005 crash recurred on a
later run:
Checked AppTimeStamp in Event Viewer's Details tab against the PE header's
TimeDateStamp — confirmed it was the current fixed binary crashing, not a
stale copy. Two hypotheses:
- Most likely: two files were sitting in Downloads (
msgbox.exeand a leftovermsgbox (1).exefrom an earlier download), and the old one got run by accident. - Ruled out: a load at a different base. The binary has no
.relocsection and hardcodes absolute VAs againstImageBase(0x140000000), but the header also setsIMAGE_FILE_RELOCS_STRIPPED(Characteristics0x0223) and noDYNAMIC_BASE(DllCharacteristics0). With no relocation information the loader cannot move the image: it either maps it at0x140000000or refuses to start it. It cannot load it somewhere else and let the hardcoded VAs crash later.
Deleted the duplicate file, reran several times cleanly with no recurrence —
consistent with hypothesis #1. A .reloc section is still what the binary
would need to support ASLR (see Known limitations).
- No
.relocsection — the binary only works if the loader can map it at its preferredImageBase(0x140000000). No ASLR support. - No error handling if either DLL/import fails to resolve.
- Written for learning, not for production use of any kind.
MIT — see LICENSE.





