Python暴力破解zip文件

具体解决问题:

下载的zip文件被加锁,密码未包含特殊字符。假设密码是4-8位。

解决方案:

先设定可能出现的字符,存放在一个字符串中。用参数代表密码的长度,然后循环遍历的模式遍历所有可能性。

具体代码如下:

import zipfile
import random
import time
import sys
import rarfile
import math


force = 0
# Zero =0
# Fir =0
# Sec =0
# Thr =0
# Forth = 0
# letters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()'


def encode_b64(num,num_len):
    table = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()'
    result = []
    temp = num
    if 0 == temp:
        result.append('0')
    else:
        while 0 < temp:
            result.append(table[temp % 72])
            temp //= 72
    code = ''.join([x for x in reversed(result)])
    if len(code)<num_len:
        for i in range(1,num_len+1-len(code)):
            code = '0'+code
    return code


def b72tob10(str):
    table = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5,
             "6": 6, "7": 7, "8": 8, "9": 9,
             "a": 10, "b": 11, "c": 12, "d": 13, "e": 14, "f": 15, "g": 16,
             "h": 17, "i": 18, "j": 19, "k": 20, "l": 21, "m": 22, "n": 23,
             "o": 24, "p": 25, "q": 26, "r": 27, "s": 28, "t": 29, "u": 30,
             "v": 31, "w": 32, "x": 33, "y": 34, "z": 35,
             "A": 36, "B": 37, "C": 38, "D": 39, "E": 40, "F": 41, "G": 42,
             "H": 43, "I": 44, "J": 45, "K": 46, "L": 47, "M": 48, "N": 49,
             "O": 50, "P": 51, "Q": 52, "R": 53, "S": 54, "T": 55, "U": 56,
             "V": 57, "W": 58, "X": 59, "Y": 60, "Z": 61,
             "~": 62, "!": 63, "@": 64, "$": 65, "%": 66, "^": 67, "&": 68,
             "*": 69, "(": 70, ")": 71}
    result = 0
    for i in range(len(str)):
        result *= 72
        result += table[str[i]]
    return result



# def get_pwd(letters,Zero,Fir,Sec,Thr,Forth):
#     str = letters[Zero]+letters[Fir]+letters[Sec]+letters[Thr]+letters[Forth]
#     return str

class MyIterator():
    # # 单位字符集合
    min_digits =0
    max_digits =0
    num_len  = 0

    def __init__(self, min_digits, max_digits):
        # 实例化对象时给出密码位数范围,一般4到10位
        if min_digits < max_digits:
            self.min_digits = min_digits
            self.max_digits = max_digits
            self.num_len = min_digits
        else:
            self.min_digits = max_digits
            self.max_digits = min_digits
            self.num_len = min_digits


    # 迭代器访问定义
    def __iter__(self):

        return self

    def __next__(self):
        # global letters
        # global Zero
        # global Fir
        # global Sec
        # global Thr
        # global Forth
        # rst = get_pwd(letters,Zero,Fir,Sec,Thr,Forth)
        # Forth +=1
        # if Forth >= len(letters) :
        #     Forth-=len(letters)
        #     Thr +=1
        #     if Thr >= len(letters):
        #         Thr -= len(letters)
        #         Sec += 1
        #         if Sec >= len(letters):
        #             Sec -= len(letters)
        #             Fir += 1
        #             if Fir >= len(letters):
        #                 Fir -= len(letters)
        #                 Zero += 1

        global force

        if force >= math.pow(72,self.num_len) :
            self.num_len += 1
            force = 0
            rst = encode_b64(force, self.num_len)
            if self.num_len> self.max_digits :
                sys.exit(0)
        else:
            rst = encode_b64(force,self.num_len)
            force += 1

        return rst


def extract():
    start_time = time.time()
    zfile = rarfile.RarFile("D:\\book\\test.rar")
    for p in MyIterator(4, 8):
        try:
            zfile.extractall(path=".", pwd=str(p).encode('utf-8'))
            print("the password is {}".format(p))
            now_time = time.time()
            print("spend time is {}".format(now_time - start_time))
            sys.exit(0)
        except Exception as e:
            pass
        print(p)


if __name__ == '__main__':
    extract()

缺点:1.限制比较多,密码字符的可能性,长度。。。

           2.运行速度比较慢

要使用Python破解zip文件,可以使用zipfile模块提供的解压缩功能。首先,你需要导入zipfile模块。然后,使用ZipFile()函数打开需要解压缩的zip文件,并指定打开模式为读取模式。接下来,你可以使用extractall()函数将zip文件中的所有文件解压缩到指定的目录中。以下是一个示例代码,展示了如何使用Python破解zip文件: ```python import zipfile zip_file = zipfile.ZipFile('example.zip', 'r') zip_file.extractall() zip_file.close() ``` 在这个示例中,我们假设有一个名为example.zipzip文件需要解压缩。首先,我们使用ZipFile()函数打开这个zip文件,并将其赋值给zip_file变量。然后,我们使用extractall()函数将zip文件中的所有文件解压缩到当前目录下。最后,我们使用close()函数关闭zip文件。 请注意,这只是一个示例代码,你需要根据实际情况修改文件名和路径。同时,为了成功破解zip文件,你可能需要提供相应的破解算法或密码字典。具体的破解过程可能因破解目标的不同而有所差异。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python 暴力破解zip文件](https://blog.csdn.net/qq_51768842/article/details/125323094)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python实现解压缩Zip文件(附完整源代码)](https://blog.csdn.net/CodeWG/article/details/131076338)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值