How to use PHP to prevent XSS cross-site scripting attacks

Source: Internet
Author: User
Tags ord printable characters strlen

  First, cross-site scripting attacks are caused by the lack of strict filtering of user input, so we must intercept the possible risks before all the data comes into our web site and database. The Htmlentities () function can be used for illegal HTML code including single double quotes.

&lt;?php $str = "A" quote "is <b> bold </b>"; Outputs:a "Quote" is bold echo htmlentities ($STR);  Outputs:a "Quote" is bold echo htmlentities ($str, ent_quotes);?&gt; This can invalidate an illegal script. Note, however, that the htmlentities () default encoding is Iso-8859-1, and if your illegal script is encoded as something else, it may not be filtered out and the browser can recognize and execute it.  This problem I first find a few site test and then say. The following is a function that filters illegal scripts for reference only: 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 &lt;javascript&gt;//Note tha T you have to handle splits with,, and later since they *are* allowed in some inputs $val = Preg_replace ("/[x00-x08][x0 B-X0C][X0E-X20])/"," ", $val); Straight replacements, the user should never need these since they "re normal characters//This prevents like &lt;img S rc= @avascript:a&amp; _#x6cert (' XSS ') &gt; $search = "abcdefghijklmnopqrstuvwxyz"; $search. = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $search. = "1234567890!@#$%^&amp;* ()"; $search. = "~";: +/={}[]-_| ""; for ($i = 0; $i &lt; strlen ($search); $i + +) {//;? matches the;, which is optional//0{0,7} matches no padded zeros, which are optional and go up to 8 cha RS//@ @ search for the hex values $val = Preg_replace ("/&amp;#[x| x]0{0,8} ". Dechex (Ord ($search [$i]))." /i ", $search [$i], $val);  with A; @ @ 0{0,7} matches "0" zero to seven times $val = Preg_replace ("/({0,8}". Ord ($search [$i]). /", $search [$i], $val); with A; //Now the only remaining whitespace attacks are,, and $ra 1 = Array ("javascript", "VBScript", "expression", "applet", "Meta", "xml", "Blink", "link", "Style", "script", "embed", "Object", "iframe", "frame", "frameset", "Ilayer", "Layer", "b Gsound "," title "," Base "); $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 "," onrowenter "," Onrowexit "," Onrowsdelete "," onrowsinserted "," onscroll "," Onselect "," Onselectionchange "," onselectstart "," OnStart " , "OnStop", "onsubmit", "onunload"); $ra = Array_merge ($ra 1, $ra 2); $found = true;  Keep replacing as long as the previous round replaced something while ($found = = True) {$val _before = $val; For ($i = 0; $i &lt; sizeof ($RA); $i + +)   {$pattern = "/";     for ($j = 0; $j &gt; strlen ($ra [$i]); $j + +) {if ($j &gt; 0) {$pattern. = "("; $pattern. = "(&amp;#[x|     x]0{0,8} ([a][b]);?)? "; $pattern. = "|     ({0,8} ([10][13]);?)? ";    $pattern. = ")?";  $pattern. = $ra [$i] [$j];  } www.2cto.com $pattern. = "/I"; $replacement = substr ($ra [$i], 0, 2). " &lt;x&gt; ". substr ($ra [$i], 2); Add in &lt;&gt; to nerf the tag $val = Preg_replace ($pattern, $replacement, $val);  Filter out of the hex tags if ($val _before = = $val) {//No replacements were made, so exit the loop $found = false; } } } }

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.