Vulnserver is a Windows based threaded TCP server application that is designed to be exploited. The program is intended to be used as a learning tool to teach about the process of software exploitation, as well as a good victim program for testing new exploitation techniques and shellcode.
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 3 Build 2600
OS Manufacturer: Microsoft Corporation
Vulnserver - Link above
python 2.7 x86, pydbg 32-bit binary, python wmi, pywin32
Immunity Debugger with mona installed
Kali 2.0 with Boofuzz fuzzer
2.1 Interface
Using netcat connect to X.X.X.X:9999 and issue the HELP command to confirm that everything is working as expected:
3. Fuzzing
For the purpose of this tutorial we will be focusing on fuzzing the GMON command. Observing that the valid argument structure for the GMON command is roughly <gmon>[space]<command_value> we can try sending GMON hello as a test and see if it’s accepted.
As can bee seen above, the command and argument executed successfully. Now that we have confirmed the structure of a command and its arguments, we can start fuzzing this command to see if we can get the program to crash when submitting various argument values to the GMON command.
The following BOOFUZZ template was created to fuzz the GMON command in an effort to get the program to crash.
The following command appears to have crashed the program indicating that the "GMON" command can be manipulated to force the program to crash
3.2 Analysing The Crash
Based on the TCP stream (Figure 4), the number of bytes that was sent by the fuzzer and that caused the crash was around 5000 bytes.
To gain a better understanding of this, the following template was used to identify the length needed to overflow the GMON command and cause the crash:
As can be seen from Figure 6 the program crashes when a string of 5950 chars is sent. Checking the dump using immunity it can be seen that we control a buffer of length 3531 bytes - Figure 7. As can be seen in the above images we have no control over EIP.
That said, If we review the SEH chain we can clearly see that we have control over the SEH chain - Figure 8.
4. nSEH & SEH Offsets
4.1 Create Unique Pattern
Using msf-pattern_create create a pattern of length 5000 bytes.
Restart the application, update the exploit as shown below and execute it
Hit ALT + S once the program crashes and note the 'Address' value of the 'Corrupt Entry' (this will be nseh) and the SE-handler value above 'Corrupt Entry' (this will be the seh value)
4.2 Get nSEH & SEH Offsets
Next use msf-patten_offset to get the nseh and seh offsets
4.3 Confirm Offsets
Update the exploit and execute it to confirm that these offsets are correct:
Outlined below are the changes you will need to make:
# Modify lines 35 - 40 to be the same as below''' INSERT THE REQUEST TEMPLATE HERE - NEEDS TO BE MODIFIED EVERY TIME'''request_template = ("GMON/.:/{}")
# Modify Lines 46 and 56 to be the same as below# CHANGE ME!iteration =0processName ="vulnserver.exe"# name of the process as it appears in tasklistexecutable = r"C:\Documents and Settings\Administrator\My Documents\Downloads\vulnserver\vulnserver.exe" # path the executable to start the process
start_buffer_address = "\x0C\xFA\xB7\x00" # Take this value straight from immunity for wahtever register -- this one is ebp remeber little endian # Address or from one of the registers usually. try both
start_buffer_address_offset =0x04# The offset you want to read. Note this will typically be 4seh_violation =True# Change me depending on crash occuring in seh handler listeningPort =9999# Address of the listening processcrashLoad = "A" * 3495 + "B" * 4 + "{}" + "D" * 1488 # load to crash the proces with {} representing where our test chars will go
responsive_test_string ="HELP"crash_wait_timeout =10# seconds to wait after a payload has been sentservice_responsive_timeout =10
5.2 Results
Execute the script. Once finished you should have identical results to those outlined below in Figure 14.
# Good Character Summary\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff
# Bad Character Summary\x00
6.0 Safe Pop Pop Ret
Using mona generate the seh.txt file by issuing !mona seh command:
Using the Find-Safe-Address.py script the following address was chosen 0x625010b4. Using immunity restart the application, set a breakpoint at 0x625010b4 and update the POC as shown below and execute it.
As can be seen from Figure 17, SEH has been overwritten with our PPR address. Press shift and F9 to pass the exception to the program and you should hit your breakpoint at 0x625010b4 as shown below in Figure 18.
7. Jumping Out Of Small Buffer
Stepping through the PPR instructions into our buffer using F7 we can see that we have 4 bytes before we hit the SEH instructions - Figure 19. Unfortunately our D buffer is only 22 bytes which is too small to do anything with. This leaves us with two options. We can either perform a long jump backwards to the start of our A buffer, or we can implement an egghunter. For the purposes of this tutorial we will implement an egghunter.
7.1. Jumping Backwards
As can be seen in Figure 19, we have approximately 22 bytes remaining. Due to the limited buffer space remaining we will need to somehow jump backwards into our A buffer to gain more space. To do this we will perform a negative jump of 70 bytes backwards into our A buffer.
Restart the application, set a breakpoint at 0x625010b4and run the updated exploit shown below:
Stepping past our breakpoint and into our buffer we are met with out negative jump - Figure 21. Following our negative jump instruction we jump back 70 bytes into our A buffer - Figure 22.
8. Egghunter
At this point there is approximately 60 bytes of uninterrupted space under our control in our A buffer. This is more than enough space for an egghunter - typically 32 bytes. The following command was used to generate our egghunter. msf-egghunter -f raw -e b00b -v egghunter -p windows -a x86 -f pythonFigure 23.
8.1 Verify That Egghunter Works
The next step is to add the shellcode for the egghunter. Restart the application, set a breakpoint at 0x625010b4and run the updated exploit shown below:
Stepping into our breakpoint and taking the jump back into our A buffer we should see the instructions for our egghunter. Set a breakpoint at 00B7FFD4 and hit F9.
Once our breakpoint is hit - Figure 25, follow it using F8 to the start of our located dummy code.
9. Generating Shellcode
Using all the information that we've gathered we can now generate the payload as shown below: