Reverse engineering PWN tasks or exploiting binary vulnerabilities (Part 2 / Stack1)
Friends, I greet you all! Let's continue “puffing” 🙂 For those who haven't read the first article – it's HERE!
Before we begin, I'll leave you with a couple of useful resources:
Theory – Articles on what Buffer Overflow is and how to use it
Practice – Cool PWN themed tasks from Codeby Games
In this article we will solve the Stack1 task (you can download the EXE file HERE).
Stack1
Let's start with static analysis, open stack1.exe in GHIDRA:
![Outputting information about our EXE file Outputting information about our EXE file](https://habrastorage.org/getpro/habr/upload_files/250/a9c/9ec/250a9c9eceb3f99f416a85eaf6fc562a.jpg)
Let's analyze our binary file:
![](https://habrastorage.org/getpro/habr/upload_files/b74/b96/177/b74b961774e534947bfdbcc7e62138a7.jpg)
![](https://habrastorage.org/getpro/habr/upload_files/323/3e3/0ce/3233e30cef719bd79c288b7b9453c70e.jpg)
We get the decompiled code:
![](https://habrastorage.org/getpro/habr/upload_files/45f/39d/402/45f39d4022d89f87fc3f81f67aecfb37.jpg)
We immediately pay attention to the use of the unsafe strcpy() function, which copies our passed argument into a 64-byte array (local_54). Last time we overwrote the variable, and this time we need to assign it a specific value, namely 0x61626364. By overwriting the variable, our condition will work, and we will see the line “you have correctly…”.
Let's move on to “dynamics”! Open x64dbg, open stack1.exe and click “Change command line arguments”:
![](https://habrastorage.org/getpro/habr/upload_files/fa9/d46/452/fa9d46452612a723dd468fa38f4c534d.jpg)
But what should we write as an argument? Remember that our array is 64 bytes, so we'll fill it with “A” characters. Next, we need to add 0x61626364. Open the ASCII table to find out what these characters are:
![](https://habrastorage.org/getpro/habr/upload_files/2b1/3aa/fc9/2b13aafc9f0676b637c1c72e625c6f42.jpg)
Excellent! Taking into account byte order our argument will look like this:
![64 characters "A" And "dcba" 64 characters "A" And "dcba"](https://habrastorage.org/getpro/habr/upload_files/bca/05e/d3f/bca05ed3fb5ab3c0fefa31670e1e3f8b.jpg)
Click “OK”, put a breakpoint on the comparison instruction “cmp EAX,61626364”:
![](https://habrastorage.org/getpro/habr/upload_files/c31/f3b/a24/c31f3ba24ecd358e3cde0447caa65b69.jpg)
Let's launch! Fill the array with “A” symbols and successfully write to EAX – 61626364 (these are our “dcba” symbols):
![](https://habrastorage.org/getpro/habr/upload_files/46c/87f/279/46c87f279ad68491245966d02e6a2718.jpg)
The CMP instruction “successfully” compares the value in the EAX register with the value 61626364, and we get the desired message in the terminal (P.S. CMP actually performs a subtraction operation between two operands to check the equality or inequality of these operands):
![Profit! Profit!](https://habrastorage.org/getpro/habr/upload_files/8bf/cd4/588/8bfcd4588ca08d1d28b6061022851296.jpg)
Thank you all for your attention! P.S. If you liked the writeup, please like it! This way I will know that the topic is useful to many people so I can continue the series of articles on this topic! 🙂