当前位置:首页 > PHP教程 > php高级应用 > 列表

php 过滤存储型XSS攻击

发布:smiling 来源: PHP粉丝网  添加日期:2022-05-21 08:38:40 浏览: 评论:0 

最近做的项目被测试测出了存在存储型XSS,至此记录一下,问题出在了 input 框 :payload:"a" οnclick=alert(1)>

也做了一些XSS过滤,但是不全,有从网上找了一些,弄了一个简单粗暴的;

后台接收 input 框字符串内容,存在被攻击,便整理了一个比较粗暴的方法

//过滤存储型XSS攻击

  1. //过滤存储型XSS攻击 
  2.  
  3. public function safe_filter(&$string)  
  4.  
  5. { 
  6.  
  7.     $ra=Array('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/','/script/','/javascript/','/vbscript/','/expression/','/applet/','/meta/','/xml/','/blink/','/link/','/style/','/embed/','/object/','/frame/','/layer/','/title/','/bgsound/','/base/','/onload/','/onunload/','/onchange/','/onsubmit/','/onreset/','/onselect/','/onblur/','/onfocus/','/onabort/','/onkeydown/','/onkeypress/','/onkeyup/','/onclick/','/ondblclick/','/onmousedown/','/onmousemove/','/onmouseout/','/onmouseover/','/onmouseup/','/onunload/'); 
  8.  
  9.         $data=str_replace(array('&','<','>'),array('&amp;','&lt;','&gt;'),$data);    
  10.  
  11.     if (!get_magic_quotes_gpc())             //不对magic_quotes_gpc转义过的字符使用    addslashes(),避免双重转义。 
  12.  
  13.     { 
  14.  
  15.        $string  = addslashes($string);           //给单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)加上反斜线转义 
  16.  
  17.     } 
  18.  
  19.     $string       = preg_replace($ra,'',$string);     //删除非打印字符,粗暴式过滤xss可疑字符串 
  20.  
  21.     $laststring     = htmlentities(strip_tags($string)); //去除 HTML 和 PHP 标记并转换为 HTML 实体 
  22.  
  23.     return $laststring; 
  24.  
  25. } 

后来,认为这些太过粗暴,就又整理了一些,以后方便参照:

  1. /** 
  2.  
  3. * 过滤html标签,引号,中文空格 
  4.  
  5.  */ 
  6.  
  7. function fileter_str( $str ) 
  8.  
  9. { 
  10.  
  11.     $str = addslashes(trim($str)); 
  12.  
  13.     $str = preg_replace("/<(.*?)>/","",$str); 
  14.  
  15.     $str = str_replace("_x000D_","",$str); //替换空格为 
  16.  
  17.     //注释上一行因为:导入英语试题会将所有空格去掉 
  18.  
  19.     // $str = str_replace(' ','',$str); 
  20.  
  21.     return $str; 
  22.  
  23. } 

还有:

  1. /** 
  2.  
  3.  * 过滤html标签,引号,中文空格,换行符 
  4.  
  5.  */ 
  6.  
  7. function fileter_add_str( $str ) 
  8.  
  9. { 
  10.  
  11. $str = str_replace("_x000D_","",$str); //替换空格为 
  12.  
  13. $str = str_replace(array("\r\n", "\r", "\n","\t"), "<br />", $str);//替换回车换行 
  14.  
  15. $str = str_replace("'", "&#39;", $str); 
  16.  
  17. // $str = str_replace(' ','',$str); 
  18.  
  19. return $str; 
  20.  
  21. } 

还有这些:

  1. //全角到半角的转换,$str待转换的字符串,$flag标识符,$flag=0半角到全角,$flag=1全角到半角 
  2.  
  3. function SBC_DBC($str, $flag) { 
  4.  
  5.     $DBC = Array(//全角 
  6.  
  7.         '0' , '1' , '2' , '3' , '4' , 
  8.  
  9.         '5' , '6' , '7' , '8' , '9' , 
  10.  
  11.         'A','B' , 'C' , 'D' , 'E' , 
  12.  
  13.         'F' , 'G' , 'H' , 'I' , 'J' , 
  14.  
  15.         'K' , 'L' , 'M' , 'N' , 'O' , 
  16.  
  17.         'P' , 'Q' , 'R' , 'S' , 'T' , 
  18.  
  19.         'U' , 'V' , 'W' , 'X' , 'Y' , 
  20.  
  21.         'Z' , 'a' , 'b' , 'c' , 'd' , 
  22.  
  23.         'e' , 'f' , 'g' , 'h' , 'i' , 
  24.  
  25.         'j' , 'k' , 'l' , 'm' , 'n' , 
  26.  
  27.         'o' , 'p' , 'q' , 'r' , 's' , 
  28.  
  29.         't' , 'u' , 'v' , 'w' , 'x' , 
  30.  
  31.         'y' , 'z' , '-' , ' ' , ':' , 
  32.  
  33.         '.' , ',' , '/' , '%' , '#' , 
  34.  
  35.         '!' , '@' , '&' , '(' , ')' , 
  36.  
  37.         '<' , '>' , '"' , ''' , '?' , 
  38.  
  39.         '[' , ']' , '{' , '}' , '\' , 
  40.  
  41.         '|' , '+' , '=' , '_' , '^' , 
  42.  
  43.         '¥' , ' ̄' , '`' 
  44.  
  45.     ); 
  46.  
  47.    
  48.  
  49.     $SBC = Array( // 半角 
  50.  
  51.         '0', '1', '2', '3', '4', 
  52.  
  53.         '5', '6', '7', '8', '9', 
  54.  
  55.         'A', 'B', 'C', 'D', 'E', 
  56.  
  57.         'F', 'G', 'H', 'I', 'J', 
  58.  
  59.         'K', 'L', 'M', 'N', 'O', 
  60.  
  61.         'P', 'Q', 'R', 'S', 'T', 
  62.  
  63.         'U', 'V', 'W', 'X', 'Y', 
  64.  
  65.         'Z', 'a', 'b', 'c', 'd', 
  66.  
  67.         'e', 'f', 'g', 'h', 'i', 
  68.  
  69.         'j', 'k', 'l', 'm', 'n', 
  70.  
  71.         'o', 'p', 'q', 'r', 's', 
  72.  
  73.         't', 'u', 'v', 'w', 'x', 
  74.  
  75.         'y', 'z', '-', ' ', ':', 
  76.  
  77.         '.', ',', '/', '%', '#', 
  78.  
  79.         '!', '@', '&', '(', ')', 
  80.  
  81.         '<', '>', '"', '\'','?', 
  82.  
  83.         '[', ']', '{', '}', '\\', 
  84.  
  85.         '|', '+', '=', '_', '^', 
  86.  
  87.         '$', '~', '`' 
  88.  
  89.     ); 
  90.  
  91.    
  92.  
  93.     if ($flag == 0) { 
  94.  
  95.         return str_replace($SBC, $DBC, $str);  // 半角到全角 
  96.  
  97.     } else if ($flag == 1) { 
  98.  
  99.         return str_replace($DBC, $SBC, $str);  // 全角到半角 
  100.  
  101.     } else { 
  102.  
  103.         return false; 
  104.  
  105.     } 
  106.  
  107. } 

仅供参考。

Tags: php过滤存储型XSS攻击

分享到: