Parchment
Parchment

Reputation: 3

Why is my Python brute force hash cracker now working?

I have written a Python hash brute-forcing script. But, the brute-force function is not working. It will accept the user input hash and loop over all iterations. However, I can see on the output that it has located the hash, but it will not then break the loop.

The hash is sha256 and input is 'blah', it will find the hash of blah, but won't then break the loop.

Thanks for any insight!

The code is:

def bruteforce(passwordHash, hashtype): #bruteforce not working?

    wordlist = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+{}:;',./?-="
    y=''
    length=1
    wordlistHash=''
    passwordHash=passwordHash

    while wordlistHash != passwordHash:
        for c in itertools.product(wordlist, repeat=length):
            word = y.join(c)
            if hashtype == 'sha256':
                wordlistHash = hashlib.sha256(word.encode("utf-8")).hexdigest()
                print(f"Trying password: {word}:{wordlistHash}")
            elif hashtype == 'md5':
                wordlistHash = hashlib.md5(word.encode("utf-8")).hexdigest()
                print(f"Trying password: {word}:{wordlistHash}")
            elif hashtype == 'sha1':
                wordlistHash = hashlib.sha1(word.encode("utf-8")).hexdigest()
                print(f"Trying password: {word}:{wordlistHash}")
            else:
                print("Please either enter a sha256, md5 or sha1 hash and restart the script")
                exit()
                if wordlistHash == passwordHash:
                    print(f"Found password: {word}")
                    break
        length=length + 1

Upvotes: 0

Views: 1262

Answers (2)

Hugo LB
Hugo LB

Reputation: 156

This is the new script, I simply added

if wordlistHash == passwordHash:
    print(f"Found password: {word}")
    break

after every, if-elif statement, otherwise the program will only display the password if the input is incorrect (because you put it in the last else statement). I don't know if I'm clear, hope it helps, here's the new program:

import hashlib
import itertools

def bruteforce(passwordHash, hashtype): #bruteforce not working?

    wordlist = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+{}:;',./?-="
    y=''
    length=1
    wordlistHash=''
    passwordHash=passwordHash

    while wordlistHash != passwordHash:
        for c in itertools.product(wordlist, repeat=length):
            word = y.join(c)
            if hashtype == 'sha256':
                wordlistHash = hashlib.sha256(word.encode("utf-8")).hexdigest()
                print(f"Trying password: {word}:{wordlistHash}")
                if wordlistHash == passwordHash:
                    print(f"Found password: {word}")
                    break
            elif hashtype == 'md5':
                wordlistHash = hashlib.md5(word.encode("utf-8")).hexdigest()
                print(f"Trying password: {word}:{wordlistHash}")
                if wordlistHash == passwordHash:
                    print(f"Found password: {word}")
                    break
            elif hashtype == 'sha1':
                wordlistHash = hashlib.sha1(word.encode("utf-8")).hexdigest()
                print(f"Trying password: {word}:{wordlistHash}")
                if wordlistHash == passwordHash:
                    print(f"Found password: {word}")
                    break
            else:
                print("Please either enter a sha256, md5 or sha1 hash and restart the script")
                exit()


        length=length + 1

bruteforce('8B7DF143D91C716ECFA5FC1730022F6B421B05CEDEE8FD52B1FC65A96030AD52', 'sha256')

Upvotes: 2

Roshin Raphel
Roshin Raphel

Reputation: 2709

Because, after the line,

print("Please either enter a sha256, md5 or sha1 hash and restart the script")

You placed an exit() function. If you want the break to work, place lines,

if wordlistHash == passwordHash:
    print(f"Found password: {word}")
    break

Outside the if statement

Upvotes: 0

Related Questions