Sandworm
Snadworm is a HackTheBox lab Linux machine released on 17/6/23 in Beta Season II.
This machine features:
GnuPG
Server-side Template Injection
Rust
Firejail Vulnerability
Reconnaissance
Port Scanning
A quick port scanning shows us there're ports 22, 80, and 443:
$ sudo nmap -p- -n -Pn -sS -T4 --min-rate 1000 -v 10.129.77.49 -oN ports.nmap
...
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp open https
...Hostname
The certificate returned tells us the hostname is ssa.htb.
We can also see the same information in the HTTP 301 response using curl.
We then add this hostname to our host file /etc/host.
No common vhosts can be found using ffuf.
Techniques
The footer suggests that the site is built on Flask.

URL Path
We can crawl hypertext and embedded links with the Python script like this.
More paths like admin, login, logout, view, and process can be found using ffuf.
Contact
The site allows users to submit PGP-encrypted messages with a guide about how to use PGP.

PGP Guide
In the guide, the site implements functionalities including PGP decrypting, encrypting, and verifying.
We can find the pgp in /pgp, save it to ssa.gpg, and import it for the recipient atlas@ssa.htb using gpg.
We can encrypt some content for atlas@ssa.htb.
We see that the message is decrypted successfully.

Unlike other functions, the site also implements javascript code for verifying signed messages.
Although the form action /guide/verify is still usable.

The response indicates that the site uses GnuPG in the backend.
Initial Access
Server-side Template Injection
Since the server will render the decrypted message controlled by us, we shall try to test if any server-side template injection vulnerability exists.

We've seen that the site is built using Flask; hence it may use Jinja as its template engine.
We first encrypt the message, where the string hi will be comment out in Jinja:
using the target's PGP and we submit through the encryption function.
We see the content is rendered successfully, which indicates that no template injection can be abused here.

We try to generate a keypair for the user named {#hi#}yes this time and we submit the message signed with this key through the verifying function.
We see the string {#hi#} disappears this time, which indicates that we've found a SSTI vulnerability.

Remote Code Execution
To abuse the vulnerability to achieve remote code execution, we shall generate keypairs with usernames like {{__import__('os').system('ls')}}.
To automate the process, we write the following script using python-gnupg and cmd modules:
We can also execute remote commands via Python code like request.application.__globals__.__builtins__.__import__('os').popen('ls').read() now:
We can get a reverse shell too.
Credential Hunting
We see the source of the site is under the path /var/www/html/SSA/SSA.
We can see the cause of the SSTI is the usage of render_template_string:

We found the MySQL credential atlas:GarlicAndOnionZ42:
We found the PGP key's passphrase $M1DGu4rD$:
We can use Python package sqlalchemy to access the MySQL database SSA.
We can then obtain a list of usernames and password hashes.
By formatting the hashes above as, for example, 12154640f87817559bd450925ba3317f93914dc22e2204ac819b90d60018bc1f:q0WZMG27Qb6XwVlZ, we can try to use hashcat to crack the password with mode 1460.
In file ~/.config/httpie/sessions/localhost_5000/admin.json, we found credential silentobserver:quietLiketheWind22.
We can login to the target using SSH with this credential now.

Privilege Escalation
Crate tipnet
We found a crate named tipnet located under directory /opt.
The compiled binary has SUID permission set.
We note that this crate uses an external crate logger located in /opt/crates/logger.
System Activities Monitoring
We can use the tool pspy64 to monitor system activities.
We see that the crate is compiled and executed by cargo run every 1 minute and 50 seconds by root using user atlas with mode e.
We also see that the crate's source codes under /opt/crates are overwritten in 10 seconds after the crate is executed.
We can also find that the credential we found early is put there deliberately.
User atlas
We found that we can modify the content of the source of the crate logger which is used in the SUID program tipnet owned by the user atalas.
So, we shall be able to execute commands as the user atlas, if we overwrite the source code /opt/crates/logger/src/lib.rs after the script /root/Cleanup/clean_c.sh being executed and before the next compilation triggered by CRON.
We insert the following code after the script /root/Cleanup/clean_c.sh being executed.
Then we shall receive a reverse shell later.
Firejail
We note that the user if in the group jailer, users of which can run the setuid-root program firejail to sandbox processes.
The version of the program firejail seems to suffer the local privilege escalation vulnerability.
By using the exploit, we can get the root user.

Miscellaneous
Last updated