Home > Blockchain >  How do I break an inner for loop every time a condition is satisfied?
How do I break an inner for loop every time a condition is satisfied?

Time:11-26

Good afternoon,

Im very new to python coding and Im trying to write a very basic md5 brute force password cracker for a school project.

I am supposed to write a script that will crack a series of MD5 hashed passwords. The passwords must be read in from a text file named “hashes.txt”, with one password on every line. The script should then start generating passwords, starting with single character, and then two characters, etc

My thought process of how to make a brute force cracker is as follows:

import hashlib
import itertools
abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@0123456789"
abc_list = list(abc)

def combo():
    md5_hash = ""
    file_name = open("hashes.txt", "r")
    for password in file_name:
        password = password.strip()
        #print(password)
        for r in range(len(abc_list)):
            combinations_object = itertools.combinations(abc_list, r)
            combinations_list = list(combinations_object)
            #print(combinations_list)
            for lis in combinations_list:
                glue = "".join(lis)
                hash_object = hashlib.md5(glue.encode())
                md5_hash = hash_object.hexdigest()
                print(glue)
                #print(md5_hash)
                #print(glue   " "   md5_hash)
                if md5_hash == password :
                    print("Your password is: "   "'"   glue  "' "  md5_hash)
                    break

The passwords I am given to crack are: Z, AD, God, 1234, AbCdE, Trojan

Every time I run the script it only outputs the password: Z and then runs through the rest without fulfilling the 'if' statement.

I have tried using the 'break' statement under the 'if' but the outcome is the same.

CodePudding user response:

You might benefit from creating and storing the combinations list once (or rather, a generator).

https://stackoverflow.com/a/31474532/11170573

import itertools

def all_combinations(any_list):
    return itertools.chain.from_iterable(
        itertools.combinations(any_list, i   1)
        for i in xrange(len(any_list)))

You can change the code as follows:

import hashlib
import itertools
abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@0123456789"
abc_list = list(abc)

combinations_object = all_combinations(abc_list)

def combo():
    file_name = open("hashes.txt", "r")
    for password in file_name:
        password = password.strip()
        for comb in combinations_object:
            comb = "".join(comb)
            hash_object = hashlib.md5(glue.encode())
            md5_hash = hash_object.hexdigest()
            if md5_hash == password :
                    print("Your password is: "   "'"   glue  "' "  md5_hash)
                    break

CodePudding user response:

When you use break, what you are saying is break the loop I'm currently in.

Notice that your condition is wrapped by three for loops, so it will only break the inner inner loop and keep going with the rest.

What you can do is what Jason Baker sugests in https://stackoverflow.com/a/438869/16627440.

Change that break to return md5_hash and call the function in your print.

if md5_hash == password :
    return md5_hash
# Outside your function
print("Your password is: "   "'"   glue  "' "  combo())
  • Related