Skip to main content
Exploit Debugging with GDB-pwndbg & pwntools
Lesson 14 of 20

Exploit Debugging with GDB-pwndbg & pwntools

Combine GDB with pwndbg and Python's pwntools library to inspect registers and stack frames at runtime, measure exact buffer offsets, and automate reproducible verification tests on training binaries.

Intermediate 28 min IoT & embedded
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Navigate registers, disassembly, and stack memory efficiently using GDB and pwndbg
  • Determine exact buffer-to-return-address distances using cyclic pattern reasoning
  • Configure pwntools context, ELF symbol lookup, and local process interaction in Python 3
  • Identify common alignment, endianness, and mitigation pitfalls when verifying lab fixes

Recommended first: Process Memory Layout, Stack Frames & Heap Overview

#Why this matters

Static tools such as readelf and checksec tell you what sections and mitigations a binary has, but they cannot show how registers and stack slots evolve while a function runs. When triaging a memory-corruption bug or verifying a starter ret2win training challenge, guessing offsets by trial and error wastes time.

Pairing GDB with pwndbg for live memory inspection and pwntools for repeatable Python scripting gives you a disciplined workflow. You inspect the exact state at a breakpoint, calculate frame geometry once, and automate clean verification tests.

#The GDB-pwndbg debugging loop

Plain GDB prints minimal output when execution pauses. pwndbg augments every breakpoint stop with a unified context view showing general-purpose registers (rip, rsp, rbp, rdi), nearby disassembly, and a dereferenced view of the stack.

  1. Inspectchecksec and ELF symbols
  2. Break & Runpwndbg at function ret
  3. Measuredistance from buffer to saved rip
  4. Scriptpwntools context and verification

Use a small set of pwndbg commands to orient yourself inside any function:

Command Purpose When to use
start Break at main and begin execution Initial launch of a local binary
b *func+offset Set breakpoint at a specific instruction Pausing right at read or ret
telescope $rsp 16 Print 16 stack slots with pointer chains Inspecting local variables and return pointers
vmmap Show mapped regions and permissions Confirming code, stack, and library addresses
ni / si Step over (ni) or into (si) one instruction Watching how call and ret change $rsp

#Measuring buffer-to-return-address offsets

Suppose a non-PIE training binary contains an unguarded buffer copy inside read_note() and a harmless helper function win_message() that prints "Training milestone reached". To find the exact distance from the start of the buffer to the saved return pointer without guessing, analysts rely on two methods:

  1. Debugger frame arithmetic: Pause just after the input call, print the buffer's start address (for example from $rdi or $rsi passed to the read function), and subtract it from the address holding the saved return pointer ($rbp + 8).
  2. De Bruijn cyclic pattern: Generate a non-repeating sequence where every 4-byte or 8-byte window is unique. Observing which 4-byte or 8-byte slice lands at the saved return address immediately identifies the byte offset.
Inspecting frame geometry in pwndbg
pwndbg> b *read_note+42
Breakpoint 1 at 0x401190
pwndbg> continue
pwndbg> p/x $rbp + 8 - $rax
$ 1 = 0x48
pwndbg> x/2gx $rbp
0x7fffffffe1c0: 0x00007fffffffe1e0  0x0000000000401215

Here, $rax holds the destination buffer address, $rbp + 8 holds the saved return address 0x0000000000401215, and the difference 0x48 (72 bytes) tells you the exact distance to the return slot. Notice also the 16-byte stack alignment rule in the x86-64 System V ABI: if a function such as puts or printf executes aligned SSE instructions, $rsp must be 16-byte aligned before call or the program may fault inside libc.

#Automating verification with pwntools

Once you know the binary's architecture, symbol addresses, and frame geometry, Python's pwntools library keeps tests readable and portable. Setting context.binary = elf configures architecture, word size, and little-endian byte packing automatically so p64() packs 64-bit addresses in the right byte order.

python
from pwn import *

# Load the local training binary and configure architecture defaults
elf = ELF("./lab_ret2win", checksec=False)
context.binary = elf
context.log_level = "info"

# Inspect key symbols from the ELF symbol table
target_addr = elf.symbols["win_message"]
log.info(f"win_message address: {hex(target_addr)}")
log.info(f"Packed little-endian bytes: {p64(target_addr)}")

# Launch a local lab process and optionally attach GDB for live inspection
io = process(elf.path)
# gdb.attach(io, gdbscript="b *read_note\ncontinue")

test_frame = {
    "padding_bytes": 72,
    "target_symbol": "<conceptual_placeholder>",
}
log.info(f"Prepared structured verification model: {test_frame}")
io.close()

Using elf.symbols["win_message"] avoids fragile hardcoded hex constants whenever a training binary is recompiled, and gdb.attach() lets you drop straight into pwndbg at the exact moment a scripted test reaches a critical function.

#Common mistakes and defences

  • Skipping context.binary = elf: Calling p64() or fit() without setting the binary context can produce 32-bit vs 64-bit packing or endianness mismatches.
  • Hardcoding addresses under PIE or ASLR: When PIE is enabled, elf.symbols returns relative offsets from 0x0, not the runtime virtual address; an address leak is needed at runtime to compute the base.
  • Line-buffering and newline mismatches: Using send() when a target reads with fgets() (which waits for \n or EOF) causes the script to hang until timeout; verify how the target reads input (read vs fgets).
  • Runtime mitigations: Stack canaries detect sequential overwrites before ret, ASLR and PIE randomize code and stack bases on every run, and hardware/OS Control-Flow Integrity (CFI) restricts indirect jumps and return targets.

#Summary

Combining GDB-pwndbg and pwntools replaces guesswork with a structured debugging workflow. Use pwndbg commands such as vmmap, telescope, and frame-pointer arithmetic to measure stack layout and alignment precisely, then encode symbol lookups and process interaction cleanly in pwntools. Understanding how offsets, endianness, and alignment behave in controlled labs also clarifies how stack canaries, PIE, ASLR, and CFI block real-world control-flow hijacking.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge