python使用密码本破解压缩文件(rar.zip)密码(本次为6位数字)

时间:2024-03-05 10:07:40
转发链接:https://blog.csdn.net
原文作者:平底锅锅锅

参考链接:https://blog.csdn.net

说明:好像只能破解zip格式,而且由rar转成zip的也不能破解...

目录:

一、生成密码本

二、破解zip(已测试通过)

三、破解rar(待更新)

一、生成密码本

f = open(\'sixPassdict.txt\',\'w\')
for id in range(1000000):
    password = str(id).zfill(6)+\'\n\'
    f.write(password)
f.close()

 

二、暴力破解zip

import sys
import zipfile
import rarfile
import threading
import datetime
import os
import subprocess
import getopt

i = 0
fileGet = ""

class MyThread(threading.Thread):
    def __init__(self, func, args, name=\'\'):
        threading.Thread.__init__(self)
        self.name = name
        self.func = func
        self.args = args
        self.result = self.func(*self.args)

    def get_result(self):
        try:
            return self.result
        except Exception:
            return None


def extractFile(fileExtr, password, fileType):
    try:
        encodestr = str.encode(password)
        if (fileType == "zip"):
            fileExtr.extractall(pwd=str.encode(password))
        else:
            fileExtr.extractall(pwd=password)
        global i
        i = i + 1
        print("search count : %d,real password is : %s" % (i, password))
        return password
    except:
        i = i + 1
        print("search count : %d,test password : %s, err:%s" % (i, password, sys.exc_info()[0]))
        pass


def mainStep():
    path = input("please input path:")
    #输入方式如:F:\desktop\123456\001.zip

    try:
        if os.path.exists(path) == False:
            print("%s : path error!" % (path))
            return
        type = os.path.splitext(path)[-1][1:]
        if type == "zip":
            fileGet = zipfile.ZipFile(path)
            with fileGet as z:
                for l in z.infolist():
                    is_encrypted = l.flag_bits & 0x1
                    if is_encrypted:
                        print("have password ")
                        break
                    else:
                        pass
            fileGet = zipfile.ZipFile(path)

        elif type == "rar":
            fileGet = rarfile.RarFile(path)
            with fileGet as z:
                if z.needs_password():
                    print("have password ")
                else:
                    print("no password")
                    return
        else:
            print("file not right")
            return

        pwdLists = open("D:\File_Git\PythonXdd\破解压缩包密码\sixPassdict.txt")
        startTime = datetime.datetime.now()

        for line in pwdLists.readlines():
            Pwd = line.strip(\'\n\')
            t = MyThread(extractFile, (fileGet, Pwd, type))
            t.start()
            if (t.get_result() is Pwd):
                break
        endTime = datetime.datetime.now()
        timeSpan = endTime - startTime
        print("search time:%ss" % (timeSpan.total_seconds()))

    except:
        print("err:%s" % sys.exc_info()[0])


if __name__ == \'__main__\':
    mainStep()