我正在尝试创建一个登录页面来演示一个简单的SQL注入攻击。 下面是从login.html接收输入的php代码:
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'sql-injection');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=@mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
/* $ID = $_POST['user']; $Password = $_POST['pass']; */
function SignIn()
{
session_start(); //starting the session for user profile page
if(!empty($_POST['user']) and !empty($_POST['pass'])) //checking the username and password which is coming from Sign-In.html, are they empty or have some text
{
$query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]'") or die(mysql_error());
$row = mysql_fetch_array($query) or die(mysql_error());
if(!empty($row['userName']) AND !empty($row['pass']))
{
if ($_POST['pass'] == $row['pass'])
{
echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";
}
else
{
echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY...";
}
}
else
{
echo "The db is corrupt";
}
}
else{
echo "The username and password fields cant be empty";
}
}
if(isset($_POST['submit']))
{
SignIn();
}
?>
db有一个表用户名,列
UsernameID userName pass
1 mrboolnew mrbool123
我想在用户名栏中输入:
Y'; UPDATE username SET userName ='mrboolnew1'WHERERE userName ='mrboolnew';
但我得到以下错误:
您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'UPDATE username SET userName ='mrboolnew1'附近使用正确的语法'WHERE userName ='mrboolnew';''在第1行
有人可以帮我解决注射问题。 提前谢谢。
答案 0 :(得分:6)
您已添加了防止注入攻击的保护,可能甚至不知道。mysql_query()
只向数据库发送一个查询(请参阅here)。不允许使用分号;也不是多个查询。
您必须更加努力地注入自己的代码。