SQL注入之堆叠注入(sql-lab第38关)
什么是堆疊注入
在SQL中,分號(;)是用來表示一條SQL語句結束的。試想一下我們在分號結束一個SQL語句后繼續構造下一條語句,會不會一起執行?因此這個想法也就造就了堆疊注入。而堆疊注入可以執行的是任意語句(增刪改查)。
例如以下這個例子:
用戶輸入:1; delete from products服務器端生成的SQL語句為:select * from products where productid=1; delete from products當執行查詢之后,第一條顯示查詢信息,第二條則是將整個表刪除。
適用條件
用戶采用PDO編程( POD(PHP Data Object))且沒有對參數進行過濾
mysqli_multi_query()函數也可以造成堆疊注入
堆疊注入的過程
1、找到注入點(確定是否存在堆疊注入)
2、開始注入
我們本次嘗試的語句:
1’;insert into users(id,username,password) values(‘100’,‘abc’,‘abc’) --+
這里以sql-lab第38關為例
http://192.168.3.10/sqli/Less-38/?id=1——》有回顯
http://192.168.3.10/sqli/Less-38/?id=1’ --+ ——》仍然有回顯
基本可以判斷它是一個字符型
在沒有插入之前,利用phpstudy后臺的命令行先查看表中有哪些數據
嘗試堆疊注入:
http://192.168.3.10/sqli/Less-38/?id=1’;insert into users(id,username,password) values(‘10000’,‘abcd’,‘abcd’) --+
插入后再查看一下:發現插入了id=10000的數據
當把insert into 語句 改成delete語句就可以把users表刪掉,所以堆疊注入危害很大。
堆疊注入代碼分析
<?php error_reporting(0); include("../sql-connections/db-creds.inc"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Less-38 **stacked Query**</title> </head><body bgcolor="#000000"> <div style=" margin-top:70px;color:#FFF; font-size:23px; text-align:center">Welcome <font color="#FF0000"> Dhakkan </font><br> <font size="3" color="#FFFF00"><?php// take the variables if(isset($_GET['id'])) { $id=$_GET['id']; //logging the connection parameters to a file for analysis. $fp=fopen('result.txt','a'); fwrite($fp,'ID:'.$id."\n"); fclose($fp);// connectivity //mysql connections for stacked query examples. $con1 = mysqli_connect($host,$dbuser,$dbpass,$dbname); // Check connection if (mysqli_connect_errno($con1)) {echo "Failed to connect to MySQL: " . mysqli_connect_error(); } else {@mysqli_select_db($con1, $dbname) or die ( "Unable to connect to the database: $dbname"); }$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1"; /* execute multi query */ if (mysqli_multi_query($con1, $sql)) {/* store first result set */if ($result = mysqli_store_result($con1)){if($row = mysqli_fetch_row($result)){echo '<font size = "5" color= "#00FF00">'; printf("Your Username is : %s", $row[1]);echo "<br>";printf("Your Password is : %s", $row[2]);echo "<br>";echo "</font>";} // mysqli_free_result($result);}/* print divider */if (mysqli_more_results($con1)){//printf("-----------------\n");}//while (mysqli_next_result($con1)); } else {echo '<font size="5" color= "#FFFF00">';print_r(mysqli_error($con1));echo "</font>"; } /* close connection */ mysqli_close($con1);}else { echo "Please input the ID as parameter with numeric value";}?> </font> </div></br></br></br><center> <img src="../images/Less-38.jpg" /></center> </body> </html>
這個函數造成了堆疊注入
防御:對參數進行一定的過濾
盡量避免使用mysqli_multi_query()函數
總結
以上是生活随笔為你收集整理的SQL注入之堆叠注入(sql-lab第38关)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iap如何初始化_IAP在线升级模块详细
- 下一篇: 客座编辑:崔辰州(1976-),男,博士