来源:自学PHP网 时间:2015-04-17 11:59 作者: 阅读:次
[导读] PHP XSS跨站攻击解决方案,在网上搜索了下,大概都是这个函数,但是说实话,真的没有完全理解这个函数的含义。首先是使用十六进制替换所有特殊字符,然后将传入的字符串完全用字...
PHP XSS跨站攻击解决方案,在网上搜索了下,大概都是这个函数,但是说实话,真的没有完全理解这个函数的含义。首先是使用十六进制替换所有特殊字符,然后将传入的字符串完全用字母符号代替。最后一步就不是太理解了。先放着,慢慢研究吧。
关于跨站攻击的几个网站:
http://chriscook.me/web-development/php-preventing-typical-xss-attacks/
http://ha.ckers.org/xss.html
https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet
[PHP]
function RemoveXSS($val) {
//http://www.asciitable.com/详细见ASCII码
//移除所有的非打印字符,CR(0a)换行 and LF(0b)垂直制表符 and TAB(9)水平制表符例外
//这样可以阻止类似<java\0script>的跨站攻击,ASCII码中\0为NULL
//注意:// note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
$val = preg_replace(‘/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/’, ”, $val);
// straight replacements, the user should never need these since they’re normal characters
// this prevents like <IMG SRC=@avascript:alert(‘XSS’)>
// 列出所有打印字符。在$val中搜索所有的打印字符或其实体,并用字母符号代替。
$search = ‘abcdefghijklmnopqrstuvwxyz’;
$search .= ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
$search .= ’1234567890!@#$%^&*()’;
$search .= ‘~`”;:?+/={}[]-_|\’\\’; www.2cto.com
for ($i = 0; $i < strlen($search); $i++) {
$val = preg_replace(‘/(&#[xX]0{0,8}’.dechex(ord($search[$i])).’;?)/i’, $search[$i], $val); // with a ;
$val = preg_replace(‘/(�{0,8}’.ord($search[$i]).’;?)/’, $search[$i], $val); // with a ;
}
$ra1 = Array(‘javascript’, ‘vbscript’, ‘expression’, ‘applet’, ‘meta’, ‘xml’, ‘blink’, ‘link’, ‘style’, ‘script’, ‘embed’, ‘object’, ‘iframe’, ‘frame’, ‘frameset’, ‘ilayer’, ‘layer’, ‘bgsound’, ‘title’, ‘base’);
$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’);
$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,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 nerf 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;
}
[/PHP]
|
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com