Skip to content

Pwntools ROP, leaks, and GDB

from pwn import *
elf = context.binary = ELF("./chall", checksec=False)
rop = ROP(elf)
print(rop.gadgets)
rop.call(elf.symbols["win"])
print(rop.dump())
chain = rop.chain()

This assumes symbols and a usable address are available. For PIE, set elf.address from the live base before asking for runtime gadget addresses. Inspect rop.dump() and the resulting bytes; automated chain construction does not prove the chain matches the target’s calling convention or alignment needs.

For a function taking arguments, set the arguments explicitly when supported by the selected gadget set:

rop.call(elf.symbols["target"], [arg1, arg2])

The x86-64 System V ABI uses argument registers for initial arguments. A chain may need gadgets to populate them. Check the generated chain and function signature in Ghidra or GDB.

If the program writes a raw pointer and then a newline, receive the line and pad it to a machine word:

leak_line = io.recvline().strip()
leaked_address = u64(leak_line.ljust(8, b"\x00"))
log.info("leak: %#x", leaked_address)

This assumes little-endian x86-64 and a raw byte leak without extra text. If output looks like 0x7f..., parse the printed hexadecimal text instead. First inspect repr(leak_line) so whitespace or protocol framing is visible.

io = gdb.debug([elf.path], gdbscript="""
set disassembly-flavor intel
break main
continue
""")

gdb.debug() launches the local process under GDB. gdb.attach(io, gdbscript="...") attaches to an existing process where supported. If the terminal or GDB path is misconfigured, use gdb.debug documentation for platform-specific setup or run the process in GDB manually.

log.info("offset: %d", offset)
log.info("binary base: %#x", elf.address)
log.info("payload length: %d", len(payload))
log.debug("payload:\n%s", hexdump(payload))

Keep the stages separate: receive prompt, send stage one, parse leak, calculate base, send stage two. A clean run should produce the same meaningful result without manual terminal interaction.