Python zipfile库修复

时间:2014-02-25 12:27:47

标签: python passwords zipfile

我遇到了问题。我已经制作了一个密码为12345的简单zip文件。 现在,当我尝试使用暴力提取密码时,zipfile选择了错误的密码。它说它找到了密码aaln0,但提取的文件是完全空的。有没有办法'修复'图书馆?或者有替代品吗?感谢

程序代码:

#!/usr/bin/env python
import itertools
import threading
import argparse
import time
import zipfile
import string
global found
found = False

def extract_zip(zFile, password):
    """
    Extract archive with password
    """
    try:
        zFile.extractall(pwd=password)
        write("[+] Password found:", password, "\n")
        global found
        found = True
    except Exception, e:
        pass

def write(*args):
    print "[%s] %s" % (time.ctime(), " ".join(args))

def main_loop(zFile, length):
    """
    Main loop
    """
    write("[*] Python Brute-Force zip cracker")
    write("[*] Zipfile: %s; password length: %s" % (zFile, length))
    try:
        zfile = zipfile.ZipFile(zFile)
    except:
        write("Cannot open zip file")
        exit(1)
    for combo in itertools.imap(''.join, itertools.product(string.letters + string.digits,
                                                            repeat=length)):
        if found:
            break
        thread = threading.Thread(target=extract_zip, args=(zfile, combo))
        thread.start()
    if not found:
        write("[-] Password not found")

def main():
    """
    Main function
    """
    parser = argparse.ArgumentParser(usage="brute-force-zipcracker.py -f <zipfile> -l <password length>")
    parser.add_argument("-f", "--zipfile", help="specify zip file", type=str)
    parser.add_argument("-l", "--length", type=int, help="password length", default=5)
    args = parser.parse_args()
    if (args.zipfile == None):
        print parser.usage
        exit(0)

    main_loop(args.zipfile, args.length)

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:4)

首先,你正在做:

for combo in itertools.imap(...):
    if found:
        break
    thread = ...
    thread.start()
if not found:
    ...

只需看一下它。
found是在一个线程中定义的,但是你正在启动多个线程,并且你全局希望它将在其中一个线程中设置。 如何确保正确的线程完成了正确的工作?如果其中一个线程中存在误报并且您不愿意从每个单独的线程中检索值,该怎么办?小心你的线程!

其次, 如果线程没有及时完成你的combo循环,你将最终进入if not found,因为线程还没有完成运行,无法找到你要找的东西,特别是如果你有一个较大的zip文件,需要几秒钟才能完成(成功的密码将开始解压缩文件,可能需要几分钟,并且在此过程完成之前不会设置found。)

最后,获取用于保护此zip文件的参数将非常简洁。

编辑:

您还可以通过以下格式向我们提供更多信息:

zFile.debug(3)
zFile.testzip()
zFile.extractall(pwd=password)

来自zipfile.ZipInfo(filename)

的其他有用的东西

到解决方案

#!/usr/bin/env python
import itertools
import argparse
import zipfile
import string

def extract_zip(filename, password):
    try:
        zFile = zipefile.ZipFile(filename)
        zFile.extractall(pwd=password)
        return True
    except zipfile.BadZipFile:
        return False

def main_loop(filename, length):
    print("[*] Python Brute-Force zip cracker")
    print("[*] Zipfile: %s; password length: %s" % (zFile, length))
    cracked = False
    for combo in itertools.imap(''.join, itertools.product(string.letters + string.digits, repeat=length)):
        cracked = extract_zip(filename, combo)
        if cracked:
            print('Yaay your password is:',combo)
            break
    if not cracked:
        print('Sorry, no luck..')
def main():
    parser = argparse.ArgumentParser(usage="brute-force-zipcracker.py -f <zipfile> -l <password length>")
    parser.add_argument("-f", "--zipfile", help="specify zip file", type=str)
    parser.add_argument("-l", "--length", type=int, help="password length", default=5)
    args = parser.parse_args()
    if (args.zipfile == None):
        print parser.usage
        exit(0)

    main_loop(args.zipfile, args.length)

if __name__ == '__main__':
    main()