Python:为什么Popen返回的进程没有标准输入?

问题描述 投票:1回答:2

[我正在尝试在python中创建一个程序以强制使用ctf C程序,在该程序中,您必须找到水果沙拉食谱才能获得该标志。

我想做什么:我希望能够在python中的C程序的stdin上编写。

问题:当stdout和stderr正确时,Popen返回的进程的标准输入为none值。

我程序的输出:

start bruteforce...
<_io.BufferedReader name=3>
<_io.BufferedReader name=5>
None

code:

如您所见,我在使用print,然后在循环调试过程std之前退出,我不明白为什么我在打印None时会得到print(process.stdin)

!/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)

谢谢!

python c stdin ctf
2个回答
0
投票

感谢阿克达里,我替换了:

process = Popen(['./fruit'], stdout=PIPE, stderr=PIPE)

with

process = Popen(['./fruit'], stdout=PIPE, stdin=PIPE)

因为我始终不使用stderr。


0
投票

我希望能够在stdin上书写

这是禁止的,至少在POSIX标准中如此,并且在Linux上没有意义。顾名思义,stdin标准输入,您应编程为读取而不是写入

当然,请注意pipe(7) -s具有输入和输出。您正在stdout上书写,这就是stdin -ed过程的popen

© www.soinside.com 2019 - 2024. All rights reserved.