具体解决问题:
下载的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.运行速度比较慢