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

# Détection du nombre de colonnes pour une attaque UNION

### Injection SQL UNION, déterminer le nombre de colonnes retournées par la requête

* **Contexte :** `category` filtre vulnérable à l'injection SQL ; la réponse de l'application inclut des résultats SQL, donc une `UNION` permet d'injecter une ligne visible supplémentaire.
* **Objectif :** trouver le nombre exact de colonnes renvoyées par la requête d'origine pour construire des éléments compatibles `UNION SELECT`.
* **Méthode rapide :**

1. **Tester avec `ORDER BY`** — incrémentez l'index jusqu'à ce qu'une erreur se produise (l'erreur indique que l'index dépasse le nombre de colonnes) :

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

* Lorsque `ORDER BY N` provoque une erreur, le nombre de colonnes est `< N`.

2. Confirmez en injectant une ligne UNION contenant NULL — construisez un SELECT UNION avec le même nombre de colonnes ; si l'application affiche la ligne injectée (valeurs nulles), le nombre est correct. Exemple (3 colonnes) :

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

* Si la ligne apparaît, la requête d'origine renvoie 3 colonnes. Sinon, ajustez le nombre de `NULL` et recommencez le test.

**Variantes utiles :**

* Utilisez des constantes pour localiser la colonne affichée (utile si seules certaines colonnes sont renvoyées) :

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

(Permet d'identifier visuellement quelles colonnes de la réponse correspondent aux colonnes injectées.)

* Si UNION échoue à cause des types, remplacez certaines constantes par CAST(NULL AS VARCHAR2) / NULL selon le SGBD, ou adaptez les types avec CAST/TO/ CHAR.

Résultat attendu : une ligne supplémentaire contenant NULL (ou des constantes) s'affichera → nombre de colonnes déterminé.


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/fr/web/sql-injection/union-attack-determining-number-of-columns.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
