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

参考资料

1 跨网站脚本http://zh.wikipedia.org/wiki/XSS

2http://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. 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

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

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. }



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

上传包吧,方便

XSS之xssprotect相关推荐

  1. 跨站点脚本(XSS)

    1. 简介 跨站点脚本(XSS)是当前web应用中最危险和最普遍的漏洞之一.安全研究人员在大部分最受欢迎的网站,包括Google, Facebook, Amazon, PayPal等网站都发现这个漏洞 ...

  2. XSS CSRF 攻击

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

  3. 86.3 安全性问题 xss、DDOS、CC、sql注入 攻击等

    主键id 暴露在url 中会暴露表的总量,而且如果监视一段时间主键,就能很低成本地得到这个网站的用户增量. Cookie中的信息是明文保存的,意味着攻击者可以通过猜测并伪造Cookie数据破解系统. ...

  4. 阿里云服务器web应用安全-XSS攻击

    以前听过XSS攻击,但是因为只是公司中众多码农中的小小一枚,几乎没有机会亲身体验过XSS攻击.由于最近机缘巧合,帮亲戚在阿里云esc上搭建了一套web应用系统,碰上了一系列安全问题,这个XSS就是其中 ...

  5. csrf和xss攻击

    XSS:跨站脚本(Cross-site scripting) CSRF:跨站请求伪造(Cross-site request forgery) 在那个年代,大家一般用拼接字符串的方式来构造动态SQL 语 ...

  6. WEB安全的防御--介绍XSS跨网站脚本[wiki]

    跨网站脚本 维基百科,自由的百科全书 跳转至: 导航. 搜索 本条目可通过翻译英语维基百科的相应条目来获得改善. 请在翻译前点击右边的"显示▼"了解重要说明.显示▼ 浏览英语条目的 ...

  7. 安全测试之xss攻击和mysql注入

    xss概念: xss(Cross Site Script)跨站脚本攻击,为不和层叠样式表(css)混淆,写为xss 存在位置:web应用系统最常见软件安全漏洞 后果:代码植入到系统页面,篡改数据.盗取 ...

  8. jQuery.append()、jQuery.html()存在的XSS漏洞

    使用jQuery.append().jQuery.html()方法时,如果其中内容包含<script>脚本而没有经过任何处理的话,会执行它. 简单的示例代码如下: 1 var xssStr ...

  9. 跨站脚本攻击(XSS)FAQ

    原作者charlee.原始链接http://tech.idv2.com/2006/08/30/xss-faq/以及本声明. 该文章简单地介绍了XSS的基础知识及其危害和预防方法.Web开发人员的必读. ...

最新文章

  1. idea本地跑如何看gc日志_不可思议,竟然还有人不会查看GC垃圾回收日志?
  2. 解析Java多线程的两点误区你必知的
  3. STM32 电机教程 29 - 无刷无感入门1
  4. CCF NOI plus 201(7)6 初赛题 解题报告
  5. 中文排版规则_非设计师的5条排版规则
  6. linux u盘加载阵列卡驱动步骤,Linux U盘加载阵列卡驱动步骤
  7. 修改路由器mac地址_你知道吗:路由器转发报文时,会剥掉MAC地址,重新封装
  8. 双向板受力特点_弹性减震球形钢支座/双向弹簧铰支座特性
  9. MyBatis简介及下载
  10. 致远项目管理SPM系统案例:华仁药业股份有限公司合同管理
  11. Linux-you need at least 8.6GB disk space to install Ubuntu,this computer has only 8GB
  12. kk5.0服务器信息怎么填,蓝凌KK5.0:企业大连接的IT落地支撑平台
  13. 【TS】1303- TypeScript 4.7 beta 发布,几个重要的更新
  14. 国庆车流激增,南京启用无人机报路况
  15. ext3文件系统基础
  16. 哈工大机器人章丘_重磅!哈工大机器人、华侨城、明水古城……章丘区春季开工16个项目,总投资1000亿元...
  17. 关于PS中RGB和CMYK的区别
  18. Union can only be performed on tables with the compatible column types
  19. 互联网大佬饭局篇之马云
  20. 前端与UI设计师的区别

热门文章

  1. iOS之深入解析Runtime的Method-Swizzling方法交换的妙用和底层原理
  2. LeetCode 算法 856. 括号的分数
  3. 计算机精英协会考核题 —— 第一题:厄密多项式
  4. 征战蓝桥 —— 2013年第四届 —— C/C++A组第10题——大臣的旅费
  5. 2015年第六届蓝桥杯 - 省赛 - C/C++大学A组 - A. 方程整数解
  6. 计算机组成原理简单选择题,计算机组成原理选择题及答案.doc
  7. jenkins 启动_CentOS 7 安装 Jenkins
  8. java后台面试自我介绍_java腾讯远程面试后台研发岗面试题分享
  9. html 整行选择状态,Layui表格选中指定行的radio单选框并滚动到该行的实现代码
  10. php 高级特性,PHP对象、模式与实践之高级特性分析