Pwntools overview
Pwntools is a Python toolkit for authorized binary challenges. Treat payloads as bytes, keep the target configuration explicit, and first reproduce the same behavior manually in GDB.
Minimal local script
Section titled “Minimal local script”from pwn import *
context.binary = elf = ELF("./chall", checksec=False)context.log_level = "info"
io = process(elf.path)io.sendlineafter(b"> ", b"1")reply = io.recvline()log.info("reply: %r", reply)io.close()process() starts a local process. Use remote(host, port) only for a challenge service you are explicitly permitted to access, and keep the local and remote transports behind a small switch. Never place credentials or private endpoints in a public write-up.
Tubes: send and receive
Section titled “Tubes: send and receive”| Operation | Purpose |
|---|---|
send(data) |
Send bytes without adding a newline. |
sendline(data) |
Send bytes followed by \n. |
sendafter(delim, data) |
Wait for a delimiter, then send. |
sendlineafter(delim, data) |
Wait, then send a newline-terminated line. |
recv(n) |
Receive up to n bytes. |
recvn(n) |
Receive exactly n bytes or time out. |
recvline() |
Read through a newline. |
recvuntil(delim) |
Read through a delimiter. |
clean(timeout=...) |
Collect currently available data. |
interactive() |
Hand the tube to the terminal. |
Use b"..." for byte strings. sendline() adds one newline; it does not remove embedded NUL bytes. A program using read, fgets, or scanf can treat line endings and NULs differently, so match its actual input routine.
Make failures visible
Section titled “Make failures visible”context.log_level = "debug" # log bytes sent and received while diagnosingio.timeout = 2Start with visible logs while debugging a protocol; lower verbosity only after the interaction is stable. Avoid swallowing EOF or timeout errors because they often reveal a prompt mismatch or early process exit.
Local vs challenge service
Section titled “Local vs challenge service”Keep local reproduction as the default. A typical small script can use:
if args.REMOTE: io = remote(args.HOST, int(args.PORT))else: io = process(elf.path)This pattern is for a challenge’s explicitly authorized endpoint. Confirm the rules, host, port, and input protocol before connecting.
Continue by topic
Section titled “Continue by topic”Official reference
Section titled “Official reference”The pwntools documentation includes the complete tubes API, ELF API, and context settings.