如何避免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,可以进行多种情形下的应用。


对于请求的处理与Valve的招待,请翻看前面的文章。

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

扫描或长按下方二维码,一起进步

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值