php中的xss攻击过滤函数
墨初 编程开发 595阅读
下面分享几个php脚本中关于xss攻击过滤的自定义函数,过滤xss可以提高网站整体的安全性,也可以保证mysql数据的安全。下面的xss攻击过滤函数大家可以参考一下。
php过滤xss攻击的函数
例:php过滤xss的函数
/** * # php过滤XSS攻击的函数 * @param string $str 需要过滤xss的数据 * * @return string 过滤后的数据 * @host https://www.73so.com */ function xss_filter($str) { if (empty($str)) return false; $str = htmlspecialchars($str); $str = str_replace( '/', "", $str); $str = str_replace( '"', "", $str); $str = str_replace( '(', "", $str); $str = str_replace( ')', "", $str); $str = str_replace( 'CR', "", $str); $str = str_replace( 'ASCII', "", $str); $str = str_replace( 'ASCII 0x0d', "", $str); $str = str_replace( 'LF', "", $str); $str = str_replace( 'ASCII 0x0a', "", $str); $str = str_replace( ',', "", $str); $str = str_replace( '%', "", $str); $str = str_replace( ';', "", $str); $str = str_replace( 'eval', "", $str); $str = str_replace( 'open', "", $str); $str = str_replace( 'sysopen', "", $str); $str = str_replace( 'system', "", $str); $str = str_replace( '$', "", $str); $str = str_replace( "'", "", $str); $str = str_replace( "'", "", $str); $str = str_replace( 'ASCII 0x08', "", $str); $str = str_replace( '"', "", $str); $str = str_replace( '"', "", $str); $str = str_replace("", "", $str); $str = str_replace(">", "", $str); $str = str_replace("<", "", $str); $str = str_replace("<SCRIPT>", "", $str); $str = str_replace("</SCRIPT>", "", $str); $str = str_replace("<script>", "", $str); $str = str_replace("</script>", "", $str); $str = str_replace("select","",$str); $str = str_replace("join","",$str); $str = str_replace("union","",$str); $str = str_replace("where","",$str); $str = str_replace("insert","",$str); $str = str_replace("delete","",$str); $str = str_replace("update","",$str); $str = str_replace("like","",$str); $str = str_replace("drop","",$str); $str = str_replace("DROP","",$str); $str = str_replace("create","",$str); $str = str_replace("modify","",$str); $str = str_replace("rename","",$str); $str = str_replace("alter","",$str); $str = str_replace("cas","",$str); $str = str_replace("&","",$str); $str = str_replace(">","",$str); $str = str_replace("<","",$str); $str = str_replace(" ",chr(32),$str); $str = str_replace(" ",chr(9),$str); $str = str_replace(" ",chr(9),$str); $str = str_replace("&",chr(34),$str); $str = str_replace("'",chr(39),$str); $str = str_replace("<br />",chr(13),$str); $str = str_replace("''","'",$str); $str = str_replace("css","'",$str); $str = str_replace("CSS","'",$str); $str = str_replace("<!--","",$str); $str = str_replace("convert","",$str); $str = str_replace("md5","",$str); $str = str_replace("passwd","",$str); $str = str_replace("password","",$str); $str = str_replace("../","",$str); $str = str_replace("./","",$str); $str = str_replace("Array","",$str); $str = str_replace("or 1='1'","",$str); $str = str_replace(";set|set&set;","",$str); $str = str_replace("`set|set&set`","",$str); $str = str_replace("--","",$str); $str = str_replace("OR","",$str); $str = str_replace('"',"",$str); $str = str_replace("*","",$str); $str = str_replace("-","",$str); $str = str_replace("+","",$str); $str = str_replace("/","",$str); $str = str_replace("=","",$str); $str = str_replace("'/","",$str); $str = str_replace("-- ","",$str); $str = str_replace(" -- ","",$str); $str = str_replace(" --","",$str); $str = str_replace("(","",$str); $str = str_replace(")","",$str); $str = str_replace("{","",$str); $str = str_replace("}","",$str); $str = str_replace("-1","",$str); $str = str_replace("1","",$str); $str = str_replace(".","",$str); $str = str_replace("response","",$str); $str = str_replace("write","",$str); $str = str_replace("|","",$str); $str = str_replace("`","",$str); $str = str_replace(";","",$str); $str = str_replace("etc","",$str); $str = str_replace("root","",$str); $str = str_replace("//","",$str); $str = str_replace("!=","",$str); $str = str_replace("$","",$str); $str = str_replace("&","",$str); $str = str_replace("&&","",$str); $str = str_replace("==","",$str); $str = str_replace("#","",$str); $str = str_replace("@","",$str); $str = str_replace("mailto:","",$str); $str = str_replace("CHAR","",$str); $str = str_replace("char","",$str); return $str; }
例2:看一个项中使用的php过滤xss的方法
if (!get_magic_quotes_gpc()) // 判断magic_quotes_gpc是否为打开 { $str = addslashes($str); // magic_quotes_gpc没有打开的时候把数据过滤 } $str = str_replace("_", "\_", $str); // 把 '_'过滤掉 $str = str_replace("%", "\%", $str); // 把' % '过滤掉 $str = nl2br($str); // 回车转换 $str = htmlspecialchars($str); // html标记转换 echo $str;
例3:个人使用的一个php过滤xss的函数
/** * 过滤参数 * @param string $str 接受的参数 * @return string * @host https://www.73so.com */ function filterWords($str) { $farr = array( "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU", "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU", "/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|dump/is" ); $str = preg_replace($farr,'',$str); return $str; } /** * 过滤接受的参数或者数组,如$_GET,$_POST * @param array|string $arr 接受的参数或者数组 * @return array|string * @host https://www.73so.com */ function filterArr($arr) { if(is_array($arr)){ foreach($arr as $k => $v){ $arr[$k] = filterWords($v); } }else{ $arr = filterWords($v); } return $arr; }