RMI(Remote Method invocation),远程方法调用,顾名思义就是让java虚拟机调用另一个虚拟机中对象的方法。它通常用于构建java分布式应用程序。但是我们要知道的是,方法是在远程的jvm上执行然后返回结果给本地的jvm,这就意味着这里存在攻击这个远程jvm的可能
p神从一个例子开始讲解RMI
RMIServer
package tmp;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
public class RMIServer {
public interface IRemoteHelloWorld extends Remote {
public String hello() throws RemoteException;//继承Remote的接口
}
public class RemoteHelloWorld extends UnicastRemoteObject implements IRemoteHelloWorld {
protected RemoteHelloWorld() throws RemoteException{
super();
}
@Override
public String hello() throws RemoteException {
System.out.println("call from");
return "yes i know";
}
}
private void start() throws Exception{
RemoteHelloWorld h =new RemoteHelloWorld();
LocateRegistry.createRegistry(1099);//注册中心代码
Naming.rebind("rmi://127.0.0.1:1099/Hello",h);
}
public static void main(String[] args) throws Exception{
new RMIServer().start();
}
}
RMIClient
package tmp;
import java.rmi.Naming;
public class RMIClient {
public static void main(String[] args) throws Exception{
RMIServer.IRemoteHelloWorld hello =(RMIServer.IRemoteHelloWorld) Naming.lookup("sasdrmi://10.17.96.127:1099/Hello");
String ret= hello.hello();
System.out.print(ret);
}
}
RMI服务中一共有三个角色
- Registry 即注册中心,负责注册服务;(像个网关)
- Server 是提供服务的地方,真正实现约定接口的地方;
- Client 是你的调用方,需要使用服务的一方。
通常情况下rmi registry就在server里面
rmi调用过程中,远程的方法实际实在server这一端进行调用,也就是说client仅仅只是得到一个执行完成后的结果。因此我们就有可能实现对rmiregistry的攻击
先用list列出所有的方法,再用lookup调用其中的危险方法
当然也有一些在比较苛刻条件下能够使用的其他方法,p神列出的很详细,我这里就不写了