【问题标题】:Brute Force Script蛮力脚本
【发布时间】:2017-03-09 20:41:51
【问题描述】:

我正在创建一个简单的蛮力脚本,但我似乎不太明白。我正在使用来自this question 的已接受答案,但我无法“尝试”等于用户密码。下面是我使用的代码(来自链接的问题),并对其进行了一些更改。

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)

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

任何帮助将不胜感激!

【问题讨论】:

  • 提示:attempt 的类型是什么?

标签: python-3.x brute-force


【解决方案1】:

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!

【讨论】:

  • 我得到了打印出来的答案(谢谢!)但它仍然继续循环遍历整个笛卡尔,即使我在末尾添加了break
  • @Goalieman:是的,我忘记了有 2 个 for 循环(而且你不能在 python 中使用 break 2)。我稍微修改了答案,现在试试。
  • 所以这是使用特殊字符,不使用它们怎么样?
猜你喜欢
  • 2012-07-07
  • 2014-09-13
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 2021-02-22
相关资源
最近更新 更多