Debugging with GDB
GDB (GNU Debugger) is the central tool for dynamic analysis on Linux: it lets you run a program step by step, stop it at specific points (breakpoints), inspect and modify registers and memory, and see exactly what it does at each moment. For pwn and reversing it’s essential: with GDB you confirm offsets, see the stack/heap state, debug your exploits, and understand code that’s unreadable statically. Powered with pwndbg or GEF, it becomes an exploitation swiss-army knife.
What it is
Section titled “What it is”GDB controls another process’s execution using the ptrace syscall: it can stop it, read and write its memory and registers, and intercept signals. On that base, pwndbg/GEF add exploit-oriented views (registers, stack, heap, disassembly) that turn GDB’s spare output into a useful dashboard.
Install the plugins (essential)
Section titled “Install the plugins (essential)”Bare GDB is awkward; pwndbg or GEF add automatic context at each stop:
- pwndbg (
github.com/pwndbg/pwndbg) — register/stack/heap views, pwn commands (heap,bins,rop,cyclic). - GEF (
github.com/hugsy/gef) — a very complete alternative with equivalent commands.
Theory: the debugging cycle
Section titled “Theory: the debugging cycle”flowchart LR
A["gdb ./bin / attach"] --> B["break at function/address"]
B --> C["run / continue"]
C --> D["stop at the breakpoint"]
D --> E["inspect: i r, x/..., disas"]
E --> F{"continue?"}
F -->|"step by step"| G["ni / si / finish"]
G --> E
F -->|"modify state"| H["set $reg / set {type}addr"]
H --> C
Essential commands
Section titled “Essential commands”- Start:
gdb ./binary,gdb -p <pid>(attach),run [args](r),starti(stop at the 1st instruction). - Breakpoints:
break main(b main),b *0x401234,b *main+42;info breakpoints,delete <n>. - Execution:
continue(c),next(n),step(s); at instruction levelnexti(ni) andstepi(si);finish(run until the current function returns). - Inspection:
info registers(i r),p $rip,disassemble func(disas).
Examining memory (x): the key
Section titled “Examining memory (x): the key”The x command follows the format x/<count><format><size> address:
- Format:
xhex,ddecimal,sstring,iinstruction,cchar. - Size:
bbyte,h2,w4,g8 (giant).
x/20gx $rsp # 20 qwords in hex from the stack pointerx/s $rdi # the string pointed to by RDI (e.g. a function argument)x/5i $rip # the next 5 instructionsExploitation (lab): a pwn workflow
Section titled “Exploitation (lab): a pwn workflow”from pwn import *context.binary = './vuln'
# launch with GDB attached and breakpoints from the exploit itselfio = gdb.debug('./vuln', gdbscript=''' break *main+120 continue''')# or attach to an already-running process:# io = process('./vuln'); gdb.attach(io, 'b *0x401234')To confirm an overflow offset by hand: run < <(cyclic 200) and, on the crash, cyclic -l $rsp (pwndbg). pwntools’s gdb.debug/gdb.attach integrate GDB with your script: you develop the exploit while watching memory at each step.
A reversing workflow
Section titled “A reversing workflow”In crackmes, GDB lets you follow logic that’s hard to read statically and modify state live:
b *check_function# step forward with ni/si watching registers and memory# the correct password often appears in a register/memory right before the cmpset $rax = 1 # force a resultset {int}0x404050 = 0 # change a value in memoryset $rip = 0x401200 # skip a check by changing the flowOther utilities
Section titled “Other utilities”- Watchpoints:
watch *0x404050(stop when a variable/memory changes). - Conditional breakpoints:
b func if $rdi == 0x1234. - Backtrace:
bt(call stack);frame <n>to switch frames.
Mitigations / Blue Team (anti-debugging)
Section titled “Mitigations / Blue Team (anti-debugging)”Malware and protected software detect GDB/debuggers (ptrace(PTRACE_TRACEME), timing checks, searching for 0xCC bytes) to hinder dynamic analysis — covered in Anti-debugging and Anti-analysis. The analyst bypasses it by patching the check, with LD_PRELOAD of a fake ptrace, or with plugins that hide the debugger.
Checklist
Section titled “Checklist”- Install pwndbg/GEF and understand the context they show
- Breakpoints by function, address, and offset
- Step-by-step execution (
ni/si/finish/continue) - Examine memory with
x(formats and sizes) - View and modify registers/memory/
RIPlive - Confirm overflow offsets (
cyclic) - Integrate GDB with pwntools (
gdb.debug/attach) - Use watchpoints and conditional breakpoints