SQL注入之二次注入(sql-lab第24关)
生活随笔
收集整理的這篇文章主要介紹了
SQL注入之二次注入(sql-lab第24关)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
什么是二次注入
二次注入可以理解為,攻擊者構造的惡意數據存儲在數據庫后,惡意數據被讀取并進入到SQL查詢語句所導致的注入。防御者可能在用戶 輸入惡意數據時對其中的特殊字符進行了轉義處理,但在惡意數據插入到數據庫時被處理的數據又被還原并存儲在數據庫中,當web程序調用存儲在數據庫中的惡意數據并執行SQL查詢時,就發生了SQL二次注入。
二次注入原理分為兩步
二次注入,可以概括為以下兩步:
第一步:插入惡意數據
進行數據庫插入數據時,對其中的特殊字符進行了轉義處理,在寫入數據庫的時候又保留了原來的數據。
第二步:引用惡意數據
開發者默認存入數據庫的數據都是安全的,在進行查詢的時候,直接從數據庫中取出惡意數據,沒有進行進一步的檢驗的處理。
二次注入的過程
找到注入點
然后構造注入語句
以sql-lab第24關為例:
可以看到一個輸入框——》注冊一個新用戶
可以看到有一個abcd用戶,密碼是abcd
username:abcd’#(單引號來閉合前面的單引號,#注釋后面內容)
password:1234
登錄:
username:abcd’#
password:1234
登錄后出現如下效果:you are logged in
查看一下
后臺代碼
new_user.php
<?php include '../sql-connections/sql-connect.php' ; ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <title><?php echo $feedback_title_ns; ?> </title> </head><body bgcolor="#000000"> <div align="right"> <a style="font-size:.8em;color:#FFFF00" href='index.php'><img src="../images/Home.png" height='45'; width='45'></br>HOME</a> </div> <font size="3" color="#FFFF00"> <div style="text-align:center"><form name="mylogin" method="POST" action="login_create.php"><h2 style="text-align:center;background-image:url('../images/Less-24-new-user.jpg');background-repeat:no-repeat;background-position:center center"> <div style="padding-top:300px;text-align:center;color:#FFFF00;"><?php echo $form_title_ns; ?></div> </h2><div align="center"> <table style="margin-top:50px;"> <tr> <td style="text-align:right"> <font size="3" color="#FFFF00"> <strong>Desired Username:</strong></font> </td><td style="text-align:left"><input name="username" id="username" type="text" value="" /> </td> </tr> <tr> <td style="text-align:right"> <font size="3" color="#FFFF00"><strong>Password:</strong> </font> </td> <td style="text-align:left"><input name="password" id="password" type="password" value="" /> </td> </tr><tr> <td style="text-align:right"> <font size="3" color="#FFFF00"> <strong>Retype Password:</strong> </font> </td> <td style="text-align:left"> <input name="re_password" id="re_password" type="password" value="" /> </td> </tr><tr> <td colspan="2" style="text-align:right"> <input name="submit" id="submit" type="submit" value="Register" /><br/><br/> </td> </tr></table> </div> </form> </div> </body> </html>index.php
<?PHP session_start(); if (isset($_SESSION['username']) && isset($_COOKIE['Auth'])) {header('Location: logged-in.php'); } ?> <?php //including the Mysql connect parameters. include("../sql-connections/sql-connect.php"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <title>Less-24 - Second Degree Injections </title> </head> <body bgcolor="#000000"><div style="text-align:center"> <form name="login" method="POST" action="login.php"><h2 style="text-align:center;background-image:url('../images/Less-24.jpg');background-repeat:no-repeat;background-position:center center"> <div style="padding-top:300px;text-align:center;color:#FFFF00;"><?php echo $form_title_in; ?></div> </h2><div align="center"> <table style="margin-top:50px;"> <tr> <td style="text-align:right"> <font size="3" color="#FFFF00"> <strong>Username:</strong> </td> <td style="text-align:left"> <input name="login_user" id="login_user" type="text" value="" /> </td> </tr> <tr> <td style="text-align:right"> <font size="3" color="#FFFF00"> <strong>Password:</strong> </td> <td style="text-align:left"> <input name="login_password" id="login_password" type="password" value="" /> </td> </tr> <tr> <td colspan="2" style="text-align:right"> <input name="mysubmit" id="mysubmit" type="submit" value="Login" /><br/><br/><a style="font-size:.8em;color:#FFFF00" href="forgot_password.php">Forgot your password?</a><font size="3" color="#FFFF00"> ||</font> <a style="font-size:.8em;color:#FFFF00" href="new_user.php">New User click here?</a> </td> </tr></table> </div> </form> </div> </body> </html>login.php
<html> <head> </head> <body bgcolor="#000000"> <font size="3" color="#FFFF00"> <div align="right"> <a style="font-size:.8em;color:#FFFF00" href='index.php'><img src="../images/Home.png" height='45'; width='45'></br>HOME</a> </div> <?PHPsession_start(); //including the Mysql connect parameters. include("../sql-connections/sql-connect.php");function sqllogin(){$username = mysql_real_escape_string($_POST["login_user"]);$password = mysql_real_escape_string($_POST["login_password"]);$sql = "SELECT * FROM users WHERE username='$username' and password='$password'"; //$sql = "SELECT COUNT(*) FROM users WHERE username='$username' and password='$password'";$res = mysql_query($sql) or die('You tried to be real smart, Try harder!!!! :( ');$row = mysql_fetch_row($res);//print_r($row) ;if ($row[1]) {return $row[1];} else {return 0;}}$login = sqllogin(); if (!$login== 0) {$_SESSION["username"] = $login;setcookie("Auth", 1, time()+3600); /* expire in 15 Minutes */header('Location: logged-in.php'); } else { ?> <tr><td colspan="2" style="text-align:center;"><br/><p style="color:#FF0000;"> <center> <img src="../images/slap1.jpg"> </center> </p></td></tr> <?PHP } ?></body> </html>pass_change.php
<html> <head> </head> <body bgcolor="#000000"> <?PHP session_start(); if (!isset($_COOKIE["Auth"])) {if (!isset($_SESSION["username"])) {header('Location: index.php');}header('Location: index.php'); } ?> <div align="right"> <a style="font-size:.8em;color:#FFFF00" href='index.php'><img src="../images/Home.png" height='45'; width='45'></br>HOME</a> </div> <?php//including the Mysql connect parameters. include("../sql-connections/sql-connect.php");if (isset($_POST['submit'])) {# Validating the user input........$username= $_SESSION["username"];$curr_pass= mysql_real_escape_string($_POST['current_password']);$pass= mysql_real_escape_string($_POST['password']);$re_pass= mysql_real_escape_string($_POST['re_password']);if($pass==$re_pass){ $sql = "UPDATE users SET PASSWORD='$pass' where username='$username' and password='$curr_pass' ";$res = mysql_query($sql) or die('You tried to be smart, Try harder!!!! :( ');$row = mysql_affected_rows();echo '<font size="3" color="#FFFF00">';echo '<center>';if($row==1){echo "Password successfully updated";}else{header('Location: failed.php');//echo 'You tried to be smart, Try harder!!!! :( ';}}else{echo '<font size="5" color="#FFFF00"><center>';echo "Make sure New Password and Retype Password fields have same value";header('refresh:2, url=index.php');} } ?> <?php if(isset($_POST['submit1'])) {session_destroy();setcookie('Auth', 1 , time()-3600);header ('Location: index.php'); } ?> </center> </body> </html> $sql = "UPDATE users SET PASSWORD='$pass' where username='$username' and password='$curr_pass' ";abcd'#上面這條語句就會變成:
update users set password='aaaa' where username='abcd'# and password=xxxx#把后面語句全部注釋掉了
先插入數據——》再引用插入數據
防御:
過濾危險字符
采用PDO編程
總結
以上是生活随笔為你收集整理的SQL注入之二次注入(sql-lab第24关)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 客座编辑:崔辰州(1976-),男,博士
- 下一篇: linux cmake编译源码,linu