Skip to content

Instantly share code, notes, and snippets.

@jdmedeiros
Last active February 15, 2023 19:59
Show Gist options
  • Save jdmedeiros/ded5f4990fcd9ddb32d8ca61658058e3 to your computer and use it in GitHub Desktop.
Save jdmedeiros/ded5f4990fcd9ddb32d8ca61658058e3 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import argparse
import hashlib
import json
import os
import socket
import sys
import time
def permutations(hashcd, charset, length):
"""
Brute force MD5 hash decoder
:param hashcd: cipher to decode
:param charset: character set to mix
:param length: number of characters to permutate
:return: decoded hash and result status
"""
seen = set()
res = ''
result = False
queue = [((charset[i],), charset[:i] + charset[i + 1:]) for i in range(len(charset))]
while queue:
current, remaining = queue.pop(0)
hashtxt = hashlib.md5(''.join(current).encode('UTF-8')).hexdigest().lower()
if len(current) == length and current not in seen:
if hashtxt == hashcd.lower():
res = ''.join(current)
result = True
break
seen.add(current)
continue
for i in range(len(remaining)):
new = current + (remaining[i],)
new_remaining = remaining[:i] + remaining[i + 1:]
queue.append((new, new_remaining))
return res, result
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--cipher", "-c", help="cipher to decode")
parser.add_argument("--set", "-s", help="character set to mix")
parser.add_argument("--min", "-n", help="minimum number of characters")
parser.add_argument("--max", "-x", help="maximum number of characters")
parser.add_argument("--output", "-o", help="output path")
args = parser.parse_args()
found = False
answer = None
success = None
for x in range(int(args.min), int(args.max) + 1):
answer, success = permutations(args.cipher, list(args.set), x)
if success:
break
message = f'{{"Hash":"{args.cipher}", "Text":"{answer}", "Result":"{"SUCCESS" if success else "FAIL"}"}}'
if args.output is not None:
filename = f'{args.output}/{socket.getfqdn()}_{os.path.basename(sys.argv[0])}_{int(time.time())}.out'
with open(filename, 'a') as the_file:
the_file.write(json.dumps(json.loads(message), indent=4) + os.linesep)
else:
print(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment