【发布时间】:2020-08-28 03:28:17
【问题描述】:
我正在尝试用 python 编写一个程序来暴力破解一个 ctf C 程序,你必须在其中找到一个水果沙拉食谱才能获得标志。
我想做的事情:我希望能够在python中的C程序的标准输入上写。
问题:Popen 返回的进程的 stdin 有一个 none 值,而 stdout 和 stderr 是正确的。
我的程序的输出:
start bruteforce...
<_io.BufferedReader name=3>
<_io.BufferedReader name=5>
None
代码:
如您所见,我使用 print 然后在循环前退出来调试进程 std,我不明白为什么我在打印 print(process.stdin) 时得到 None
!/usr/bin/python3
import random
import os
import sys
from subprocess import *
from contextlib import contextmanager
from io import StringIO
fruit = ["banana", "raspberry", "orange", "lemon"]
comb = ""
found = False
print("start bruteforce...")
process = Popen(['./fruit'], stdout=PIPE, stderr=PIPE)
print(process.stdout)
print(process.stderr)
print(process.stdin)
sys.exit(1)
while True:
for i in range(4):
pick = random.choice(fruit)
inp, output = process.stdin, process.stdout
comb += pick
comb += " "
inp.write(pick)
inp.write("\n")
out = output.read().decode('utf-8')
if "flag" in out:
found = True
break
if found == True:
print("found : " + com)
break
print(comb + " : is not valid")
comb = ""
os.kill(p.pid, signal.CTRL_C_EVENT)
谢谢你!
【问题讨论】:
-
根据文档"With the default settings of None, no redirection will occur; the child’s file handles will be inherited from the parent."。因此子进程获得与父程序相同的输入。我认为您需要指定
stdin=PIPE以获得将输入传递给程序的句柄。 -
谢谢!我没有太注意传递给 Popen 的论点,并认为我不必指定 stdin !@Ackdari