无法杀死我的 python 代码。怎么了?
好的,我正在用 python 编写一个非常简单的密码破解程序,用字母数字字符暴力破解密码。目前此代码仅支持 1 个字符的密码和内部包含 md5 哈希密码的密码文件。它最终将包括指定您自己的字符限制的选项(破解程序尝试多少个字符直到失败)。现在,当我想让这段代码消失时,我无法杀死它。我已经包含了尝试和例外片段,但它不起作用。我做错了什么?
代码: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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
允许
KeyboardInterrupt
结束程序的方法是不执行任何操作。它们的工作原理是不依赖任何在except 块中捕获它们的东西;当异常从程序(或线程)中一路冒出时,程序就会终止。
您所做的就是捕获
KeyboardInterrupt
并通过打印消息然后继续来处理它们。至于为什么程序会卡住,没有任何东西会导致
num1
改变,所以md5计算每次都是相同的计算。如果您想迭代alphaNumeric
中的符号,请执行以下操作:for symbol in alphaNumeric: # do some with 'symbol'
。当然,这仍然只会考虑每一种可能的单字符密码。你必须比这更努力......:)
我认为你也对类的使用感到困惑。 Python 不要求您将所有内容包装在类中。程序末尾的
main
没有任何作用;您的代码之所以能够运行,是因为当编译器尝试找出main
类时,就会对其进行求值。这是语法的滥用。您想要做的就是将此代码放入主函数中,然后调用该函数(与当前调用checker
的方式相同)。The way to allow a
KeyboardInterrupt
to end your program is to do nothing. They work by depending on nothing catching them in anexcept
block; when an exception bubbles all the way out of a program (or thread), it terminates.What you have done is to trap the
KeyboardInterrupt
s 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 inalphaNumeric
, 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 amain
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 callchecker
currently).除了打印之外,您还需要在捕获
KeyboardInterrupt
时实际退出程序,您只是打印一条消息。Besides printing, you need to actually exit your program when capturin
KeyboardInterrupt
, you're only printing a message.这对我有用......
This is what worked for me...
好吧,当您使用
try
和except
块时,错误发生时就会引发错误。就您而言,KeyboardInterrupt
是您的错误。但是当KeyboardInterrupt
被激活时,什么也没有发生。这是因为except
部分没有任何内容。您可以在导入sys
后执行此操作:sys.exit()
是安全退出程序的简单方法。这可以用于制作带有密码的程序,以便在密码错误或类似情况下结束程序。这应该修复except
部分。现在到try
部分:如果将
break
作为try
部分的结尾,则不会发生任何事情。为什么?由于break
仅适用于循环,因此大多数人倾向于对while
循环执行此操作。让我们举一些例子。其中之一是:break
语句将立即停止并结束循环,与其兄弟continue
不同,后者继续循环。这只是额外的信息。现在,如果您在类似这样的情况下有break
:这将取决于
if
语句的位置。如果它在循环中,它将停止循环。如果没有,什么都不会发生。我假设您尝试退出该功能,但没有成功。看起来程序应该结束,所以请使用 sys.exit() ,就像我上面向您展示的那样。另外,您应该将最后一段代码(在类中)分组到一个单独的函数中。我希望这对您有帮助!Well, when you use that
try
andexcept
block, the error is raised when that error occurs. In your case,KeyboardInterrupt
is your error here. But whenKeyboardInterrupt
is activated, nothing happens. This due to having nothing in theexcept
part. You could do this after importingsys
: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 theexcept
part. Now to thetry
part:If you have
break
as the end of thetry
part, nothing is going to happen. Why? Becausebreak
only works on loops, most people tend to do it forwhile
loops. Let's make some examples. Here's one:The
break
statement will stop and end the loop immediately unlike its brothercontinue
, which continues the loop. That's just extra information. Now if you havebreak
in a something like this: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 usesys.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!