Skip to content

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.

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.

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.

context.log_level = "debug" # log bytes sent and received while diagnosing
io.timeout = 2

Start 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.

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.

The pwntools documentation includes the complete tubes API, ELF API, and context settings.