Skip to content

Instantly share code, notes, and snippets.

@m-242
Created November 17, 2022 13:04
Show Gist options
  • Save m-242/c65410ff339b3bbb3b521754ba4bda2a to your computer and use it in GitHub Desktop.
Save m-242/c65410ff339b3bbb3b521754ba4bda2a to your computer and use it in GitHub Desktop.
Simple script to merge active directory user:hash exports and hashcat hash:password potfiles.
#!/usr/bin/env python3
import sys, getopt
def load_users_hash(filepath: str) -> list[str]:
"""Load the user:hash file in memory as an array"""
users_to_hash = []
with open(filepath, "r") as file_users:
for line in file_users:
ll = line.strip().split(":")
if len(ll) == 2:
users_to_hash.append((ll[0], ll[1]))
return users_to_hash
def load_hash_password(filepath: str) -> dict[str, str]:
"""Load the cracked passwords in a dict, with the hash as key for fast lookup"""
hash_to_password = {}
with open(filepath, "r") as file_cracked:
for line in file_cracked:
ll = line.strip().split(":")
if len(ll) == 2:
password_hash, password = ll[0], ll[1]
hash_to_password[password_hash] = password
return hash_to_password
def usage() -> None:
print(
"""
Stupid simple script to merge active directory user:hash exports and hashcat hash:password potfiles.
required args:
-u/--userfile the file containing username:hash combos
-p/--passwordfile the file containing hash:password combos
optionnal args:
-o/--outputfile write the output to a file (instead of default stdout)
-h/--help print help and exit
"""
)
def main(argv: list[str]) -> None:
"""Main function, we take care of the arguments, load the proper files and find matches."""
print("Made with <3 by some bad coder")
users_file = ""
password_file = ""
outputfile = ""
# arg parsing
try:
opts, args = getopt.getopt(
argv, "hu:p:o:", ["help", "userfile=", "passwordfile=", "outputfile="]
)
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit(0)
if opt in ("-u", "--userfile"):
users_file = arg
if opt in ("-p", "--passwordfile"):
password_file = arg
if opt in ("-o", "--outputfile"):
outputfile = arg
if users_file == "" or password_file == "":
usage()
sys.exit(1)
# Proper code
users_to_hash = load_users_hash(users_file)
hash_to_password = load_hash_password(password_file)
if outputfile != "":
with open(outputfile, "w") as output:
for user, unified_hash in users_to_hash:
if unified_hash in hash_to_password:
password = hash_to_password[unified_hash]
else:
password = ""
output.write(f"{user}:{password}:{unified_hash}\n")
else:
for user, unified_hash in users_to_hash:
if unified_hash in hash_to_password:
password = hash_to_password[unified_hash]
else:
password = ""
print(f"{user}:{password}:{unified_hash}")
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment