利用Python之zipfile模块破解Zip文档口令以及详细的排错过程

  本代码利用zipfile模块对zip加密文档进行破解,本身并不复杂,主要用到的方法也只有一个,但是在编写以及调试该代码的时候遇到了各种错误,为了给大家参考,排错过程如下详解:

# python zipfile_crack.py -f video.zip -w rockyou.txt                                                                                         1************************************************** 

                ********** ZIP Cracker V1.0 by Jason Wong********

                ************************************************** 

Traceback (most recent call last):

  File "/root/Desktop/Hack_Project/violent_python/zipfile_crack.py", line 64, in <module>

    zip.run()

  File "/root/Desktop/Hack_Project/violent_python/zipfile_crack.py", line 54, in run

    for word in f.readlines():

  File "/usr/lib/python3.9/codecs.py", line 322, in decode

    (result, consumed) = self._buffer_decode(data, self.errors, final)

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 933: invalid continuation byte
 

  此时感觉程序不会再有什么问题,但是竟然出现解码错误,而这就是一个普通的英文字典而已,因此进行排查,怀疑是不是字典有问题(这是Kali Linux内置的比较大的字典),自己手动编写了一个简单的字典文件,passwordlist.txt

 

# python zipfile_crack.py -f video.zip -w passwordlist.txt 

                ************************************************** 

                ********** ZIP Cracker V1.0 by Jason Wong******** 

                **************************************************
admin
root
root123
qwer12345
test
123456 987654321 hello msfadmin [+] Failed to crack!!!
 

  运行以后发现,竟然没有破解成功(msfadmin就是密码,但是没有破解出来),因此不知道是什么原因,捕捉一下具体异常是什么。

 

─# python zipfile_crack.py -f video.zip -w passwordlist.txt 

                ************************************************** 

                ********** ZIP Cracker V1.0 by Jason Wong******** 

                **************************************************

Try password: admin

pwd: expected bytes, got str

Try password: root

pwd: expected bytes, got str

Try password: root123

pwd: expected bytes, got str

Try password: qwer12345

pwd: expected bytes, got str

Try password: test

pwd: expected bytes, got str

Try password: 123456

pwd: expected bytes, got str

Try password: 987654321

pwd: expected bytes, got str

Try password: hello

pwd: expected bytes, got str

Try password: msfadmin

pwd: expected bytes, got str

[+] Failed to crack!!!

 

 

  终于知道是什么原因是,需要将pwd变量编码(utf-8)以后再赋给extractall方法。但是将password编码重新运行代码,却又出现下面的错误:

 

# python zipfile_crack.py -f video.zip -w passwordlist.txt 

                ************************************************** 

                ********** ZIP Cracker V1.0 by Jason Wong******** 

                ************************************************** 

Try password: admin

That compression method is not supported

Try password: root

That compression method is not supported

Try password: root123

That compression method is not supported

Try password: qwer12345

That compression method is not supported

Try password: test

That compression method is not supported

Try password: 123456

That compression method is not supported

Try password: 987654321

That compression method is not supported

Try password: hello

That compression method is not supported

Try password: msfadmin

That compression method is not supported

[+] Failed to crack!!!

 

  根据错误提示,说是不支持这种压缩算法,查看了一下模块的文档,

 

class ZipFile(builtins.object)

     |  ZipFile(file, mode='r', compression=0, allowZip64=True, compresslevel=None, *, strict_timestamps=True)     |

     |  Class with methods to open, read, write, close, list zip files.     |

     |  z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True,
     |              compresslevel=None)     |

     |  file: Either the path to the file, or a file-like object.

     |        If it is a path, the file will be opened and closed by ZipFile.

     |  mode: The mode can be either read 'r', write 'w', exclusive create 'x',

     |        or append 'a'.

     |  compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),

     |               ZIP_BZIP2 (requires bz2) or ZIP_LZMA (requires lzma).

     |  allowZip64: if True ZipFile will create files with ZIP64 extensions when

     |              needed, otherwise it will raise an exception when this would

     |              be necessary.

     |  compresslevel: None (default for the given compression type) or an integer

     |                 specifying the level to pass to the compressor.

     |                 When using ZIP_STORED or ZIP_LZMA this keyword has no effect.

     |                 When using ZIP_DEFLATED integers 0 through 9 are accepted.

     |                 When using ZIP_BZIP2 integers 1 through 9 are accepted

 


  最后发现用linux本身自带的zip命令对文档进行压缩和加密,就可以成功解密该文档,此前是在windows上用winrar软件压缩加密(虽然选择了zip压缩算法)。

 

  还有如果用比加大的字典比如rockyou.txt,用readlines方法来读取每行,会出错,因此改用循环readline,即每次读取一行,但开始的时候用f.readline() is None来判断已经读到文件的末尾,但是这回进入死循环,改用长度进行判断if len(word) == 0

import zipfile
import threading
import sys
import os
import optparse
import termcolor
import queue


class ZipFileCracker:
    def __init__(self) -> None:
        self.zipfile = self.get_params()[0]
        self.wordlist = self.get_params()[1]
        self.zipfile_obj = zipfile.ZipFile(self.zipfile)
        self.q = queue.Queue()      # share data /signal between threads
        self.banner()

    def get_params(self):
        parser = optparse.OptionParser("Usage: <Program> -f zipfile -w wordlist")
        parser.add_option('-f', '--zipfile', dest='zipfile', type='string', help='Specify zip file to crack ')
        parser.add_option('-w', '--wordlist', dest='wordlist', type='string', help='Specify wordlist to crack')
        options, args = parser.parse_args()
        if options.zipfile is None or options.wordlist is None:
            print(parser.usage)
            sys.exit(0)
        if not os.path.exists(options.zipfile):
            print("The target file does not exist")
            sys.exit(0)
        if not os.path.exists(options.wordlist):
            print("The wordlist does not exist")
            sys.exit(0)
        return options.zipfile, options.wordlist


    def banner(self):
        banner= """
                **************************************************

                ********** %s********

                **************************************************
        """ % termcolor.colored("ZIP Cracker V1.0 by Jason Wong",'blue')
        print(banner)

    def open_zip_file(self, password):
        print("Trying password: %s" % password)
        try:
            self.zipfile_obj.extractall(pwd=password.encode('utf-8'))
            print("[-] Password Found: %s" % termcolor.colored(password, 'blue'))
            self.q.put("Found")            
        except:
            pass
  

    def run(self):
        with open(self.wordlist, 'r') as f:
            # for word in f.readlines():
            #     if self.q.empty():
            #         t = threading.Thread(target=self.open_zip_file, args=(word.strip(),))
            #         t.start()
            #         t.join()
            while True:
                try:
                    word = f.readline()
                except:
                    pass
                if len(word) == 0:
                    break                    # reaches the end of the file
                if self.q.empty():
                    t = threading.Thread(target=self.open_zip_file, args=(word.strip(),))
                    t.start()
                    t.join()

            
        if self.q.empty():
            print('[+] Failed to crack!!!')    #Does not find the password in the wordlist

if __name__ == '__main__':
    zip = ZipFileCracker()
    zip.run()

 

posted @ 2022-05-17 12:30  Jason_huawen  阅读(453)  评论(0编辑  收藏  举报