4

I am trying to develop a UI based app in Java to use Metasploit (OS: Kali Linux). Basically I want to start a terminal and run the command "msfconsole" and then once the Metasploit Framework Console starts I want to execute commands in it.

Using ProcessBuilder Class of Java I am able to open up a terminal and start the Metasploit Framework Console but I am unable to execute any commands under the Framework. I am new to Runtime and ProcessBuilder class of Java. After starting the Framework Console, I did try executing the "help" command of the Metasploit Framework but that command was directly executed under the terminal and hence terminal help was displayed. So can anyone help me out with this? The code snippet is given below. Also I have included a supporting image at the end of this post.

    String line;
    Scanner scan = new Scanner(System.in);

    ProcessBuilder builder = new ProcessBuilder("/bin/bash");
    builder.redirectErrorStream(true);
    Process process = builder.start();

    OutputStream stdin = process.getOutputStream();
    InputStream stderr = process.getErrorStream();
    InputStream stdout = process.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

    String input = scan.nextLine();
    input += "\n";
    writer.write(input);
    writer.flush()

    input = scan.nextLine();
    input += "\n";
    writer.write(input);
    writer.flush();

    while ((line = reader.readLine()) != null) {
        System.out.println("Stdout: " + line);
    }
    writer.close();

I have encircled the part where I want to enter the commands. The image shows Metasploit Console running inside a terminal window:

I have encircled the part where I want to enter the commands. The image shows Metasploit Console running inside a terminal window.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Vru26
  • 49
  • 2
  • See this answer using the Robot class - https://stackoverflow.com/a/2549184/864369 – Dan W Apr 02 '18 at 20:37
  • Thank you for your reply but I am still not able to execute commands. I have included an image indicating where I actually want to execute the commands. Using "Robot" class I am able to execute the terminal specific commands on the terminal but I am not able to execute the commands after starting the Metasploit Console. – Vru26 Apr 05 '18 at 08:18
  • The above problem has been solved but now when I try to invoke the functions to type the commands by clicking on a button then the actionPerformed function is executed infinite number of times. The commands are typed using the Robot Class as suggested above. Can anyone help me out with this new problem? – Vru26 Apr 11 '18 at 18:37
  • Possible duplicate of [Running interactive shell program in Java](https://stackoverflow.com/questions/29733038/running-interactive-shell-program-in-java) – ivan_pozdeev Sep 10 '18 at 01:16

0 Answers0