XSS漏洞解析(二)
上篇我们讲了XSS的一些相关的内容,这篇我们就直接上代码demo解决实际问题吧。
主要的问题是xssfilter的编写,我们直接去网上找一下框架,一般有js,php,java等语言都有相关的XSS的相关支持框架,小编自己就用擅长的java来解决.
一、首先我下载了lucy-xss这个jar
二、demo例子如下:
import com.nhncorp.lucy.security.xss.XssFilter; import com.nhncorp.lucy.security.xss.XssPreventer; import com.nhncorp.lucy.security.xss.XssSaxFilter; /** * XSS漏洞测试 * @author mingge * */ public class TestXSS { public static void main(String[] args) { String dirty22= "\"><script>alert('xss');</script>";// XSS漏洞 String clean22 = XssPreventer.escape(dirty22);// 被转码后的数据 System.out.println(clean22); System.out.println(XssPreventer.unescape(clean22)); XssSaxFilter filter22 = XssSaxFilter.getInstance("lucy-xss-superset-sax.xml"); String clean1 = "<TABLE class=\"NHN_Layout_Main\" style=\"TABLE-LAYOUT: fixed\" cellSpacing=\"0\" cellPadding=\"0\" width=\"743\">" + "</TABLE>" + "<SPAN style=\"COLOR: #66cc99\"></SPAN>"; String filtered = filter22.doFilter(clean1); System.out.println(clean1); System.out.println(filtered); XssFilter filter = XssFilter.getInstance("lucy-xss-superset.xml"); String dirty = "<img src=\"<img src=1\\ onerror=alert(1234)>\" onerror=\"alert('XSS')\">"; String expected = "<img src=\"\"><!-- Not Allowed Attribute Filtered ( onerror=alert(1234)) --><img src=1\\>\" onerror=\"alert('XSS')\">"; String clean = filter.doFilter(dirty); System.out.println(expected); System.out.println(clean); dirty = "<img src='<img src=1\\ onerror=alert(1234)>\" onerror=\"alert('XSS')\">"; expected = "<img src=''><!-- Not Allowed Attribute Filtered ( onerror=alert(1234)) --><img src=1\\>\" onerror=\"alert('XSS')\">"; clean = filter.doFilter(dirty); System.out.println(expected); System.out.println(clean); } }
这个demo很容易转码,是复合我们需求的。
这个框架的web配置如下:
<filter> <filter-name>xssEscapeServletFilter</filter-name> <filter-class>com.navercorp.lucy.security.xss.servletfilter.XssEscapeServletFilter</filter-class> </filter> <filter-mapping> <filter-name>xssEscapeServletFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
是不是很简单.
如果你想看源码,请参考:https://github.com/naver/lucy-xss-filter
day day up。。。