0

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
2
  • 3
    Please review your code indentation (ideally 4 spaces per level) - whitespace is important in Python! Commented Jan 6, 2014 at 22:40
  • Yes! :-) And then post also a dictionary.txt example Commented Jan 6, 2014 at 22:47

1 Answer 1

2

This is an indentation mistake. For things to work properly, your 'testPass' function should look like this:

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

That way, it will loop through every word in the dictionary, and return once a password is found. If it loops through everything without finding it, it should then print "password not found". Your problem is that your code (spaced out a bit for clarity) is actually indented like this:

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

See the difference? Like this, it will only check the first word, since the second "return" is within the loop. Please verify your indentation (ideally 4 spaces, as @jonrsharpe said, or a tab) and try it again. I suspect this will solve your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Tabs versus spaces -- the eternal holy war. Good answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.