【问题标题】:Python script to brute force a bash input用于暴力破解 bash 输入的 Python 脚本
【发布时间】:2018-09-26 04:20:43
【问题描述】:

Bash 程序:

user@root:~/Downloads# ./program
What is the password?

所以它要求输入,如果你得到正确的密码,它会继续程序,否则它会退出(为了这个问题,密码是一个数字 0 到 1000)。

我需要编写一个 Python 2 脚本来暴力破解密码。我认为伪代码会是这样的:

import subprocess    
x = 0
while x <= 1000:
    subprocess.Popen('./program', stdin=PIPE)
    input x
    if program exits:
        continue
    else:
        break
    x += 1

非常了解使用Popen 在终端中运行命令,但是我不确定如何使用子进程输入字符串 - 我所做的任何谷歌搜索都会导致我对那些用其他输入做其他事情的人说。

我还纠结于如何检查程序是否退出。

谢谢你:)

【问题讨论】:

  • 你的python脚本需要写在./program的STDIN上。这可能会有所帮助:stackoverflow.com/questions/37560427/…。或者对同一概念进行进一步研究。我没有将其标记为重复,因为它可能无法完全满足您的要求。

标签: python bash python-2.7 terminal subprocess


【解决方案1】:

使用 Popen 的 communicate 可以在这里工作:

import subprocess
for x in range(0,1000):
    proc = subprocess.Popen('./program', stdin=subprocess.PIPE)
    proc.communicate(str(x))
    if proc.returncode:
        continue

    print "Found the password: " + str(x)
    break

【讨论】:

  • 完美运行。我不得不将shell=True 添加到Popen,因为它引发了一些奇怪的错误。谢谢! :)
【解决方案2】:

你可以试试这样的:

from subprocess import check_output
import shlex

output = check_output(shlex.split(your_command_as_string))

如果您的程序不接受密码作为命令行参数,您可以使用以下方法:

import subprocess
import shlex

prog = subprocess.Popen(
    shlex.split(your_command_as_string),
    stdin=subprocess.PIPE
) # run program with piped stdin

for password in your_passwords:
    prog.stdin.write("{}\n".format(password)) # feed password
    if prog.Poll() is not None: # check if program finished
        print(password)
        break

【讨论】:

    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多