2

I have a small Bruteforce program to do for school. I've made a program, but when I run my code, A Memory Error appear... Here is the message from my IDE :

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

I suppose most of the errors are due to how I use loop no ? As a noob I never meet this type of error... I giving you 1 more information, I'm running my code on Windows

How can I Optimize my code and how to fix too ? Here is my code :

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)

2 Answers 2

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

Here you're creating a list that is over 50**6 long. Of course you're going to get a memory error. Use a generator instead:

passwords = (''.join(word) for word in itertools.product(Alphabet, repeat=CharLength))
Sign up to request clarification or add additional context in comments.

Comments

0

The password list as-is will have approximately 100 billion entries, so you would need upwards of 1 TB of memory to be able to hold it. Instead of making it a list, leave it as a generator to loop over later:

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

(Although for 100 billion entries, you'll probably be waiting a while.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.