Pwntools ROP, leaks, and GDB
Ask pwntools for a ROP chain
Section titled “Ask pwntools for a ROP chain”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.
Parse a leaked pointer
Section titled “Parse a leaked pointer”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.
Debug a script in GDB
Section titled “Debug a script in GDB”io = gdb.debug([elf.path], gdbscript="""set disassembly-flavor intelbreak maincontinue""")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 each stage
Section titled “Log each stage”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.