Aman Grover Aman Grover - 4 months ago 14
Python Question

Equal strings doesn't return to true

I have started learning Python and I have started with the book Violent Python. In it the first chapter describes a script to crack *nix based password hashes with the help of

crypt
class. Here is the code:

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)
print cryptPass+":"cryptWord
if(cryptPass == cryptWord):
print "[+] Password found : "+word
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()


I have a passwords.txt file which contains the username:password(password hash) strings, and another file named dictionary.txt which contains the dictionary words.These are the contents of passwords.txt file :

apple:HXJintBqUVCEY
mango:HXInAjNhZF7YA
banana:HXKfazJbFHORc
grapes:HXtZSWbomS0xQ


and the dictionary.txt :

apple
abcd
efgh
ijkl
mnop


The password hash computed from testpass() method and the password hash from passwords.txt for username apple are equal , when I print both of them. But the output here for all 4 usernames is "[-] Password not found". Why the == test fails here?

Answer

Perhaps you have a trailing \n at the end of the line. Try change:

        cryptPass = line.split(':')[1].strip(' ')

to:

        cryptPass = line.split(':')[1].strip('\n').strip(' ')

or even simpler (as suggested in comments):

        cryptPass = line.split(':')[1].strip()