防止SQL注入和XSS跨站攻击代码

http://blog.csdn.net/jbb0403/article/details/36626515原文


这两天用360网站安全检测网站,检测出SQL注入漏洞和XSS跨站攻击脚本N多项bug,然后上网找各种资料,把大部分的资料都看了一遍,最后自己总结了如下代码:

a、防止SQL注入代码:

[php]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. //防止SQL注入 hxm_20140701  只对字符串有效  
  2. function clean_SQLinject($string){  
  3.     if(!get_magic_quotes_gpc()){  
  4.         $string = mysql_real_escape_string($string);  
  5.         $string = addslashes($string);  
  6.     }  
  7.     $string = str_replace("_","\_",$string);  
  8.     $string = str_replace("%","\%",$string);  
  9.     return $string;  
  10. }  

b、防止XSS跨站攻击代码:

[php]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. //防止xss跨站攻击 hxm_20140701  数组或字符串都有效  
  2. function clean_xss(&$string){  
  3.     if(!is_array($string)){  
  4.         $string = trim($string);  
  5.         $string = strip_tags($string);  
  6.         $string = htmlspecialchars($string);  
  7.         $string = str_replace (array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string);  
  8.         $no = '/%0[0-8bcef]/';  
  9.         $string = preg_replace ($no''$string);  
  10.         $no = '/%1[0-9a-f]/';  
  11.         $string = preg_replace ($no''$string);  
  12.         $no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';  
  13.         $string = preg_replace ($no''$string);  
  14.         return True;  
  15.     }  
  16.   
  17.     $keys = array_keys($string);  
  18.     foreach($keys as $key){  
  19.         clean_xss($string[$key]);  
  20.     }  
  21. }  

c、后来改文件时候很多地方的查询语句的where条件用到了大量post或get过来的参数,一个一个过滤闲太麻烦,所以发明了如下函数:

d、对传过来的post和get参数组直接过滤,包含过滤SQL注入和XSS攻击:

[php]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. //对传过来的整个post数组进行SQL注入和XSS过滤   hxm_20140701  
  2. function clean_post($post) {    
  3.   if(!get_magic_quotes_gpc()){    // 判断magic_quotes_gpc是否为打开    
  4.     $post = addslashes($post);    // 进行magic_quotes_gpc没有打开的情况对提交数据的过滤    
  5.   }    
  6.   $post = str_replace("_""\_"$post);    // 把 '_'过滤掉    
  7.   $post = str_replace("%""\%"$post);    // 把 '%'过滤掉    
  8.   $post = nl2br($post);    // 回车转换    
  9.   $post = htmlspecialchars($post);    // html标记转换 防止xss   
  10.    
  11.   return $post;    
  12. }  

备注(关于XSS):

1、PHP防止XSS跨站脚本攻击的方法:是针对非法的HTML代码包括单双引号等,使用htmlspecialchars()函数 。

2、在使用htmlspecialchars()函数的时候注意第二个参数, 直接用htmlspecialchars($string) 的话,第二个参数默认是ENT_COMPAT,函数默认只是转化双引号(“), 不对单引号(‘)做转义.

3、所以,htmlspecialchars函数更多的时候要加上第二个参数, 应该这样用: htmlspecialchars($string,ENT_QUOTES).当然,如果需要不转化引号,用htmlspecialchars($string,ENT_NOQUOTES).

4、另外, 尽量少用htmlentities, 在全部英文的时候htmlentities和htmlspecialchars没有区别,都可以达到目的.但是,中文情况下, htmlentities却会转化所有的html代码,连同里面的它无法识别的中文字符也给转化了。

5、htmlentities和htmlspecialchars这两个函数对 '之类的字符串支持不好,都不能转化, 所以用htmlentities和htmlspecialchars转化的字符串只能防止XSS攻击,不能防止SQL注入攻击.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值