xss存储漏洞问题分析解决

  背景:1、XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中。比如这些代码包括HTML代码和客户端脚本。攻击者利用XSS漏洞旁路掉访问控制——例如同源策略(same origin policy)。这种类型的漏洞由于被黑客用来编写危害性更大的网络钓鱼(Phishing)攻击而变得广为人知。对于跨站脚本攻击,黑客界共识是:跨站脚本攻击是新型的“缓冲区溢出攻击“,而JavaScript是新型的“ShellCode”。

   解决: 编写拦截过滤器对表单提交的数据进行处理过滤处理

   解决案例:

第一步:web..xml中添加配置

<filter>
		<filter-name>xssFilter</filter-name>
		<filter-class>com.core.http.XssFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>xssFilter</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
	</filter-mapping>

    第二步:编写过滤器     

public class XssFilter implements Filter {
	
	FilterConfig filterConfig = 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 {
		chain.doFilter(new XssHttpServletRequestWrapper((HttpServletRequest) request), response);
	}
}

     操作类:      

public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
	
	public XssHttpServletRequestWrapper(HttpServletRequest servletRequest) {
		super(servletRequest);
	}
	 @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] = cleanXSS(values[i]);
		}
		
		return encodedValues; 
	}
	 @Override
	public String getParameter(String parameter) {
		
		String value = super.getParameter(parameter);
		if (value == null) return null;
		
		return cleanXSS(value);
	}
	
	 @Override
	public String getHeader(String name) {
		String value = super.getHeader(name);
		if (value == null) return null;
		
		return cleanXSS(value);
	}
	
	private String cleanXSS(String value) {
        
		value = value.replaceAll(";", ";");
		value = value.replaceAll("\\(", "(").replaceAll("\\)", ")");
		value = value.replaceAll("'", "'");
		value = value.replaceAll("`", "`");
		value = value.replaceAll("CR", "
"); //换行
		value = value.replaceAll("LF", "
"); //回车
		value = value.replaceAll("eval\\((.*)\\)", "");
		value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
        //value = value.replaceAll("script", "");
		/** filter script tag*/
		String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
		Pattern p_script =Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(value);   
		value = m_script.replaceAll("");
		value = value.replaceAll("<", "<").replaceAll(">", ">");
		return value;
	}
}
       该过滤器主要是实现对html中前端的一些字符进行转义,script区域进行删除替换操作
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伏特加的滋味

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值