Reputation: 3
I'm fairly new to Python and I'm trying to create a simple program that collects MD5 Hash passwords and then matches them to a dictionary I've created with common passwords in it.
I can collected the MD5 passwords no problem, the problem is when I try to compare them to a dictionary of words I can't get it working at all.
Any tips or direction would be appreciated, I'm clueless as to what to do next and I've searched for many days online before resorting to asking for assistance.
My code is as follows,
import sys, re, hashlib
def dict_attack(passwd_hash):
print 'dict_attack(): Cracking hash:', passwd_hash
#set up list of common password words
passwords = open('J:/dictionary.txt')
passwd_found = False
if passwd_found:
print 'dict_attack(): Password recovered: ' (passwd)
def main():
print'[dict_crack] Tests'
passwd_hash = '4297f44b13955235245b2497399d7a93'
dict_attack(passwd_hash)
if __name__ == '__main__':
main()
RELEVANT CODE FOR FURTHER QUESTION
hash_to_crack = password
dict_file = "J:/dictionary.txt"
with open(dict_file) as fileobj:
for line in fileobj:
line = line.strip()
if hashlib.md5(line).hexdigest() == hash_to_crack:
print "Successfully cracked the hash %s: It's %s" % (hash_to_crack, line)
return ""
print "Failed to crack the file."
Upvotes: 0
Views: 6688
Reputation: 1303
The following script does the trick and can work with extremely large files since it doesn't read the whole dictionary at once. Good luck with your code. Comment if you have any problems or questions about this.
import hashlib
hash_to_crack = "5badcaf789d3d1d09794d8f021f40f0e"
dict_file = "dict.txt"
def main():
with open(dict_file) as fileobj:
for line in fileobj:
line = line.strip()
if hashlib.md5(line).hexdigest() == hash_to_crack:
print "Successfully cracked the hash %s: It's %s" % (hash_to_crack, line)
return ""
print "Failed to crack the file."
if __name__ == "__main__":
main()
EDIT: Multiple hash cracking. Enjoy.
import hashlib
hashes_to_crack = ["5badcaf789d3d1d09794d8f021f40f0e", "d0763edaa9d9bd2a9516280e9044d885", "8621ffdbc5698829397d97767ac13db3"]
dict_file = "dict.txt"
def main(hash_to_crack):
with open(dict_file) as fileobj:
for line in fileobj:
line = line.strip()
if hashlib.md5(line).hexdigest() == hash_to_crack:
print "Successfully cracked the hash %s: It's %s" % (hash_to_crack, line)
return ""
print "Failed to crack the file."
if __name__ == "__main__":
for hashToCrack in hashes_to_crack:
main(hashToCrack)
Upvotes: 0
Reputation: 413
I'm missing some code in your snippet... maybe this could be a starting point to elaborate (not tested, not sure it works):
from hashlib import md5
_hashes = { md5( pwd.strip() ).hexdigest() : pwd.strip()
for pwd in open('J:/dictionary.txt') }
def main():
print'[dict_crack] Tests'
passwd_hash = '4297f44b13955235245b2497399d7a93'
if passwd_hash in _hashes:
print "found %s = %s" % (passwd_hash, _hashes[passwd_hash])
if __name__ == '__main__':
main()
Upvotes: 1