蛮力脚本

Brute Force Script

我正在创建一个简单的暴力破解脚本,但我似乎不太明白。我正在使用来自 this question 的已接受答案,但我无法让 'attempt' 等于用户密码。下面是我使用的代码(来自链接的问题),并做了一些改动。

from string import printable
from itertools import product

user_password = 'hi' # just a test password
for length in range(1, 10): # it isn't reasonable to try a password more than this length
    password_to_attempt = product(printable, repeat=length)
    for attempt in password_to_attempt:
        if attempt == user_password:
            print("Your password is: " + attempt)

我的代码一直运行到到达笛卡尔坐标的末尾,并且从不打印最终答案。不确定发生了什么。

如有任何帮助,我们将不胜感激!

itertools.product() 为您提供 元组 而非字符串的集合。所以,你最终可能会得到结果 ('h', 'i'),但这和 'hi'.

是不一样的

您需要将字母组合成一个字符串进行比较。另外,您应该在找到密码后停止该程序。

from string import printable
from itertools import product

user_password = 'hi' # just a test password
found = False

for length in range(1, 10): # it isn't reasonable to try a password more than this length
    password_to_attempt = product(printable, repeat=length)

    for attempt in password_to_attempt:
        attempt = ''.join(attempt) # <- Join letters together

        if attempt == user_password:
            print("Your password is: " + attempt)
            found = True
            break

    if found:
        break

Try it online!