【python】zip密码爆破源码
前几日下载到一个带密码的zip文件,苦猜密码无所得后,决定自己刚掉它(额,爆破耗时间,大家需谨慎。最后以惨败告终,但是这已是后话),下面将源码分享出来,也作为自己的一次总结,新手发帖,请多包涵。目前已知 winrar压缩的没有勾选传统zip加密的文件破解不了,等有时间研究一下。源码如下:
#codig:utf-8
import time
import zipfile
from threading import Thread
import multiprocessing
from multiprocessing import Queue
# input
path = "123.zip" # 文件路径
g_maxprocess = 1 # 分配进程数
g_minlength = 3 # 最小长度
g_maxlength = 3 # 最大长度
g_startnum = 0 #开始数
thread_queue = []
# 字符集,将可能的字符放在此数组里面。
g_chars = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v','w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V','W', 'X', 'Y', 'Z',
'.', '#'
]
# 提取文件
def extractFile(binfile, password):
try:
binfile.extractall(pwd= bytes(password,"utf-8"))
print("This file\'s password is " + password)
return password
except:
return None
# 获取密码
def dict_builder(startnum=0, endnum= None, minlength=g_minlength, maxlength=g_maxlength, chars = g_chars):
base = len(chars) ** (minlength-1)
end = len(chars) ** maxlength
if startnum > base:
base = startnum
if endnum is not None and endnum<end:
end = endnum
start = base
def get_char(num):
if num < len(chars):
return chars
else:
return get_char(num // len(chars)) + chars
for i in range(start, end):
yield i, get_char(i)
def mainstep(id, queue, startnum=0, endnum= None):
print("thread {} start: startnum:{}\tendnum:{}".format(id, startnum, endnum))
binfile = zipfile.ZipFile(path)
print("start dict_builder....")
for Pwd in dict_builder(startnum, endnum):
numnow = Pwd
print("now is:{}".format(Pwd))
if (numnow - startnum)%10000 == 0:
queue.put("{} has deal with 10000,now is {} ,end is {}".format(id, numnow, endnum))
password = extractFile(binfile, Pwd)
if password is not None:
queue.put("crack_ok")
queue.put("password")
break
queue.put("exit")
print("process {} exiting....".format(id))
# 创建进程
if __name__ == '__main__':
maxnum = len(g_chars) ** g_maxlength
numpct = 1./g_maxprocess
num_pool = []
for i in range(0, g_maxprocess):
start = g_startnum + int(( maxnum - g_startnum) * numpct * i)
end = g_startnum + int(( maxnum - g_startnum) * numpct * (i + 1))
num_pool.append((start, end))
pool = multiprocessing.Pool(processes=(g_maxprocess + 1))
manager = multiprocessing.Manager()
for i in range(0, g_maxprocess):
q = manager.Queue()
thread_queue.append(q)
startnum, endnum = num_pool
pool.apply_async(func=mainstep, args=(i, q, startnum, endnum))
pool.close()
queuesize =
while True:
for i in range(len(thread_queue)):
q = thread_queue
if queuesize != -1:
size = q.qsize()
queuesize = size
if size !=0:
str = q.get()
print(str)
if str == "crack_ok":
time.sleep(1)
paswd = q.get()
print("crack success pwd:{}".format(paswd))
break
if str == "exit":
queuesize = -1
if sum(queuesize) == 0: # 队列为空,睡眠0.5S
print("empty sleep...")
time.sleep(0.5)
if sum(queuesize) == 0 - len(thread_queue): # 进程全部退出,破解失败
print("crack failed...")
break
print("kill all thread...")
pool.terminate()
print('All subprocesses done.')
LarryLeung 发表于 2019-3-18 12:10
感谢分享,顺便问问楼主最后失败是因为耗时还是怎么?
1、在不知道密码长度的情况下,计算耗时会随着长度的呈指数增加。
2、经过我自己的测试winrar压缩zip文件时,有一个“传统zip加密选项”,勾选的话就算密码正确,也无法解压文件,这个或许与winrar对此做了其他的操作或python zipfile库的使用方式有关。
因此,此上程序还有很多优化的地方。 D:\workspace_py\test\venv\Scripts\python.exe D:/workspace_py/test/zip.py
<class 'str'>
empty sleep...
thread 0 start: startnum:0 endnum:262144
start dict_builder....
now is:###
process 0 exiting....
exit
crack failed...
kill all thread...
All subprocesses done.
Process finished with exit code 0
厉害啊,主要学习一下进程用法 杂用啊,大佬 感谢分享,顺便问问楼主最后失败是因为耗时还是怎么? NakupendaB 发表于 2019-3-18 09:45
杂用啊,大佬
直接更改path的路径,用python3运行即可,另根据自己电脑的情况调节进程数 新人来报到 …………
看了看,就是担心 她的速度。 这个好啊,刚好有个文件加密了。试试
默默奉上吾爱币,希望楼主多加些代码注释,让我等明白一些 wuchen2138 发表于 2019-3-18 17:28
直接更改path的路径,用python3运行即可,另根据自己电脑的情况调节进程数
多谢了。看来我这个小白不会,哈哈。支持