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
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. }
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
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. }
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() );
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值