参考资料
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. }

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

上传包吧,方便

  • 大小: 79.9 KB

转自:链接

XSS之xssprotect(转)的更多相关文章

  1. 漏洞科普:对于XSS和CSRF你究竟了解多少

    转自:http://www.freebuf.com/articles/web/39234.html 随着Web2.0.社交网络.微博等等一系列新型的互联网产品的诞生,基于Web环境的互联网应用越来越广 ...

  2. 跨站脚本 XSS<一:防御方法>

    1. 过滤特殊字符 避免XSS的方法之一主要是将用户所提供的内容进行过滤,许多语言都有提供对HTML的过滤: PHP的htmlentities()或是htmlspecialchars(). Pytho ...

  3. 跨站脚本 XSS<一:介绍>

    *XSS 利用的是用户对指定网站的信任,CSRF 利用的是网站对用户网页浏览器的信任 跨站脚本(Cross-site scripting,通常简称为XSS)是一种网站应用程序的安全漏洞攻击,是代码注入 ...

  4. XSS 和 CSRF 攻击

    web安全中有很多种攻击手段,除了SQL注入外,比较常见的还有 XSS 和 CSRF等 一.XSS(Cross Site Scripting)跨站脚本 XSS其实就是Html的注入问题,攻击者的输入没 ...

  5. XSS CSRF 攻击

    XSS:跨站脚本(Cross-site scripting) CSRF:跨站请求伪造(Cross-site request forgery)定义: 跨网站脚本(Cross-site scripting ...

  6. XSS攻击原理及防御措施

    概述 XSS攻击是Web攻击中最常见的攻击方法之一,它是通过对网页注入可执行代码且成功地被浏览器 执行,达到攻击的目的,形成了一次有效XSS攻击,一旦攻击成功,它可以获取用户的联系人列 表,然后向联系 ...

  7. 网络XSS攻击和CSRF攻击原理及防范

    网络XSS攻击和CSRF攻击原理及防范 原文地址:http://www.freebuf.com/articles/web/39234.html 随着Web2.0.社交网络.微博等等一系列新型的互联网产 ...

  8. XSS/XSRF

    一.XSS 1.1 xss的含义 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为 ...

  9. 浅谈XSS攻击原理与解决方法

    概述 XSS攻击是Web攻击中最常见的攻击方法之一,它是通过对网页注入可执行代码且成功地被浏览器 执行,达到攻击的目的,形成了一次有效XSS攻击,一旦攻击成功,它可以获取用户的联系人列表,然后向联系人 ...

随机推荐

  1. Struts2 拦截器配置以及实现

    @(Java ThirdParty)[Struts|Interceptor] Struts2 拦截器配置以及实现 Struts2的拦截器应用于Action,可以在执行Action的方法之前,之后或者两 ...

  2. Python之路【第二十三篇】爬虫

    difference between urllib and urllib2 自己翻译的装逼必备 What is the difference between urllib and urllib2 mo ...

  3. spring jdbc 查询结果返回对象、对象列表

    首先,需要了解spring jdbc查询时,有三种回调方式来处理查询的结果集.可以参考 使用spring的JdbcTemplate进行查询的三种回调方式的比较,写得还不错. 1.返回对象(queryF ...

  4. struts2 spring mybatis 整合(test)

    这几天搭了个spring+struts2+mybatis的架子,练练手,顺便熟悉熟悉struts2. 环境:myEclipse10+tomcat7+jdk1.6(1.8的jre报错,所以换成了1.6) ...

  5. html5第二天

    哎..以为自己能每天坚持写呢.前面8天一直在D3的东西..都没有时间研究html5.草草的翻了一下HTML5和CSS3权威指南.对整个页面设计有了一个大概的把握,但是让自己做肯定还会有写问题.暂时ht ...

  6. sqlservcer行列互转

    普通行列转换 行转列 假设有张学生成绩表(tb)如下:Name Subject Result张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94*/---- ...

  7. json 使用 (下)

    使用JSON JSON也就是JavaScript Object Notation,是一个描述数据的轻量级语法.JSON的优雅是因为它是JavaScript语言的一个子集.接下来你将看到它为什么如此重要 ...

  8. golang的内置类型map的一些事

    golang的map类型是一个比较特殊的类型,不同于int, string, byte这样的基本类型,在经过一番探究之后得出了一些结论: 1.golang的map类型虽然是内置类型,但和基本类型有很大 ...

  9. 数据结构 浙大MOOC 笔记二 线性结构

    线性表及其表现 第二章的内容是关于三种最基本的数据结构 结合<DDSA>第三章 表.栈和队列做一个总结 首先简单说明一下各个数据结构的特点: 数组:连续存储,遍历快且方便,长度固定,缺点是 ...

  10. ng-repeat 嵌套访问父作用域里的属性

    在一个项目中,需要嵌套循环输出一个二维表的里的数据 数据结构 [ { id:1, list:[ { id:1, name:'li' } ] }, { id:2, list:[ { id:1, name ...