Skip to content

Instantly share code, notes, and snippets.

@yosignals
Created December 22, 2022 18:59
Show Gist options
  • Save yosignals/80db7d8d06f8060abd0eecde933c9c68 to your computer and use it in GitHub Desktop.
Save yosignals/80db7d8d06f8060abd0eecde933c9c68 to your computer and use it in GitHub Desktop.
Useful for separating a mix of hashes, my use case was historical breach data where uncracked passwords where varying in Hash type, this aims to try and sort through them into files you can throw at hashcat or JtR
import hashlib
def sort_hashes(filename):
# Create a list of hash types
hash_types = [
'md5',
'sha1',
'sha224',
'sha256',
'sha384',
'sha512',
'sha3_224',
'sha3_256',
'sha3_384',
'sha3_512',
'blake2b',
'blake2s',
]
# Create a dictionary to store the sorted hashes
sorted_hashes = {t: [] for t in hash_types + ['unknown']}
# Open the file and read the hashes
with open(filename, 'r') as f:
hashes = f.read().splitlines()
# Iterate through the list of hashes
for h in hashes:
# Iterate through the list of hash types
for t in hash_types:
# Check if the hash is a valid hash of the current type
if hashlib.new(t, b'').digest_size * 2 == len(h):
try:
int(h, 16)
sorted_hashes[t].append(h)
break
except ValueError:
pass
# If the hash doesn't match any of the above, add it to the "unknown" group
else:
sorted_hashes['unknown'].append(h)
# Return the sorted hashes
return sorted_hashes
# Example usage
sorted_hashes = sort_hashes('hashes.txt')
# Write the sorted hashes to separate files
for t, hashes in sorted_hashes.items():
with open(f'{t}_hashes.txt', 'w') as f:
f.write('\n'.join(hashes))
@yosignals
Copy link
Author

yosignals commented Dec 22, 2022

it's worth noting that many breach files are pretty crude, but a common theme is the password or hash is usually the last string on that line of text, so here's what I did to get my hashes.txt:

cat $FILE | rev | cut -f 1 -d : | rev > hashes.txt

This reverses the data in the file so the last chunk is the first chunk of data in the line (and back to front as a string (rev, reversed)) then cuts the (now) first line, reverses it back to its starting point and pushes it into a file called hashes.txt, wish bash bosh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment