This is my writeup and walkthrough for Obscurity from Hack The
Box.
A medium linux machine with some vulnerable
Python scripts from HTB just
retired with the following:
Misconfigured vulnerable Python web server to
RCE.
Weak encryption algorithm to sign two messages, some text and
the user password file.
Vulnerable script to get the root password hash from the
shadow file.
Enumeration
1. Nmap
As always let’s add
10.10.10.168
to our
/etc/hosts
file as
obscure.htb
and start scan the host with
nmap
to see the open ports and services running on the machine:
As we see there are 4 ports two of them are open
22
running OpenSSH 7.6p1 and it tells us the OS
version is Ubuntu, and
8080
running a http-proxy called BadHTTPServer with
some successful request headers, so let’s jumb into the browser
to see what is it.
2. Web Recon
It looks like a simple web page with some text and I noticed
there is some contact info and an interesting message at the
bottom of the page.
The message tells us there is some secret development directory
that contains the source code of the running server
SuperSecureServer.py, so first thing came to my mind is to run any directory
brutceforcing tools like
gobuster.
But this did not work, so I can remember from my experience in
software development the main source code is usually in some
sort of well known folders like
/src,
/app,
/bin
…etc, and as we saw from the message above a lot of
development
word. I tried some of sub-words of that name with the file and
it worked with
/develop/SuperSecureServer.py.
Let’s download the file and see how the server source code loo
like.
Simply, the given code contains three classes:
Response class to handle parse the responses
with the given data.
Request class also to parse the incomming
requests.
Server class the actual listeining main
server with some functions to handle the web requests and
documents (pages).
After analysing the code, we noticed some interesting lines in
Server class that may lead to get an
RCE on the machine.
The function
serveDoc
takes two inputs
path
from the web page and
docRoot
the documents root folder on the box. Then, the
handleRequest
function takes three arguments
request
the incoming request,
conn
the client connection object, and
address
the host and port of the client. After that it calls the
serveDoc
function with the request path and document folder
DocRoot.
After the
serveDoc
function takes the inputs, URL decode the
path
and it calls
exec
function with
output = 'Document: {path}'. And we know that we can control the path variable from the
web page, so now first thing we should do is to test this in our
machine to understand how it works then try it in the remote
host.
Exploitation
I made two lines of code to connect to the server locally.
Also let’s create some files (pages) to imitate the actual
server. Now, we should bypass the special char
' then
try to excute some commands. It’s ok if you stuck at this point
for some time just to understand how to exploit it.
Firstly, let’s try some paths to test the server then jump into
the exploit. In the exploit we can add a single quote and a
semicolon to terminate the string, then add some
Python code to execute and a hash to comment
anything after that command as following
';print(5+5)#, so let’s encode it and send it with
burp.
I used Burp Suite to make the requests you can
use any tool you want.
Oh! that’s cool it executed the
print(5+5)
and it prints
10 to
the terminal. Now, let’s get some Python one
line reverse shell, put it instead of
print,
and exploit the remote server.
Here’s our payload:
And after executing the payload we gained a shell as
www-data.
Privilege Escalation
1. Own User
In this part let’s dig deep into the system and search for any
things that seem important to escalate our privilege. In the
/home/robert
folder there are some interesting readable files.
After copying the files using
netcat
into our machine. The first file
SuperSecureCrypt.py
a simple Python encryption script to encrypt
data with a given key.
After looking at the code It’s just a simple ceaser cipher with
a text key instead of a number.
The second file
check.txt
it’s a message that says:
The file
out.txt
is the encryption of
check.txt, so simply we can retrieve the key be decoding the encrypted
data with the original text as a key. And after that, we can
decrypt the
passreminder.txt
file to get the user password.
I made a simple script to decrypt the file anf get the user
password:
After running the script we got some readable text and some
garbage:
And now we owned user
robert:
2. Own Root
After we got user there is another interesting readable file
/home/robert/BetterSSH/BetterSSH.py
owned by root.
The above script just mimics the SSH connection
with the actual system users by reading the username and compare
the password with the actual one in the
/etc/shadow
file. If it’s true it gives a shell to the user, if it’s not it
aborts.
But, the deadly bug in the script is when it copies the contents
of the shadow file into a random file in
/tmp/SSH
directory and sleep
0.1s,
then delete it. That’s so nice, we can take advantage of that by
trying to read the contents of the random file before the
deletion process and copy it into our machine.
There are many ways to exploit this bug one of them is to use
watch
command, but I prefer to write some code, so let’s write some
bash
script to print the contents of the random file in the terminal.
And we got the root hash, let’s crack it with our friend
john.