4

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
4

4 回答 4

1

让 aKeyboardInterrupt结束程序的方法是什么都不做。他们的工作依赖于没有任何东西将他们困在一个except街区里。当一个异常从程序(或线程)中冒出时,它就会终止。

您所做的是捕获KeyboardInterrupts 并通过打印一条消息然后继续处理它们。

至于为什么程序卡住了,没有什么会导致num1改变,所以md5计算每次都是一样的计算。如果您想遍历 中的符号alphaNumeric,请执行以下操作:for symbol in alphaNumeric: # do something with 'symbol'.

当然,这仍然只考虑每个可能的单字符密码。你将不得不更加努力地尝试...... :)

我认为您也对类的使用感到困惑。Python要求您将所有内容都包装在一个类中。main程序末尾的 没有任何用处;您的代码会运行,因为当编译器试图找出main类是什么时会对其进行评估。这是对语法的滥用。您要做的是将此代码放在 main函数中,然后调用该函数(与checker当前调用的方式相同)。

于 2011-08-02T01:58:23.060 回答
1

除了打印之外,您还需要在 capturin 时实际退出程序KeyboardInterrupt,您只是在打印一条消息。

于 2011-08-02T02:11:49.747 回答
0

好吧,当您使用它tryexcept阻止时,发生该错误时会引发错误。在你的情况下,KeyboardInterrupt你的错误在这里。但是当KeyboardInterrupt被激活时,什么也没有发生。这是因为该except零件中没有任何内容。您可以在导入后执行此操作sys

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

sys.exit()是安全退出程序的简单方法。这可用于制作带有密码的程序以在密码错误或类似情况下结束程序。那应该修复except部分。现在到try部分:

如果你有break作为try部分的结尾,什么都不会发生。为什么?因为break只适用于循环,所以大多数人倾向于使用while循环。让我们举一些例子。这是一个:

while 1:
    print 'djfgerj'
    break

break语句将立即停止并结束循环,不像它的兄弟continue继续循环。这只是额外的信息。现在,如果你有break这样的东西:

if liners == 0:
    break

这将取决于该if声明的位置。如果它处于循环中,它将停止循环。如果没有,什么都不会发生。我假设您尝试退出不起作用的功能。看起来程序应该结束了,所以sys.exit()像我上面展示的那样使用。此外,您应该将最后一段代码(在类中)分组到一个单独的函数中。我希望这可以帮助你!

于 2015-01-16T00:36:08.770 回答
0

这对我有用...

import sys

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

except KeyboardInterrupt:
    print "interupt" 
    sys.exit()  
于 2013-02-22T02:24:52.497 回答