XSS安全漏洞的几种修复方式

来源:互联网 发布:淘宝刷单群 编辑:程序博客网 时间:2024/05/22 06:31

什么是XSS

跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS,跨站脚本攻击指的是恶意攻击者往Web页面里插入恶意的html,javaScript代码,当用户浏览该页之时,嵌入其中Web里面的html,javaScript代码会被执行,从而达到恶意的特殊目的,如,盗取用户Cookie、破坏页面结构、重定向到其它网站等。不过,因为是一种被动的攻击手法,没有相应的软件来完成自动化攻击,对website有http-only、crossdomian.xml(接下来会介绍这两种方法)没有用,有一定几率不成功,而且还耗时间,所以xss现在是一门热门但不太受重视的Web攻击手法。

XSS攻击解决办法

一、SpringMVC架构下@InitBinder方法
Controller方法的参数类型可以是基本类型,也可以是封装后的普通Java类型。若这个普通Java类型没有声明任何注解,则意味着它的每一个属性都需要到Request中去查找对应的请求参数,服务端通过Request的getParameter方法取到的参数都是字符串形式,WebDataBinder的作用就是把字符串形式的参数转换成服务端真正需要的类型。
每次请求到来后的参数解析都会利用WebDataBinderFactory创建一个binder对象,然后从这个binder中取得最终解析好的参数对象。WebDataBinderFactory是在InvocableHandlerMethod中定义的,即不同的Controller方法有着不同的WebDataBinderFactory。
@InitBinder用于在@Controller中标注于方法,表示为当前控制器注册一个属性编辑器或者其他,只对当前的Controller有效,所以要用@InitBinder实现过滤输入,转义输出,就必须在每个需要的Controller中使用@InitBinder,我的方法就是创建一个BaseController,每个需要实现此业务的都去继承它。

import org.springframework.stereotype.Controller;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;@Controllerpublic class BaseController {    @InitBinder    public void webInitBinder(WebDataBinder binder){        binder.registerCustomEditor(String.class, new StringEditor());    }}public class StringEditor extends PropertyEditorSupport {    @Override    public void setAsText(String text) throws IllegalArgumentException {        if (StringUtils.isBlank(text)) {            return;        }        try {            //Spring自带html标签转义与反转义            super.setValue(HtmlUtils.htmlEscape(text));        } catch (Exception e) {            throw new IllegalArgumentException(e);        }    }}

二、WebBindingInitializer
WebBindingInitializer:实现WebBindingInitializer,重写initBinder注册的属性编辑器是全局的属性编辑器,对所有的Controller都有效

public class WebBinderInitializerUtils implements WebBindingInitializer{    @Override    public void initBinder(WebDataBinder binder, WebRequest request) {        binder.registerCustomEditor(String.class,new StringEditor());    }}public class StringEditor extends PropertyEditorSupport {    @Override    public void setAsText(String text) throws IllegalArgumentException {        if (StringUtils.isBlank(text)) {            return;        }        try {            //Spring自带html标签转义与反转义            super.setValue(HtmlUtils.htmlEscape(text));        } catch (Exception e) {            throw new IllegalArgumentException(e);        }    }}    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <property name="cacheSeconds" value="0"/>        <property name="webBindingInitializer">            <bean class="cn.gov.wu.biz.utils.WebBinderInitializerUtils"/>        </property>    </bean>

注:低于3.2版本的Spring请使用AnnotationMethodHandlerAdapter,我的Spring版本为4.2.1.RELEASE,所以配置项中使用RequestMappingHandlerAdapter,因为AnnotationMethodHandlerAdapter已经废弃掉了

这里写图片描述

Spring MVC架构内还有蛮多方法可以实现此种功能,具体可见:http://www.tuicool.com/articles/BzqUja

三、HttpOnly
如果在cookie中设置了HttpOnly属性,那么通过javaScript脚本将无法读取到cookie信息,这样能有效的防止XSS攻击,但是注意,只是不能读取,但是可以覆盖,攻击者如果发现网站的XSS漏洞,就可以利用HttpOnly cookie发动session fixation攻击。

response.setHeader("Set-Cookie", "cookiename=value;Path=/;Domain=domainvalue;Max-Age=seconds;HTTPOnly");
1 0
原创粉丝点击