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.

4
  • See this answer using the Robot class - stackoverflow.com/a/2549184/864369
    – Dan W
    Apr 2, 2018 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 5, 2018 at 8: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, 2018 at 18:37
  • Possible duplicate of Running interactive shell program in Java Sep 10, 2018 at 1:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.