PHP defends against XSS attacks

Source: Internet
Author: User
Tags ord printable characters
PHP defends against XSS attacks, using methods and details to see http://www.tongqiong.com/read.php?tid-474.html
  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
  4. Note that you had 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
  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 is 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. }
  20. Http://www.tongqiong.com
  21. Now the only remaining whitespace attacks is \ t, \ n, and \ r
  22. $ra 1 = Array (' JavaScript ', ' VBScript ', ' expression ', ' applets ', ' meta ', ' xml ', ' blink ', ' link ', ' style ', ' script ', ' em Bed ', ' object ', ' iframe ', ' frame ', ' frameset ', ' ilayer ', ' layer ', ' bgsound ', ' title ', ' base ');
  23. $ra 2 = 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 ', ' onrowent 'Er ', ' onrowexit ', ' onrowsdelete ', ' onrowsinserted ', ' onscroll ', ' onselect ', ' onselectionchange ', ' onselectstart ', ' OnStart ', ' onstop ', ' onsubmit ', ' onunload ');
  24. $ra = Array_merge ($ra 1, $ra 2);
  25. $found = true; Keep replacing as long as the previous round replaced something
  26. while ($found = = True) {
  27. $val _before = $val;
  28. for ($i = 0; $i < sizeof ($RA); $i + +) {
  29. $pattern = '/';
  30. for ($j = 0; $j < strlen ($ra [$i]); $j + +) {
  31. if ($j > 0) {
  32. $pattern. = ' (';
  33. $pattern. = ' (&#[xx]0{0,8} ([9ab]);) ';
  34. $pattern. = ' | ';
  35. $pattern. = ' | (? {0,8} ([9|10|13]);) ';
  36. $pattern. = ') * ';
  37. }
  38. $pattern. = $ra [$i] [$j];
  39. }
  40. $pattern. = '/I ';
  41. $replacement = substr ($ra [$i], 0, 2). ' '. substr ($ra [$i], 2);//Add in <> to nerf the tag
  42. $val = Preg_replace ($pattern, $replacement, $val); Filter out the hex tags
  43. if ($val _before = = $val) {
  44. No replacements were made, so exit the loop
  45. $found = false;
  46. }
  47. }
  48. }
  49. return $val;
  50. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.