暴力穷举zip、rar压缩文件的密码
生成密码的方式类似与时钟,末尾遍历完了第k位所有的字符,就让第k位的前一位到下一位字符,第k位回到第0个字符。
对python还不太熟悉,效率比较低,但是能破解简单的密码。
import zipfile #密码可能有的字符 testSetstr = "w.abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#$%&'()*+,-/:;<=>?@[\]^_`{|}~" # testSetstr = "0123456789" # testSetLen = len(testSetstr) #字符中长度 maxtestLen = 5 #要猜的密码的最大长度,时间耗时越久。 filename = 'test.zip' #要解压的文件名 filedir = 'data/' #解压路径 def testPassword(teststr): r = zipfile.is_zipfile(filename) if r: fz = zipfile.ZipFile(filename,'r') try: for file in fz.namelist(): fz.extract(file,filedir,pwd=str.encode(teststr)) print("密码是",teststr) return True except: pass fz.close() else: print('不是zip文件,不能解压') return True return False def test(n): alist = [0] * n while(alist[0] < testSetLen): testlist = [] for i in range(n): testlist.append(testSetstr[alist[i]]) teststr = "".join(testlist) if(testPassword(teststr)): return True alist[n - 1] += 1 for i in range(n-1,0,-1): if(alist[i] > testSetLen - 1): alist[i] = 0 alist[i - 1] += 1 if(i - 1 == 0): print("进度 ",100.0 * alist[i - 1] / testSetLen ,"%") else: break return False def main(): for i in range(maxtestLen): print("正在测试长度为",i + 1,"的密码。") if(test(i + 1)): return main()
对于rar文件也类似,但是我还没跑出来……太慢了:
from unrar import rarfile #密码可能有的字符 testSetstr = "w.abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#$%&'()*+,-/:;<=>?@[\]^_`{|}~" # testSetstr = "0123456789" # testSetLen = len(testSetstr) #字符中长度 maxtestLen = 20 #要猜的密码的最大长度,时间耗时越久。 filename = 'xxxxx.rar' #要解压的文件名 filedir = 'data/' #解压路径 rar = rarfile.RarFile(filename) def testPassword(teststr): try: rar.extractall(filedir,pwd=teststr) print(filename,"的密码是",teststr) return True except: pass return False def test(n): alist = [0] * n while(alist[0] < testSetLen): testlist = [] for i in range(n): testlist.append(testSetstr[alist[i]]) teststr = "".join(testlist) if(testPassword(teststr)): return True alist[n - 1] += 1 for i in range(n-1,0,-1): if(alist[i] > testSetLen - 1): alist[i] = 0 alist[i - 1] += 1 if(i - 1 == 0): print("进度 ",100.0 * alist[i - 1] / testSetLen ,"%") else: break return False def main(): for i in range(maxtestLen): print("正在测试长度为",i + 1,"的密码。") if(test(i + 1)): return main()