python hash cracking script

Posted by Suraj Singh on March 24, 2017 · 23 mins read
hii readers,



                        Today I Going To Show You How To Create Hash Cracking Python Script Using Python Hashlib Module? and this tutorials is also very good example of python hashlib module?. if you want to learn python in penetration way then this tutorials is created for you.


readers,

            here, first let me explain you, how our script will work.

In this Script,

               We Will Take All Passwords one by one and then,

 Encrypt every single password one by one and try to match that string with user provided hash.

here,

let me show you a practical example.

For Hashing String In Hash Using Hashlib.


#!/usr/bin/python

import hashlib

here, hashing Codes.


# here self == Salt Name
# like ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
#
# and
# string = data for hashing
#
def encrypt_salt(self, string):

if self.salt=="md5":
return hashlib.md5(string).hexdigest()

if self.salt=="sha1":
return hashlib.sha1(string).hexdigest()

if self.salt=="sha224":
return hashlib.sha224(string).hexdigest()

if self.salt=="sha256":
return hashlib.sha256(string).hexdigest()

if self.salt=="sha384":
return hashlib.sha384(string).hexdigest()

if self.salt=="sha512":
return hashlib.sha512(string).hexdigest()


as you can see in above example. for md5, we need only need

hashlib.md5(your_string_here).hexdigest().

let's move ahead and try to create real life tools hash cracker using python.

here, I am sharing my code. but you can modify these codes as you want.

1. pyhashcrack.py


  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python
#
# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This Script Is Created For http://www.bitforestinfo.com
# This Script is Written By
#
#
##################################################
######## Please Don't Remove Author Name #########
############### Thanks ###########################
##################################################
#
#
__author__='''

######################################################
By
######################################################

Suraj Singh


surajsinghbisht054@gmail.com
http://www.bitforestinfo.com/


######################################################
'''
# =================Other Configuration================
# Usages :
usage = "usage: %prog [options] "
# Version
Version="%prog 0.0.1"
# ====================================================
print __author__

# Import Modules
import hashlib
import optparse
import sys
import time


class hash_crack_engine:
def __init__(self):
self.starttime = time.time()
self.extract_input_data()
self.open_and_get_file_data()
self.start_cracking_engine()
self.closetime = time.time()
self.close_all_process()


def encrypt_salt(self, string):
if self.salt=="md5":
return hashlib.md5(string).hexdigest()
if self.salt=="sha1":
return hashlib.sha1(string).hexdigest()
if self.salt=="sha224":
return hashlib.sha224(string).hexdigest()
if self.salt=="sha256":
return hashlib.sha256(string).hexdigest()
if self.salt=="sha384":
return hashlib.sha384(string).hexdigest()
if self.salt=="sha512":
return hashlib.sha512(string).hexdigest()


def start_cracking_engine(self):
self.got_hash = []
for i in self.words:
if self.encrypt_salt(i.strip("\n")) in self.hashlist:
print "[+] Hash Cracked! {} = {}".format(i.strip('\n'), self.encrypt_salt(i.strip("\n")))
self.result.write(" {} : {}\n".format(i.strip('\n'), self.encrypt_salt(i.strip("\n"))))
self.got_hash.append(i.strip("\n"))
if len(self.got_hash)==len(self.hashlist):
break
self.pwdtries = self.pwdtries + 1
return

def extract_input_data(self):
self.starttime=time.time()
self.pwdtries=0
# Extracting Function
parser = optparse.OptionParser(usage, version=Version)
parser.add_option("-f", "--file", action="store", type="string", dest="filename",help="Please Specify Path of Hash File", default=None)
parser.add_option("-d", "--dict", action="store", type="string", dest="dictionery", help="Please Specify Path of Password Dictionery.", default=None)
parser.add_option("-o", "--output", action="store", type="string", dest="output", help="Please Specify Path for Saving Cracked hash", default='cracked_hash.txt')
parser.add_option("-s", "--salt", action="store", type="string", dest="salt", help="Please Provide Hash Salt. ex: md5, sha1, sha256, sha512", default=None)
(option, args)=parser.parse_args()
# Record Inputs Data
print "[+] Extracting Input Data..."
self.filename=option.filename
self.dictionery=option.dictionery
self.output = option.output
self.salt = option.salt

if not self.salt:
print "[+] Please Provide Hash Salts."
sys.exit(0)
if self.salt not in hashlib.algorithms:
print "[+] Please Provide Valid Salt. \n\t\tEx : {}".format(hashlib.algorithms)
sys.exit(0)

if not self.filename:
print "[+] Please Provide Hash File."
sys.exit(0)

if not self.dictionery:
print "[+] Please Provide Password Source."
sys.exit(0)
if not self.output:
print "[+] Please Provide Output Path."
sys.exit(0)
return

def open_and_get_file_data(self):
hashlist = open(self.filename,'r')
wordlist = open(self.dictionery, 'r')
self.hashlist = [i.strip('\n') for i in hashlist.xreadlines()]
self.words = wordlist.xreadlines()
self.result = open(self.output,'a')
return

def close_all_process(self):
self.result.close()
self.time_management()
return

def time_management(self):
print "[*] Starting Time ",self.starttime
print "[*] Closing Time ",self.closetime
print "[*] Password Try ",self.pwdtries
print "[*] Average Speed ",self.pwdtries/(self.closetime-self.starttime)
return

# main trigger function
if __name__=="__main__":
hash_crack_engine()

and readers, if you wants the latest example.

then, check here my GitHub repository. click here


I know, these codes look very lengthy.

but my readers,

for useful features. this lengthy is important.

now,

let me show you some important functions of these codes.

Features :


Usage: pyhashcrack.py [options] 

Options:
--version show program's version number and exit
-h, --help show this help message and exit
-f FILENAME, --file=FILENAME
Please Specify Path of Hash File
-d DICTIONERY, --dict=DICTIONERY
Please Specify Path of Password Dictionery.
-o OUTPUT, --output=OUTPUT
Please Specify Path for Saving Cracked hash
-s SALT, --salt=SALT Please Provide Hash Salt. ex: md5, sha1, sha256,
sha512


Example :



python pyhashcrack.py -f test/hash.txt -s md5 -d test/dict.txt 


If You Like My Article,

Like, Comment, Share.

                  Suraj


More Update, Visit ours Regularly. 
And Subscribe Our Blog, 

Follow Us and share it.
For Any Type of Suggestion Or Help
Contact me:
Suraj
surajsinghbisht054@gmail.com