当前位置:   article > 正文

ESAPI——预防XSS攻击工具使用简介_esapi xss

esapi xss

XSS:跨站脚本攻击。原理是攻击者向有XSS漏洞的网站中输入恶意的HTML代码,当其它用户浏览该网站时,这段HTML代码会自动执行,从而达到攻击的目的。如,盗取用户Cookie、破坏页面结构、重定向到其它网站等。

最常见的最经典的XSS bug语句<script>alert(/XSS/)</script> 比如在存在XSS bug的网站的输入框输入前面的语句,当访问网页时会弹出对话框。

...............

本篇文章主要针对最近使用过的防御XSS的小工具——ESAPI,使用的是maven项目;在pom.xml 中加入依赖:

  1. <dependency>
  2. <groupId>org.owasp.esapi</groupId>
  3. <artifactId>esapi</artifactId>
  4. <version>2.1.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.jsoup</groupId>
  8. <artifactId>jsoup</artifactId>
  9. <version>1.7.3</version>
  10. </dependency>
在classpath下加入配置文件:validation.properties和ESAPI.properties

编写filter过滤器ManageSecurityFilter类实现 Filter接口,对所有后台请求使用filter过滤,在filter中将request中有隐患的关键字过滤掉。

  1. import java.io.IOException;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. import javax.servlet.Filter;
  5. import javax.servlet.FilterChain;
  6. import javax.servlet.FilterConfig;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.ServletRequest;
  9. import javax.servlet.ServletResponse;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. /**
  13. * XSS安全过滤器
  14. *
  15. * @author wjl
  16. * @date 2014-4-10 下午2:12:02
  17. */
  18. public class ManageSecurityFilter implements Filter {
  19. private static final String FILTER_APPLIED = ManageSecurityFilter.class.getName() + ".FILTERED";
  20. private Set<String> excludePathRegex = new HashSet<String>();
  21. public void setExcludePathRegex( Set<String> excludePathRegex ) {
  22. this.excludePathRegex = excludePathRegex;
  23. }
  24. @Override
  25. public void init( FilterConfig filterConfig ) throws ServletException {}
  26. @Override
  27. public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
  28. if( !( request instanceof HttpServletRequest ) || !( response instanceof HttpServletResponse ) ) {
  29. throw new ServletException( "XSSFilter just supports HTTP requests" );
  30. }
  31. HttpServletRequest httpRequest = ( HttpServletRequest )request;
  32. String uri = httpRequest.getRequestURI();
  33. for( String regex : excludePathRegex ) {
  34. if( uri.matches( regex ) ) {
  35. chain.doFilter( request, response );
  36. return;
  37. }
  38. }
  39. // Apply Filter
  40. if( null != httpRequest.getAttribute( FILTER_APPLIED ) ) {
  41. chain.doFilter( request, response );
  42. return;
  43. }
  44. try {
  45. request.setAttribute( FILTER_APPLIED, Boolean.TRUE );
  46. SecurityRequestWrapper requestWrapper = new SecurityRequestWrapper( httpRequest );
  47. chain.doFilter( requestWrapper, response );
  48. } finally {
  49. httpRequest.removeAttribute( FILTER_APPLIED );
  50. }
  51. }
  52. @Override
  53. public void destroy() {}
  54. }
在编写 使用了ESAPI的类SecurityRequestWrapper
  1. import javax.servlet.http.HttpServletRequest;
  2. import javax.servlet.http.HttpServletRequestWrapper;
  3. import org.jsoup.Jsoup;
  4. import org.jsoup.nodes.Document.OutputSettings;
  5. import org.jsoup.safety.Whitelist;
  6. import org.owasp.esapi.ESAPI;
  7. public class SecurityRequestWrapper extends HttpServletRequestWrapper {
  8. private final static Whitelist WHITELIST = Whitelist.relaxed();
  9. private final static OutputSettings OUTPUTSETTINGS = new OutputSettings().prettyPrint( false );
  10. static {
  11. WHITELIST.addTags( "embed", "object", "param", "span", "div", "img" );
  12. WHITELIST.addAttributes( ":all", "style", "class", "id", "name" );
  13. WHITELIST.addAttributes( "object", "width", "height", "classid", "codebase" );
  14. WHITELIST.addAttributes( "param", "name", "value" );
  15. WHITELIST.addAttributes( "embed", "src", "quality", "width", "height", "allowFullScreen",
  16. "allowScriptAccess", "flashvars", "name", "type", "pluginspage" );
  17. }
  18. public SecurityRequestWrapper( HttpServletRequest servletRequest ) {
  19. super( servletRequest );
  20. }
  21. @Override
  22. public String[] getParameterValues( String parameter ) {
  23. String[] values = super.getParameterValues( parameter );
  24. if( null == values ) {
  25. return null;
  26. }
  27. int count = values.length;
  28. String[] encodedValues = new String[ count ];
  29. for( int i = 0; i < count; i++ ) {
  30. encodedValues[ i ] = filterValue( values[ i ] );
  31. }
  32. return encodedValues;
  33. }
  34. @Override
  35. public String getParameter( String parameter ) {
  36. String value = super.getParameter( parameter );
  37. return filterValue( value );
  38. }
  39. @Override
  40. public String getHeader( String name ) {
  41. String value = super.getHeader( name );
  42. return filterValue( value );
  43. }
  44. private String filterValue( String value ) {
  45. if( null != value ) {
  46. // avoid encoded attacks.
  47. value = ESAPI.encoder().canonicalize( value );
  48. // Avoid null characters
  49. value = value.replaceAll( "\0", "" );
  50. value = value.replaceAll("<", "& lt;").replaceAll(">", "& gt;");
  51. value = value.replaceAll("\\(", "& #40;").replaceAll("\\)", "& #41;");
  52. value = value.replaceAll("'", "& #39;");
  53. value = value.replaceAll("eval\\((.*)\\)", "");
  54. value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
  55. value = value.replaceAll("script", "");
  56. // Clean out HTML
  57. value = Jsoup.clean( value, "", WHITELIST, OUTPUTSETTINGS );
  58. }
  59. return value;
  60. }
  61. }

配置web.xml文件,在web.xml文件中加入:

  1. <filter>
  2. <filter-name>manageSecurityFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>manageSecurityFilter</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>

配置spring配置文件,在servelt的配置文件中加入:

  1. <!-- Security的beans -->
  2. <bean id="manageSecurityFilter" class="com.baidu.disconf.web.security.ManageSecurityFilter">
  3. <property name="excludePathRegex">
  4. <set>
  5. <value>/console/compass/manage.*</value>
  6. <value>/console/coupon/.*</value>
  7. <value>/console/customize/block/.*</value>
  8. <value>/console/review/.*</value>
  9. <value>/console/ripple/.*</value>
  10. <value>/console/wdjcraw/.*</value>
  11. <value>/console/audit/reason/.*</value>
  12. <value>/console/audit/.*</value>
  13. </set>
  14. </property>
  15. </bean>







   



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

闽ICP备14008679号