> 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/web/sql-injection/union-attack-determining-number-of-columns.md).

# UNION Attack Column Count Detection

UNION Attack Column Count Detection notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### SQL Injection UNION Attack, Determining the Number of Columns Returned by the Query

* **Background:** `category` filter vulnerable to SQL injection; The application response includes SQL results, so a `UNION` allows to inject an additional visible line.
* **Objective:** find the exact number of columns returned by the original query to build compatible `UNION SELECT`.
* **Quick method:**

1. **Test with `ORDER BY`** — increment the index until an error occurs (the error indicates that the index exceeds the number of columns):

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

* When `ORDER BY N` causes an error, the number of columns is `< N`.

2. Confirm by injecting an UNION line containing NULL — build a SELECT UNION with the same number of columns; if the application shows the injected line (null values), the number is correct. Example (3 columns):

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

* If the row appears, the original query returns 3 columns. Otherwise, adjust the number of `NULL` and retest.

**Variantes utiles :**

* Use constants to locate the displayed column (useful if only some columns are returned):

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

(Lets visually identify which columns of the response correspond to the injected columns.)

* If UNION fails due to types, replace some constants with CAST(NULL AS VARCHAR2) / NULL according to DBMS, or adapt types with CAST/TO/ CHAR.

Expected result: an additional row containing NULL (or constants) will display → number of columns determined.


---

# 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/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.
