关于XSS漏洞修复
XSS即恶意脚本攻击漏洞,详细的描述⽹上介绍的很详细。我说⼀下⾃⼰在⼯作中对这种漏洞的修复⽅案,仅供参考。
第⼀阶段:使⽤ESAPI
这是⼀个Apache开发的安全组件,主要⽤于解决SQL注⼊和恶意脚本注⼊。
使⽤ESAPI防⽌XSS攻击时,⾸先应当配置过滤器(注意在过滤器中chain.doFilter(..)⽅法中的Request对象进⾏包装,在包装类中对请求参数进⾏筛选操作);其次将过滤器注册到l⽂件中;最后配置Request的包装类,在其中对请求信息进⾏过滤。
1import java.io.IOException;
2import javax.servlet.Filter;
3import javax.servlet.FilterChain;
4import javax.servlet.FilterConfig;
5import javax.servlet.ServletException;
6import javax.servlet.ServletRequest;
7import javax.servlet.ServletResponse;
8import javax.servlet.http.HttpServletRequest;
9
10/*
11 *配置XSS过滤器
12*/
13public class XSSFilter implements Filter {
14 @Override
15public void init(FilterConfig filterConfig) throws ServletException {
16 }
17 @Override
18public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
19throws IOException, ServletException {
20 chain.doFilter(new XSSRequestWrapper((HttpServletRequest) request), response);
21 }
22 @Override
23public void destroy() {
24 }
25 }
View Code
1<!-- XSS攻击防范 -->
2<filter>
3<filter-name>XSSFilter</filter-name>
4<filter-class>过滤器全类名</filter-class>
5</filter>
6<filter-mapping>
7<filter-name>XSSFilter</filter-name>
8<url-pattern>/*</url-pattern>
9<dispatcher>REQUEST</dispatcher>
10<dispatcher>FORWARD</dispatcher>
11</filter-mapping>
View Code
1import java.URLDecoder;
2import Pattern;
3import javax.servlet.http.HttpServletRequest;
4import javax.servlet.http.HttpServletRequestWrapper;
5import org.owasp.esapi.ESAPI;
6
7/*
8 * 对传⼊参数进⾏校验
9*/
10public class XSSRequestWrapper extends HttpServletRequestWrapper {
11public XSSRequestWrapper(HttpServletRequest servletRequest) {
12super(servletRequest);
13 }
14 @Override
15public String[] getParameterValues(String parameter) {
16 String[] values = ParameterValues(parameter);
17if (values == null) {
18return null;
19 }
20int count = values.length;
21 String[] encodedValues = new String[count];
22for (int i = 0; i < count; i++) {
23 encodedValues[i] = stripXSS(values[i]);
24 }
25return encodedValues;
26 }
27 @Override
28public String getParameter(String parameter) {
29 String value = Parameter(parameter);
30return stripXSS(value);
31 }
32 @Override
33public String getHeader(String name) {
34 String value = Header(name);
35return stripXSS(value);
36 }
37
38private String stripXSS(String value) {
39if (value != null) {
40//解析URL编码,防⽌前台的编码后的参数进⼊ESAPI过滤后⽆法正常解码
replaceall()
41 value = URLDecoder.decode(value);
42// 使⽤ESAPI的防护功能避免XSS攻击
43 value = der().canonicalize(value);
44// 去除空字符
45 value = placeAll("", "");
46// 过滤脚本标签间(如:<script>..</script>)的内容
47 Pattern scriptPattern = Patternpile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
48 value = scriptPattern.matcher(value).replaceAll("");
49//过滤由项⽬路径(src)直接访问项⽬资源的⾏为
50 scriptPattern = Patternpile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
51 value = scriptPattern.matcher(value).replaceAll("");
52 scriptPattern = Patternpile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
53 value = scriptPattern.matcher(value).replaceAll("");
54// 过滤单个</script>标签
55 scriptPattern = Patternpile("</script>", Pattern.CASE_INSENSITIVE);
56 value = scriptPattern.matcher(value).replaceAll("");
57// 过滤单个<script ...> 标签
58 scriptPattern = Patternpile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
59 value = scriptPattern.matcher(value).replaceAll("");
60// 过滤 eval(...) 表达式
61 scriptPattern = Patternpile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
62 value = scriptPattern.matcher(value).replaceAll("");
63// 过滤e x pression(...) 表达式
64 scriptPattern = Patternpile("e x pression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
65 value = scriptPattern.matcher(value).replaceAll("");
66// 过滤javascript:表达式攻击
67 scriptPattern = Patternpile("javascript:", Pattern.CASE_INSENSITIVE);
68 value = scriptPattern.matcher(value).replaceAll("");
69// 过滤vbscript:表达式攻击
70 scriptPattern = Patternpile("vbscript:", Pattern.CASE_INSENSITIVE);
71 value = scriptPattern.matcher(value).replaceAll("");
72// 过滤onload=事件
73 scriptPattern = Patternpile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
74 value = scriptPattern.matcher(value).replaceAll("");
75 }
76return value;
77 }
78 }
View Code
注意:开启ESAPI的防护功能时(value = der().canonicalize(value);),需要提前对要筛选的值进⾏URL解码(value = URLDecoder.decode(value);),否则会出现中⽂乱码。
第⼆阶段(可选):使⽤CSF(Content Security Policy)安全策略
使⽤⽅式:
1.通过<meta http-equiv="Content-Security-Policy" content="script-src 'self'; object-src 'none'; style-src ; child-src https:">标签将CSP嵌⼊到页⾯中。但是这种做法会对所有页⾯添加⼀个过滤项,加长了页⾯的访问时间,降低了⽤户体验;我加到项⽬中的时候还出现了某些样式⽂件加载不全的现象,没有必要不建议这么做。
2.通过使⽤Nginx配置HTTP 头信息的Content-Security-Policy的字段,但是⼀般的公司都不允许更改Nginx,⼀旦Nginx做了某种配置,整个项⽬都受影响,这种⽅式基本没戏。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论