> 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

Notas sobre Detecção da Contagem de Colunas em Ataque UNION para laboratórios da PortSwigger Web Security Academy, incluindo fluxo de exploração, teste de payloads, validação no Burp Suite e contexto de remediação.

### Ataque SQL Injection UNION, Determinando o Número de Colunas Retornadas pela Consulta

* **Contexto:** `categoria` 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:** encontre o número exato de colunas retornadas pela consulta original para construir uma consulta compatível `UNION SELECT`.
* **Método rápido:**

1. **Teste com `ORDER BY`** — incremente o índice até que ocorra 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 — construa 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'-- -
  ```

(Isso 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 conforme 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 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/pt-br/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.
