首页 编程教程正文

【python】zip密码爆破源码

piaodoo 编程教程 2020-02-22 22:16:51 1621 0 python教程

本文来源吾爱破解论坛

前几日下载到一个带密码的zip文件,苦猜密码无所得后,决定自己刚掉它(额,爆破耗时间,大家需谨慎。最后以惨败告终,但是这已是后话),下面将源码分享出来,也作为自己的一次总结,新手发帖,请多包涵。目前已知 winrar压缩的没有勾选传统zip加密的文件破解不了,等有时间研究一下。

源码如下:

[Python] 纯文本查看 复制代码

#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[num]
        else:
            return get_char(num // len(chars)) + chars[num % len(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[0]
        print("now is:{}".format(Pwd[1]))
        if (numnow - startnum)%10000 == 0:
            queue.put("{} has deal with 10000,now is {} ,end is {}".format(id, numnow, endnum))
        password = extractFile(binfile, Pwd[1])
        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[i]
        pool.apply_async(func=mainstep, args=(i, q, startnum, endnum))
    pool.close()
    queuesize = [0 for i in range(len(thread_queue))]
    while True:
        for i in range(len(thread_queue)):
            q = thread_queue[i]
            if queuesize[i] != -1:
                size = q.qsize()
                queuesize[i] = 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[i] = -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.')

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/8088.html

搜索