简单防止脚本攻击--XSSFilter

Posted liulihaha

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单防止脚本攻击--XSSFilter相关的知识,希望对你有一定的参考价值。

使用到装饰模式

将请求进来的HttpServletRequest扩展为XSSRequestWrapper,并重写getParameterValues,getParameter,getHeader方法,将获得的value值中包含的敏感字符处理掉

下面贴上XSSRequestWrapper(包含处理方法)

public class XSSRequestWrapper extends HttpServletRequestWrapper {

        public XSSRequestWrapper(HttpServletRequest request) {
            super(request);
        }
        
        @Override
        public String[] getParameterValues(String parameter) {
            String[] values = super.getParameterValues(parameter);
            if (values == null) {
                return null;
            }
            int count = values.length;
            String[] encodedValues = new String[count];
            for (int i = 0; i < count; i++) {
                encodedValues[i] = stripXSS(values[i]);
            }
            return encodedValues;
        }
        
        @Override
        public String getParameter(String parameter) {
            String value = super.getParameter(parameter);
            return stripXSS(value);
        }
        
        @Override
        public String getHeader(String name) {
            String value = super.getHeader(name);
            return stripXSS(value);
        }
        
        private String stripXSS(String value) {
            if (value != null) {
                // NOTE: It‘s highly recommended to use the ESAPI library and uncomment the following line to
                // avoid encoded attacks.
                // value = ESAPI.encoder().canonicalize(value);
                // Avoid null characters
                value = value.replaceAll("", "");
                // Avoid anything between script tags
                Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
                value = scriptPattern.matcher(value).replaceAll("");
                // Avoid anything in a src="http://www.yihaomen.com/article/java/..." type of e­xpression
                scriptPattern = Pattern.compile("src[
]*=[
]*\‘(.*?)\‘", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
                scriptPattern = Pattern.compile("src[
]*=[
]*\"(.*?)\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
                // Remove any lonesome </script> tag
                scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
                value = scriptPattern.matcher(value).replaceAll("");
                // Remove any lonesome <script ...> tag
                scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
                // Avoid eval(...) e­xpressions
                scriptPattern = Pattern.compile("eval\((.*?)\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
                // Avoid e­xpression(...) e­xpressions
                scriptPattern = Pattern.compile("e­xpression\((.*?)\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
                // Avoid javascript:... e­xpressions
                scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
                value = scriptPattern.matcher(value).replaceAll("");
                // Avoid vbscript:... e­xpressions
                scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
                value = scriptPattern.matcher(value).replaceAll("");
                // Avoid onload= e­xpressions
                scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
            }
            return value;
        }
    }

以上是关于简单防止脚本攻击--XSSFilter的主要内容,如果未能解决你的问题,请参考以下文章

Java防止XSS攻击

SpringBoot整合XssFilter,Jsoup等实现请求参数的过滤,处理Xss攻击及sql注入

SpringBoot2.0 防止XSS攻击

Web安全之防止XSS跨站脚本攻击

HTMLPurifier插件防止XSS脚本攻击

XSS过滤JAVA过滤器filter 防止常见SQL注入