参考资料
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. Ubuntu 16.04 安装 Kodi v17 “Krypton” Alpha 2

    Ubuntu 16.04 安装 Kodi v17 “Krypton” Alpha 2:sudo add-apt-repository ppa:team-xbmc/xbmc-nightlysudo ap ...

  2. gevent

    gevent是一个基于协程的python网络库. 特性: 1.基于libev的事件循环 2.基于greenlet 轻量级的执行单元  (what is greenlet ?) 3.来自python标准 ...

  3. 关于LESS

    LESS 是动态的样式表语言,通过简洁明了的语法定义,使编写 CSS 的工作变得非常简单. 翻译成大白话:写CSS算是体力活,并没有编程的感觉,不给前端人员装逼的机会,于是就搞了这玩意,相当于编程写C ...

  4. RESTful 接口调试分享利器 restc

    这个工具来自于https://elemefe.github.io/restc/  这里对Abp进行了一次封装 1.在项目中添加nuget包 Abp.Web.Api.Restc 2.在项目Abp模块的D ...

  5. CH模拟赛 皇后游戏

    /* 做的时候手推了一下n=2的四种情况,再排一下序就可以了,证明不是很严谨,但我想这就行了,毕竟全是套路 */ #include<iostream> #include<cstdio ...

  6. Python:基本语法1

    I.Python中的转义符注意情况 如果'本身是一个字符,则可将其用" "括起来: 如果字符串内部既有',又有",则可用转义字符\,比如: 'I\'m\"OK\ ...

  7. Html报表用Excel打开保持表格线【Html报表模板】

    注:本人调试的最简版,前两处红色部分是为了输出Excel表格线:x:str表示输出为文本样式,避免被输出为科学计数法. <!DOCTYPE html PUBLIC "-//W3C//D ...

  8. mysql 目录的了解以及Linux

    Linux   目录                描述 /                                                                       ...

  9. Open Xml 读取Excel中的图片

      在我的一个项目中,需要分析客户提供的Excel, 读出其中的图片信息(显示在Excel的第几行,第几列,以及图片本身). 网络上有许多使用Open Xml插入图片到Word,Excel的文章, 但 ...

  10. Error:failed to find Build Tools revision 23.0.0 rc3

    解决,选择AS里有的版本就可以了,已有的我这就一个23.0.3,导入的项目是23.0.2 Donate:)