以下を使用して Windows でバイナリを起動しています。
process = subprocess.Popen(cmd, stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = ch.input)
ch.input comes from:
ch = InputStreamChunker('\n')
ch.daemon = True
ch.start()
これは、stdout から読み取る非ブロッキングのクールな方法でした。これは、この SO の質問に対する受け入れられた回答でした: subprocess.Popen.stdout (非ブロッキング) からすべての利用可能なデータを読み取るにはどうすればよいですか
これは、プロセスの起動/監視を試みるスクリプトの大部分です。
import subprocess
import time
from e import *
import msvcrt
ch = InputStreamChunker('\n')
ch.daemon = True
ch.start()
cmd = "C:\TEST.EXE"
process = subprocess.Popen(cmd, stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = ch.input)
i = process.stdin
answers = []
waitforresults(ch, answers, expect = 5)
an = 1
for i in answers:
print "ANSWER # " + str(an) + ":" + i.read()
an= an+1
answers = []
time.sleep(5)
msvcrt.ungetch('s')
ch.flush()
waitforresults(ch, answers, expect = 1)
for a in answers:
print a.getvalue()
process.terminate()
ch.stop()
del process, ch
print "DONE"
とにかく、このメソッドを使用してプロセスから読み取ることができます。次を使用してプロセスに書き込もうとすると:
i = process.stdin i.write("s\n") または i.write("s")
出力はコンソール ウィンドウに表示されますが、バイナリにはキャプチャされません。IDA Pro でバイナリを調べたところ、実行中にユーザー入力をキャプチャする非標準的な方法が使用されていることがわかりました。
このプロセスは、S、R、P、または Q (ステータス、再開、一時停止、終了) を待機するインタラクティブな cli ツールです。これは getche() を使用して実現されます。確認するために IDA Pro にバイナリを投げました。
v7 = getche();
if ( (unsigned int)dword_41F060 > 1 )
sub_4030A0(&unk_41C111, v5);
WaitForSingleObject((HANDLE)dword_41F040, 0xFFFFFFFFu);
if ( v7 == 113 )
{
if ( dword_41F060 != 1 )
{
if ( dword_41F060 )
dword_41F060 = 6;
}
}
else
{
if ( v7 <= 113 )
{
if ( v7 == 112 )
{
if ( dword_41F060 == 2 )
QueryPerformanceCounter((LARGE_INTEGER *)&qword_41F128);
dword_41F060 = 3;
sub_4030A0("Paused", v5);
}
}
else
{
if ( v7 == 114 )
{
if ( dword_41F060 == 3 )
{
QueryPerformanceFrequency((LARGE_INTEGER *)&v9);
QueryPerformanceCounter((LARGE_INTEGER *)&v8);
v3 = (double)(v8 - qword_41F128);
LODWORD(v4) = sub_413FF0(v9, v10, 1000, 0);
dbl_41F138 = v3 / (double)v4 + dbl_41F138;
}
dword_41F060 = 2;
sub_4030A0("Resumed", v5);
}
else
{
if ( v7 == 115 )
sub_40AFC0();
}
}
}
このイベントをトリガーする方法を知っている人はいますか? これは有望に見えました: http://docs.python.org/library/msvcrt.htmlと私は使用してみました:
msvcrt.ungetch("s") "文字 char をコンソール バッファに「押し戻す」ようにします。これは、getch() または getche() によって読み取られる次の文字になります。"
これは文字「s」をコンソールにプッシュしましたが、getche() でブレークポイントをトリガーしませんでした。文字 S を手動で押すと機能し、IDA pro がブレークポイントにヒットします。
半分?:)
編集:
2 つの小さな Windows コンソール アプリを作成して、何が機能し、何が機能しないかを示し、Python が正常であることを確認しました。
この最初のものは、標準入力に書き込むことによって入力を認識することができず、2番目のものは認識できます。
#include "stdafx.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
char response;
printf("Enter \"s\":\n");
response = _getch();
printf("You entered %c\n", response);
return 0;
}
これに書き込むことができます:
#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;
}
EDIT 2 私も試しました: import subprocess import time from e import * import msvcrt
ch = InputStreamChunker('\n')
ch.daemon = True
ch.start()
cmd = ["test-getch.exe"]
process = subprocess.Popen(cmd, stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = ch.input)
i = process.stdin
answers = []
msvcrt.putch('s')
ch.flush
waitforresults(ch, answers, expect = 1)
for answer in answers:
print answer.getvalue()
i.close()
time.sleep(3)
#process.terminate()
ch.stop()
del process, ch
print "DONE"