// Path: java/vulns-servlet/src/main/java/com/baidu/rasp/service/IHello.java
// public interface IHello extends java.rmi.Remote{
//     String sayHello(String name) throws RemoteException;
// }
// 
// Path: java/vulns-servlet/src/main/java/com/baidu/rasp/service/impl/HelloImpl.java
// public class HelloImpl extends UnicastRemoteObject implements IHello {
// 
// 
//     public HelloImpl() throws RemoteException {
//     }
// 
//     @Override
//     public String sayHello(String name) throws RemoteException {
//         return "Hi," + name;
//     }
// }
// 
// Path: java/vulns-servlet/src/main/java/com/baidu/rasp/task/JNDIBadTask.java
// public class JNDIBadTask implements Callable {
// 
//     private String host;
//     private int port;
//     private String serviceName;
// 
//     public JNDIBadTask(String host,int port,String serviceName){
//         this.host = host;
//         this.port = port;
//         this.serviceName = serviceName;
//     }
// 
//     @Override
//     public Object call() {
//         return CallBadService(this.host,this.port,this.serviceName);
//     }
// 
//     public static String CallBadService(String host,int port,String serviceName) {
//         String uri = "rmi://"+host +":"+port +"/"+serviceName;
//         Context ctx = null;
//         try {
//             ctx = new InitialContext();
//             ctx.lookup(uri);
//             return "调用成功";
//         } catch (NamingException e) {
//             e.printStackTrace();
//         }
//         return "调用失败";
//     }
// }
// 
// Path: java/vulns-servlet/src/main/java/com/baidu/rasp/task/JNDITask.java
// public class JNDITask implements Callable {
// 
//     private String host;
//     private int port;
//     private String serviceName;
// 
//     public JNDITask(String host, int port, String serviceName) {
//         this.host = host;
//         this.port = port;
//         this.serviceName = serviceName;
//     }
// 
//     @Override
//     public Object call() {
//         return CallService(this.host, this.port, this.serviceName);
//     }
// 
//     public static String CallService(String host, int port, String serviceName) {
//         try {
//             Registry registry = LocateRegistry.getRegistry(host, port);
//             IHello service = (IHello) registry.lookup(serviceName);
//             return service.sayHello("test");
//         } catch (RemoteException e) {
//             e.printStackTrace();
//         } catch (NotBoundException e) {
//             e.printStackTrace();
//         }
//         return "";
//     }
// }


import com.baidu.rasp.service.IHello;
import com.baidu.rasp.service.impl.HelloImpl;
import com.baidu.rasp.task.JNDIBadTask;
import com.baidu.rasp.task.JNDITask;
import com.sun.jndi.rmi.registry.ReferenceWrapper;
import javax.naming.Reference;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
package com.baidu.rasp;


public class JDNI extends HttpServlet {

    public ExecutorService executor = Executors.newFixedThreadPool(5);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // jdk1.8高版本请设置此属性,否则报如下错误
        // javax.naming.ConfigurationException: The object factory is untrusted. Set the system property 'com.sun.jndi.rmi.object.trustURLCodebase' to 'true'.
        System.setProperty("com.sun.jndi.rmi.object.trustURLCodebase","true");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");//告知浏览器编码方式;
        resp.setCharacterEncoding("UTF-8");
        String option = req.getParameter("option");
        String host = req.getParameter("host");
        if ("".equals(host)){
            host = "localhost";
        }
        int port = Integer.parseInt(req.getParameter("port"));
        String serviceName = req.getParameter("serviceName");
        if ("start".equals(option)){
            IHello service = new HelloImpl();
            Registry registry = LocateRegistry.createRegistry(port);
            registry.rebind(serviceName, service);
            resp.getWriter().println("启动成功");
        }else if ("call".equals(option)){
// Path: java/vulns-servlet/src/main/java/com/baidu/rasp/service/IHello.java
// public interface IHello extends java.rmi.Remote{
//     String sayHello(String name) throws RemoteException;
// }
// 
// Path: java/vulns-servlet/src/main/java/com/baidu/rasp/service/impl/HelloImpl.java
// public class HelloImpl extends UnicastRemoteObject implements IHello {
// 
// 
//     public HelloImpl() throws RemoteException {
//     }
// 
//     @Override
//     public String sayHello(String name) throws RemoteException {
//         return "Hi," + name;
//     }
// }
// 
// Path: java/vulns-servlet/src/main/java/com/baidu/rasp/task/JNDIBadTask.java
// public class JNDIBadTask implements Callable {
// 
//     private String host;
//     private int port;
//     private String serviceName;
// 
//     public JNDIBadTask(String host,int port,String serviceName){
//         this.host = host;
//         this.port = port;
//         this.serviceName = serviceName;
//     }
// 
//     @Override
//     public Object call() {
//         return CallBadService(this.host,this.port,this.serviceName);
//     }
// 
//     public static String CallBadService(String host,int port,String serviceName) {
//         String uri = "rmi://"+host +":"+port +"/"+serviceName;
//         Context ctx = null;
//         try {
//             ctx = new InitialContext();
//             ctx.lookup(uri);
//             return "调用成功";
//         } catch (NamingException e) {
//             e.printStackTrace();
//         }
//         return "调用失败";
//     }
// }
// 
// Path: java/vulns-servlet/src/main/java/com/baidu/rasp/task/JNDITask.java
// public class JNDITask implements Callable {
// 
//     private String host;
//     private int port;
//     private String serviceName;
// 
//     public JNDITask(String host, int port, String serviceName) {
//         this.host = host;
//         this.port = port;
//         this.serviceName = serviceName;
//     }
// 
//     @Override
//     public Object call() {
//         return CallService(this.host, this.port, this.serviceName);
//     }
// 
//     public static String CallService(String host, int port, String serviceName) {
//         try {
//             Registry registry = LocateRegistry.getRegistry(host, port);
//             IHello service = (IHello) registry.lookup(serviceName);
//             return service.sayHello("test");
//         } catch (RemoteException e) {
//             e.printStackTrace();
//         } catch (NotBoundException e) {
//             e.printStackTrace();
//         }
//         return "";
//     }
// }


// Path: java/vulns-servlet/src/main/java/com/baidu/rasp/JDNI.java
import com.baidu.rasp.service.IHello;
import com.baidu.rasp.service.impl.HelloImpl;
import com.baidu.rasp.task.JNDIBadTask;
import com.baidu.rasp.task.JNDITask;
import com.sun.jndi.rmi.registry.ReferenceWrapper;
import javax.naming.Reference;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

package com.baidu.rasp;


public class JDNI extends HttpServlet {

    public ExecutorService executor = Executors.newFixedThreadPool(5);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // jdk1.8高版本请设置此属性,否则报如下错误
        // javax.naming.ConfigurationException: The object factory is untrusted. Set the system property 'com.sun.jndi.rmi.object.trustURLCodebase' to 'true'.
        System.setProperty("com.sun.jndi.rmi.object.trustURLCodebase","true");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");//告知浏览器编码方式;
        resp.setCharacterEncoding("UTF-8");
        String option = req.getParameter("option");
        String host = req.getParameter("host");
        if ("".equals(host)){
            host = "localhost";
        }
        int port = Integer.parseInt(req.getParameter("port"));
        String serviceName = req.getParameter("serviceName");
        if ("start".equals(option)){
            IHello service = new HelloImpl();
            Registry registry = LocateRegistry.createRegistry(port);
            registry.rebind(serviceName, service);
            resp.getWriter().println("启动成功");
        }else if ("call".equals(option)){
            String result = JNDITask.CallService(host,port,serviceName);