Unable to kill my python code. What's wrong?

Okay, that's why I write a very simplified password to crack passwords in python, which translates a password with alphanumeric characters. Currently, this code only supports 1 character passwords and a password file with an internal password of md5. Ultimately, he will turn on the option to indicate your own character restrictions (how many characters he tries to crack until it works). Right now I can’t kill this code when I want it to die. I included a try besides snippit, however it does not work. What have I done 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 
+4
source share
4 answers

The way to let KeyboardInterrupt terminate your program is to do nothing . They work depending on what they are not caught in the except block; when an exception bubbles completely from the program (or thread), it terminates.

What you did was capture KeyboardInterrupt and process them by printing a message, and then continue.

As for why the program gets stuck, there is nothing that num1 would ever cause, so md5 calculation will be the same calculation every time. If you want to alphaNumeric over characters in alphaNumeric , do the following: for symbol in alphaNumeric: # do something with 'symbol' .

Of course, this will only take into account all possible single-character passwords. You will have to make more efforts than this ... :)

I think you are also confused about using classes. Python does not require you to wrap everything inside a class. main at the end of your program is nothing useful; your code runs because it evaluates when the compiler tries to figure out what the main class is. This is a misuse of syntax. What you want to do is put this code in the main function and call the function (just like you call checker now).

+1
source

Besides printing, you should actually exit your program when capturin KeyboardInterrupt , you only print a message.

+1
source

This is what worked for me ...

 import sys try: ....code that hangs.... except KeyboardInterrupt: print "interupt" sys.exit() 
0
source

Well, when you use this try and except block, an error occurs when this error occurs. In your case, KeyboardInterrupt is your mistake here. But when KeyboardInterrupt activated, nothing happens. This is due to the lack of anything in the except part. You can do this after importing sys :

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

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

If you break as the end of the try part, nothing will happen. What for? Since break only works on loops, most people tend to do this for while loops. Let's make some examples. Here is one:

 while 1: print 'djfgerj' break 

The break statement will stop and end the loop right away, unlike its continue brother, which continues the loop. This is just additional information. Now, if you have a break in something like this:

 if liners == 0: break 

It will depend on where this if . If it is in a loop, it will stop the loop. If not, nothing will happen. I assume that you tried to exit a function that did not work. It seems like the program should end, so use sys.exit() as I showed you above. In addition, you should group this last piece of code (in the class) into a separate function. Hope this helps you!

0
source

All Articles