Python 如何为字母和字母数字密码创建 Bruteforce 密码破解程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40269605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:21:03  来源:igfitidea点击:

How to create a Bruteforce password cracker for alphabetical and alphanumerical passwords?

pythonpasswordsbrute-forcecracking

提问by Alex H

I need to make small programs for school to brute force crack different types of passwords; I'm looking to create a brute force python code that will run through every possible combination of alphabetical and alphanumerical passwords and give me the password and the amount of time it took to crack.

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

I did the same with purely numerical passwords and got this:

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

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()

I then tried to do something somewhat similar for alphabet/alphanumerical passwords but did not work whatever I tried:

然后我尝试对字母/字母数字密码做一些类似的事情,但无论我尝试过什么都不起作用:

    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')

It is vital that the program include either number of failures or the time it took to find the password, which is why i can't simply make a password generator.

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

Please note: I am fairly new to coding and am very grateful for your help :)

请注意:我对编码还很陌生,非常感谢您的帮助:)

回答by Cory Kramer

Here's a naiive brute force method that will guess numbers (string.digits) and lower case letters (string.ascii_lowercase). You can use itertools.productwith repeatset to the current password length guessed. You can start at 1character passwords (or whatever your lower bound is) then cap it at a maximum length too. Then just returnwhen you find the match.

这是一个天真的蛮力方法,可以猜测数字 ( string.digits) 和小写字母 ( string.ascii_lowercase)。您可以使用itertools.productwithrepeat设置为当前猜测的密码长度。您可以从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)
            print(guess, attempts)

print(guess_password('abc'))

Output

输出

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

回答by brianpck

One possible option which would preserve almost exactly your current code is to convert to base 36 with the following "digits": 0-9a-z. This will give you every possible alpha-numeric combination for n characters if you search in range(36**n).

一种可以几乎完全保留您当前代码的可能选项是使用以下“数字”转换为基数 36:0-9a-z. 如果您在range(36**n).

Using a simplified function from How to convert an integer in any base to a string?:

使用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])

You can then loop through numbers as in your example:

然后,您可以像示例中一样遍历数字:

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

To get all 3-letter possibilities, you can loop as follows:

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

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