php mysql_real_escape_string addslashes及mysql绑定参数防SQL注入攻击
phpmysql_real_escape_stringaddslashes及mysql绑定参数防SQL注入攻击
php防止SQL注入攻击一般有三种方法:
- 使用mysql_real_escape_string函数
- 使用addslashes函数
- 使用mysqlbind_param()
本文章向大家详细介绍这三个方法在防止SQL注入攻击中的效果及区别。
mysql_real_escape_string防sql注入攻击
mysql_real_escape_string()函数转义SQL语句中使用的字符串中的特殊字符。
在有些时候需要将mysql_real_escape_string与mysql_set_charset一起使用,因为如果不指定编码,可能会存在字符编码绕过mysql_real_escape_string函数的漏洞,比如:
$name=$_GET['name']; $name=mysql_real_escape_string($name); $sql="select*fromtablewherenamelike'%$name%'";
当输入name值为name=41%bf%27%20or%20sleep%2810.10%29%3d0%20limit%201%23时,sql语句输出为:
SELECT*FROMtableWHEREnameLIKE'%41¿\\\'orsleep(10.10)=0limit1#%';
这时候引发SQL注入攻击。
下面是mysql_real_escape_string函数防止SQL注入攻击的正确做法:
<?php functioncheck_input($value) { //去除斜杠 if(get_magic_quotes_gpc()) { $value=stripslashes($value); } //如果不是数字则加引号 /*http://www.manongjc.com/article/1242.html*/ if(!is_numeric($value)) { $value="'".mysql_real_escape_string($value)."'"; } return$value; } $con=mysql_connect("localhost","hello","321"); if(!$con) { die('Couldnotconnect:'.mysql_error()); } //进行安全的SQL mysql_set_charset('utf-8'); $user=check_input($_POST['user']); $pwd=check_input($_POST['pwd']); $sql="SELECT*FROMusersWHERE user=$userANDpassword=$pwd"; mysql_query($sql); mysql_close($con); ?>
addslashes防sql注入攻击
国内很多PHPcoder仍在依靠addslashes防止SQL注入(包括我在内),我还是建议大家加强中文防止SQL注入的检查。addslashes的问题在于可以用0xbf27来代替单引号,而addslashes只是将0xbf27修改为0xbf5c27,成为一个有效的多字节字符,其中的0xbf5c仍会被看作是单引号,所以addslashes无法成功拦截。当然addslashes也不是毫无用处,它可用于单字节字符串的处理。
addslashes会自动给单引号,双引号增加\,这样我们就可以安全的把数据存入数据库中而不黑客利用,参数'a..z'界定所有大小写字母均被转义,代码如下:
echoaddcslashes('foo[]','a..z');//输出:foo[] $str="isyournameo'reilly?";//定义字符串,其中包括需要转义的字符 echoaddslashes($str);//输出经过转义的字符串
mysqlbind_param()绑定参数防止SQL注入攻击
什么叫绑定参数,给大家举个例子:
<?php $username="aaa"; $pwd="pwd"; $sql="SELECT*FROMtableWHEREusername=?ANDpwd=?"; bindParam($sql,1,$username,'STRING');//以字符串的形式.在第一个问号的地方绑定$username这个变量 bindParam($sql,2,$pwd,'STRING');//以字符串的形式.在第二个问号的地方绑定$pwd这个变量 echo$sql; ?>
你肯定不知道会输出什么..更无法知道绑定参数有什么好处!这样做的优势是什么.更不知道bindParam这个函数到底做了什么.
下面我简单的写一下这个函数:
<?php /** *模拟简单的绑定参数过程 * *@paramstring$sqlSQL语句 *@paramint$location问号位置 *@parammixed$var替换的变量 *@paramstring$type替换的类型 */ $times=0; //这里要注意,因为要“真正的"改变$sql的值,所以用引用传值 functionbindParam(&$sql,$location,$var,$type){ global$times; //确定类型 switch($type){ //字符串 default://默认使用字符串类型 case'STRING': $var=addslashes($var);//转义 $var="'".$var."'";//加上单引号.SQL语句中字符串插入必须加单引号 break; case'INTEGER': case'INT': $var=(int)$var;//强制转换成int //还可以增加更多类型.. } //寻找问号的位置 for($i=1,$pos=0;$i<=$location;$i++){ $pos=strpos($sql,'?',$pos+1); } //替换问号 $sql=substr($sql,0,$pos).$var.substr($sql,$pos+1); } ?>
注:由于得知道去除问号的次数..所以我用了一个global来解决.如果放到类中就非常容易了.弄个私有属性既可
通过上面的这个函数.我们知道了..绑定参数的防注入方式其实也是通过转义进行的..只不过是对于变量而言的..
我们来做一个实验:
<?php $times=0; $username="aaaa"; $pwd="123"; $sql="SELECT*FROMtableWHEREusername=?ANDpwd=?"; bindParam($sql,1,$username,'STRING');//以字符串的形式.在第一个问号的地方绑定$username这个变量 bindParam($sql,2,$pwd,'INT');//以字符串的形式.在第二个问号的地方绑定$pwd这个变量 echo$sql;//输出SELECT*FROMtableWHEREusername='aaaa'ANDpwd=123 ?>
可以看到.生成了非常正规的SQL语句.那么好.我们现在来试下刚才被注入的那种情况
<?php $times=0; $username="aaa"; $pwd="fdsafda'or'1'='1"; $sql="SELECT*FROMtableWHEREusername=?ANDpwd=?"; bindParam($sql,1,$username,'STRING');//以字符串的形式.在第一个问号的地方绑定$username这个变量 bindParam($sql,2,$pwd,'STRING');//以字符串的形式.在第二个问号的地方绑定$pwd这个变量 echo$sql;//输出SELECT*FROMtableWHEREusername='aaa'ANDpwd='fdsafda\'or\'1\'=\'1' ?>
可以看到.pwd内部的注入已经被转义.当成一个完整的字符串了..这样的话.就不可能被注入了.
总结:
上面三个方法都可以防止sql注入攻击,但第一种方法和第二种方法都存在字符编码的漏洞,所以本文章建议大家使用第三种方法。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!