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

# Detección del número de columnas en un ataque UNION

Notas de Detección del número de columnas en un ataque UNION para laboratorios de PortSwigger Web Security Academy, incluido el flujo de explotación, pruebas de payloads, validación con Burp Suite y contexto de remediación.

### Ataque UNION de inyección SQL, determinación del número de columnas devueltas por la consulta

* **Contexto:** `categoría` filtro vulnerable a la inyección SQL; la respuesta de la aplicación incluye resultados SQL, por lo que un `UNION` permite inyectar una línea visible adicional.
* **Objetivo:** encontrar el número exacto de columnas devueltas por la consulta original para construir una `UNION SELECT`.
* **Método rápido:**

1. **Probar con `ORDER BY`** — incrementa el índice hasta que ocurra un error (el error indica que el índice supera el número de columnas):

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

* Cuando `ORDER BY N` provoca un error, el número de columnas es `< N`.

2. Confírmalo inyectando una línea UNION que contenga NULL — construye un SELECT UNION con el mismo número de columnas; si la aplicación muestra la línea inyectada (valores nulos), el número es correcto. Ejemplo (3 columnas):

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

* Si la fila aparece, la consulta original devuelve 3 columnas. De lo contrario, ajusta el número de `NULL` y vuelve a probar.

**Variantes útiles:**

* Usa constantes para localizar la columna mostrada (útil si solo algunas columnas se devuelven):

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

(Permite identificar visualmente qué columnas de la respuesta corresponden a las columnas inyectadas.)

* Si UNION falla por tipos, reemplaza algunas constantes con CAST(NULL AS VARCHAR2) / NULL según el DBMS, o adapta los tipos con CAST/TO/ CHAR.

Resultado esperado: se mostrará una fila adicional que contenga NULL (o constantes) → número de columnas 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/es/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.
