【问题标题】:Python: Why do my process returned by Popen has a "none" stdin?Python:为什么我的 Popen 返回的进程有一个“无”标准输入?
【发布时间】: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)

谢谢你!

【问题讨论】:

标签: python c stdin ctf


【解决方案1】:

我希望能够在stdin上写字

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

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

【讨论】:

  • 他想在子进程的stdin 上写,而不是自己写,如果子进程有自己的标准输入并且像管道一样可写,这完全没问题。我认为你错过了问题的重点。
【解决方案2】:

感谢 Ackdari 修复,我替换了:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2017-01-26
    • 2017-08-22
    • 2018-11-14
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    相关资源
    最近更新 更多