00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 final class RemoveXSS {
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 public function RemoveXSS($val, $replaceString = '<x>') {
00038 return self::process($val, $replaceString);
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 public static function process($val, $replaceString = '<x>') {
00051
00052 if ($replaceString == '') {
00053 $replaceString = '<x>';
00054 }
00055
00056
00057
00058 $val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x19])/', '', $val);
00059
00060
00061
00062 $search = '/&#[xX]0{0,8}(21|22|23|24|25|26|27|28|29|2a|2b|2d|2f|30|31|32|33|34|35|36|37|38|39|3a|3b|3d|3f|40|41|42|43|44|45|46|47|48|49|4a|4b|4c|4d|4e|4f|50|51|52|53|54|55|56|57|58|59|5a|5b|5c|5d|5e|5f|60|61|62|63|64|65|66|67|68|69|6a|6b|6c|6d|6e|6f|70|71|72|73|74|75|76|77|78|79|7a|7b|7c|7d|7e);?/ie';
00063 $val = preg_replace($search, "chr(hexdec('\\1'))", $val);
00064 $search = '/�{0,8}(33|34|35|36|37|38|39|40|41|42|43|45|47|48|49|50|51|52|53|54|55|56|57|58|59|61|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|126);?/ie';
00065 $val = preg_replace($search, "chr('\\1')", $val);
00066
00067
00068 $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base', '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');
00069 $ra_tag = array('applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
00070 $ra_attribute = array('style', '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');
00071 $ra_protocol = array('javascript', 'vbscript', 'expression');
00072
00073
00074 $val2 = preg_replace('/(&#[xX]?0{0,8}(9|10|13|a|b);)*\s*/i', '', $val);
00075 $ra = array();
00076
00077 foreach ($ra1 as $ra1word) {
00078
00079
00080
00081 if (stripos($val2, $ra1word ) !== false ) {
00082
00083 if (in_array($ra1word, $ra_protocol)) {
00084 $ra[] = array($ra1word, 'ra_protocol');
00085 }
00086 if (in_array($ra1word, $ra_tag)) {
00087 $ra[] = array($ra1word, 'ra_tag');
00088 }
00089 if (in_array($ra1word, $ra_attribute)) {
00090 $ra[] = array($ra1word, 'ra_attribute');
00091 }
00092
00093
00094 }
00095 }
00096
00097 if (count($ra) > 0) {
00098
00099 $found = true;
00100 while ($found == true) {
00101 $val_before = $val;
00102 for ($i = 0; $i < sizeof($ra); $i++) {
00103 $pattern = '';
00104 for ($j = 0; $j < strlen($ra[$i][0]); $j++) {
00105 if ($j > 0) {
00106 $pattern .= '((&#[xX]0{0,8}([9ab]);)|(�{0,8}(9|10|13);)|\s)*';
00107 }
00108 $pattern .= $ra[$i][0][$j];
00109 }
00110
00111 switch ($ra[$i][1]) {
00112 case 'ra_protocol':
00113
00114 $pattern .= '((&#[xX]0{0,8}([9ab]);)|(�{0,8}(9|10|13);)|\s)*(?=:)';
00115 break;
00116 case 'ra_tag':
00117
00118 $pattern = '(?<=<)' . $pattern . '((&#[xX]0{0,8}([9ab]);)|(�{0,8}(9|10|13);)|\s)*(?=[^\da-z])';
00119 break;
00120 case 'ra_attribute':
00121
00122
00123 $pattern .= '[\s\!\#\$\%\&\(\)\*\~\+\-\_\.\,\:\;\?\@\[\/\|\\\\\]\^\`]*(?==)';
00124 break;
00125 }
00126 $pattern = '/' . $pattern . '/i';
00127
00128 $replacement = substr_replace($ra[$i][0], $replaceString, 2, 0);
00129
00130 $val = preg_replace($pattern, $replacement, $val);
00131 if ($val_before == $val) {
00132
00133 $found = false;
00134 }
00135 }
00136 }
00137 }
00138
00139 return $val;
00140 }
00141 }
00142
00143 ?>