Windows Exploit Development [chapter 0x1] - Revisiting ROP Chain in Windows 7: 10-Strike Network Inventory Explorer 8.54
Hello world!
Before we getting start with the content, I would like to mentioned that this material is not meant for beginner. If you never done Windows Exploit Development before I suggest to start reading my old blog about breaking the code series(link) this will give a general idea on how to setup and conduct research on windows binary.
This post was inspired from the following exploit-db. You may notice later that I used different exploit to gain control of the program but the approach still the same with the original author.
Setup Windows Lab
In this lab I used Windows 7 from windows IE developer official (website) and install several tools for exploit development in it, such as:
-
Python 2.7
-
Immunity Debugger
-
Mona
-
The vulnerable program, you can download it from the following link
Note 1: DEP is set to always ON!
DEP is stands for Data Execution Prevention. This security mechanism is used to make the stack area of program to be not executable, thus, this means if we try to put shellcode in the stack it will failed. To bypass this protection, we will use ROP(Return Oriented Programming) to reuse the code inside the program to call virtualprotect function in order to make the stack area executable again.
Note 2: Exploit offset calculation is based on the how long is the username of the windows machine, in this case my username is IEUser.
Note 3: To get accurate result when debugging the exploit, everytime the program crash you need to detach the immunity debugger, restart the program and attach it again. I know it’s a lot of work.
Crash and Take Over the Program EIP
First, we need to crash the program and take over the EIP register. This register is the one responsible to control the execution flow.
The following python code will generate a payload.txt file that will crash the program by overwritting the EIP register. Upload the file by go to the “computer” section and choose “From Text File”.
Then go to the immunity debugger, you need to type “shift+f9” to jump the SEH exception to get the following result. If you having trouble to used this shortcut you can go to virtualbox options “input” > “keyboard” > “Soft Keyboard” to emulate this shortcut.
At this stage, we need to know at what offset in our input to gain control of the EIP register, we will be using one of the mona features to generate pattern for the payload.
To get the pattern you can go to immunity folder and look for pattern.txt file. Copied the content under ASCII section and update the previous python source code to used this payload.
Upload the new payload to the vulnerable program and don’t forgot to pass the exception using “shift+f9” shortcut. The EIP now is overwrite with the pattern, next we will use mona feature to get the exact location of our input to control the EIP.
Using the following command, we are able to identify the offset of our EIP which is 207. But how about the SEH exception? that would be not much of problem, why? if you look closely the SEH is located 4 bytes(211) after the EIP so all we need to do is to find a gadget that jump to the next 8 or more bytes in the stack so we will no get caught up in the SEH exception.
There are a lot of gadgets inside the program that can be used to satisfy the condition but because I’m lazy, I’m just going to used the one that suggested by the original author. It’s located at 0x10013e29 and it points to instructions:
this instructions basically will increment the ESP value to 12 bytes, thus, this will jump the nSEH and SEH and make the execution return to the stack. Update the source code:
Put breakpoint at 0x10013e29, so we know that the exploit is working. I suggest to use hardware breakpont in immunity debugger so you don’t have to put breakpoint multiple times. After that you can upload the new payload and pass the exception, at the end you will be end up at your breakpoint like below:
Continue the execution by click “step over” 2 times this will make the program jump next 10 bytes and return the execution to the stack, cool!
Constructing ROP Chain
Now, it’s time to create the ROP Chain so we can reuse the instruction in the program and execute virtualprotect() function.
Some tips to create ROP Chain in windows, make sure to use gadgets that the following features is set to false:
-
Rebase, although this is not security mechanism. This module will move instructions of used modules inside the program if there is conflicted address.
-
ASLR, randomize the instructions of the library
You can check it using mona by using this command:
In shorts, there are 4 DLL that we can used to construct ROP chain:
-
sqlite3.dll
-
ssleay32.dll
-
MSVCR71.dll
-
LIBEAV32.dll
Using this information lets create ROP chain using mona:
the following command instruct mona create ROP Chain with 4 modules that have mentioned before and make sure the instructions don’t have this 4 bad characters. We need to get rid this 4 bad characters, so our exploit is not altered at runtime.
So this is going to be my strategy to construct the ROP:
As we know 32 bit binary used stack to put the parameter of called function, thus, I will used pushad instruction that will get all of the content of registers and put it in the stack following this order:
EAX, ECX, EDX, EBX, EBP, ESI and EDI
Each of this register will be contain the following value(I combined the ROP Chain by using rop_chain.txt and rop.txt file that generated by mona using the previous command):
EAX -> Virtualprotect() function pointer
ECX -> Address of any location inside the program that is writable (lpfOldProtect)
EDX -> Protection level flag 0x40(set to be executable again) (flNewProtect). Since there is going to be null byte we are going to use neg instruction that will flip each of the bits.
EBX -> Affected number of bytes we will set this to 0x201(shellcode size, more or less) (dwSize).
EBP -> Any gadget that will return the execution back to the stack
EDI -> ROP NOP(JUNK)
ESI -> Jump to EAX to execute VirtualProtect(). To get the pointer we will used mona again:
Using this information our final payload will be:
Running this script will create a payload.txt file and when you upload it to the program, it will execute calculator.
Cool! so in this post we will look on how to bypass ASLR and DEP protection in windows 7 by elevating ROP in our exploit. I hope you enjoy this post and take care.