怎样避免Manager应用被人利用

发布时间:2021-12-21 11:48:29 作者:柒染
来源:亿速云 阅读:113

怎样避免Manager应用被人利用,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

使用Tomcat时,你一定发现Tomcat的webapps目录中自带了许多的应用,有演示特性与样例的,有进行应用管理的等等,这其中就包含Manager应用。

我们前面的文章曾经分析过Manager应用的内部实现,具体可以移步这里查看:

深入Tomcat的Manager

在Tomcat的manager应用的META-INF/context.xml中,有这样一行注释:

  <!--

  Remove the comment markers from around the Valve below to limit access to

  the manager application to clients connecting from localhost

  -->

  <!--

  <Valve className="org.apache.catalina.valves.RemoteAddrValve"

  allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

  -->

在Tomcat的邮件组里刚好有人也在问这个问题。提问者说有人在猜他的管理员密码,想通过这个,登录到Manager应用。而Manager应用可以直接控制容器内应用的生命周期,可以直接进行Tomcat内应用的启动停止和解部署等,还是很危险的。

通过上面的context.xml中的配置,可以限制只有本地才能访问manager应用,这样除非你的主机被hack掉,否则manager应用还是不会被直接利用的。

这样就解决了Manager应用被非法利用的危险。

下面我们来深入源码,来了解下Tomcat内部是如何进行处理来实现的该功能。

通过上面的配置内容,我们能看出,实现的本质是基于Tomcat的Valve组件来进行请求的过滤处理的。关于Valve之前也曾写过内容:

Tomcat的AccessLogValve介绍

而本次在RemoteAddrValve中,调用的invoke方法注释是这样写的:

/**
* Extract the desired request property, and pass it (along with the
* specified request and response objects) to the protected
* <code>process()</code> method to perform the actual filtering.
* This method must be implemented by a concrete subclass.
*/

也就是解析出需要的参数,传到process方法中。这个方法是其父类

RequestFilterValve的方法,传入的参数是request中的远程请求地址:

request.getRequest().getRemoteAddr();

再看process方法,内容如下:

void process(String property, Request request, Response response) {

   if (isAllowed(property)) {
       getNext().invoke(request, response);
       return;
   }
   // Deny this request
   denyRequest(request, response);
}

基本逻辑类于我们常说的黑名单白名单。可以配置哪些是允许的,哪些是禁止的。

再翻到上面看Manager应用的配置,是配置了allow属性,设置了允许的请求地址,其它不在此范围的请求都会被拒绝。

isAllow方法,在判断时使用java.util.regex进行正则的判断。首先是根据配置的是allow还是deny进行具体的property解析和匹配。

public boolean isAllowed(String property) {
   // Use local copies for thread safety
   Pattern deny = this.deny;
   Pattern allow = this.allow;
   // Check the deny patterns, if any
   if (deny != null && deny.matcher(property).matches()) {
       return false;
   }
   // Check the allow patterns, if any
   if (allow != null && allow.matcher(property).matches()) {
       return true;
   }
   // Allow if denies specified but not allows
   if (deny != null && allow == null) {
       return true;
   }
   // Deny this request
   return false;
}

对于RemoteFilterValve,在官方文档还有这样一个样例,可以在拒绝请求招待时,跳转到指定的端口。

<Valve className="org.apache.catalina.valves.RemoteAddrValve"

   addConnectorPort="true"

   allow="127\.\d+\.\d+\.\d+;\d*|::1;\d*|0:0:0:0:0:0:0:1;\d*|.*;8443"/>

通过分号进行分隔,后面跟上跳转的端口。

与RemoteAddrValve类似的,Tomcat还提供了一个RemoteHostValve,可以进行远程主机的过滤,配置与功能与我们上面的介绍基本一致。

Tomcat内置了丰富的Valve,可以进行多种情形下的应用。

和Tomcat学设计模式 | Facade模式与请求处理

关于怎样避免Manager应用被人利用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. Nexus Repository Manager的应用
  2. OracleVM Manager 安装

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

manager

上一篇:如何从尝试抛弃慢查询分析MYSQL

下一篇:IOS用户是如何录屏的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》