XSS漏洞解析(二)

上篇咱們講了XSS的一些相關的內容,這篇咱們就直接上代碼demo解決實際問題吧。php

主要的問題是xssfilter的編寫,咱們直接去網上找一下框架,通常有js,php,java等語言都有相關的XSS的相關支持框架,小編本身就用擅長的java來解決.java

 

1、首先我下載了lucy-xss這個jargit

 

2、demo例子以下:github

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')\"&gt;";
        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')\"&gt;";
        clean = filter.doFilter(dirty);
        System.out.println(expected);
        System.out.println(clean);
    }
}

這個demo很容易轉碼,是複合咱們需求的。web

 

這個框架的web配置以下:app

<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-filterxss

 

day day up。。。測試

相關文章
相關標籤/搜索