zoukankan      html  css  js  c++  java
  • XSS之xssprotect(转)

    参考资料
    1 跨网站脚本 http://zh.wikipedia.org/wiki/XSS
    2 http://code.google.com/p/xssprotect/
    一 跨网站脚本介绍
         跨网站脚本(Cross-site scripting,通常简称为XSS或跨站脚本或跨站脚本攻击)是一种网站应用程序的安全漏洞攻击,是代码注入的一种。它允许恶意用户将代码注入到网页 上,其他用户在观看网页时就会受到影响。这类攻击通常包含了HTML以及用户端脚本语言。
    XSS攻击通常指的是通过利用网页开发时留下的漏洞,通过巧妙的方法注入恶意指令代码到网页,使用户加载并执行攻击者恶意制造的网页程序。这些恶 意网页程序通常是JavaScript,但实际上也可以包括Java, VBScript, ActiveX, Flash 或者甚至是普通的HTML。攻击成功后,攻击者可能得到包括但不限于更高的权限(如执行一些操作)、私密网页内容、会话和cookie等各种内容。
    二 常用的XSS攻击手段和目的
      盗用 cookie ,获取敏感信息。
    利用植入 Flash ,通过 crossdomain 权限设置进一步获取更高权限;或者利用Java等得到类似的操作。
    利用 iframe、frame、XMLHttpRequest或上述Flash等方式,以(被攻击)用户的身份执行一些管理动作,或执行一些一般的如发微博、加好友、发私信等操作。
      利用可被攻击的域受到其他域信任的特点,以受信任来源的身份请求一些平时不允许的操作,如进行不当的投票活动。
      在访问量极大的一些页面上的XSS可以攻击一些小型网站,实现DDoS攻击的效果。
    三 漏洞的防御和利用
    避免XSS的方法之一主要是将用户所提供的内容进行过滤,许多语言都有提供对HTML的过滤:

        PHP的htmlentities()或是htmlspecialchars()。
        Python的cgi.escape()。
        ASP的Server.HTMLEncode()。
        ASP.NET的Server.HtmlEncode()或功能更强的Microsoft Anti-Cross Site Scripting Library
        Java的xssprotect(Open Source Library)。
        Node.js的node-validator。
    四 xssprotect
    在Eclipse中通过svn检出项目源地址:http://xssprotect.googlecode.com/svn/trunk/如下图

    通用使用方式:http://code.google.com/p/xssprotect/wiki/HowTouse
    Java代码  收藏代码
    1. package com.xss.example;  
    2.   
    3. import java.io.IOException;  
    4. import java.io.StringReader;  
    5. import java.io.StringWriter;  
    6.   
    7. import com.blogspot.radialmind.html.HTMLParser;  
    8. import com.blogspot.radialmind.html.HandlingException;  
    9. import com.blogspot.radialmind.xss.XSSFilter;  
    10.   
    11. public class XSSTest {  
    12.   
    13.     public static void main(String[] args) {  
    14.         String html = "<html><head> <title> New Document </title> " +  
    15.                 "<script type='text/javascript'>  alert('dddd');   </script> " +  
    16.                 "</head> <body>" +  
    17.                 "222 <iframe  src='www.google.com'/>  1111" +  
    18.                 "<embed ></embed>" +  
    19.                 "<link>ddd</link>" +  
    20.                 "</body></html>";  
    21.         String v = protectAgainstXSS(html);  
    22.         System.out.println(v);  
    23.   
    24.     }  
    25.       
    26.     public static String protectAgainstXSS( String html ) {  
    27.         StringReader reader = new StringReader( html );  
    28.         StringWriter writer = new StringWriter();  
    29.         String text = null;  
    30.         try {  
    31.             // Parse incoming string from the "html" variable  
    32.             HTMLParser.process( reader, writer, new XSSFilter(), true );  
    33.             // Return the parsed and cleaned up string  
    34.             text =  writer.toString();  
    35.         } catch (HandlingException e) {  
    36.             // Handle the error here in accordance with your coding policies...  
    37.         }finally{  
    38.             try {  
    39.                 writer.close();  
    40.                 reader.close();  
    41.             } catch (IOException e) {                 
    42.                 e.printStackTrace();  
    43.             }             
    44.         }  
    45.         return text;  
    46.     }  
    47. }  

    在它的源代码中有二个类需要关注下:
    BaseTestCase.java
    Java代码  收藏代码
    1. public abstract class BaseTestCase extends TestCase {  
    2.     protected void testExecute( String html, String result ) {  
    3.         StringReader reader = new StringReader( html );  
    4.         StringWriter writer = new StringWriter();  
    5.           
    6.         try {  
    7.             HTMLParser.process( reader, writer, new XSSFilter(), true );  
    8.             String buffer = new String( writer.toString() );  
    9.             System.out.println( buffer );  
    10.             assertEquals( result, buffer );  
    11.         } catch (HandlingException e) {  
    12.             e.printStackTrace();  
    13.             fail( e.getMessage() );  
    14.         }  
    15.     }  
    16. }  

    XSSFilter.java
    Java代码  收藏代码
    1. /** 
    2.  * Copyright 2008 Gerard Toonstra 
    3.  * 
    4.  * As an exception, this particular file  
    5.  * in the project is public domain to allow totally 
    6.  * free derivations of this code. 
    7.  *  
    8.  */  
    9.   
    10. package com.blogspot.radialmind.xss;  
    11.   
    12. import java.util.HashSet;  
    13. import java.util.Set;  
    14.   
    15. import com.blogspot.radialmind.html.IHTMLFilter;  
    16.   
    17. /** 
    18.  * Implementation of a relatively simple XSS filter. This implementation removes 
    19.  * dangerous tags and attributes from tags. It does not verify the validity of  
    20.  * URL's (that may contain links to JavaScript for example). It does not remove all 
    21.  * event handlers that may still contain XSS vulnerabilities.  
    22.  *  
    23.  * Embedded objects are removed because those may contain XSS vulnerabilities in  
    24.  * their own scripting language (ActionScript for Flash for example). 
    25.  *  
    26.  * Feel free to derive your own implementation from this file. 
    27.  *  
    28.  * @author gt 
    29.  * 
    30.  */  
    31. public class XSSFilter implements IHTMLFilter {  
    32.       
    33.     private static final Set<String> FORBIDDEN_TAGS = new HashSet<String>();  
    34.       
    35.     // The tags to be removed. Case insensitive of course.  
    36.     static {  
    37.         FORBIDDEN_TAGS.add( "script" );  
    38.         FORBIDDEN_TAGS.add( "embed" );  
    39.         FORBIDDEN_TAGS.add( "object" );  
    40.         FORBIDDEN_TAGS.add( "layer" );  
    41.         FORBIDDEN_TAGS.add( "style" );  
    42.         FORBIDDEN_TAGS.add( "meta" );  
    43.         FORBIDDEN_TAGS.add( "iframe" );  
    44.         FORBIDDEN_TAGS.add( "frame" );  
    45.         FORBIDDEN_TAGS.add( "link" );  
    46.         FORBIDDEN_TAGS.add( "import" );  
    47.         FORBIDDEN_TAGS.add( "xml" );  
    48.     }  
    49.       
    50.     /** 
    51.      * This function is called to determine if an attribute should be filtered or not. 
    52.      *  
    53.      * @param tagName   The name of the tag the attribute belongs to 
    54.      * @param attrName  The name of the attribute to be filtered 
    55.      * @param attrValue The value of the attribute 
    56.      */  
    57.     public boolean filterAttribute(String tagName, String attrName, String attrValue) {  
    58.         if ( attrName.toLowerCase().startsWith( "on" )) {  
    59.             return true;  
    60.         }  
    61.           
    62.         return isScriptedAttributeValue( attrValue );  
    63.     }  
    64.   
    65.     /** 
    66.      *  This method is called to determine if a tag should be filtered 
    67.      *  
    68.      * @param tagName   The name of the tag that was parsed 
    69.      */  
    70.     public boolean filterTag(String tagName) {  
    71.         if ( FORBIDDEN_TAGS.contains( tagName )) {  
    72.             return true;  
    73.         }  
    74.         return false;  
    75.     }  
    76.   
    77.     /** 
    78.      * This method is called to modify attribute values, if required 
    79.      *  
    80.      * @param tagName   The name of the tag the attribute belongs to 
    81.      * @param attrName  The name of the attribute within the tag 
    82.      * @param attrValue     The value of the attribute 
    83.      */  
    84.     public String modifyAttributeValue(String tagName, String attrName, String attrValue) {  
    85.         return attrValue;  
    86.     }  
    87.   
    88.     /** 
    89.      * This method is called to be able to modify the text of a node. 
    90.      *  
    91.      * @param tagName   The name of the tag where the text is part of. 
    92.      * @param text      The value of the text within the tagnode (within <tag>...</tag>) 
    93.      */  
    94.     public String modifyNodeText(String tagName, String text) {  
    95.         return text;  
    96.     }  
    97.       
    98.     /** 
    99.      * Private method that determines if an attribute value is scripted 
    100.      * (potentially loaded with an XSS attack vector). 
    101.      *  
    102.      * @param attrValue The value of the attribute 
    103.      * @return "true" if the attribute is scripted. "false" if not. 
    104.      */  
    105.     private boolean isScriptedAttributeValue( String attrValue ) {  
    106.         attrValue = decode( attrValue );  
    107.         attrValue = attrValue.trim().toLowerCase();  
    108.   
    109.         if ( attrValue.contains( "javascript:" )) {  
    110.             return true;  
    111.         }  
    112.         if ( attrValue.contains( "mocha:" )) {  
    113.             return true;  
    114.         }  
    115.         if ( attrValue.contains( "eval" )) {  
    116.             return true;  
    117.         }  
    118.         if ( attrValue.contains( "vbscript:" )) {  
    119.             return true;  
    120.         }  
    121.         if ( attrValue.contains( "livescript:" )) {  
    122.             return true;  
    123.         }  
    124.         if ( attrValue.contains( "expression(" )) {  
    125.             return true;  
    126.         }  
    127.         if ( attrValue.contains( "url(" )) {  
    128.             return true;  
    129.         }  
    130.         if ( attrValue.contains( "&{" )) {  
    131.             return true;  
    132.         }  
    133.         if ( attrValue.contains( "&#" )) {  
    134.             return true;  
    135.         }  
    136.         return false;  
    137.     }  
    138.       
    139.     /** 
    140.      * Private method to remove control characters from the value 
    141.      *  
    142.      * @param value The value being modified 
    143.      * @return  The value free from control characters 
    144.      */  
    145.     private String decode( String value ) {  
    146.         value = value.replace("u0000", "" );  
    147.         value = value.replace("u0001", "" );  
    148.         value = value.replace("u0002", "" );  
    149.         value = value.replace("u0003", "" );  
    150.         value = value.replace("u0004", "" );  
    151.         value = value.replace("u0005", "" );  
    152.         value = value.replace("u0006", "" );  
    153.         value = value.replace("u0007", "" );  
    154.         value = value.replace("u0008", "" );  
    155.         value = value.replace("u0009", "" );  
    156.         value = value.replace(" ", "" );  
    157.         value = value.replace("u000B", "" );  
    158.         value = value.replace("u000C", "" );  
    159.         value = value.replace(" ", "" );  
    160.         value = value.replace("u000E", "" );  
    161.         value = value.replace("u000F", "" );  
    162.         value = value.replace("u0010", "" );  
    163.         value = value.replace("u0011", "" );  
    164.         value = value.replace("u0012", "" );  
    165.         value = value.replace("u0013", "" );  
    166.         value = value.replace("u0014", "" );  
    167.         value = value.replace("u0015", "" );  
    168.         value = value.replace("u0016", "" );  
    169.         value = value.replace("u0017", "" );  
    170.         value = value.replace("u0018", "" );  
    171.         value = value.replace("u0019", "" );  
    172.         value = value.replace("u001A", "" );  
    173.         value = value.replace("u001B", "" );  
    174.         value = value.replace("u001C", "" );  
    175.         value = value.replace("u001D", "" );  
    176.         value = value.replace("u001E", "" );  
    177.         value = value.replace("u001F", "" );  
    178.         return value;  
    179.     }  
    180. }  

    通过这个过滤就知道它要做什么了
    上传包吧,方便
    • 大小: 79.9 KB

    转自:链接

  • 相关阅读:
    python 冒泡排序
    python链式调用REST API把参数放到URL中
    python assert断言用法
    python实现斐波那契数列
    Pycharm快捷键集合
    linux shell中$0,$?,$!等的特殊用法
    搭建邮箱服务器
    linux安装IB驱动方法
    Oracle:Redhat 7.4+Oracle Rac 11.2.0.4 执行root.sh报错处理
    Struts学习(一)
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/5329476.html
Copyright © 2011-2022 走看看