溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎樣避免Manager應(yīng)用被人利用

發(fā)布時(shí)間:2021-12-21 11:48:29 來源:億速云 閱讀:121 作者:柒染 欄目:大數(shù)據(jù)

怎樣避免Manager應(yīng)用被人利用,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

使用Tomcat時(shí),你一定發(fā)現(xiàn)Tomcat的webapps目錄中自帶了許多的應(yīng)用,有演示特性與樣例的,有進(jìn)行應(yīng)用管理的等等,這其中就包含Manager應(yīng)用。

我們前面的文章曾經(jīng)分析過Manager應(yīng)用的內(nèi)部實(shí)現(xiàn),具體可以移步這里查看:

深入Tomcat的Manager

在Tomcat的manager應(yīng)用的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的郵件組里剛好有人也在問這個(gè)問題。提問者說有人在猜他的管理員密碼,想通過這個(gè),登錄到Manager應(yīng)用。而Manager應(yīng)用可以直接控制容器內(nèi)應(yīng)用的生命周期,可以直接進(jìn)行Tomcat內(nèi)應(yīng)用的啟動(dòng)停止和解部署等,還是很危險(xiǎn)的。

通過上面的context.xml中的配置,可以限制只有本地才能訪問manager應(yīng)用,這樣除非你的主機(jī)被hack掉,否則manager應(yīng)用還是不會(huì)被直接利用的。

這樣就解決了Manager應(yīng)用被非法利用的危險(xiǎn)。

下面我們來深入源碼,來了解下Tomcat內(nèi)部是如何進(jìn)行處理來實(shí)現(xiàn)的該功能。

通過上面的配置內(nèi)容,我們能看出,實(shí)現(xiàn)的本質(zhì)是基于Tomcat的Valve組件來進(jìn)行請(qǐng)求的過濾處理的。關(guān)于Valve之前也曾寫過內(nèi)容:

Tomcat的AccessLogValve介紹

而本次在RemoteAddrValve中,調(diào)用的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.
*/

也就是解析出需要的參數(shù),傳到process方法中。這個(gè)方法是其父類

RequestFilterValve的方法,傳入的參數(shù)是request中的遠(yuǎn)程請(qǐng)求地址:

request.getRequest().getRemoteAddr();

再看process方法,內(nèi)容如下:

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

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

基本邏輯類于我們常說的黑名單白名單??梢耘渲媚男┦窃试S的,哪些是禁止的。

再翻到上面看Manager應(yīng)用的配置,是配置了allow屬性,設(shè)置了允許的請(qǐng)求地址,其它不在此范圍的請(qǐng)求都會(huì)被拒絕。

isAllow方法,在判斷時(shí)使用java.util.regex進(jìn)行正則的判斷。首先是根據(jù)配置的是allow還是deny進(jìn)行具體的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;
}

對(duì)于RemoteFilterValve,在官方文檔還有這樣一個(gè)樣例,可以在拒絕請(qǐng)求招待時(shí),跳轉(zhuǎn)到指定的端口。

<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"/>

通過分號(hào)進(jìn)行分隔,后面跟上跳轉(zhuǎn)的端口。

與RemoteAddrValve類似的,Tomcat還提供了一個(gè)RemoteHostValve,可以進(jìn)行遠(yuǎn)程主機(jī)的過濾,配置與功能與我們上面的介紹基本一致。

Tomcat內(nèi)置了豐富的Valve,可以進(jìn)行多種情形下的應(yīng)用。

和Tomcat學(xué)設(shè)計(jì)模式 | Facade模式與請(qǐng)求處理

關(guān)于怎樣避免Manager應(yīng)用被人利用問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI