随机碰撞
这个破解的目标密码是6位的数字,大小写字母
key_list = [i for i in range(48,58)] + [ i for i in range(65,91)] + [ i for i in range(97,123)]
import random
import zipfile
flag=0
def check_zip_pwd(zipFil,wt_fil, password):
global flag
try:
print(password)
password = password.encode()
an = zipFil.open(wt_fil, mode='r', pwd=password)
if len(an.read(20)) == 20:
flag=1
print('password success:', password)
except Exception as ce:
# time.sleep(0.1)
pass
def get_random_key():
return "".join([chr(key_list[random.randint(0,61)]) for i in range(6)])
if __name__ == '__main__':
dest_zip = "" #目标文件
dest_dir = "/".join(dest_zip.split('/')[:-1])
zipFile = zipfile.ZipFile(dest_zip)
wt_file = zipFile.namelist()[0]
while True:
if flag==1:
break
may_key = get_random_key()
check_zip_pwd(zipFile, wt_file, password=may_key)
# check_zip_pwd(zipFile, wt_file, password=m_key)
# zipFile.close()
暴力破解
import zipfile
def check_zip_pwd(zipFil,wt_fil, password):
try:
password = password.encode()
an = zipFil.open(wt_fil, mode='r', pwd=password)
if len(an.read(20)) == 20:
return True
except Exception as ce:
return False
def crack_zip(key_list):
for a in key_list:
for b in key_list:
for c in key_list:
for d in key_list:
for e in key_list:
for f in key_list:
m_key = chr(a) + chr(b) + chr(c) + chr(d) + chr(e) + chr(f)
flag = check_zip_pwd(zipFile, wt_file, password=m_key)
if flag == True:
return (flag,m_key)
return (False,None)
if __name__ == '__main__':
key_list = [i for i in range(48, 58)] + [i for i in range(65, 91)] + [i for i in range(97, 123)]
dest_zip = "" # 解压目标
dest_dir = "/".join(dest_zip.split('/')[:-1])
zipFile = zipfile.ZipFile(dest_zip)
wt_file = zipFile.namelist()[0]
flag,password = crack_zip(key_list)
if flag==True:
print('cracked the password is ',password)
else:
print('false')