I am attempting to automate the execution of an interactive command line tool written in C++.
When launched, the binary waits for the letter S, Q, or P (Status, Quit, or Pause). It uses a nonstandard msvcrt function "getche" to acquire the key stroke (instead of a gets() for example) without the user having to hit enter.
I tried communicating with the process in the standard way (writing to stdin and using process.communicate[]) but it doesn't get the input. After a few hours of trying different things I created two small sample projects in Visual Studio to replicate the issue and make sure I am sane(ish).
This is the python script used to call the binary:
import subprocess
import time
cmd = ["test-getch.exe"]
process = subprocess.Popen(cmd, stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
i = process.stdin
#msvcrt.ungetch('s')
i.write("S\n")
print process.communicate()[0]
i.close()
time.sleep(3)
print "DONE"
These are the two binaries. This first one I can communicate with:
#include "stdafx.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
char response [2];
printf("Enter \"s\":\n");
gets(response);
printf("You entered %s", response);
return 0;
}
This one I can't communicate with:
#include "stdafx.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
int response;
printf("Enter \"a\":\n");
response = getche();
printf("You entered %c", response);
return 0;
}
It appears that getche() doesn't listen on stdin and probably listens for some kind of keyboard event. Anyone know how to deal with this?
EDIT: I should also mention I discovered the method of capturing input using IDA Pro. I did not write the original binary that I am attempting to automate. It is a closed source tool so I have no way of re-writing how it accepts input without patching the binary.
I've actually chosen a rather insane solution that works... I know pydbg quite well and it seems that attaching to the process and calling the functions I need via process instrumentation works. It's totally overkill but I can detach from the process afterwards. and have it run normally.
[1] Pydbg: http://pedram.redhive.com/PyDbg/docs/