內(nèi)容回顧 | 記一次Tomcat一句話木馬修復(fù)

tomcat啟動(dòng)過(guò)程的,只能把相關(guān)內(nèi)容推遲下。不過(guò)今天回顧的內(nèi)容也是和tomcat相關(guān)的,是在上家公司修復(fù)tomcat漏洞的一次記錄,各位小伙伴可以參考下。起因
近期,網(wǎng)絡(luò)中心反饋,這邊有一個(gè)服務(wù)器發(fā)現(xiàn)了一句話木馬,我想著應(yīng)該是我的服務(wù),因?yàn)橹斑@個(gè)服務(wù)就中過(guò)一次毒,后來(lái)升級(jí)了tomcat,刪除了無(wú)用的文件和配置。但是這次還是中毒了,查了一些資料,修改了配置,Tomcat也升級(jí)到最新版本,這次應(yīng)該不會(huì)再有問(wèn)題了,再有問(wèn)題的話,我再來(lái)繼續(xù)更新??
版本信息
Tomcat:9.0.34
配置過(guò)程
刪除管理及示例項(xiàng)目
進(jìn)入Tomcat安裝目錄
cd %tomcat_home%
刪除webapps下所有內(nèi)容(當(dāng)然自己的項(xiàng)目如果已經(jīng)部署過(guò)了,別刪除)
rm -rf *
為什么要?jiǎng)h除,因?yàn)閠omcat默認(rèn)部署了項(xiàng)目管理服務(wù)和示例項(xiàng)目,特別是管理模塊,很可能被非法利用
配置server.xml
文件路徑:
%tomcat_home%/config/server.xml
1、修改服務(wù)器端口
下面是默認(rèn)配置,改成你自己的端口號(hào):
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
比如
<Connector port="9999" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
2、修改服務(wù)器關(guān)閉命令及端口
下面是默認(rèn)配置,改成你自己的端口號(hào)和命令:
<Server port="8005" shutdown="SHUTDOWN">
比如
<Server port="9995" shutdown="AFADSLFJKDFJALSDF887676">
3、關(guān)閉熱部署
默認(rèn):
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
關(guān)閉:
<Host name="localhost" appBase="webapps"
unpackWARs="false" autoDeploy="false">
我覺得,這里應(yīng)該是很重要很關(guān)鍵的,之前我一直是啟用熱部署的,感覺很方便,但如果有人把惡意代碼傳到webapps下面,同樣也會(huì)自動(dòng)部署,這是很危險(xiǎn)的。還有需要注意的是,如果unpackWARs="false"就意味著你要自己手動(dòng)解壓war包,tomcat不會(huì)自動(dòng)解壓,這樣更安全,但是也有很多不變。linux環(huán)境的話,可以通過(guò)unzip解壓:
unzip -op *.war
4、關(guān)閉用戶配置
將如下配置注釋掉即可
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
修改web.xml
文件路徑:
%tomcat_home%/config/web.xml
1、增加錯(cuò)誤處理頁(yè)面
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/403.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
...
為什么要增加錯(cuò)誤處理頁(yè)面,我覺得第一個(gè)原因是避免泄露太多的錯(cuò)誤信息,比如Tomcat的版本信息,以及不想暴露的錯(cuò)誤信息;另一個(gè)原因是出于美觀考慮,自帶的錯(cuò)誤頁(yè)面太丑了
2、修改tomcat版本信息
為什么要修改呢,因?yàn)槿绻惚┞┝俗约旱膖omcat版本信息,而且你恰好有好多漏洞沒修復(fù),對(duì)方根據(jù)你的版本信息就能查到你可能有哪些潛在漏洞,但如果你隱藏了版本信息,想要發(fā)掘你的容器漏洞還是有很多難度的。下來(lái),我們看下如何修改:
首先,進(jìn)入Tomcat的lib目錄:
cd %tomcat_home%/lib
找到catalina.jar,用壓縮軟件打開,在jar包的如下路徑找到版本配置文件:ServerInfo.properties
org.apache.catalia.util
修改前的內(nèi)容如下:
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
server.info=Apache Tomcat/9.0.33
server.number=9.0.33.0
server.built=Mar 11 2020 09:31:38 UTC
然后自己可以隨意修改,比如:
server.info=Apache Tomcat/999999999999999
server.number=999999999999999999
server.built=999999999999999999999 UTC
然后再替換重新打包,當(dāng)然如果用的是壓縮軟件的話直接保存就好了。linux環(huán)境的話,可以將對(duì)應(yīng)的jar包拉下來(lái),替換后再上傳。
當(dāng)你修改完版本信息,tomcat的錯(cuò)誤頁(yè)再也看不到正確的版本信息了。
3、屏蔽jsp
之前項(xiàng)目有用到j(luò)sp,現(xiàn)在已經(jīng)改成html了,所以我可以屏蔽jsp文件。但是本次我沒有配置這個(gè),如果后面還有問(wèn)題的話,我會(huì)考慮這個(gè)方法。這次就不說(shuō)了,我也還沒查相關(guān)資料
總結(jié)
希望這次過(guò)后,服務(wù)不會(huì)再出現(xiàn)問(wèn)題。這次的問(wèn)題可能和三個(gè)月前剛發(fā)布的Tomcat漏洞有關(guān)系,因?yàn)檎f(shuō)的是AJP服務(wù),我看服務(wù)器的配置,沒有開啟這個(gè)配置,所以也就沒有升級(jí)服務(wù)器。
漏洞編號(hào):
CVE-2020-1938CNVD-202-1048
受影響的版本如下:
Apache Tomcat6Apache Tomcat7 < 7.0.100Apache Tomcat8 < 8.5.51Apache Tomcat9< 9.0.31
如果哪個(gè)小伙伴的服務(wù)器版本不幸在列的話,趕緊抓緊時(shí)間修復(fù)吧,別存在僥幸心理。
- END -