当前位置:   article > 正文

XSS之xssprotect

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

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

在它的源代码中有二个类需要关注下:
BaseTestCase.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. try {
  6. HTMLParser.process( reader, writer, new XSSFilter(), true );
  7. String buffer = new String( writer.toString() );
  8. System.out.println( buffer );
  9. assertEquals( result, buffer );
  10. } catch (HandlingException e) {
  11. e.printStackTrace();
  12. fail( e.getMessage() );
  13. }
  14. }
  15. }

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

通过这个过滤就知道它要做什么了


转自:http://liuzidong.iteye.com/blog/1744023


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/847421
推荐阅读
相关标签
  

闽ICP备14008679号