Exploitation of binary vulnerabilities or learning to “pump” (Part 1)
Hello everyone! In this series of writeups we will analyze known tasks on exploitation of binary vulnerabilities from Exploit Exercises (they are no longer there, so I recompiled them for Win). About what Buffer Overflow is is written perfectly and with examples HERE (great article by @Mogen). By the way, there are a lot of cool problems on the topic of PWN on Codeby Gamesso I recommend it 🙂
So… we will solve our problems using static (Ghidra) and dynamic analysis (x64dbg). And most importantly, we will do it without the source code of the vulnerable program, unlike how it is done HERE And HERE.
Stack0
To solve this problem I will use my “sand-reverse-laboratory”, where I already have everything “installed” 🙂
![](https://habrastorage.org/getpro/habr/upload_files/ae2/ce1/c57/ae2ce1c574e89bbde49c225699c6b08e.jpg)
First, we'll do “statics”. We launch Hydra and throw in the program that we'll “pump”.
![](https://habrastorage.org/getpro/habr/upload_files/ade/86d/1ca/ade86d1ca44a676f71d94243d3dd8b27.jpg)
Let's start the analysis:
![](https://habrastorage.org/getpro/habr/upload_files/8b6/35d/dc1/8b635ddc15ebe14096e99c3b7ead30a8.jpg)
We get the disasm listing and decompiled code:
![](https://habrastorage.org/getpro/habr/upload_files/4e3/048/0fc/4e30480fca0cc6a61675b4b1b707f7e6.jpg)
Rename the variables (there is little code here – not really necessary, but it's a good habit):
![](https://habrastorage.org/getpro/habr/upload_files/7dd/05f/98c/7dd05f98c555ca149897f83dcd58adea.jpg)
So, we see that we have a 64-character buf array and a “bad” gets() function. We also have a condition: if the var1 variable = 0, we will get “Try again”. Accordingly, to get to the else branch, we need to somehow modify the var1 variable. Select the code and remember the “test eax, eax” instruction, we will need it a little later.
![](https://habrastorage.org/getpro/habr/upload_files/88d/6f7/cab/88d6f7cabd4cd3ad3a3d783f01f9b749.jpg)
Let's move on to “dynamics”. Open the program in x64dbg, look at strings and find that same “Try again”, fall through the instructions, see “test eax, eax” and “test al, al”. Put a breakpoint on any of these instructions.
![](https://habrastorage.org/getpro/habr/upload_files/414/49c/f2f/41449cf2fbc18baf8ac98c11907786bc.jpg)
![](https://habrastorage.org/getpro/habr/upload_files/1cc/d42/65a/1ccd4265accb0b40c270dab7b3bff6b7.jpg)
“Submit to the entrance” 10 characters.
![](https://habrastorage.org/getpro/habr/upload_files/753/d9b/964/753d9b964d2bf508892c9882a9d5ddf4.jpg)
We see that the EAX register has not changed at all (equal to 0). PS The “test eax, eax” instruction in x86/x64 assembly language performs a bitwise logical AND operator between the eax register and itself. The “test” instruction does not change the value in the eax register, but affects the processor flags, in particular, the zero flag (ZF), which is set if the result of the AND operation is zero. This means that if eax contains zero, then ZF will be set, and if eax is not zero, then ZF will be cleared. This allows using the je (jump if equal) or jne (jump if not equal) instructions to jump to certain labels depending on the EAX value.
We make a conditional jump (je) and get “Try again”.
![](https://habrastorage.org/getpro/habr/upload_files/8cd/808/ddc/8cd808ddc3b790c607e282d3b590d583.jpg)
![](https://habrastorage.org/getpro/habr/upload_files/f09/d04/d47/f09d04d47183355ebe45eba5f7353fc1.jpg)
Great! What if we send 65 characters instead of 64?
![](https://habrastorage.org/getpro/habr/upload_files/f14/98e/e60/f1498ee60b50ff758f01d485234a036f.jpg)
We see that there is a one in EAX. Thus, 64 bytes “led” us to the beginning of the changed variable (var1), and the next byte will be placed in the changed variable. In other words, 65 bytes of data will change 0x00000000 to 0x00000041 (the 65th character “A”). Thus, the unsafe gets() function allowed us to overflow buf and change the var1 variable, which led us to the following result:
![](https://habrastorage.org/getpro/habr/upload_files/280/ef5/65a/280ef565ab1d674e2399c37455f73dac.jpg)
Thank you all for your attention 🙂