我正在执行以下子流程。。。
p.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"])
它挂着。
但如果我执行kmwe236@kmwe236:~/CS485/prog3/target26$ ./hex2raw < exploit4.txt | ./rtarget
,那么它执行得很好。使用输入或管道操作员是否有问题?
我也试过sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"], shell=True)
整个代码如下所示更新建议
import subprocess as sp
import pdb
for i in range(4201265,4201323):
pdb.set_trace()
d = hex(i)[2:]
output = " "
for i in range(len(d),0,-2):
output = output + d[i-2:i] + " "
out_buffer = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" + output + "00 00 00 00"
text_file = open("exploit4.txt", "w")
text_file.write("%s" % out_buffer)
# sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"], shell=True)
with open("exploit4.txt") as inhandle:
p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)
p2 = sp.Popen("./rtarget",stdin=p.stdout,stdout=sp.PIPE)
[output,error] = p2.communicate()
我得到一个错误是
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
调试后,它发生在调用p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)
的激发子进程
由于使用重定向和管道,因此必须启用shell=True
sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"],shell=True)
但在两个可执行文件上都使用CCD_ 6并将CCD_。以下示例,适用于您的情况:
import subprocess
with open("exploit4.txt") as inhandle:
p = subprocess.Popen("./hex2raw",stdin=inhandle,stdout=subprocess.PIPE)
p2 = subprocess.Popen("./rtarget",stdin=p.stdout,stdout=subprocess.PIPE)
[output,error] = p2.communicate()
print(output)
# checking return codes is also a good idea
rc2 = p2.wait()
rc = p.wait()
说明:
- 打开输入文件,获取其句柄
inhandle
- 打开第一个子流程,将
stdin
、inhandle
和stdout
重定向到输出流。获取管道手柄(p( - 打开第二个子进程,将
stdin
与前一个进程stdout
和stdout
重定向到输出流 - 设第二过程CCD_ 15。它将通过消耗输出来"拉动"第一个进程:两个进程都以管道方式工作
- 获取返回代码并打印结果
注意:您会得到"格式错误",因为一个或两个可执行文件实际上是shell或其他非本机可执行文件。在这种情况下,只需将shell=True
选项添加到相关的Popen
调用中即可。