python 破解并解压zip压缩文件

本文介绍了一种在Linux环境下使用fcrackzip工具破解ZIP文件密码的方法,包括暴力破解和字典破解两种方式,并提供了相应的Python脚本来实现解压加密的ZIP文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

运行的环境是linux,用到的工具也是再linux 下的开源工具fcrackzip ,unbuntu和debian系统可以直接apt-get install fcrackzip

至于fcrack可以直接百度搜索用法

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import zipfile
import commands
import sys
import re
import logging
import os


def ZipFcrackzip_b(zip_filename):
    '''
    暴力破解zip文件密码
    :param zip_filename:zip文件路径
    :return: 返回破解之后的密码
    '''
    if zip_filename.count(" ") != 0:
        zip_filename=zip_filename.replace(" ","\ ")
    if zip_filename.count("(") != 0:
        zip_filename=zip_filename.replace("(","\(")
    if zip_filename.count(")") != 0:
        zip_filename=zip_filename.replace(")","\)")
    try:
        _,value_pass=commands.getstatusoutput("fcrackzip -b -u -c1 -l 4-8 "+zip_filename)
    except Exception as e:
        logging.debug("Unable to decipher the password      exception : %s",
                      repr(e))
    str_re_password=re.sub(".*== ","",value_pass)
    return str_re_password[2:]




def ZipFcrackzip_d(zip_filename,dic_file):
    '''
    字典破解zip文件密码
    :param zip_filename: zip文件路径
    :param dic_file: 字典的路径
    :return: 返回破解之后的密码
    '''
    if zip_filename.count(" ") != 0:
        zip_filename=zip_filename.replace(" ","\ ")
    if zip_filename.count("(") != 0:
        zip_filename=zip_filename.replace("(","\(")
    if zip_filename.count(")") != 0:
        zip_filename=zip_filename.replace(")","\)")
    try:
        _,value_pass=commands.getstatusoutput("fcrackzip -u -D -p "+dic_file+" "+zip_filename)
    except Exception as e:
        logging.debug("Unable to decipher the password      exception : %s",
                      repr(e))

    str_re_password=re.sub(".*== ","",value_pass)
    return str_re_password[2:]





def unpacks_zip(file_name,out_dir):
    '''
    解压加密zip文件
    :param file_name: zip文件
    :param out_dir: 解压的路径
    :return:
    '''
    zip_tag=set()
    try:
        zip_file = zipfile.ZipFile(file_name,"r")
    except Exception as e:
        logging.debug("unpack zip file:%s   exception : %s",file_name,
                      repr(e))

    for file_list in zip_file.infolist():
        if file_list.flag_bits & 0x01:
            if file_list.filename.endswith(".zip"):
                zip_tag.add(str(file_list.filename))

            try:
                zip_file.extract(file_list.filename,out_dir,ZipFcrackzip_b(file_name))
            except Exception as e:
                print (ZipFcrackzip_b(file_name))
                logging.debug("bad password for zip file %s  , exception : %s",file_list.filename,
                                repr(e))

        else:
            try:
                zip_file.extract(file_list.filename,out_dir)
            except Exception as e:
                logging.debug("extract zip file %s , exception : %s",file_list.filename,
                              repr(e))

    zip_file.close()
    #递归调用解压zip文件中的zip
    if zip_tag:
        while zip_tag:
            file_path=str(out_dir)+str(zip_tag.pop())
            try:
                unpacks_zip(file_path,out_dir)
            except Exception as e:
                logging.debug("error for the zip_extract , exception : %s",
                              repr(e))





if __name__ == '__main__':
    if len(sys.argv) < 3:
        print ("usage: python xx.py input_file  output_dir")
        sys.exit(-1)
    reload(sys)
    sys.setdefaultencoding('utf-8')
    file_name = sys.argv[1]
    dir_name = sys.argv[2]

    if file_name.endswith('.zip'):
        unpacks_zip(file_name,dir_name)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ghost丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值