1

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

我想要做什么:我希望能够在 python 中的 C 程序的标准输入上编写。

问题:Popen 返回的进程的标准输入没有值,而标准输出和标准错误是正确的。

我的程序的输出:

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

代码 :

如您所见,我正在使用 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)

感谢您 !

4

2 回答 2

0

感谢Ackdari,我更换了:

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

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

因为无论如何我都没有使用stderr。

于 2020-05-12T14:14:15.410 回答
0

我希望能够写在stdin

这是被禁止的,至少在 POSIX 标准中是被禁止的,并且在 Linux 上没有任何意义。正如它的名字所暗示的那样,它stdin 是一个标准输入,你的程序应该而不是它。

当然,请注意pipe(7) -s 有一个输入和一个输出。你正在写你的,stdout这就是-ed 过程。stdinpopen

于 2020-05-12T14:19:23.387 回答