<?php
$conn=@mysql_connect("localhost",'root','') or die("数据库连接失败!");;
mysql_select_db("injection",$conn) or die("您要选择的数据库不存在");
$name=$_POST['username'];
$pwd=$_POST['password'];
$sql="select * from users where username='$name' and password='$pwd'";
$query=mysql_query($sql);
$arr=mysql_fetch_array($query);
if(is_array($arr)){
header("Location:manager.php");
}else{
echo "您的用户名或密码输入有误,请重新登录!";
}
?>
每个程序都会有一些验证机制,上述是一种简单的的验证机制(特指可以被万能语句攻破)在我们登陆页面有:
输入用户名:“username"
输入密码:”password"
name就是输入的用户名,pwd 就是输入的密码(“$”表示变量,存储)
$sql="select * from users where username='$name' and password='$pwd'";
当我们输入正确的用户名与密码就可以登陆进去,但是这个验证机制也可以通过SQL语句来构造特殊的字符串来通过验证。
例如:'or 1=1#
:再用户名中输入:'or 1=1#带入到select *from users where username='... 'and password='.... ';
就变成了:select *from users where username='’or 1=1 # 'and password=' ...';
#为注释,不执行#后面的语句:
select *from users where username='’or 1=1
”where“表示条件,为真则执行,验证成功,为假则不执行,验证失败。
"or"是或的意思,根据离散数学的逻辑判断规则,可知一真一假为真,1=1为真,所以语句等价为select *from users,查询出表中所有的字段
再例如:1’or 1=1 #
select *from users where username='... 'and password='....
select *from users where username'=1’or 1=1 #'and password='....
select *from users where username'=1’or 1=1
select *from users