Here is python script to crack unix hash using dictionary attack.First understand how hash file look like.
victim: HX9LLTdc/jiDE: 503:100:Iama Victim:/home/victim:/bin/sh
We only interested in first two term of string.Here victim is username and HX9LLTdc/jiDE hash password.
First two character of hash password is salt in our case HX is salt which has been added to the hash.
In python there is crypt module available to generate unix hash for specific salt.

As you can see we calculate unix hash with help of given plain text password & salt.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import crypt
def testPass(cryptPass):
salt = cryptPass[0:2]
dictFile = open(‘dictionary.txt’, ‘r’)
for word in dictFile.readlines():
word = word.strip(‘\n’)
cryptWord = crypt.crypt(word, salt)
if cryptWord == cryptPass:
print ‘[+] Found Password: ‘ + word + ‘\n’
return
print ‘[-] Password Not Found.\n’
return
def main():
passFile = open(‘passwords.txt’)
for line in passFile.readlines():
if ‘:’ in line:
user = line.split(‘:’)[0]
cryptPass = line.split(‘:’)[1].strip(‘ ‘)
print ‘[*] Cracking Password For: ‘ + user
testPass(cryptPass)
if __name__ == ‘__main__’:
main()
(1)There should be password hash file which name should be passwords.txt & it should be in same folder with script.
(2)There should be dictionary file which name should be dictionary.txt & it should be in same folder with script.
(1)main function:-
passFile = open(‘passwords.txt’) :- Open password file.
for line in passFile.readlines(): iterate through number of lines in file. It is used when there is more than one user string in password file ;now we assume that we have only one string in password file so we can ignore it.
if ‘:’ in line: :- If there is : present in string than execute following stattemens
user = line.split(‘:’)[0] :-
line.split(‘:’)[0] :- split string by delimiter :
for example our sting is as follow
victim: HX9LLTdc/jiDE: 503:100:Iama Victim:/home/victim:/bin/sh
than it separate string by delimiter :
So line.split(‘:’)[0] becomes victim ;line.split(‘:’)[1] becomes HX9LLTdc/jiDE and so on.
Now we save line.split(‘:’)[0] value to username variable.
cryptPass = line.split(‘:’)[1].strip(‘ ‘) :- as mentioned previously it extract hash from string & strip(‘ ‘) remove leading whitespace & saved to variable cryptPass.
So still now we only extract username & hash from password file & save it to variable. Next is calling of testPass function.
(2)testPass :-
salt = cryptPass[0:2] :- As we mentioned in starting that first two character are salt ;so we extract salt from hash. & saved it to variable salt.
dictFile = open(‘dictionary.txt’, ‘r’) = Open dictionary in read only mode.
for word in dictFile.readlines(): :- iterate through the number of word in dictionary.
word = word.strip(‘\n’) :- strip word from dictionary file & \n is used for next word when for loop is running next time.
cryptWord = crypt.crypt(word, salt) :- it generate unix hash from word of dictionary file & salt which we extracted from hash.
if cryptWord == cryptPass: Now if calculated hash & hash in password file will be match than it will print password found on screen otherwise for loop will running for number of words in dictionary file.
Usage of script
chmod +x script_name.py
./script_name.py

