Challenge Info
Basic ret2win style challenge - overflow a buffer to redirect program execution to a function that prints the flag.
Recon
1 | $ file ret2win |
64-bit executable, dynamically linked, not stripped - symbols will be available for analysis.
1 | pwndbg> checksec |
Key takeaways:
- No stack canary - buffer overflow won’t be detected
- NX enabled - can’t execute shellcode on the stack
- No PIE - addresses are fixed, no ASLR on the binary
Finding the Target
1 | pwndbg> info functions |
Three functions of interest: main, pwnme, and conveniently named ret2win at 0x400756.
Vulnerability Analysis
1 | pwndbg> disass pwnme |
The vulnerability is clear - classic buffer overflow pattern:
| Safe Pattern | Unsafe Pattern (This Binary) |
|---|---|
sub rsp,0x20 (allocate 32 bytes) |
sub rsp,0x20 (allocate 32 bytes) |
lea rax,[rbp-0x20] (point to buffer) |
lea rax,[rbp-0x20] (point to buffer) |
mov edx,0x20 (read 32 bytes) |
mov edx,0x38 (read 56 bytes) |
The program allocates 32 bytes but reads 56 bytes - that’s 24 bytes of overflow, enough to overwrite the saved RBP (8 bytes) and return address (8 bytes).
Calculating the Offset
Stack layout at pwnme:
1 | [buffer: 32 bytes] [saved RBP: 8 bytes] [return address: 8 bytes] |
Offset to return address: 32 + 8 = 40 bytes
Exploitation
The exploit is straightforward:
- Send 40 bytes of padding to reach the return address
- Overwrite return address with
ret2winfunction address
1 | from pwn import * |
When pwnme executes ret, it pops our crafted address into RIP and jumps to ret2win, which prints the flag.