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

# Detecção da Contagem de Colunas em Ataque UNION

### Ataque de injeção SQL UNION, determinando o número de colunas retornadas pela consulta

* **Contexto:** `category` filtro vulnerável à injeção SQL; a resposta da aplicação inclui resultados SQL, então uma `UNION` permite injetar uma linha visível adicional.
* **Objetivo:** descobrir o número exato de colunas retornadas pela consulta original para construir uma compatível `UNION SELECT`.
* **Método rápido:**

1. **Teste com `ORDER BY`** — incremente o índice até ocorrer um erro (o erro indica que o índice excede o número de colunas):

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

* Quando `ORDER BY N` causa um erro, o número de colunas é `< N`.

2. Confirme injetando uma linha UNION contendo NULL — crie um SELECT UNION com o mesmo número de colunas; se a aplicação mostrar a linha injetada (valores nulos), o número está correto. Exemplo (3 colunas):

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

* Se a linha aparecer, a consulta original retorna 3 colunas. Caso contrário, ajuste o número de `NULL` e teste novamente.

**Variantes úteis:**

* Use constantes para localizar a coluna exibida (útil se apenas algumas colunas forem retornadas):

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

(Permite identificar visualmente quais colunas da resposta correspondem às colunas injetadas.)

* Se o UNION falhar devido aos tipos, substitua algumas constantes por CAST(NULL AS VARCHAR2) / NULL de acordo com o SGBD, ou adapte os tipos com CAST/TO/ CHAR.

Resultado esperado: uma linha adicional contendo NULL (ou constantes) será exibida → número de colunas determinado.


---

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