打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
shiro安全框架扩展教程--如何防止可执行文件的入侵攻击

        前面的教程有一章是讲解如何突破上传的,当被人通过上传功能突破的防线那就杯具了,有点hack知识的人都知道,很多攻击都是优先寻找上传的功能,因为能突破

就会剩下很多的功夫,比如hack上传了一个asp,php或者jsp文件,然后通过抓包路径获取了文件存放地址,然后直接请求就能通过这个可执行的文件获取到数据库的信息,

或者是遍历目录下载文件,寻找文件中的其他漏洞以获得更高的权限,下面我就演示下简单的防范手段,就算被突破了上传也会有下一堵墙在一定程度上防止执行脚本


我主要是使用shiro写了一个filter过滤需要请求信息,如遇到黑名单则记录信息,看下面贴的代码


  1. package com.silvery.security.shiro.filter;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. import javax.servlet.ServletRequest;  
  7. import javax.servlet.ServletResponse;  
  8. import javax.servlet.http.HttpServletRequest;  
  9.   
  10. import org.apache.shiro.web.filter.authz.AuthorizationFilter;  
  11. import org.slf4j.Logger;  
  12. import org.slf4j.LoggerFactory;  
  13.   
  14. import com.silvery.utils.PatternUtils;  
  15. import com.silvery.utils.WebUtils;  
  16.   
  17. /**  
  18.  *   
  19.  * 黑名单可执行程序请求过滤器  
  20.  *   
  21.  * @author shadow  
  22.  *   
  23.  */  
  24. public class SimpleExecutiveFilter extends AuthorizationFilter {  
  25.   
  26.     protected static final String[] blackUrlPathPattern = new String[] { "*.aspx*", "*.asp*", "*.php*", "*.exe*",  
  27.             "*.jsp*", "*.pl*", "*.py*", "*.groovy*", "*.sh*", "*.rb*", "*.dll*", "*.bat*", "*.bin*", "*.dat*",  
  28.             "*.bas*", "*.c*", "*.cmd*", "*.com*", "*.cpp*", "*.jar*", "*.class*", "*.lnk*" };  
  29.   
  30.     private static final Logger log = LoggerFactory.getLogger(SimpleExecutiveFilter.class);  
  31.   
  32.     @Override  
  33.     protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object obj) throws Exception {  
  34.   
  35.         HttpServletRequest httpRequest = (HttpServletRequest) request;  
  36.   
  37.         String reqUrl = httpRequest.getRequestURI().toLowerCase().trim();  
  38.   
  39.         for (String pattern : blackUrlPathPattern) {  
  40.             if (PatternUtils.simpleMatch(pattern, reqUrl)) {  
  41.                 log.error(new StringBuffer().append("unsafe request >>> ").append(" request time: ").append(  
  42.                         new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).append("; request ip: ")  
  43.                         .append(WebUtils.getClientIP()).append("; request url: ").append(httpRequest.getRequestURI())  
  44.                         .toString());  
  45.                 return false;  
  46.             }  
  47.         }  
  48.   
  49.         return true;  
  50.   
  51.     }  
  52.   
  53. }  


下一步把刚刚写的过滤器配置到shiro的过滤链中


  1. <!-- 过滤链配置 -->  
  2.     <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  3.         <property name="securityManager" ref="securityManager" />  
  4.         <property name="loginUrl" value="/" />  
  5.         <property name="successUrl" value="/cms/index.do" />  
  6.         <property name="unauthorizedUrl" value="/static/unauthorized.html" />  
  7.         <property name="filters">  
  8.             <map>  
  9.                 <entry key="role">  
  10.                     <bean  
  11.                         class="com.silvery.security.shiro.filter.SimpleRoleAuthorizationFilter" />  
  12.                 </entry>  
  13.                 <entry key="authc">  
  14.                     <bean  
  15.                         class="com.silvery.security.shiro.filter.SimpleFormAuthenticationFilter" />  
  16.                 </entry>  
  17.                 <entry key="exec">  
  18.                     <bean class="com.silvery.security.shiro.filter.SimpleExecutiveFilter" />  
  19.                 </entry>  
  20.             </map>  
  21.         </property>  
  22.     </bean>  

最后配置下我们需要过滤的请求目录,一般都是全量过滤,但是有些静态资源是不应该过滤的,所以应该注意顺序,让anon权限的放到放到exec的前面


  1. <!-- 权限资源配置 -->  
  2.     <bean id="filterChainDefinitionsService"  
  3.         class="com.silvery.security.shiro.service.impl.SimpleFilterChainDefinitionsService">  
  4.         <property name="definitions">  
  5.             <value>  
  6.                 /static/** = anon  
  7.                 /** = exec  
  8.             </value>  
  9.         </property>  
  10.     </bean>  

最后请求下php,jsp等那些文件是返回到无权限的页面,我们的简单防范已经达到目的了,下一章节可能讲如何防范xss和csrf攻击的防范


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Shiro security限制登录尝试次数
shiro实现不同身份使用不同Realm进行验证
shiro spring
springrain技术详解(1)
基于Spring框架的Shiro配置
Spring的date类型注入
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服