【发布时间】:2011-11-07 01:44:26
【问题描述】:
我在 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()
这是从标准输出读取的一种很酷的非阻塞方法。这是这个 SO 问题的公认答案: How can I read all availably data from subprocess.Popen.stdout (non blocking)?
这是我尝试启动/监控进程的大部分脚本:
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 中的二进制文件,发现它使用非标准方法在运行时捕获用户输入。
该进程是一个交互式 cli 工具,它等待 S、R、P 或 Q(状态、恢复、暂停、退出)***并在用户按下 ente***r 时捕获输入。这是使用 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 击中断点
停顿? :)
编辑:
我创建了两个小型 Windows 控制台应用程序来演示什么有效,什么无效,并确保我的 python 是健全的。
第一个我无法通过写入标准输入来识别输入,第二个我可以。
#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;
}
编辑 2 我也试过: 导入子流程 进口时间 从电子进口 * 导入 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"
【问题讨论】:
-
你试过
msvcrt.putch()吗? -
我确实尝试过.. "C:\code>C:\Python27\python.exe te.py sMain talk: 0 answers received, expecting 1" ..... 如您所见s 被推送到控制台,但没有被 text.exe 拾取
-
我会问
getche()作为一个单独的问题,只使用普通的subprocess函数(没有InputStreamChunker(),例如Popen(..stdin=PIPE).communicate('s'))和一个简单的C 程序。添加Windows标签。 -
ungetch() 用于在 you 读取流时备份一点。它通常只保证备份一个字符,并不意味着输出。无论如何,你不应该经历所有这些扭曲来写入管道。你试过在 Popen 中设置
bufsize=0吗?
标签: python popen getch subprocess