首页 > 技术文章 > sql 注入

wenson 2014-10-06 20:51 原文

sql 注入

第一种

语句:$sql="select * form users where username='$username' and password='$password'";

1.1使用万能密码注入: bb' or 1='1'

sql语句 :select * form users where username='aa' and password='bb' or 1='1';

1.2 使用万能用户名:aa' union select * form users;/*

sql语句:select * form users where username='aa' union select * form users;/* and password='bb';

/* 在mysql 中表示后面的不执行

 


第二种

语句:$sql="select * form users where username=$username and password=$password";


1.1数字型注入(int)

这种没有'',mysql 数据库会把你的输入当做数字对待,如果是字符串会爆错


1.1.1 使用万能密码注入:22 union select * form users; (char vachar having根据自己喜欢的注入,进行
猜解)

sql语句:(数字类型)select *form users where username=11 and password=22 union select * form
users;

 


1.1.2 使用万能用户名:11 union select * form users/*

sql语句:select *form users where username=11 union select * form users;/* and password=22;

/* 在mysql 中表示后面的不执行

 

 

防范:

1.1在服务器中配置:magic_quotes_gps=On php.ini中
第一种sql注入会失效。第二种依然可以使用
语句:$sql="select * form users where username='$username' and password='$password'";
magic_quotes_gps=On服务器会对所有的加入“\”转义

name=\'111'\ 黑客可以通过 char(98) ASCII码来转义

1.2 密码的比对
通过查询数据库的密码进行比对,如果密码正确的话,则可以通过。

1.3 使用PDO来解决注入
在php.ini文件开启pdo extension=php_pdo_mysql_dll 去掉即可

PDO代码:
$sql="select * form users where username=? and password=?";
创建一个pdo对象
$myPdo=new PDO("mysql:host=localhost;port=3306;dbname=pdo","root","root");
设置编码
$myPdo->exec("set name utf8");
预处理$sql
$pdoStatment=$myPdo->prepare($sql);
把接收到的用户名和密码填入
$pdoStatment->execute(array($username,$password));
取出结果
$res=$pdoStatement->fetch();

if(empty($res)){
echo"你的用户名或者密码错误<a herf="Login.php">返回</a>"
}else{
header("Location:MangeUsers.php");
}

 

 

搜索引擎的注意


$keyword=addslashes($keyWord);
$keyWord=str_replace("%","\%",$keyWord);
$keyWord=str_replace("_","\_",$keyWord);

 

 

 

 

本文来自:清远大学城网实习岗位(www.qydxc.com)

推荐阅读