> 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-determining-number-of-columns.md).

# UNION攻撃のカラム数検出

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

### SQLインジェクションのUNION攻撃、クエリが返す列数の特定

* **背景:** `カテゴリ` SQLインジェクションに脆弱なフィルタ; アプリケーションの応答にはSQL結果が含まれているため、 `UNION` 追加の表示行を注入できます。
* **目的：** 互換性のある `UNION SELECT`.
* **簡単な方法:**

1. **でテストする `ORDER BY`** — エラーが発生するまでインデックスを増やす（そのエラーは、インデックスが列数を超えたことを示す）:

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

* 〜の場合 `ORDER BY N` がエラーを引き起こす場合、列数は `< N`.

2. NULL を含む UNION 行を注入して確認する — 同じ列数の SELECT UNION を作成する。アプリケーションが注入された行（NULL 値）を表示すれば、その数は正しい。例（3列）:

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

* その行が表示されれば、元のクエリは3列を返しています。そうでなければ、 `NULL` の数を調整して再試行してください。

**有用なバリエーション:**

* 表示される列を特定するために定数を使う（返される列が一部だけの場合に便利）:

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

（応答のどの列が注入された列に対応するかを視覚的に識別できます。）

* 型の違いで UNION が失敗する場合は、DBMS に応じて一部の定数を CAST(NULL AS VARCHAR2) / NULL に置き換えるか、CAST/TO/ CHAR で型を合わせてください。

期待される結果: NULL（または定数）を含む追加の行が表示される → 列数が判定される。


---

# 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-determining-number-of-columns.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.
