Python Hashcracker (Dictionary Attack)

September 16, 2015 | Views: 17973

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 https://cybrary.it

#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”
sys.exit()

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

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

for option, argument in options:
if option in (“-h”, “–help”):
usage()
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 == “”:
usage()

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

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

try:
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)
dictfile.close()
sys.exit()
else:
print “Attempting password ‘{0}’, Attemp No. {1}”.format(word, counter)
else:
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)
dictfile.close()
sys.exit()
except KeyboardInterrupt:
print “User Exit”
sys.exit()

main()

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

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

Happy Hacking,

CameToLearn

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

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

    [ http://greysec.ir/ebook/BlackHat.Python.Python.Programming.for.Hackers_%5Bwww.graymind.ir%5D.pdf ]

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

    Here is a link which is formatted with tabs.
    http://pastebin.com/w6JvQrTF

  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

 
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel