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
- package com.xss.example;
- import java.io.IOException;
- import java.io.StringReader;
- import java.io.StringWriter;
- import com.blogspot.radialmind.html.HTMLParser;
- import com.blogspot.radialmind.html.HandlingException;
- import com.blogspot.radialmind.xss.XSSFilter;
- public class XSSTest {
- public static void main(String[] args) {
- String html = "<html><head> <title> New Document </title> " +
- "<script type='text/javascript'> alert('dddd'); </script> " +
- "</head> <body>" +
- "222 <iframe src='www.google.com'/> 1111" +
- "<embed ></embed>" +
- "<link>ddd</link>" +
- "</body></html>";
- String v = protectAgainstXSS(html);
- System.out.println(v);
- }
- public static String protectAgainstXSS( String html ) {
- StringReader reader = new StringReader( html );
- StringWriter writer = new StringWriter();
- String text = null;
- try {
- // Parse incoming string from the "html" variable
- HTMLParser.process( reader, writer, new XSSFilter(), true );
- // Return the parsed and cleaned up string
- text = writer.toString();
- } catch (HandlingException e) {
- // Handle the error here in accordance with your coding policies...
- }finally{
- try {
- writer.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return text;
- }
- }
在它的源代码中有二个类需要关注下:
BaseTestCase.java
- public abstract class BaseTestCase extends TestCase {
- protected void testExecute( String html, String result ) {
- StringReader reader = new StringReader( html );
- StringWriter writer = new StringWriter();
- try {
- HTMLParser.process( reader, writer, new XSSFilter(), true );
- String buffer = new String( writer.toString() );
- System.out.println( buffer );
- assertEquals( result, buffer );
- } catch (HandlingException e) {
- e.printStackTrace();
- fail( e.getMessage() );
- }
- }
- }
XSSFilter.java
- /**
- * Copyright 2008 Gerard Toonstra
- *
- * As an exception, this particular file
- * in the project is public domain to allow totally
- * free derivations of this code.
- *
- */
- package com.blogspot.radialmind.xss;
- import java.util.HashSet;
- import java.util.Set;
- import com.blogspot.radialmind.html.IHTMLFilter;
- /**
- * Implementation of a relatively simple XSS filter. This implementation removes
- * dangerous tags and attributes from tags. It does not verify the validity of
- * URL's (that may contain links to JavaScript for example). It does not remove all
- * event handlers that may still contain XSS vulnerabilities.
- *
- * Embedded objects are removed because those may contain XSS vulnerabilities in
- * their own scripting language (ActionScript for Flash for example).
- *
- * Feel free to derive your own implementation from this file.
- *
- * @author gt
- *
- */
- public class XSSFilter implements IHTMLFilter {
- private static final Set<String> FORBIDDEN_TAGS = new HashSet<String>();
- // The tags to be removed. Case insensitive of course.
- static {
- FORBIDDEN_TAGS.add( "script" );
- FORBIDDEN_TAGS.add( "embed" );
- FORBIDDEN_TAGS.add( "object" );
- FORBIDDEN_TAGS.add( "layer" );
- FORBIDDEN_TAGS.add( "style" );
- FORBIDDEN_TAGS.add( "meta" );
- FORBIDDEN_TAGS.add( "iframe" );
- FORBIDDEN_TAGS.add( "frame" );
- FORBIDDEN_TAGS.add( "link" );
- FORBIDDEN_TAGS.add( "import" );
- FORBIDDEN_TAGS.add( "xml" );
- }
- /**
- * This function is called to determine if an attribute should be filtered or not.
- *
- * @param tagName The name of the tag the attribute belongs to
- * @param attrName The name of the attribute to be filtered
- * @param attrValue The value of the attribute
- */
- public boolean filterAttribute(String tagName, String attrName, String attrValue) {
- if ( attrName.toLowerCase().startsWith( "on" )) {
- return true;
- }
- return isScriptedAttributeValue( attrValue );
- }
- /**
- * This method is called to determine if a tag should be filtered
- *
- * @param tagName The name of the tag that was parsed
- */
- public boolean filterTag(String tagName) {
- if ( FORBIDDEN_TAGS.contains( tagName )) {
- return true;
- }
- return false;
- }
- /**
- * This method is called to modify attribute values, if required
- *
- * @param tagName The name of the tag the attribute belongs to
- * @param attrName The name of the attribute within the tag
- * @param attrValue The value of the attribute
- */
- public String modifyAttributeValue(String tagName, String attrName, String attrValue) {
- return attrValue;
- }
- /**
- * This method is called to be able to modify the text of a node.
- *
- * @param tagName The name of the tag where the text is part of.
- * @param text The value of the text within the tagnode (within <tag>...</tag>)
- */
- public String modifyNodeText(String tagName, String text) {
- return text;
- }
- /**
- * Private method that determines if an attribute value is scripted
- * (potentially loaded with an XSS attack vector).
- *
- * @param attrValue The value of the attribute
- * @return "true" if the attribute is scripted. "false" if not.
- */
- private boolean isScriptedAttributeValue( String attrValue ) {
- attrValue = decode( attrValue );
- attrValue = attrValue.trim().toLowerCase();
- if ( attrValue.contains( "javascript:" )) {
- return true;
- }
- if ( attrValue.contains( "mocha:" )) {
- return true;
- }
- if ( attrValue.contains( "eval" )) {
- return true;
- }
- if ( attrValue.contains( "vbscript:" )) {
- return true;
- }
- if ( attrValue.contains( "livescript:" )) {
- return true;
- }
- if ( attrValue.contains( "expression(" )) {
- return true;
- }
- if ( attrValue.contains( "url(" )) {
- return true;
- }
- if ( attrValue.contains( "&{" )) {
- return true;
- }
- if ( attrValue.contains( "&#" )) {
- return true;
- }
- return false;
- }
- /**
- * Private method to remove control characters from the value
- *
- * @param value The value being modified
- * @return The value free from control characters
- */
- private String decode( String value ) {
- value = value.replace("\u0000", "" );
- value = value.replace("\u0001", "" );
- value = value.replace("\u0002", "" );
- value = value.replace("\u0003", "" );
- value = value.replace("\u0004", "" );
- value = value.replace("\u0005", "" );
- value = value.replace("\u0006", "" );
- value = value.replace("\u0007", "" );
- value = value.replace("\u0008", "" );
- value = value.replace("\u0009", "" );
- value = value.replace("\n", "" );
- value = value.replace("\u000B", "" );
- value = value.replace("\u000C", "" );
- value = value.replace("\r", "" );
- value = value.replace("\u000E", "" );
- value = value.replace("\u000F", "" );
- value = value.replace("\u0010", "" );
- value = value.replace("\u0011", "" );
- value = value.replace("\u0012", "" );
- value = value.replace("\u0013", "" );
- value = value.replace("\u0014", "" );
- value = value.replace("\u0015", "" );
- value = value.replace("\u0016", "" );
- value = value.replace("\u0017", "" );
- value = value.replace("\u0018", "" );
- value = value.replace("\u0019", "" );
- value = value.replace("\u001A", "" );
- value = value.replace("\u001B", "" );
- value = value.replace("\u001C", "" );
- value = value.replace("\u001D", "" );
- value = value.replace("\u001E", "" );
- value = value.replace("\u001F", "" );
- return value;
- }
- }
通过这个过滤就知道它要做什么了![]()
上传包吧,方便

- 大小: 79.9 KB
- xssProtect-0.1.jar (39.8 KB)
- 下载次数: 1113
- antlr-3.0.jar (533.7 KB)
- 下载次数: 745
转自:链接
XSS之xssprotect(转)的更多相关文章
- 漏洞科普:对于XSS和CSRF你究竟了解多少
转自:http://www.freebuf.com/articles/web/39234.html 随着Web2.0.社交网络.微博等等一系列新型的互联网产品的诞生,基于Web环境的互联网应用越来越广 ...
- 跨站脚本 XSS<一:防御方法>
1. 过滤特殊字符 避免XSS的方法之一主要是将用户所提供的内容进行过滤,许多语言都有提供对HTML的过滤: PHP的htmlentities()或是htmlspecialchars(). Pytho ...
- 跨站脚本 XSS<一:介绍>
*XSS 利用的是用户对指定网站的信任,CSRF 利用的是网站对用户网页浏览器的信任 跨站脚本(Cross-site scripting,通常简称为XSS)是一种网站应用程序的安全漏洞攻击,是代码注入 ...
- XSS 和 CSRF 攻击
web安全中有很多种攻击手段,除了SQL注入外,比较常见的还有 XSS 和 CSRF等 一.XSS(Cross Site Scripting)跨站脚本 XSS其实就是Html的注入问题,攻击者的输入没 ...
- XSS CSRF 攻击
XSS:跨站脚本(Cross-site scripting) CSRF:跨站请求伪造(Cross-site request forgery)定义: 跨网站脚本(Cross-site scripting ...
- XSS攻击原理及防御措施
概述 XSS攻击是Web攻击中最常见的攻击方法之一,它是通过对网页注入可执行代码且成功地被浏览器 执行,达到攻击的目的,形成了一次有效XSS攻击,一旦攻击成功,它可以获取用户的联系人列 表,然后向联系 ...
- 网络XSS攻击和CSRF攻击原理及防范
网络XSS攻击和CSRF攻击原理及防范 原文地址:http://www.freebuf.com/articles/web/39234.html 随着Web2.0.社交网络.微博等等一系列新型的互联网产 ...
- XSS/XSRF
一.XSS 1.1 xss的含义 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为 ...
- 浅谈XSS攻击原理与解决方法
概述 XSS攻击是Web攻击中最常见的攻击方法之一,它是通过对网页注入可执行代码且成功地被浏览器 执行,达到攻击的目的,形成了一次有效XSS攻击,一旦攻击成功,它可以获取用户的联系人列表,然后向联系人 ...
随机推荐
- .htaccess语法之RewriteCond与RewriteRule指令格式详细解释
htaccess语法之RewriteCond与RewriteRule指令格式详细解释 (2012-11-09 18:09:08) 转载▼ 标签: htaccess it 分类: 网络 上文htacc ...
- 在使用 vscode 时 eslint 检测 .vue 文件中的less 部分内容
问题: 在使用 vscode 以及 eslint 来检测 基于 webpack 的 vue-cli 的项目中,eslint 无法检测到 .vue 文件中的less 部分内容. 解答: 1.通过 下载 ...
- MATLAB-RSP 随笔
1.Getting Started with Signal Analyzer App 161216 (1) 在命令行中输入:signalAnalyzer. (2)在工作空间中选择要分析的信号
- c/c++ long long 和__64int区别
在C/C++中,64为整型一直是一种没有确定规范的数据类型.现今主流的编译器中,对64为整型的支持也是标准不一,形态各异.一般来说,64位整型的定义方式有long long和__int64两种(VC还 ...
- markdown语法与使用
Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 语法 # 文本 =>h1标签 ##文本 =>h2标签 *文本* =&g ...
- shell脚本重新挂载出问题的卷
#!/bin/bash#卷的全路径示例#pathexample =mount -t cetusfs 127.0.0.1:/cinderv/var/lib/cinder/volumes/0f6a20f4 ...
- MySQL日志
在MySQL中共有4中日志:错误日志.二进制日志.查询日志和慢查询日志 一.错误日志 错误日志名 host_name.err,并默认在参数DATADIR指定的目录中写入日志文件.可使用 --log-e ...
- webstorm快捷键大全
使用webstorm一段时间了,这里分享一下常用到的快捷键,不用死记,孰能生巧! Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*-*/ ) Shift+F6 重构-重命名 Ctrl+ ...
- 分页查询和分页缓存查询,List<Map<String, Object>>遍历和Map遍历
分页查询 String sql = "返回所有符合条件记录的待分页SQL语句"; int start = (page - 1) * limit + 1; int end = pag ...
- (转)System.Drawing.Color的颜色对照表
经常使用System.Drawing.Color, 本篇介绍一下颜色与名称及RGB值的对应关系. 1. 颜色与名称的对照表(点击下图放大看): 2. 颜色与RGB值对照表: Color.AliceBl ...