# Brute force password cracker in Python

I made a brute force password cracker with Python, but it's extremely slow. How can I make it faster?

import itertools
import string
import time

chars = string.ascii_uppercase + string.digits
start = time.perf_counter()
guess = ''.join(guess)
t = list(guess)
t[5] = '-'
t[11] = '-'
t[17] = '-'
tg = ''.join(t)
if tg == real:
return 'Scan complete. Code: \'{}\'. Time elapsed: {}'.format(tg, (time.perf_counter() - start))



As I said, it's extremely slow. It takes at least 9 days to find a single password.

• For a start you could change password_length to 20, you are needlessly duplicating your searches (each of your proposal passwords is constructed 36^3 different times) Feb 12, 2019 at 16:27
• @RussHyde thanks for your comment. I compared my old code and your code with AAAAA-AAAAA-AAAAA-FORTN but my old code is faster than yours with 5 seconds. My old code got 25 seconds but your one got 30 seconds. Why? I thought it was going to make it faster. Feb 12, 2019 at 16:40
• Do it a thousand times, and post the mean and std-dev Feb 12, 2019 at 16:59
• You could also strip the "-" from a single copy of the real password, rather than mutating every guess Feb 12, 2019 at 17:14
• @AkınOktayATALAY Try Russ Hyde's suggested change with a password that extends past the first dash and you'll see the difference much more clearly: AAAAA-AAAAA-AAAAB-AAAAA. Feb 13, 2019 at 1:34

You should exploit the structure of the password, if it has any. Here you have a 20 character password separated into four blocks of five characters each, joined with a -. So don't go on generating all combinations of length 23, only to throw most of them away.

You also str.join the guess, then convert it to a list, then replace the values and str.join it again. You could have saved yourself the first str.join entirely by directly converting to list.

You know the length of the password, so no need to hardcode it. Just get it from the real password (or, in a more realistic cracker, pass the length as a parameter).

With these small changes your code would become:

def guess_password(real):
chars = string.ascii_uppercase + string.digits
if guess == real:
return guess


Here I used some string formatting to get the right format.

Note also that the timing and output string are not in there. Instead make the former a decorator and the latter part of the calling code, which should be protected by a if __name__ == "__main__": guard to allow you to import from this script without running the brute force cracker:

from time import perf_counter
from functools import wraps

def timeit(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = perf_counter()
ret = func(*args, **kwargs)
print(f"Time elapsed: {perf_counter() - start}")
return ret
return wrapper

@timeit
...

if __name__ == "__main__":


On my machine this takes 9.96 s ± 250 ms, whereas your code takes 12.3 s ± 2.87 s for the input string "AAAAA-AAAAA-AAAAA-FORTN".

But in the end you will always be limited by the fact that there are a lot of twenty character strings consisting of upper case letters and digits. Namely, there are $$\36^{20} = 13,367,494,538,843,734,067,838,845,976,576\$$ different passwords that need to be checked (well, statistically you only need to check half of them, on average, until you find your real password, but you might get unlucky). Not even writing your loop in Assembler is this going to run in less than days.

• I got output guess = password_format.format(*guess) IndexError: tuple index out of range Feb 12, 2019 at 17:23
• @AkınOktayATALAY: In that case you gave it a password of a different format (or used a previous revision, I had a typo in the password length). It works with the two given strings. Feb 12, 2019 at 17:26
• I think I should change password_length = len(real) - 5 to password_length = len(real) - 3 Feb 12, 2019 at 17:27
• @AkınOktayATALAY: Yes, I already did that (about a minute after posting the answer for the first time), just update the page. Feb 12, 2019 at 17:31
• @AkınOktayATALAY: Take your time. It is usually not a bad idea to wait at least 24 hours, so everybody on the globe had a chance to see the question and think about answering. Maybe I missed something. Feb 12, 2019 at 17:32

There are other ways beyond improving the code itself.

1. Beyond changes which reduce allocations a lot, like:
t = list(guess)


guess = ''.join(guess)
t = list(guess)


Reduces the runtime 11s -> 6.7s.

1. You can use a different runtime which will speed up almost any code:
➜  /tmp python3 foo.py
Scan complete. Code: 'AAAAA-AAAAA-AAAAA-FORTN'. Time elapsed: 6.716003532
➜  /tmp pypy3 foo.py
Scan complete. Code: 'AAAAA-AAAAA-AAAAA-FORTN'. Time elapsed: 3.135087580012623

1. Or precompile the existing code into a module which you can load again in your standard python code:
# cythonize -3 -i foo.py
Compiling /private/tmp/foo.py because it changed.
[1/1] Cythonizing /private/tmp/foo.py
running build_ext
building 'foo' extension
...

# ipython3

In [1]: import foo
Scan complete. Code: 'AAAAA-AAAAA-AAAAA-FORTN'. Time elapsed: 3.846977077

• thanks the cythonize worked but the PyPy is 3x slow for me. Do you know why? Feb 13, 2019 at 14:43
• ¯\_(ツ)_/¯ sorry Feb 13, 2019 at 22:56
import hashlib
from urllib.request import urlopen

############# append the below code ################

try:
except Exception as e:
print("Hey there was some error while reading the wordlist, error:", e)
exit()
return wordlistfile

return result.hexdigest()