Web網(wǎng)絡(luò)安全分析二次注入攻擊原理詳解
二次注入攻擊
二次注入攻擊的測試地址:http://127.0.0.1/sqli/double1.php?username=test 和 http://127.0.0.1/sqli/double2.php?id=1。 其中,double1.php頁面的功能是注冊用戶名,也是插入SQL語句的地方;double2.php頁面的功能是通過參數(shù)ID讀取用戶名和用戶信息。
第一步,訪問double1.php?username=test',如圖40所示。
圖40 注冊用戶名test'
從頁面返回結(jié)果可以看到用戶名test'對用的ID為9,訪問double2.PHP?id=9,結(jié)果如圖41所示。
圖40 訪問test'的信息
從返回結(jié)果可以看到服務(wù)端返回了MySQL的錯誤(多了一個單引號引起的語法錯誤),這時回到第一步,先訪問double1.php?username=test' order by 1--+,獲取一個新的id=10,當(dāng)再次訪問double2.php?id=10時,頁面返回空白;再次嘗試,訪問double1.php?username=test' order by 10--+,獲取一個新的id=11,當(dāng)再次訪問double2.php?id=11時,頁面返回錯誤信息(Unknown column ‘10' in ‘order clause'),如圖42所示。
圖42 訪問order by 10的結(jié)果
這說明空白頁面就是正常返回,通過不斷地嘗試,筆者判斷出數(shù)據(jù)庫中一共有3個字段。
訪問double1.php?username=test' union select 1,2,3--+,獲取一個新id=12,再訪問double2.php?id=12,發(fā)現(xiàn)頁面返回了union select中的1和2字段,結(jié)果如圖43所示。
圖43 使用Union語句的結(jié)果
在2或3的位置,插入我們的語句,比如訪問double1.php?username=test' union select 1,user(),3--+,獲得新的id=13,再訪問double2.php?id=13,得到user()的結(jié)果,如圖44所示,使用此方法就可以獲取數(shù)據(jù)庫中的數(shù)據(jù)。
圖44 利用二次注入獲取數(shù)據(jù)
二次注入代碼分析
二次注入中double1.php頁面的代碼如下所示,實現(xiàn)了簡單的用戶注冊功能,程序獲取到GET參數(shù)username的參數(shù)password,然后將username和password拼接到SQL語句,使用insert語句插入到數(shù)據(jù)庫中。由于參數(shù)username使用addslashes進(jìn)行轉(zhuǎn)義(轉(zhuǎn)義了單引號,導(dǎo)致單引號無法閉合),參數(shù)password進(jìn)行了MD5哈希,所以此處不存在SQL注入漏洞。
<?php header('Content-type:text/html;charset=utf-8');
$con=mysqli_connect("localhost","root","root","test");
if (mysqli_connect_errno())
{
echo "連接失敗: " . mysqli_connect_error();
}
$username = @$_GET['username'];
$password = @$_GET['password'];
$result = mysqli_query($con,"insert into users(`username`,`password`) values ('".addslashes($username)."','".md5($password)."')");
echo "新的id為:".mysqli_insert_id($con);
?>
當(dāng)訪問username=test'&password=123456時,執(zhí)行的SQL語句為:
insert into users(`username`,`password`) values ('test\'','e10adc3949ba59abbe56e057f20f883e')
從圖45中的數(shù)據(jù)庫里可以看到,插入的用戶是test'。
圖45 插入到數(shù)據(jù)庫中的數(shù)據(jù)
在二次注入中double2.php中的代碼如下所示。首先將GET參數(shù)ID轉(zhuǎn)換成int類型(防止拼接到SQL語句時,存在SQL注入漏洞),然后到users表中獲取ID對應(yīng)的username,接著到person表中查詢username對應(yīng)的數(shù)據(jù)。
<?php header('Content-type:text/html;charset=utf-8');
$con=mysqli_connect("localhost","root","root","test");
if (mysqli_connect_errno())
{
echo "連接失敗: " . mysqli_connect_error();
}
$id = intval(@$_GET['id']);
$result = mysqli_query($con,"select * from users where `id`=".$id);
$row = mysqli_fetch_array($result);
$username = $row['username'];
$result2 = mysqli_query($con,"select * from person where `username`='".$username."'");
if (!$result2)
{
echo mysqli_error($con);
exit();
}
if($row2 = mysqli_fetch_array($result2))
{
echo $row2['username'] . " : " . $row2['money'];
}
else
{
echo mysqli_error($con);
}
?>
但是此處沒有對$username進(jìn)行轉(zhuǎn)義,在第一步中我們注冊的用戶名是test',此時執(zhí)行的SQL語句為:
select * from users where `username`='test''
單引號被帶入SQL語句中,由于多了一個單引號,所以頁面會報錯。
以上就是Web網(wǎng)絡(luò)安全分析二次注入攻擊原理詳解的詳細(xì)內(nèi)容,更多關(guān)于Web網(wǎng)絡(luò)安全二次注入攻擊的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
無線網(wǎng)攻擊工具進(jìn)攻方法及防范技巧小結(jié)
對無線網(wǎng)安全攻防有興趣的人應(yīng)該都需要一套工具,英特網(wǎng)上有很多免費的工具。本文不求全面,但求能提供一些指導(dǎo)和建議。2008-02-02
網(wǎng)絡(luò)安全流量分析工具蟻劍詳細(xì)介紹
蟻劍(AntSword)是一款開源的跨平臺WebShell管理工具,它主要面向于合法授權(quán)的滲透測試安全人員以及進(jìn)行常規(guī)操作的網(wǎng)站管理員,蟻劍推崇模塊化的開發(fā)思想,遵循開源,就要開得漂亮的原則,致力于為不同層次的人群提供最簡單易懂、方便直接的代碼展示及其修改說明2022-09-09
Web網(wǎng)絡(luò)安全解析cookie注入攻擊原理
這篇文章主要為大家介紹了Web網(wǎng)絡(luò)安全分析cookie注入攻擊原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
網(wǎng)絡(luò)安全常見問題隱患及其應(yīng)對措施詳解
這篇文章主要為大家介紹了網(wǎng)絡(luò)安全常見問題隱患及其應(yīng)對措施詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
16進(jìn)制的轉(zhuǎn)換之javascript運行時會自動轉(zhuǎn)碼
16進(jìn)制的轉(zhuǎn)換之javascript運行時會自動轉(zhuǎn)碼...2007-05-05

