【问题标题】:How to create a brute-force password cracker for alphabetical and alphanumerical passwords?如何为字母和字母数字密码创建强力密码破解程序?
【发布时间】:2017-03-09 06:03:13
【问题描述】:

我需要为学校制作小程序来暴力破解不同类型的密码;我正在寻找创建一个蛮力 python 代码,它将遍历字母和字母数字密码的所有可能组合,并给我密码和破解所需的时间。

我对纯数字密码做了同样的事情,得到了这个:

import datetime as dt

Password4 = 123456

def crack_password():
    start = dt.datetime.now()
    for n in range(1000000):
        password_guess = '{0:04d}'.format(n)
             if password_guess == str(Password4):
                end = dt.datetime.now()
                print("Password found: {} in {}".format(password_guess, end - start))
               break
    guesses = crack_password()

然后,我尝试对字母/字母数字密码做一些类似的操作,但无论我尝试什么都没有用:

    import random

    letters = [str(i) for i in range('a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p')]
    s = [''.join([a,b,c,d,e,f,g,h]) for a in letters for b in letters for c   in letters for d in letters for e in letters for f in letters for g in letters  for h in letters]
    random.shuffle(s)
    real_password = 'aaaaaaaa'
    i = 0

    for code in s:
        if code == real_password:
            print()
            print('The password is: ', code)
             break
        else:
            i += 1
            print(i, ' failures', end='\r')

程序必须包含失败次数或找到密码所需的时间,这就是为什么我不能简单地制作密码生成器。

【问题讨论】:

  • 我建议你看看这个 PHP 脚本,我觉得它写得很好:gist.github.com/popc0rn/5859209
  • 感谢@YannChabot,但是我正在寻找在 python 中执行此程序并且真的不知道如何在不同语言之间进行翻译
  • letters = [str(i) for i in range('a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p')] 不像你想的那样工作......
  • letters = 'abcdefghijklmnopqstuvwxyz' 是你的目标

标签: python passwords brute-force cracking


【解决方案1】:

这是一种简单的暴力破解方法,可以猜测数字 (string.digits) 和小写字母 (string.ascii_lowercase)。您可以使用 itertools.product 并将 repeat 设置为猜测的当前密码长度。您可以从1 字符密码(或任何您的下限)开始,然后将其设置为最大长度。然后找到匹配项时只需 return

import itertools
import string

def guess_password(real):
    chars = string.ascii_lowercase + string.digits
    attempts = 0
    for password_length in range(1, 9):
        for guess in itertools.product(chars, repeat=password_length):
            attempts += 1
            guess = ''.join(guess)
            if guess == real:
                return 'password is {}. found in {} guesses.'.format(guess, attempts)
            # uncomment to display attempts, though will be slower
            #print(guess, attempts)

print(guess_password('abc'))

输出

a 1
b 2
c 3
d 4
...
aba 1369
abb 1370
password is abc. found in 1371 guesses.

【讨论】:

  • @AlexH 可以使用print(guess, attempts,end="\r") 重写同一行,但是为什么打印失败的密码甚至可以避免它并节省一些cpu 周期。
【解决方案2】:

几乎完全保留当前代码的一个可能选项是使用以下“数字”转换为基数 36:0-9a-z。如果您在 range(36**n) 中搜索,这将为您提供 n 个字符的所有可能的字母数字组合。

使用来自How to convert an integer in any base to a string? 的简化函数:

def baseN(num, b=36, numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
    return ((num == 0) and numerals[0]) or (baseN(num // b, b, numerals).lstrip(numerals[0]) + numerals[num % b])

然后您可以像您的示例中那样遍历数字:

>>> for i in range(10000, 10005):
...     print(baseN(i).zfill(5))
...
007ps
007pt
007pu
007pv
007pw

要获得所有 3 个字母的可能性,您可以循环如下:

for i in range(36**3):
    possible = baseN(i).zfill(3)

【讨论】:

  • 我喜欢这个解决方案,因为它更明确地显示了不同基数的计数是如何工作的。备注:您对 zfills 的使用假定数字中的第一个字符是零字符。为了使其更通用,最好执行 baseN(i).rjust(3, numerics[0])
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 2011-06-20
  • 2011-02-07
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多