Python 内存错误与 Itertool



我有一个小型的暴力程序要为学校做。我已经制作了一个程序,但是当我运行代码时,出现了内存错误......这是我的IDE的消息:

password = [''.join(word) for word in itertools.product(Alphabet, repeat=CharLength)] 内存错误

我想大多数错误是由于我如何使用循环号?作为一个菜鸟,我从来没有遇到过这种类型的错误......我给你 1 更多信息,我正在 Windows 上运行我的代码

如何优化我的代码以及如何修复? 这是我的代码:

import hashlib
import itertools

#possible characters in user password
Alphabet = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_.;#@")
#minimum password value
CharLength = 6
#getting passwords and username from shadow file
with open("shadow_test", "r") as ins:
    array = []
    users = []
    passwd = []
    #getting everyline from shadow file into an array
    for line in ins:
        array.append(line)
    #saving only username and passwords
    for pw in array:
        str(pw)
        r= pw.split(":")
        users.append(r[0])
        passwd.append(r[1])
    list = []
    #removing passowrd with * or !
    for mdp in passwd:
        if mdp != '*' and mdp != '!':
            str(mdp)
            list.append(mdp)
            # trying to Bruteforce
            for _ in range(12):
                passwords = [''.join(word) for word in itertools.product(Alphabet, repeat=CharLength)]
                print(*passwords)
                for pswd in passwords:
                    hash_object = hashlib.md5(str.encode(pswd)).hexdigest()
                    # hash_object.update(*passwords.encode('utf-8'))
                    generatedpassword = '$1$' + hash_object
                    for compare in list:
                        for user in users:
                            #print('on cherche le Mot de passe : ' + compare +' pour ' +user)
                            #print('mot de passe MD5 généré : ' +generatedpassword)
                            #print('mot de passe clair généré : ' +pswd)

                            if generatedpassword == list:
                                print('Le Mot de passe pour' + user + ' est : ' + compare)
passwords = [''.join(word) for word in itertools.product(Alphabet, repeat=CharLength)]

在这里,您将创建一个长度超过 50**6 的列表。当然,您会收到内存错误。请改用生成器:

passwords = (''.join(word) for word in itertools.product(Alphabet, repeat=CharLength))

按原样password列表将包含大约 1000 亿个条目,因此您需要超过 1 TB 的内存才能容纳它。 不要将其作为列表,而是将其保留为生成器以供以后循环:

passwords = (''.join(word) for word in itertools.product(Alphabet, repeat=CharLength))

(尽管对于 1000 亿个条目,您可能会等待一段时间。

最新更新