Buffer Overflow Prep OSCP

To start always run Immunity Debugger as Administrator

After that we go to >> File >> Open > Open the binary oscp.exe, then run the program with Run. Then we connect using netcat:

nc $ip_victim 1337

We execute the commandHELP and then OVERFLOW1 test,as a result, the following should appear OVERFLO1 COMPLETE

Mona Configuration

Once the program is running, in the field at the bottom of the Immunity Debugger window we execute the following:

Fuzzing

On your Linux machine we create a script called fuzzer.py with the code:

We run the script and wait for the server to crack, noting the highest number of bytes we send.

Crash Replication & Controlling EIP

We create the exploit.py file with the following content:

Run the following command changing the value of "-l 2000", to the value at which the server crashed in Fuzzing

Copy the output text and paste it into the variable "payload" of exploit.py

On windows, we run the oscp.exe binary again (we have to do this every time we are going to run the exploit.py).

The script should crash the server again, but this time in the command box type the following (changing the distance value to the same value with which you generated the previous pattern)

Mona should show us a window with logs, we would specifically look for the following one:

Copy the last number and paste it into the variable offset of exploit.py. The variable payload and the one of the retn we add BBBB

Restart oscp.exe and run the file. Now the EIP record should be 42424242

Finding Bad Characters

We generate a bytearray, removing the null byte (\x00) by default.

!mona bytearray -b "\x00"

Now we generate a string of bad characters identical to the bytearray with the following code:

The same string is pasted as payload in exploit.py

Run the exploit.py script. When it crashes, we copy from the ESP registry the adress and use the following command in mona

!mona compare -f C:\mona\oscp\bytearray.bin -a <address>

A pop-up window will open indicating which are the bad charts but be careful, we only have to take the one following the previous one (\x00).

In this case we have the following possible bad chars: 00 07 2e 2f a0 a1

As we had previously taken 00, we should now take 07, so in the command box we would write

And we also have to remove it from the payload variable in the exploit.py.

What follows would be the same, run the exploit.py with the new payload, capture the ESP adress, compare the new bad chars and extract the one following the x07. This step should be repeated until the bad chars column does not show any more characters.

Finding a Jump Point

We would have to execute the following command that looks for all the "jmp esp" instructions without the characters we marked (our bad chars)

!mona jmp -r esp -cpb "\x00\x07\x2e\xa0"

As the program is a bit endian (Reverse) we should write the address in reverse, so that instead of staying \x62\x50\x11\xaf we should be left with \xaf\x11\x50\x62we update our variable retn =\xaf\x11\x50\x62 and also the variable padding = "\x90" * 16

Generate Payload

Now we generate our reverse shell using msfvenom specifying the bad chars so it doesn't include them

Then we paste it into the exploit.py payload so that it looks like this:

Exploit!

Configure netcat on listening port

nc -lvnp 6666

Execute the exploit.py and we should have our Reverse Shell.

Last updated