当前位置:   article > 正文

antisamy的配置以及使用实现XSS防御_antisamy-esapi怎么防富文本

antisamy-esapi怎么防富文本


一、maven、antisamy介绍以及XSS:

  antisamy是owasp的开源项目,它用来确保用户输入的HTML/CSS符合应用规范的API,可以有效防止xss攻击。它提供了用于验证用户输入的富文本以防御跨站脚本的API,适用于java编写的web项目。它提供了一些标准策略文件,根据自己产品的实际需求,在此基础上配置一份适合自己产品的策略文件。

具体参考

http://anquan.163.com/module/pedia/article-00016.html

二、所需的相关文件:


三、antisamy在eclipse的配置

   

注意Tomcat应用服务器的安装。具体详见 http://jingyan.baidu.com/article/3065b3b6efa9d7becff8a4c6.html

转换为maven项目后发现在Libraries下为发现maven的下拉菜单,如下图所示:

解决方法:

修改pom.xml中的代码,即增加以下代码:

  1. <dependencies>
  2. <dependency>
  3. <groupId>log4j</groupId>
  4. <artifactId>log4j</artifactId>
  5. <version>1.2.12</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.owasp.antisamy</groupId>
  9. <artifactId>antisamy</artifactId>
  10. <version>1.5.3</version>
  11. </dependency>
  12. </dependencies>
保存后刷新项目即可以看到maven下出现了相关的jar文件,即已经将该jar包进行了下载,而不需要自己在下载在加入path路径:


此时,即将maven和antisamy配置完成。

整体截图:


pom.xml代码:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>webTest</groupId>
  <artifactId>webTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
   </build>
	<dependencies>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.12</version>
		</dependency>
		<dependency>
			<groupId>org.owasp.antisamy</groupId>
			<artifactId>antisamy</artifactId>
			<version>1.5.3</version>
		</dependency>
	</dependencies>
</project>

增加了以下代码:


四、tomcat安装

这里依赖于【eclipse创建javaweb项目的环境配置】

具体参见http://blog.csdn.net/redarmy_chen/article/details/7048317

也可以参照以下链接安装和部署:

http://jingyan.baidu.com/article/3065b3b6efa9d7becff8a4c6.html

需要注意的是在添加目录时要采用英文名。


五、代码

XssFilter.java代码如下:(注意代码的包的)

  1. import java.io.IOException;
  2. import javax.servlet.Filter;
  3. import javax.servlet.FilterChain;
  4. import javax.servlet.FilterConfig;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.ServletRequest;
  7. import javax.servlet.ServletResponse;
  8. import javax.servlet.http.HttpServletRequest;
  9. public class XssFilter implements Filter {
  10. @SuppressWarnings("unused")
  11. private FilterConfig filterConfig;
  12. public void destroy() {
  13. this.filterConfig = null;
  14. }
  15. public void doFilter(ServletRequest request, ServletResponse response,
  16. FilterChain chain) throws IOException, ServletException {
  17. chain.doFilter(new RequestWrapper((HttpServletRequest) request), response);
  18. }
  19. public void init(FilterConfig filterConfig) throws ServletException {
  20. this.filterConfig = filterConfig;
  21. }
  22. }

相关代码的注释可以参见:

http://blog.csdn.net/goskalrie/article/details/51350736

RequestWrapper.java代码:

  1. import java.util.Iterator;
  2. import java.util.Map;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletRequestWrapper;
  5. import org.owasp.validator.html.AntiSamy;
  6. import org.owasp.validator.html.CleanResults;
  7. import org.owasp.validator.html.Policy;
  8. import org.owasp.validator.html.PolicyException;
  9. import org.owasp.validator.html.ScanException;
  10. public class RequestWrapper extends HttpServletRequestWrapper {
  11. public RequestWrapper(HttpServletRequest request) {
  12. super(request);
  13. }
  14. @SuppressWarnings({ "rawtypes", "unchecked" })
  15. public Map<String,String[]> getParameterMap(){
  16. Map<String,String[]> request_map = super.getParameterMap();
  17. Iterator iterator = request_map.entrySet().iterator();
  18. while(iterator.hasNext()){
  19. Map.Entry me = (Map.Entry)iterator.next();
  20. //System.out.println(me.getKey()+":");
  21. String[] values = (String[])me.getValue();
  22. for(int i = 0 ; i < values.length ; i++){
  23. System.out.println(values[i]);
  24. values[i] = xssClean(values[i]);
  25. }
  26. }
  27. return request_map;
  28. }
  29. @SuppressWarnings({ "rawtypes", "unchecked" })
  30. public String getParameter(String name) {
  31. String v=super.getParameter(name);
  32. if(v==null)
  33. return null;
  34. return xssClean(v);
  35. }
  36. @SuppressWarnings({ "rawtypes", "unchecked" })
  37. public String[] getParameterValues(String name) {
  38. String[] v=super.getParameterValues(name);
  39. if(v==null || v.length==0)
  40. return v;
  41. for(int i=0;i<v.length;i++){
  42. v[i]=xssClean(v[i]);
  43. }
  44. return v;
  45. }
  46. private String xssClean(String value) {
  47. AntiSamy antiSamy = new AntiSamy();
  48. try {
  49. Policy policy = Policy.getInstance("/antisamy-slashdot.xml");
  50. //CleanResults cr = antiSamy.scan(dirtyInput, policyFilePath);
  51. final CleanResults cr = antiSamy.scan(value, policy);
  52. //瀹夊叏鐨凥TML杈撳嚭
  53. System.out.println("clean:"+cr.getCleanHTML());
  54. return cr.getCleanHTML();
  55. } catch (ScanException e) {
  56. e.printStackTrace();
  57. } catch (PolicyException e) {
  58. e.printStackTrace();
  59. }
  60. return value;
  61. }
  62. }

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>sdl</display-name>
	<!-- XSS -->
	<filter>
		<filter-name>XSS</filter-name>
		<filter-class>XssFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>XSS</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>


六、验证


htmlTest.html代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <form action="main.jsp" method="POST">
  9. First Name: <input type="text" name="first_name">
  10. <br />
  11. Last Name: <input type="text" name="last_name" />
  12. <input type="submit" value="Submit" />
  13. </form>
  14. </body>
  15. </html>

如下所示:


main.jsp代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>Using GET Method to Read Form Data</h1>
<ul>
<li><p><b>First Name:</b>
   <%= request.getParameter("first_name")%>
</p></li>
<li><p><b>Last  Name:</b>
   <%= request.getParameter("last_name")%>
</p></li>
</ul>
</body>
</html>

如下所示:



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/150582
推荐阅读
相关标签
  

闽ICP备14008679号