上次写了一个比较简易的通过数字枚举的方法进行破解加密的excel文档,功能太过于简易。这次改变了思路,通过读取txt密码字段的方式循环对加密的xls、xlsx文件进行爆破。逻辑和功能也非常简单,这里仅作为自己练习python的实例。

引用到了python第三方库:pywin32
字典文件格式为任意.txt,每行密码为一行

代码如下:

# -*- coding: utf-8 -*-
from win32com.client import Dispatch
import os


# 获取密码函数
def excel_decrypt(src_file: str, pass_list: list, del_src: bool = False) -> bool:
    """
    Excel自动解密
    :param src_file: 待解密Excel文件路径
    :param pass_list: 密码, list
    :param del_src: 是否删除原始加密文件
    :return:
    """
    flag = False
    for pwd in pass_list:
        try:
            xlapp = Dispatch("Excel.Application")
            wb = xlapp.Workbooks.Open(src_file, False, True, None, pwd)
            file_name = src_file.split("\\")[-1]
            file_location = src_file[0:(len(src_file) - len(file_name))]
            save_path = os.path.join(file_location, ("已解密_" + file_name))
            xlapp.DisplayAlerts = False
            xlapp.ActiveWorkbook.SaveAs(save_path, None, "", "")
            wb.Close()
            xlapp.Quit()
            flag = True
            print("=>", "破解成功,密码为:[%s]" % pwd)
            if del_src:
                try:
                    os.remove(src_file)
                    print("->", "源文件删除成功,[%s]" % src_file)
                except Exception as e:
                    print("->", "源文件删除失败,[%s]" % src_file, repr(e))
            break
        except Exception as e:
            print("->", "尝试密码:[%s]" % pwd, repr(e))
    print('----------------------')
    return flag


# 主程序入口
if __name__ == "__main__":
    path = input("请输入您要处理的[表格文件]路径:")
    words = input("请输入您准备好的[密码文件]路径:")
    if path == '' or words == '':
        print("无效的路径,程序已自动关闭")
    else:
        print('----------------------')
        if os.access(path, os.F_OK) and os.access(words, os.F_OK):
            f = open(words, "r")
            diy_passwords1 = f.read().splitlines()
            f.close()
            print(type(diy_passwords1), diy_passwords1)
            print('->', path, "破解中,请耐心等待………")
            print(excel_decrypt(path, pass_list=diy_passwords1, del_src=False))
        else:
            print('=> 请检查以下文件是否有效且可读:')
            print('=> [表格文件]', path)
            print('=> [密码文件]', words)

添加新评论