本篇文章给大家带来的内容是关于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;@compentpublic 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'll need to remove the spaces from the html entities belowvalue = value.replaceall("<", "& lt;")。replaceall(">", "& gt;");value = value.replaceall("\\(", "& #40;")。replaceall("\\)", "& #41;");value = value.replaceall("'", "& #39;");value = value.replaceall("eval\\((。*)\\)", "");value = value.replaceall("[\\\"\\\'][\\s]*javascript:(。*)[\\\"\\\']", "\"\"");value = value.replaceall("script", "");return value;}}
以上就是xss过滤器如何配置?xss过滤器配置方法的详细内容。