springboot xss防护
概述
XSS(Cross Site Script)全称跨站脚本攻击,为了跟CSS区分开来,所以变成了XSS。它允许恶意代码植入到正常的页面中,盗取正常用户的账号密码,诱使用户访问恶意的网站。
攻击
实施XSS攻击必须具备两个条件
- 向web页面注入恶意代码。
- 这些恶意代码能够被浏览器执行。
看一个简单的demo,更能清晰的了解什么XSS攻击
@RequestMapping(value = "/demo11",method = RequestMethod.GET) public String demo11(@RequestParam String name,@RequestParam int age) { return name; }
我们输入访问地址
127.0.0.1:8080/demo11?name=<script>alert("正在发动xss攻击")</script><a href="http://www.baidu.com">澳门皇家赌场上线了<a>&age=11
点击诱惑连接
这就是一个简单的反射性XSS攻击,也叫非持久性XSS,将非法代码提交到服务器,服务器解析并响应,而响应中包含XSS代码,最后浏览器解析并执行。另一种XSS攻击也叫持久型XSS,将XSS代码发送到服务器,如果没有后台效验,直接给存上了,在回显时,会从服务起解析加载出来,浏览器发现XSS代码,当做正常的HTML和js解析,那XSS攻击就产生了(有兴趣的童鞋可以尝试下)。
防护
- XSS防护最简单的就是从源头把控,把输入的一些不合法数据过滤或转义,让xss攻击代码无效,保证数据的安全性,还得控制参数的长度等。
- 如果源头没把控住,那就得从出口处理需要显示的数据。
springboot的防护
用到的工具包,maven坐标
<!--XSS--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.4</version> </dependency>
这里初始化了两个方法:getParameter和getParameterValues,getParameter方法是直接通过请求获取查询字符串类型的入参调用的方法。如果是通过springMVC注解类型来获得参数的话,调的是getParameterValues的方法
import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class XssAndSqlHttpServletRequestWrapper extends HttpServletRequestWrapper { private HttpServletRequest request; public XssAndSqlHttpServletRequestWrapper(HttpServletRequest request) { super(request); this.request = request; } @Override public String getParameter(String name) { String value = request.getParameter(name); if (!StringUtils.isEmpty(value)) { value = StringEscapeUtils.escapeHtml4(value); } return value; } @Override public String[] getParameterValues(String name) { String[] parameterValues = super.getParameterValues(name); if (parameterValues == null) { return null; } for (int i = 0; i < parameterValues.length; ++i) { String value = parameterValues[i]; parameterValues[i] = StringEscapeUtils.escapeHtml4(value); } return parameterValues; } }
过滤json类型的代码
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import org.apache.commons.text.StringEscapeUtils; import java.io.IOException; public class XssStringJsonSerializer extends JsonSerializer<String> { @Override public Class<String> handledType() { return String.class; } @Override public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { if (s == null) { String encodedValue = StringEscapeUtils.escapeHtml4(s); jsonGenerator.writeString(encodedValue); } } }
过滤器
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import java.io.IOException; @Component @WebFilter public class XssFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; XssAndSqlHttpServletRequestWrapper xssAndSqlHttpServletRequestWrapper = new XssAndSqlHttpServletRequestWrapper(request); filterChain.doFilter(xssAndSqlHttpServletRequestWrapper,servletResponse); } @Override public void destroy() { } @Bean @Primary public ObjectMapper xssObjectMapper(Jackson2ObjectMapperBuilder builder) { //解析器 ObjectMapper objectMapper = builder.createXmlMapper(false).build(); //注册xss解析器 SimpleModule xssModule = new SimpleModule("XssStringJonSerializer"); xssModule.addSerializer(new XssStringJsonSerializer()); objectMapper.registerModule(xssModule); return objectMapper; } }
lz使用的springboot security安全框架,将过滤器放置在添加过滤里头。
@Bean public XssFilter authenticationXssFilterBean() { return new XssFilter(); } /*xss过滤*/ http.addFilterBefore(authenticationXssFilterBean(),UsernamePasswordAuthenticationFilter.class);
试验
127.0.0.1:8080/huangweicheng/hwc/test?name=<script>alert(‘hello‘);</script>
后台日志,传入的xss代码就成功转义了
相关推荐
sswqycbailong 2020-07-28
csxiaoqiang 2020-07-26
码农成长记 2020-07-19
layloge 2020-07-05
layloge 2020-06-26
liangjielaoshi 2020-06-25
csxiaoqiang 2020-06-16
某先生 2020-06-13
ItBJLan 2020-06-11
layloge 2020-06-07
csxiaoqiang 2020-06-03
sswqycbailong 2020-06-01
layloge 2020-05-30
码农成长记 2020-05-28
qidu 2020-05-26
zhuangnet 2020-05-20
zhuangnet 2020-05-19
xiaoemo0 2020-05-16
码农成长记 2020-05-10