I am trying to stay on top of not only completing HTB machines but posting my walkthroughs as well. It takes a lot of work to put everything together, but my intention is to share my methodology and hopefully help someone along the way.
Today, we will be walking through 'Headless', an easy-rated machine on HTB created by dvir1. The machine begins with identifying an XSS vulnerability to steal an administrator's cookie. This stolen cookie is then used to access a separate page vulnerable to code injection. Finally, a Bash script was abused to escalate privileges to root.
Without further ado, let's do this thing.
Enumeration
I started by running an Nmap scan across all ports to identify the services running on the host. Based on the results of the first scan, I ran a more targeted Nmap scan, focusing on the open ports discovered in the initial scan.
Two ports were found: 22 and 5000. Let's run a more targeted Nmap scan against just these open ports.
22 - SSH
I am going to skip enumerating SSH for now. There is not much we can do with it right now without some user credentials or SSH keys.
5000 - HTTP
A web server running Werkzeug with an under-construction page for the index page. The first thing I wanted to check when running Werkzeug was to see if debugging was active via /console, but sadly, it was not.
There was a button for questions that led to a contact support page with a form for us to fill out.
When the form was filled out with dummy data, there was no feedback showing a successful submission. To test for potential vulnerabilities, I used a basic XSS payload in the message field. If successful, this payload should trigger an alert message popup.
However, instead of a popup, I received a message indicating that a hacking attempt had been detected. It appears the application is configured to identify malicious payloads and alert someone if any are found.
While the content of the fields on the contact page was not reflected back to us, the headers were. Knowing that someone could look at these reports if we use naughty characters, we could use XSS to retrieve another cookie.
I filled out the form again, but this time, I intercepted the request in Burp Suite and modified the Referer header to include the same XSS payload I had previously used in the message field.
This triggered the 'hacking detected' message again. Triggering this message was important because it means the report is sent to someone for review, potentially executing our XSS payload on their end.
After forwarding the modified request, the 'alert 1' popup appeared, confirming the execution of the XSS payload in the header. This confirms that while the message field in the form is not vulnerable, certain headers are.
Although I did not test this, the User-Agent header could also be tested to confirm if it is vulnerable as well.
I filled out the contact support form again, using the same message as before. This time, I used an XSS payload in the Referer header that attempted to load an image from my Kali VM was set to include the cookie in the request.
Reading the is_admin cookie this way was possible as it was set without the HttpOnly flag.
If successful, I would see the HTTP request come through to my Kali VM, along with a different cookie than the one I already had.
After forwarding the request, I got exactly what I wanted to see, the cookie of the assumed administrator.
We know this is an administrator cookie because decoding the first part of the Base64-encoded cookie returns admin, whereas previously it was user.
Original Cookie:
New Cookie:
Finding /Dashboard Directory
So we have a new cookie - now what? We need to figure out where we can use it.
Turning to directory fuzzing with ffuf, I found a new directory called /dashboard along with /support which we already knew about.
Going to this directory returned an unauthorized error. Darn.
Using the developer tools in Firefox, I confirmed this is where the is_admin cookie is referenced. I replaced the existing cookie with the one obtained by sending the XSS payload via the support form. After refreshing the page, the administrator dashboard successfully loaded
Generating the report returns that all systems are up and running.
If this same request is intercepted in Burp Suite, there is only one parameter - date.
Abusing Command Injection for Foothold
I started testing various payloads and found this field is vulnerable to command injection by appending the whoami and hostname commands.
date=2023-09-15;whoami;hostname
Using the command injection to get a list of users on the box via /etc/passwd.
date=2023-09-15;cat /etc/passwd|grep /bin/bash
With arbitrary code execution confirmed, I used a Base64-encoded Bash one-liner to get a reverse shell.
Sending the request resulted in a reverse shell as the dvir user.
Grabbing the user flag.
Userland Enumeration
I found an interesting email in /var/mail/dvir that describes a systems check script and that we were given special privileges to use it.
Following the slight hint in the email, I checked the sudo privileges for dvir. Sure enough, they can run /user/bin/syscheck with sudo privileges. This sounds familiar…
This script does a decent job of referencing binaries by their absolute paths. However, there is a reference to a file called initdb.sh that is the only one referenced by a relative path.
Privlege Escalation via /usr/bin/syscheck
As the syscheck script references the initdb.sh file in the current working directory and we can run the script with sudo privileges, we can create this file wherever we want, and the script will use it.
I created a new intidb.sh file that copies /bin/dash to /tmp/dash and then marks the copied dash binary with SUID bit set. I granted executable rights to the initdb.sh script as it is being invoked by the syscheck script.
After running /usr/bin/syscheck with sudo privileges, there is a new dash binary with SUID bits set in /tmp. This is the work of our malicious initdb.sh file.
Getting a root shell is trivial with this dash binary.
This box was an interesting opportunity to flex your web application hacking skills, featuring both an XSS vulnerability and a command injection vulnerability. The privilege escalation phase was interesting, especially to stress the importance of absolute versus relative paths and their potential for exploitation. Hope you enjoyed this writeup!