Home  >  Article  >  Operation and Maintenance  >  How to configure Xss filter? Xss filter configuration method

How to configure Xss filter? Xss filter configuration method

云罗郡主
云罗郡主Original
2019-01-22 15:15:546874browse

本篇文章给大家带来的内容是关于Xss过滤器如何配置?Xss过滤器配置方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1.XSS是什么?

跨站脚本(cross site script)简称为XSS,是一种经常出现在web应用中的计算机安全漏洞,也是web中最主流的攻击方式。

XSS是指恶意攻击者利用网站没有对用户提交数据进行转义处理或者过滤不足的缺点,进而添加一些代码,嵌入到web页面中去,使别的用户访问都会执行相应的嵌入代码。

2.XSS攻击的危害

1、盗取用户资料,比如:登录帐号、网银帐号等

2、利用用户身份,读取、篡改、添加、删除企业敏感数据等

3、盗窃企业重要的具有商业价值的资料

4、非法转账

5、强制发送电子邮件

6、网站挂马

7、控制受害者机器向其它网站发起攻击

3.防止XSS解决方案

XSS的根源主要是没完全过滤客户端提交的数据 ,所以重点是要过滤用户提交的信息。

 将重要的cookie标记为http only, 这样的话js 中的document.cookie语句就不能获取到cookie了.

 只允许用户输入我们期望的数据。 例如:age用户年龄只允许用户输入数字,而数字之外的字符都过滤掉。

 对数据进行Html Encode 处理: 用户将数据提交上来的时候进行HTML编码,将相应的符号转换为实体名称再进行下一步的处理。

 过滤或移除特殊的Html标签, 例如: 3f1c4e4b6b16bbbd69b2ee476dc4f83a, d5ba1642137c3f32f4f4493ae923989c , f7142607ca3bcca4ac1473eda942a5a5 for >, " for

 过滤js事件的标签。例如 “onclick=”, “onfocus” 等等。

项目以SpringBoot项目为例:

XssFilter:

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
@Compent
public class XssFilter implements Filter {
FilterConfig filterConfig = null;
private List<String> urlExclusion = null;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void destroy() {
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String servletPath = httpServletRequest.getServletPath();
if (urlExclusion != null && urlExclusion.contains(servletPath)) {
chain.doFilter(request, response);
} else {
chain.doFilter(new XssHttpServletRequestWrapper((HttpServletRequest) request), response);
}
}
public List<String> getUrlExclusion() {
return urlExclusion;
}
public void setUrlExclusion(List<String> urlExclusion) {
this.urlExclusion = urlExclusion;
}
}

XssHttpServletRequestWrapper:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
public XssHttpServletRequestWrapper(HttpServletRequest servletRequest) {
super(servletRequest);
}
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] = cleanXSS(values[i]);
}
return encodedValues;
}
public String getParameter(String parameter) {
String value = super.getParameter(parameter);
if (value == null) {
return null;
}
return cleanXSS(value);
}
public String getHeader(String name) {
String value = super.getHeader(name);
if (value == null)
return null;
return cleanXSS(value);
}
private String cleanXSS(String value) {
//You&#39;ll need to remove the spaces from the html entities below
value = value.replaceAll("<", "& lt;")。replaceAll(">", "& gt;");
value = value.replaceAll("\\(", "& #40;")。replaceAll("\\)", "& #41;");
value = value.replaceAll("&#39;", "& #39;");
value = value.replaceAll("eval\\((。*)\\)", "");
value = value.replaceAll("[\\\"\\\&#39;][\\s]*javascript:(。*)[\\\"\\\&#39;]", "\"\"");
value = value.replaceAll("script", "");
return value;
}
}


The above is the detailed content of How to configure Xss filter? Xss filter configuration method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn