> 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

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

* **Antecedentes:** `category` filtro vulnerable a 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 compatible `UNION SELECT`.
* **Método rápido:**

1. **Probar con `ORDER BY`** — incremente 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. Confirme inyectando una línea UNION que contenga NULL — construya 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, ajuste el número de `NULL` y pruebe de nuevo.

**Variantes útiles:**

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

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

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

* Si UNION falla debido a los tipos, reemplace algunas constantes con CAST(NULL AS VARCHAR2) / NULL según el DBMS, o adapte 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 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/es/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.
