Zz from: http://thinkblog.sinaapp.com /? P = 72
XSS, also known as CSS (Cross Site Script), is a Cross-Site scripting attack. A malicious attacker inserts malicious html code into a Web page. When a user browses this page, the html code embedded in the Web page is executed, in this way, some people are attacked.
Below is a piece of code in thinkphp, used to filter xss
ThinkPHP \ Code \ ThinkPHP \ Common \ extend. php
<? Php/*** @ remove XSS (Cross-Site Scripting) function * @ par $ val string parameter, malicious script code, such as <script language = "javascript"> alert ("hello world "); </script> * @ return the processed string * @ Recoded By Androidyue **/function RemoveXSS ($ val) {// remove all non-printable characters. CR (0a) and LF (0b) and TAB (9) are allowed // this prevents some character re-spacing such as <java \ 0 script> // note that you have to handle splits with \ n, \ r, and \ t later si Nce they * are * allowed in some inputs $ val = preg_replace ('/([\ x00-\ x08, \ x0b-\ x0c, \ x0e-\ x19])/', '', $ val); // straight replacements, the user shocould never need these since they're normal characters // this prevents like $ search = 'abcdefghijklmnopqrstuvwxy'; $ search. = 'abcdefghijklmnopqrstuvwxy'; $ search. = '000000! @ # $ % ^ & * () '; $ Search. = '~ '";:? +/= {} []-_ | \ '; For ($ I = 0; $ I <strlen ($ search); $ I ++) {//;? Matches the;, which is optional // 0 {0, 7} matches any padded zeros, which are optional and go up to 8 chars // @ search for the hex values $ val = preg_replace ('/(& # [xX] 0 {0, 8 }'. dechex (ord ($ search [$ I]). ';?) /I ', $ search [$ I], $ val); // with; // @ 0 {} matches '0' zero to seven times $ val = preg_replace ('/(& #0 }'. ord ($ search [$ I]). ';?) /', $ Search [$ I], $ val); // with a;} // now the only remaining whitespace attacks are \ t, \ n, and \ r $ ra1 = Array ('javascript ', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink ', 'link ', 'style', 'script', 'embed ', 'object', 'iframe', 'frameset', 'ilayer', 'lay', 'bgsound ', 'title', 'base'); $ ra2 = Array ('onabport', 'onactivate', 'onafterprint ', 'onafterupdat', 'onbeforeactivat E ', 'onbeforecopy', 'onbeforecut', 'weight', 'onbeforeeditfocal', 'onbeforepaste ', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdat', 'onblur ', 'onbounce ', 'oncellchang', 'onchange', 'onclick', 'ontextmenu', 'oncontrolselect', 'oncopy', 'oncut ', 'ondataavailable', 'ondatasetchanged ', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragl Eave ', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdat', 'onfilterchang', 'onfinish', 'ondetail', 'onfocusin ', 'onfocusout', 'onhelp', 'onkeylow', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture ', 'onmouselow', 'onmouseenter ', 'onmouseleave ', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel ', 'onmove', 'onmoveend', 'onmovestart', 'onpa Ste ', 'onpropertychang', 'onreadystatechang', 'onreset', 'onsize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowsdelete ', onrowsinserted, onscroll, onselect, onselectionchange, onselectstart, onstart, onstop, onsubmit, onunload '); $ ra = array_merge ($ ra1, $ ra2); $ found = true; // keep replacing as long as the previous round replaced something while ($ found = = True) {$ val_before = $ val; for ($ I = 0; $ I <sizeof ($ ra); $ I ++) {$ pattern = '/'; for ($ j = 0; $ j <strlen ($ ra [$ I]); $ j ++) {if ($ j> 0) {$ pattern. = '('; $ pattern. = '(& # [xX] 0 {0, 8} ([9ab]);)'; $ pattern. = '|'; $ pattern. = '| (& #0 {0, 8} ([9 | 10 | 13]);)'; $ pattern. = ') *';} $ pattern. = $ ra [$ I] [$ j];} $ pattern. = '/I'; $ replacement = substr ($ ra [$ I], 0, 2 ). '<x> '. substr ($ ra [$ I], 2); // add in <> to ner F the tag $ val = preg_replace ($ pattern, $ replacement, $ val); // filter out the hex tags if ($ val_before = $ val) {// no replacements were made, so exit the loop $ found = false ;}} return $ val ;} // test the result. // echo RemoveXSS ("<script language = 'javascript '> alert ('Hello World'); </script>");?>
In addition, there is another tool: HTML Purifier, which is twice the efficiency of the preceding document. It wants to know what to do later and listen to the next decomposition.