Python Hashcracker (Dictionary Attack)

September 16, 2015 | Views: 19152

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

This is a good example of a simple Python script, which you can create in less than 30 minutes.

Python is really useful for creating security tools. You can create many tools like Port Scanners, Hashcrackers, Servers and Clients…and many more.

A good book I recommend reading that focuses on Python security tools is “Black Hat Python” by Justin Seitz.

Without further ado, here’s the code for the Python Hashcracker I created (in Python 2.7).


===================START OF CODE==========================

import hashlib, sys, getopt

#Coded by CameToLearn from

#TODO: Add input of multiple hashes

def usage():
print “-v              |    Print attempts”
print “-h              |    This menu”
print “-H, –hash      |    Hash to crack”
print “-t –type       |    Supports: md5, sha1, sha224, sha256, sha384, sha512”
print “-d –dict       |    Dictionary file to crack with”

#    print “-i, –inputfile |    Input file of hashes”

def main():
counter = 0
input_hash = “”
input_hash_type = “”
#input_hash_file = “”
wordlist = “”
verbose = 0

options, arguments = getopt.getopt(sys.argv[1:], “vhi:H:t:d:”, [“hash=”, “type=”, “help”, “inputfile=”, “dict=”])
except getopt.GetoptError as err:
print str(err)

for option, argument in options:
if option in (“-h”, “–help”):
elif option == “-v”:
verbose = 1
elif option in (“-H”, “–hash”):
input_hash = argument
elif option in (“-t”, “–type”):
input_hash_type = argument.lower()
elif option in (“-d”, “–dict”):
wordlist = argument

“””elif option in (“-i”, “–inputfile”):
input_hash_file = argument”””

if input_hash == “” or input_hash_type == “” or wordlist == “”:

if input_hash_type == “md5”:
htype = hashlib.md5
elif input_hash_type == “sha1”:
htype = hashlib.sha1
elif input_hash_type == “sha224”:
htype = hashlib.sha224
elif input_hash_type == “sha256”:
htype = hashlib.sha256
elif input_hash_type == “sha384”:
htype = hashlib.sha384
elif input_hash_type == “sha512”:
htype = hashlib.sha512

dictfile = open(wordlist,’r’)
except Exception as e:
print “Unable to open file, make sure you typed the correct filename.”, e

if verbose:
for word in dictfile:
counter += 1
word = word.strip()
hashed = htype(word).hexdigest()
if hashed == input_hash:
print “Hash cracked, at attempt {0}. hashed word is: ‘{1}’ “.format(counter, word)
print “Attempting password ‘{0}’, Attemp No. {1}”.format(word, counter)
for word in dictfile:
counter += 1
word = word.strip()
hashed = htype(word.strip()).hexdigest()
if hashed == input_hash:
print “Hash cracked, at attempt {0}. hashed word is: ‘{1}’ “.format(counter, word)
except KeyboardInterrupt:
print “User Exit”


=======================END OF CODE==========================

I highly recommend using the rockyou.txt wordlist found on Kali Linux and plenty of internet websites.

Happy Hacking,


Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
Ready to share your knowledge and expertise?
  1. thanks for this tutorial!

  2. Nice tutorial/info/knowledge..
    The book recommendation is the best for security tools for python.

    [ ]

  3. Update: Seems like website removed the tabs I inserted into the code.

    Here is a link which is formatted with tabs.

  4. Nice tutorial, keep it up… and Thanks for the book recommendation, i just got a copy now.

Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge


We recommend always using caution when following any link

Are you sure you want to continue?