无法杀死我的 python 代码。怎么了?

发布于 2024-11-27 19:12:49 字数 1303 浏览 2 评论 0原文

好的,我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱花坊 2024-12-04 19:12:49

允许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 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).

厌倦 2024-12-04 19:12:49

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

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

梦断已成空 2024-12-04 19:12:49

这对我有用......

import sys

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

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

This is what worked for me...

import sys

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

except KeyboardInterrupt:
    print "interupt" 
    sys.exit()  
久夏青 2024-12-04 19:12:49

好吧,当您使用 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() ,就像我上面向您展示的那样。另外,您应该将最后一段代码(在类中)分组到一个单独的函数中。我希望这对您有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文