Python script to crack MD5 hash using dictionary attack- (2)

Continue from the previous post ; Now we used python to crack MD5 hash using dictionary attack.

#!/usr/bin/python
import sys, re, hashlib
# Check hash length
def chklength(hashes):
if len(hashes) != 32:
print “[-] Improper length for md5 hash.”
sys.exit(1)

# Attempts to crack hash against any givin wordlist.
def dict_attack():
hashes = raw_input(‘\nPlease specify hash value: ‘)
chklength(hashes)

wordlist = raw_input(‘\nPlease specify wordlist path: ‘)
try:
words = open(wordlist, “r”)
except(IOError):
print “[-] Error: Check your wordlist path.\n”
sys.exit(1)

words = words.readlines()
print “\n”,len(words),”words loaded…”
for word in words:
hash = hashlib.md5(word[:-1])
value = hash.hexdigest()
if hashes == value:
print “[+] Password is:”+word,”\n”
sys.exit(0)

print(‘\n1 – Dictionary Attack’)
print(‘2 – Exit’)
selection = raw_input(‘\nSelect an option from above: ‘)
sys.stdout.flush()

if selection == “1”:
dict_attack()
pass

elif selection == “2”:
sys.exit(0)

There are three function in this script.
(1)chklength
(2)dict_attack
(3)man function

we are going in reverse to understand script.

(1)Main function :- when you execute script first two lines display text on screen & asked user input for that task.
print(‘\n1 – Dictionary Attack’)
print(‘2 – Exit’)

Whatever selection from user is saved into the variable selection.
If selection is 1 ; it called function dict_attack() & if selection is 2 it terminate script.

(2)dict_attack :-
hashes = raw_input(‘\nPlease specify hash value: ‘) :- Asked to enter MD5 hash & saved it to variable hashes.

chklength(hashes) :- Now it call function chklength with argument specified is hashes. So what chklength function is do. It check length of MD5 hash ;because MD5 hash is 32 character ;it check if hash is proper or not ;If it is not 32 character long than it print Improper length for md5 hash & exit script.

wordlist = raw_input(‘\nPlease specify wordlist path: ‘) :- It ask to enter path of wordlist file & saved string to variable name wordlist.

words = open(wordlist, “r”) :- open file in read only mode.

except(IOError):
print “[-] Error: Check your wordlist path.\n”
sys.exit(1)

Above part indicate that if we entered wrong path name than it throw error message & stop execution of script.

words = words.readlines() :- read dictionary file.

print “\n”,len(words),”words loaded…” :- Print number of line in dictionary file on screen.

for word in words:
hash = hashlib.md5(word[:-1])
value = hash.hexdigest()

Above part indicate that it takes first word from word list file than calculate MD5 hash & then calculate digest & final value is saved in variable value.

If our calculated value & entered value is same than it print password is “+word,”\n” , otherwise for loop is running for number of words in dictionary file & doing above process.

md5-crack-python-script

md5-crack-python-script

Usage of script:-

chmod +x script_name.py

./script_name.py

One thought on “Python script to crack MD5 hash using dictionary attack- (2)

Leave a comment