开发者

Can't kill my python code. What's wrong?

Okay, so I'm writing a very simplistic password cracker in python that brute forces a password with alphanumeric characters. Currently this code only supports 1 character passwords and a password file with a md5 hashed password inside. It will eventually include the option to specify your own character limits (how many characters the cracker tries until it fails). Right now I cannot kill this code when I want it to die. I have included a try and except snippit, however it's not working. What did I do wrong?

Code: http://pastebin.com/MkJGmmDU

import linecache, hashlib

alphaNumeric = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",1,2,3,4,5,6,7,8,9,0]

class main:
    def checker():
            try:
                    while 1:
                            if hashlib.md5(alphaNumeric[num1]) == passwordHash:
                                    print "Success! Your password is: " + str(alphaNumeric[num1])
                                    break
            except KeyboardInterrupt:
                    print "Keyboard Interrupt."

    global num1, passwordHash, fileToCrack, numOfChars
    print "What file do you want to crack?"
  开发者_开发技巧  fileToCrack = raw_input("> ")
    print "How many characters do you want to try?"
    numOfChars = raw_input("> ")
    print "Scanning file..."
    passwordHash = linecache.getline(fileToCrack, 1)[0:32]
    num1 = 0

    checker()

main


The way to allow a KeyboardInterrupt to end your program is to do nothing. They work by depending on nothing catching them in an except block; when an exception bubbles all the way out of a program (or thread), it terminates.

What you have done is to trap the KeyboardInterrupts and handle them by printing a message and then continuing.

As for why the program gets stuck, there is nothing that ever causes num1 to change, so the md5 calculation is the same calculation every time. If you wanted to iterate over the symbols in alphaNumeric, then do that: for symbol in alphaNumeric: # do something with 'symbol'.

Of course, that will still only consider every possible one-character password. You're going to have to try harder than that... :)

I think you're also confused about the use of classes. Python does not require you to wrap everything inside a class. The main at the end of your program does nothing useful; your code runs because it is evaluated when the compiler tries to figure out what a main class is. This is an abuse of syntax. What you want to do is put this code in a main function, and call the function (the same way you call checker currently).


Besides printing, you need to actually exit your program when capturin KeyboardInterrupt, you're only printing a message.


This is what worked for me...

import sys

try:
    ....code that hangs....

except KeyboardInterrupt:
    print "interupt" 
    sys.exit()  


Well, when you use that try and except block, the error is raised when that error occurs. In your case, KeyboardInterrupt is your error here. But when KeyboardInterrupt is activated, nothing happens. This due to having nothing in the except part. You could do this after importing sys:

try:
    #Your code#
except KeyboardInterrupt:
    print 'Put Text Here'
    sys.exit()

sys.exit() is an easy way to safely exit the program. This can be used for making programs with passwords to end the program if the password is wrong or something like that. That should fix the except part. Now to the try part:

If you have break as the end of the try part, nothing is going to happen. Why? Because break only works on loops, most people tend to do it for while loops. Let's make some examples. Here's one:

while 1:
    print 'djfgerj'
    break

The break statement will stop and end the loop immediately unlike its brother continue, which continues the loop. That's just extra information. Now if you have break in a something like this:

if liners == 0:
    break

That's going to depend where that if statement is. If it is in a loop, it is going to stop the loop. If not, nothing is going to happen. I am assuming you made an attempt to exit the function which didn't work. It looks like the program should end, so use sys.exit() like I showed you above. Also, you should group that last piece of code (in the class) into a seperate function. I hope this helps you!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜