Debugging with GDB/WinDbg
Dynamic Analysis & Debugging for VR · VR Analysis & Discovery track
8 min read
In this module
The Debugger as a Research Instrument
You've used debuggers to step through programs and poke at memory. In vulnerability research, the debugger is your primary instrument — watching how a program digests attacker-controlled data, catching the precise moment corruption happens, verifying that exploits actually work. This lesson covers the techniques that go beyond basic stepping and inspecting.
Software vs. Hardware Breakpoints
GDB supports two fundamentally different breakpoint mechanisms:
Software breakpoints work by replacing the first byte of an instruction with INT 3 (opcode 0xCC on x86). When the CPU executes this byte, it raises a breakpoint exception (#BP), the kernel stops the process, and GDB puts the original byte back and gives you control. Software breakpoints are unlimited in number, but they change the code. Read-only text pages aren't a problem on Linux: GDB writes the 0xCC through ptrace, and the kernel's poke path uses FOLL_FORCE to patch a private copy of the page anyway. Code that genuinely can't be written (ROM or flash on an embedded target), or that checksums itself, is another matter.
(gdb) break *0x401230 # Set software breakpoint at address(gdb) break parse_header # Set software breakpoint at function(gdb) break parser.c:142 # Set software breakpoint at source lineHardware breakpoints use the CPU's debug registers (DR0-DR3 on x86, with DR7 as the control register). They monitor an address without modifying memory. This means they work on code you can't patch, such as ROM, and on code that checks its own integrity (anti-debugging), and the same registers can trigger on data reads and writes, not just execution.
(gdb) hbreak *0x401230 # Hardware breakpoint at address(gdb) hbreak parse_header # Hardware breakpoint at functionThe limitation: x86 CPUs only have 4 debug registers, so you can only have 4 hardware breakpoints active simultaneously. Use them when software breakpoints cannot work or when you need to break on memory access (watchpoints).
Watchpoints: Catching Memory Corruption
Watchpoints are underrated. They break execution the moment a specific memory location is touched:
(gdb) watch *(int*)0x7fff5000 # Break on WRITE to this address(gdb) rwatch *(int*)0x7fff5000 # Break on READ from this address(gdb) awatch *(int*)0x7fff5000 # Break on READ or WRITEThe killer use case: you know a specific memory location gets corrupted (perhaps the return address on the stack, or a heap chunk header). Set a watchpoint on it and run the program. GDB stops at the exact instruction that performs the corruption:
(gdb) # Find the stack address of the return address(gdb) info frameStack frame at 0x7fffffffe010: rip = 0x401234; saved rip = 0x401500 ...
(gdb) # Watch the saved return address(gdb) watch *(long*)0x7fffffffe008Hardware watchpoint 1: *(long*)0x7fffffffe008
(gdb) continueHardware watchpoint 1: *(long*)0x7fffffffe008
Old value = 0x401500New value = 0x4141414141414141
0x00401267 in parse_input (buf=0x7fffffffdfc0) at parser.c:8787 strcpy(local_buf, buf);The watchpoint caught the exact instruction (strcpy) that stomped the return address. Root cause, identified.
Conditional Breakpoints
When a function is called thousands of times but you only care about specific conditions, use conditional breakpoints:
(gdb) break memcpy if $rdx > 0x1000# Only breaks when memcpy's length argument (RDX in x86-64 System V ABI)# exceeds 4096 bytes
(gdb) break recv if $rdi == 5# Only breaks when recv is called on file descriptor 5
(gdb) break parse_chunk if chunk_type == 0x49444154# Only breaks when parsing an IDAT chunk (requires debug symbols)Conditional breakpoints dramatically reduce the noise when debugging complex programs that process many inputs before reaching the interesting case.
Conditional breakpoints can be slow because GDB must evaluate the condition every time the breakpoint is hit. For a function called millions of times (like memcpy in a busy server), this can make the program crawl. Hardware-assisted conditional breakpoints are faster but limited. For high-frequency breakpoints, consider using GDB scripting to implement the logic more efficiently.
GDB's Python API
GDB embeds a Python interpreter that can access all of GDB's functionality. This enables powerful automation for vulnerability research:
# gdb_vuln_monitor.py - Monitor for potential buffer overflowsimport gdb
class MemcpyMonitor(gdb.Breakpoint): def __init__(self): super().__init__("memcpy")
def stop(self): # x86-64 System V ABI: RDI=dst, RSI=src, RDX=len dst = int(gdb.parse_and_eval("$rdi")) src = int(gdb.parse_and_eval("$rsi")) length = int(gdb.parse_and_eval("$rdx"))
# Check for suspicious lengths if length > 0x10000: print(f"[!] Large memcpy: {length:#x} bytes") print(f" dst={dst:#x} src={src:#x}")
# Print backtrace gdb.execute("bt 5") return True # Stop execution
return False # Continue execution
MemcpyMonitor()print("[*] memcpy monitor installed")Load this script in GDB with source gdb_vuln_monitor.py. It silently monitors every memcpy call and only stops when the length exceeds a threshold, printing a backtrace when it triggers.
Examining Memory and Registers
Effective VR debugging requires fluent memory examination:
(gdb) x/32xb $rsp # 32 bytes at stack pointer, hex bytes(gdb) x/8xg $rsp # 8 quadwords at stack pointer(gdb) x/s 0x402000 # String at address(gdb) x/10i $rip # 10 instructions at instruction pointer(gdb) x/4xw $rdi # 4 words at first argument
(gdb) info registers # All general-purpose registers(gdb) info registers rax rdx # Specific registers
(gdb) p/x *(struct header *)$rdi # Print struct at RDI as hexFor vulnerability research, you often need to see the relationship between a buffer, its boundaries, and the data that overflows it:
(gdb) # Examine a buffer overflow in progress(gdb) p &local_buf # Where does the buffer start?$1 = (char (*)[64]) 0x7fffffffdfc0
(gdb) x/80xb 0x7fffffffdfc0 # Show buffer + 16 bytes beyond# Look for the pattern overflowing past offset 64WinDbg for Windows Targets
When researching Windows targets, WinDbg is the primary debugger. Its command syntax differs from GDB but serves the same purposes:
# WinDbg equivalents of common GDB commandsbp kernel32!CreateFileW # Breakpoint (like GDB's break)ba w4 0x00ff1234 # Hardware breakpoint on write (like watch)g # Go/continuep # Step overt # Step intok # Stack backtrace (like bt)r # Show registersdb 0x00ff1000 L100 # Display bytes (like x/256xb)dd esp L10 # Display DWORDs at stack pointerdt _HEAP 0x00ff0000 # Display typed structure
# WinDbg MASM expressionsbp kernel32!CreateFileW ".if (poi(esp+4)==0x00401000) {} .else {gc}"# Conditional breakpoint using MASM syntaxWinDbg's !exploitable extension is particularly valuable for VR - it automatically classifies crashes by likely exploitability (EXPLOITABLE, PROBABLY_EXPLOITABLE, UNKNOWN, NOT_LIKELY_EXPLOITABLE).
Remote and Multi-Process Debugging
Many VR targets are not simple local programs. You may need to debug:
Remote targets - the program runs in a VM or on different hardware:
# On the target machine, start GDB servergdbserver :1234 ./target_program
# On your analysis machine, connect(gdb) target remote 192.168.1.100:1234Forking servers - network daemons that fork a child for each connection:
(gdb) set follow-fork-mode child # Follow child process after fork(gdb) set detach-on-fork off # Keep both parent and child attachedMulti-threaded programs - where race conditions may be the vulnerability:
(gdb) info threads # List all threads(gdb) thread 3 # Switch to thread 3(gdb) set scheduler-locking on # Only run the current thread (prevents races)These configurations are necessary because real vulnerable software is almost never a simple single-process, single-threaded program. Plan accordingly.