python - 密码破解程序

标签 python passwords

我正在制作一个密码强度测试程序,该程序将根据花费的时间以及破解密码的尝试次数来给出评级。

我希望以最简单的方法之一破解输入,从一个元素 [a] 开始一直到 [z] 并然后添加第二个元素,之后将第一个元素设置回 [a] ,新元素将通过 [a] - [z],第二个元素将为第一个元素变成的每个字母重复此操作。为所有其他元素的每次完整旋转添加一个新元素。直到找到正确的密码。

我的问题是我找不到从 [a]-[z] 开始然后转到 [a], [a]-[z]; 的方法。 [b]、[a]-[z]; 等,不断堆叠,直到找到正确的密码。我知道这是一种极其无效的方法,但我现在只是尝试基础知识。

这是我的尝试。我尝试了一些其他方法,但都空手而归,所以我只好将其发布在这里。

usrPassword = str(input('')) 

while ''.join(passCheck) != usrPassword :

    for passwordLength in range(0, 10) : # this loop would be meant to go on forever.

        passCheck.append(' ')

        for i in range(0, passwordLength) :

            if ''.join(passCheck) != usrPassword :

                for j in range(32, 127) :

                    passCheck[i] = chr(j)
                    print(passCheck)

最佳答案

这可能不是您要求的逻辑,但我认为它更有效,因为它一次破解每个字母:

import string

usrPassword = list(input('input password: ')) # List of characters
passCheck = ['a'] * len(usrPassword) # Randomly initialize to some string of same length

print("Start cracking the password: {}".format(passCheck))
allowed_chars = list(string.printable) # All printable characters

while ''.join(passCheck) != ''.join(usrPassword):
    for ind in range(len(usrPassword)):
        match_char = [char for char in allowed_chars if char == usrPassword[ind]][0]
        passCheck[ind] = match_char
        print("Attempt {}/{}: {}".format(ind+1, len(usrPassword), passCheck))

print("User input: {} \nMatched password: {}".format(''.join(usrPassword), ''.join(passCheck)))

目前这适用于所有可打印字符。

示例输出:

input password: PasswORD-/!=12345
Start cracking the password: ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 1/17: ['P', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 2/17: ['P', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 3/17: ['P', 'a', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 4/17: ['P', 'a', 's', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 5/17: ['P', 'a', 's', 's', 'w', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 6/17: ['P', 'a', 's', 's', 'w', 'O', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 7/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 8/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'D', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 9/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'D', '-', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 10/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'D', '-', '/', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 11/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'D', '-', '/', '!', 'a', 'a', 'a', 'a', 'a', 'a']
Attempt 12/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'D', '-', '/', '!', '=', 'a', 'a', 'a', 'a', 'a']
Attempt 13/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'D', '-', '/', '!', '=', '1', 'a', 'a', 'a', 'a']
Attempt 14/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'D', '-', '/', '!', '=', '1', '2', 'a', 'a', 'a']
Attempt 15/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'D', '-', '/', '!', '=', '1', '2', '3', 'a', 'a']
Attempt 16/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'D', '-', '/', '!', '=', '1', '2', '3', '4', 'a']
Attempt 17/17: ['P', 'a', 's', 's', 'w', 'O', 'R', 'D', '-', '/', '!', '=', '1', '2', '3', '4', '5']
User input: PasswORD-/!=12345 
Matched password: PasswORD-/!=12345

如果您有任何疑问,请告诉我。

编辑:

如果您不想使用输入密码的长度:

import string

usrPassword = list(input('input password: ')) # List of characters
passCheck = []

print("Start cracking the password: {}".format(passCheck))
allowed_chars = list(string.printable) # All printable characters

while ''.join(passCheck) != ''.join(usrPassword):
    for ind in range(len(usrPassword)):
        match_char = [char for char in allowed_chars if char == usrPassword[ind]][0]
        passCheck.append(match_char)
        print("Attempt {}/{}: {}".format(ind+1, len(usrPassword), passCheck))

print("User input: {} \nMatched password: {}".format(''.join(usrPassword), ''.join(passCheck)))

这会产生与上面相同的输出,但附加到 passCheck 而不是使用预分配。

示例输出:

input password: PassworD-/!=12345
Start cracking the password: []
Attempt 1/17: ['P']
Attempt 2/17: ['P', 'a']
Attempt 3/17: ['P', 'a', 's']
Attempt 4/17: ['P', 'a', 's', 's']
Attempt 5/17: ['P', 'a', 's', 's', 'w']
Attempt 6/17: ['P', 'a', 's', 's', 'w', 'o']
Attempt 7/17: ['P', 'a', 's', 's', 'w', 'o', 'r']
Attempt 8/17: ['P', 'a', 's', 's', 'w', 'o', 'r', 'D']
Attempt 9/17: ['P', 'a', 's', 's', 'w', 'o', 'r', 'D', '-']
Attempt 10/17: ['P', 'a', 's', 's', 'w', 'o', 'r', 'D', '-', '/']
Attempt 11/17: ['P', 'a', 's', 's', 'w', 'o', 'r', 'D', '-', '/', '!']
Attempt 12/17: ['P', 'a', 's', 's', 'w', 'o', 'r', 'D', '-', '/', '!', '=']
Attempt 13/17: ['P', 'a', 's', 's', 'w', 'o', 'r', 'D', '-', '/', '!', '=', '1']
Attempt 14/17: ['P', 'a', 's', 's', 'w', 'o', 'r', 'D', '-', '/', '!', '=', '1', '2']
Attempt 15/17: ['P', 'a', 's', 's', 'w', 'o', 'r', 'D', '-', '/', '!', '=', '1', '2', '3']
Attempt 16/17: ['P', 'a', 's', 's', 'w', 'o', 'r', 'D', '-', '/', '!', '=', '1', '2', '3', '4']
Attempt 17/17: ['P', 'a', 's', 's', 'w', 'o', 'r', 'D', '-', '/', '!', '=', '1', '2', '3', '4', '5']
User input: PassworD-/!=12345 
Matched password: PassworD-/!=12345

关于python - 密码破解程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59080600/

相关文章:

c# - 为自动化存储密码的最佳实践

Java:这是对 BCrypt 的良好使用吗?

javascript - 使用 AJAX 和 PHP 验证简单密码

android - 保护我的应用程序的密码

Python - 尝试替换字符串列表中的单词但遇到单字母单词问题

python - getpass 不适用于 spyder (Python)

python - 尝试显示相关 GenericForeignKey 的嵌套表示时出错

python - 如何使用 Python 安全地编码内容配置 HTTP header ?

python - Pandas 滚动申请不做任何事情

python - 如何将输入与 Python 列表中的字符串进行比较?