python - 如何为字母和字母数字密码创建暴力密码破解程序?

标签 python python-3.x brute-force cracking

我正在尝试为字母和字母数字组合创建一个强力 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')

它应该报告尝试次数或花费的时间。

最佳答案

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

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.

关于python - 如何为字母和字母数字密码创建暴力密码破解程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40269605/

相关文章:

algorithm - 蛮力幻方

firebird - 查找旧版 firebird/Interbase 数据库密码

python - 根据文档文本字符串重命名多个文件

python - Scrapy 和 Django 导入报错

python - 输出未正确显示所有 utf-8

PHP:反洪水/垃圾邮件系统

c++ - Python SIP 暴露函数

python - pandas 和 seaborn - 没有颜色的热图

python - 通过 pipenv 自定义模块搜索路径(PYTHONPATH)

python - 使用 PyGObject 右键单击​​上下文菜单