Python: Making a Brute Force Hash Checker with Known Characters

Clash Royale CLAN TAG#URR8PPP
Python: Making a Brute Force Hash Checker with Known Characters
I've created a rather simple program to brute force the specified type of hash, with the ability to append known characters to the beginning of it.
import datetime
import hashlib
import itertools
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
def checkHashes(targetHash, unhashedStringLength, hashType, discovered):
print("Start = " + str(datetime.datetime.now()))
typeOfHash = getattr(hashlib, hashType)
for trialString in itertools.product(characters, repeat=unhashedStringLength-len(discovered)):
s = discovered + "".join(trialString)
if(typeOfHash(s.encode('utf-8')).hexdigest() == targetHash):
print("Finish = " + str(datetime.datetime.now()))
return s
That being said, I'm currently unable to solve a hash with known characters throughout it. For example, if I were to have "AB234***ANS*34" presented to me, with the "*" being unknown characters, I wouldn't be able to tell the program that I know more than the first five characters. Is there any way to brute force a string of characters with known and unknowns throughout it without slowing the process? Also, if there is anything I can do to make the program run more efficiently, I'm more than happy to take suggestions.
Sorry if my wording is confusing. If you need anything clarified, please ask!
1 Answer
1
You can intersperse fixed and variable parts in your arguments to product(). For example, using your "AB234***ANS*34" example, but with a much smaller character set to make the output short (2 possible characters for each of the 4 wildcards = 2**4 = 16):
product()
"AB234***ANS*34"
from itertools import product
characters = "xy"
template = [["AB234"],
characters, characters, characters,
["ANS"],
characters,
["34"]]
for trialString in product(*template):
s = "".join(trialString)
print(s)
displays the 16 possibilities:
AB234xxxANSx34
AB234xxxANSy34
AB234xxyANSx34
AB234xxyANSy34
AB234xyxANSx34
AB234xyxANSy34
AB234xyyANSx34
AB234xyyANSy34
AB234yxxANSx34
AB234yxxANSy34
AB234yxyANSx34
AB234yxyANSy34
AB234yyxANSx34
AB234yyxANSy34
AB234yyyANSx34
AB234yyyANSy34
As far as product() is concerned, something like ["AB234"] is a sequence with a single element, so that single element is included in every product it returns.
product()
["AB234"]
As to efficiency, the task is inherently exponential-time. Nothing will make it fantastically faster. Industrial-strength crackers resort to assembler for low-level speed, and spread the task over as many CPUs as they can afford to run ;-)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.