XSS学习笔记(五)-XSS防御

如果只生产XSS的地方都与输入或输出相关联的。所以错过了主要矛盾。而且,我们将有一个解决问题的办法:您可以输入端砚格过滤,是可能的过滤输出时间,输出到用户的GET或POST中是否有敏感字符:

输入过滤:
过滤 ' " , <  >   <!--
client: 开启 XSS filter 

输出过滤(转义):
转义: ' "   <  <!-- (转义为&#32; 等HTML硬编码)
过滤: <script> href </script>模型 , window.location 模型 等

浏览器防御:IE 8 以上的内核 开启 XSS filter ,chrome ctrl+ shift + N  开启隐身浏览 , firefox Noscript 插件

事实上有非常多測试XSS攻击的工具:

Paros proxy (http://www.parosproxy.org)
Fiddler (http://www.fiddlertool.com/fiddler) (点击Toolbar上的"TextWizard" button)
Burp proxy (http://www.portswigger.net/proxy/)
TamperIE (http://www.bayden.com/dl/TamperIESetup.exe)

以下贴一个 常见的php过滤XSS的函数:
<?PHP
/**
 * @blog http://www.phpddt.com
 * @param $string
 * @param $low 安全别级低
 */
function clean_xss(&$string, $low = False)
{
    if (! is_array ( $string ))
    {
        $string = trim ( $string );
        $string = strip_tags ( $string );
        $string = htmlspecialchars ( $string );
        if ($low)
        {
            return True;
        }
        $string = str_replace ( array ('"', "\", "'", "/", "..", "../", "./", "//" ), '', $string );
        $no = '/%0[0-8bcef]/';
        $string = preg_replace ( $no, '', $string );
        $no = '/%1[0-9a-f]/';
        $string = preg_replace ( $no, '', $string );
        $no = '/[x00-x08x0Bx0Cx0E-x1Fx7F]+/S';
        $string = preg_replace ( $no, '', $string );
        return True;
    }
    $keys = array_keys ( $string );
    foreach ( $keys as $key )
    {
        clean_xss ( $string [$key] );
    }
}
//just a test
$str = 'phpddt.com<meta http-equiv="refresh" content="0;">';
clean_xss($str); //假设你把目光出来。你知道xss强大的攻击
echo $str;
?>

由clean_xss()要过滤恶意内容!
原文地址:https://www.cnblogs.com/mfrbuaa/p/4590990.html