Pwntools exploit script generator
Generate a working pwntools template with the right context, process or remote connection, and the boilerplate every exploit repeats.
Open in ctfpalEvery pwn solution starts from the same twenty lines. Generating them removes the transcription errors - the wrong architecture in context, a recvuntil that hangs because the prompt string was off by a space.
from pwn import *
context.binary = elf = ELF('./vuln')
context.log_level = 'debug'
LOCAL = True
io = process('./vuln') if LOCAL else remote('host.example', 1337)
io.recvuntil(b'> ')
payload = flat({
offset: [pop_rdi, binsh, ret, system],
})
io.sendline(payload)
io.interactive()The template also encodes the local-versus-remote switch that every exploit needs, because you develop against a local process with a debugger attached and then flip one flag to fire at the scoreboard. Getting that structure right at the start saves rewriting the script under time pressure once the exploit finally works.
The pieces worth knowing
- `context.binary` sets architecture, endianness, and bit width for every other helper at once - set it and
p64andflatdo the right thing automatically. - `recvuntil` on a distinctive prompt, not a fixed byte count. Byte counts break the moment output length changes.
- `gdb.attach(io)` drops you into a debugger with the process live at that point - the single most useful debugging tool in the library.
- `context.log_level = 'debug'` prints every byte sent and received, which is how you find out that the remote wanted
\r\n.
Part of a module
10. Binary exploitation
Read a binary’s protections, find an overflow offset in one crash, and build a ROP chain when the stack is not executable.
Practise on real challenges
Go deeper
- From crash to shell: stack overflows, offsets, ret2win, and ret2libcA segfault is not an exploit. The path from an unexpected crash to a controlled instruction pointer to a shell, with the mitigation checks that decide which technique you need and the stack-alignment detail that breaks working exploits.
Related tools
ROP chain and payload builder
Assemble an exploit payload from padding, addresses, and raw bytes, with a live hexdump and offset ruler - the pwntools flat() workflow.
Struct pack and unpack (p32, p64, u32, u64)
Convert integers to little-endian byte strings and back at 8, 16, 32, and 64 bits - the pwntools p32/p64 helpers without the install.
Libc base address calculator
Turn a leaked libc pointer into the library’s base address, then resolve any other symbol - the arithmetic every ret2libc exploit runs on.
Shellcode assembler and library
Assemble x86 and x86-64 shellcode, or pick a ready execve(/bin/sh) payload, with null-byte-free variants and length reporting.