ret2win Technique
- •
What it is
- •
Overwrite a saved return address to redirect execution to an existing useful function, such as
win(). Because the destination is existing code, injected shellcode is unnecessary.
- •
- •
How it works
- •
Find the offset from the buffer start to the saved return address (see Stack Frame Layout, or a cyclic pattern).
- •
Find the target function's address — trivial if the binary isn't PIE (address is fixed and visible via
nm/objdump, or auto-resolved by pwntools if the binary is unstripped). - •
Overflow the buffer with padding up to the offset, then the target address packed little-endian (
p64(...)). - •
On return, the function's epilogue pops the corrupted "return address" into
ripand jumps straight there instead of back to the real caller.
- •
- •
- •
- •
pwntools
- •
elf = ELF('./vuln') payload = b'A' * offset + p64(elf.sym['win']) # Requires a known runtime address.
- •
- •
Debugging
- •
disas win b win
- •
- •
Variants
- •
PIE: derive the binary base from an address tied to that image, then add the target offset; an unrelated leak alone is insufficient.
- •
Return-to-middle: land at a verified instruction past a guard. Check required registers, stack state, and any skipped prologue.
- •
ret2shellcode: return into injected code in a known executable buffer. This is a different destination from ret2win; see Shellcode and NX-DEP (No-eXecute).
- •
- •
Related
- •
Cards
- •
Why is ret2win possible without bypassing NX?
- •
ret2win uses existing executable instructions; it does not execute newly injected bytes from a non-executable page.
- •
- •
What's the one extra step ret2win needs when the binary is PIE?
- •
A leaked runtime address to compute the win function's actual location, since its address changes every run.
- •
- •
Linked references 11
- Address Space Layout Randomization (ASLR)
- Backdoor-Assisted Info Leak (Recursive Self-Leak)
- Buffer Overflow
- Exit Guard Bypass (Magic Value on Stack)
- Global Offset Table (GOT-PLT)
- NX-DEP (No-eXecute)
- pwn-college
- Read-Counter Manipulation (Canary Skip)
- Self-Referential GOT Overwrite (Target Depends on Corrupted Slot)
- Stack Canary
- Stack Frame Layout