I'm currently on my third year of a Software Development degree at I'm thinking about branching out into some security modules next year, I picked up "Violent Python" as an intro and to learn a new language, however I'm stuck on the first exercise.
I'm no newbie to programming and perhaps I'm just tired - it's been a long day on little sleep - but this simple script wont work:
import crypt
def testPass(cryptPass):
salt = cryptPass[0:2]
dictFile = open('dictionary.txt')
for word in dictFile.readlines():
word = word.strip('\n')
print "[*] Attempting password: "+word+"\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()
Passwords.txt has two users from /etc/passwd (victim and root) and it loops through them fine. I have three passwords in my dictionary.txt and for some reason it only attempts the first password:
[*] Cracking Password For: victim
[*] Attempting password: break
[-] Password Not Found.
[*] Cracking Password For: root
[*] Attempting password: break
[-] Password Not Found.
Can someone explain why the code above lifted from the book didn't work? I managed to solve the problem by using a 'with open' instead:
with open('dictionary.txt') as f:
for word in f:
word = word.strip("\n")
cryptWord = crypt.crypt(word,salt)
if (cryptWord == cryptPass):
print "[+] Found Password: "+word+"\n"
return
print "[-] Password Not Found.\n"
return