国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php實現XSS安全過濾的方法

2024-05-04 23:38:17
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php實現XSS安全過濾的方法,實例分析了php針對XSS進行安全過濾的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php實現XSS安全過濾的方法。分享給大家供大家參考。具體如下:

 

 
  1. function remove_xss($val) { 
  2. // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed 
  3. // this prevents some character re-spacing such as <java/0script> 
  4. // note that you have to handle splits with /n, /r, and /t later since they *are* allowed in some inputs 
  5. $val = preg_replace('/([/x00-/x08,/x0b-/x0c,/x0e-/x19])/'''$val); 
  6. // straight replacements, the user should never need these since they're normal characters 
  7. // this prevents like <IMG SRC=@avascript:alert('XSS')> 
  8. $search = 'abcdefghijklmnopqrstuvwxyz'
  9. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  10. $search .= '1234567890!@#$%^&*()'
  11. $search .= '~`";:?+/={}[]-_|/'//'
  12. for ($i = 0; $i < strlen($search); $i++) { 
  13. // ;? matches the ;, which is optional 
  14. // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars 
  15. // @ @ search for the hex values 
  16. $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i'$search[$i], $val); // with a ; 
  17. // @ @ 0{0,7} matches '0' zero to seven times 
  18. $val = preg_replace('/(?{0,8}'.ord($search[$i]).';?)/'$search[$i], $val); // with a ; 
  19. // now the only remaining whitespace attacks are /t, /n, and /r 
  20. $ra1 = array('javascript''vbscript''expression''applet''meta''xml''blink''link''style''script''embed''object''iframe''frame''frameset''ilayer''layer''bgsound''title''base'); 
  21. $ra2 = array('onabort''onactivate''onafterprint''onafterupdate''onbeforeactivate''onbeforecopy''onbeforecut''onbeforedeactivate''onbeforeeditfocus''onbeforepaste''onbeforeprint''onbeforeunload''onbeforeupdate''onblur''onbounce''oncellchange''onchange''onclick''oncontextmenu''oncontrolselect''oncopy''oncut''ondataavailable''ondatasetchanged''ondatasetcomplete''ondblclick''ondeactivate''ondrag''ondragend''ondragenter''ondragleave''ondragover''ondragstart''ondrop''onerror''onerrorupdate''onfilterchange''onfinish''onfocus''onfocusin''onfocusout''onhelp''onkeydown''onkeypress''onkeyup''onlayoutcomplete''onload''onlosecapture''onmousedown''onmouseenter''onmouseleave''onmousemove''onmouseout''onmouseover''onmouseup''onmousewheel''onmove''onmoveend''onmovestart''onpaste''onpropertychange''onreadystatechange''onreset''onresize''onresizeend''onresizestart''onrowenter''onrowexit''onrowsdelete''onrowsinserted''onscroll''onselect''onselectionchange''onselectstart''onstart''onstop''onsubmit''onunload'); 
  22. $ra = array_merge($ra1$ra2); 
  23. $found = true; // keep replacing as long as the previous round replaced something 
  24. while ($found == true) { 
  25. $val_before = $val
  26. for ($i = 0; $i < sizeof($ra); $i++) { 
  27. $pattern = '/'
  28. for ($j = 0; $j < strlen($ra[$i]); $j++) { 
  29. if ($j > 0) { 
  30. $pattern .= '('
  31. $pattern .= '(&#[xX]0{0,8}([9ab]);)'
  32. $pattern .= '|'
  33. $pattern .= '|(?{0,8}([9|10|13]);)'
  34. $pattern .= ')*'
  35. $pattern .= $ra[$i][$j]; 
  36. $pattern .= '/i'
  37. $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag 
  38. $val = preg_replace($pattern$replacement$val); // filter out the hex tags 
  39. if ($val_before == $val) { 
  40. // no replacements were made, so exit the loop 
  41. $found = false; 
  42. return $val

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳朔县| 新兴县| 淅川县| 屏山县| 滦平县| 新民市| 新营市| 普洱| 黔西县| 库伦旗| 沧源| 麻江县| 宜州市| 乃东县| 措美县| 广河县| 海原县| 五家渠市| 和平区| 中超| 鲁甸县| 绥棱县| 萨迦县| 珲春市| 沭阳县| 隆昌县| 佛坪县| 荔波县| 桐梓县| 河东区| 昌吉市| 旅游| 田林县| 达拉特旗| 镇沅| 安徽省| 乐山市| 陆良县| 富宁县| 二连浩特市| 碌曲县|