6

Essentially, I'm running a silent command-line scan using Java. I then wait for this scan to finish and the process to close before moving on. However, the process doesn't end until you "Press any key to continue". Another thing to note is that the command line window is not visible at the time, which is intended, so the process just stays active in the background, idle, once the scan is done. Here is a snippet of the code:

Main.print("Performing RKR Scan...");
try {
    Process p1 = Runtime.getRuntime().exec(dir + "RootkitRemover.exe /noupdate"); 
    try {
        p1.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
} catch (IOException e) {
    Main.print("Error Scanning With RKR: " + e);
}

Is there any way of getting through this "Press any key" thing, whether it be an official solution or just a bypass?

JTApps
  • 5,330
  • 7
  • 37
  • 55
  • 1
    You're answer is going to lie in the command line options for RootKitRemover.exe. My guess is running that command by itself finishes with the "press any key to continue" message. As your executing it via the exec method, its essentially the same as executing it via command line – Robert H Aug 01 '13 at 14:02
  • http://stackoverflow.com/a/755568/1654265 – Andrea Ligios Aug 01 '13 at 14:07
  • There is, unfortunately, no solution within the command line options here. The program, unfortunately, lacks a comprehensive command line options base, which is mildly amusing being as it's a command line program. – JTApps Aug 01 '13 at 14:11
  • Perhaps finding the pid of the executing application and then sendign a sigterm to it would be the way to go - Take a look here: http://stackoverflow.com/questions/81902/how-to-find-and-kill-running-win-processes-from-within-java – Robert H Aug 01 '13 at 14:20
  • The problem then lies within knowing when the scan is finished before terminating the process. – JTApps Aug 01 '13 at 14:21
  • You could read the InputStream and catch the "Press any key", when this happens you could do like ejk314 propose or terminate the process. – Marc-Andre Aug 01 '13 at 14:43
  • I like the logic behind this one the best, as another answer expounded a bit on. I'm just having a tough time getting the InputStream and printing it out (in order to read it later on). I can't find the proper syntax for it. – JTApps Aug 01 '13 at 14:45
  • Have you find a solution? – Marc-Andre Aug 07 '13 at 16:42

6 Answers6

2

As I said in my comment, you could probably listen to the InputStream until the "Press any key". Then you should write a newLine to the process via the OutputStream. Here is some code to help you:

   public class BatchEnter {

    public static void main(String[] args) throws Exception {
        List<String> params = Arrays.asList(new String[] { "cmd", "/C", "C:/test/test.bat" });
        ProcessBuilder builder = new ProcessBuilder(params);
        builder.directory(new File("C:/test")).redirectErrorStream(true);
        final Process p = builder.start();

        final BufferedReader wr = new BufferedReader(new InputStreamReader(p.getInputStream()));
        final BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(p.getOutputStream()));
        String line = "";
        try {
            while ((line = wr.readLine()) != null) {
                if (line.equals("Press any key")) {
                    String newLine = "\n\r";
                    writer.write(newLine);

                }
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        p.waitFor();
    }
}

Some remark on the code:

  • You should change the exception managing cause I'm just doing e.printStackTrace()
  • My process is a command window, I don't know if it will work for your exe

Edit:

If you're not getting any output, you could simply try to send the newLine feeds until the program finish. You could for exemample, write the newLine each second until the process stop. Something like :

final BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(p.getOutputStream()));
    while(true){
        String newLine = "\n\r";
        writer.write(newLine);
    }

You could start a thread that write to the process, and stop it when the p.waitFor() return. You should change the condition too, for something more clean like each second or while the thread is not stop. Beware that you will have IOException if you're writting to the stream and the process finish. And this is a really not something I would rely on, but if it could help you.

Marc-Andre
  • 914
  • 1
  • 15
  • 32
  • Before I cram all of this in here, I'm going to start by just seeing if I can get the outputstream to log to my console first, and then work towards actually reading it. – JTApps Aug 01 '13 at 14:56
  • @JTApps If you to see the output in your logging you could remove the `if` part and output `line` into your log – Marc-Andre Aug 01 '13 at 14:58
  • I'm trying that, and not getting any output at all. I basically just ripped the middle chunk of code out, as seen here: http://pastebin.com/raw.php?i=d7wFadYR – JTApps Aug 01 '13 at 15:03
  • Yeah, I've got nothing. Using that code, for whatever reason, doesn't give me any kind of output from the console, so I have no way of getting as far as reading it in order to submit a key press from here. Any suggestions? – JTApps Aug 01 '13 at 15:36
  • You could try to keep sending the newLine until you hit the "Press any key" to continue – Marc-Andre Aug 01 '13 at 15:42
  • Are you able to grab the input if you're starting a command window that start RootkitRemover.exe? – Marc-Andre Aug 01 '13 at 19:29
1

Could you get the outputstream of the process and then monitor the output from the process and when press any key comes up then write to the inputstream.

http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

Michael Dillon
  • 1,017
  • 6
  • 16
  • It seems viable, but I'm not sure exactly what you're saying. Can I do something like... `System.out.println(p1.getOutputStream());`? EDIT: Fun fact, that returns `java.io.BufferedOutputStream@2a8ceeea` – JTApps Aug 01 '13 at 14:19
  • While loop over the outputstream calling next and hasNext until you get the characters you want "press any key to continue". Then pop a character into the inputstream. – Michael Dillon Aug 01 '13 at 14:27
  • Alright, it makes good sense to me now. The only problem I'm having right now is actually returning the text from the outputstream to begin checking for the characters I want. I can't get the syntax right, apparently, and looking online is only offering help for printing to the program console and not from the command line to program console. – JTApps Aug 01 '13 at 14:30
1

An idea... Instead of calling directly RootkitRemover.exe, put the call in CMD file (rootkitremover.cmd).

In the CMD, you pipe an ENTER key to RootKitRemover.exe

echo ^M|RootkitRemover.exe /noupdate

[EDIT]

To hide the window, start the CMD with the /min switch (or /b for no window at all).

import java.io.IOException;

public class Testing {

    /**
     * @param args
     * @throws IOException 
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws IOException, InterruptedException {
        String fileName = "c:\\temp\\test.cmd";
        String[] commands = {"cmd", "/c", "start", "/min", "\"DummyTitle\"", fileName};
        Process p1 = Runtime.getRuntime().exec(commands);
        p1.waitFor();
        System.out.println("done.");
    }
}
RealHowTo
  • 32,895
  • 11
  • 68
  • 83
  • I get a feeling that it's going to make the window pop up, which is exactly what I wouldn't want. I'll give it a try, because you never know, though. EDIT: I was right, unfortunately. – JTApps Aug 01 '13 at 14:40
0

You could try just writing a new line char to the output stream of the process before the waitFor().

p1.getOutputStream().write((int)'\n');
ejk314
  • 371
  • 1
  • 11
0

Did you try to put a System.exit() somewhere at the end of your code?

I would start by trying to determine if the process showing the "Press any key" message comes from your Java process or the underlying executable (RootkitRemover)

Arnaud Jeansen
  • 1,399
  • 12
  • 25
0

According to here adding /C might help.

Carries out the command specified by string and then terminates

/K is it's opposite.

"RootkitRemover.exe /C /noupdate"
Tala
  • 8,298
  • 4
  • 30
  • 35