vba&python破解各种密码合集

系列文章目录

文章目录

  • 系列文章目录
  • 前言
  • 一、excel设置工作薄&工作表保护
  • 二、VBA解除excel工作薄&工作表保护
  • 三、修改源文件解除excel工作表保护
  • 四、excel设置VBA Project保护
  • 五、VBA解除VBA Project密码
  • 六、修改源文件解除VBA Project密码
  • 七、excel设置进入密码
  • 八、vba解除excel进入密码
  • 九、python破解excel进入密码
    • 1.配置python
    • 2.源码如下
  • 十、vba自带密码展示
  • 十一、vba解除excel结构限制
  • 总结

前言

一、excel设置工作薄&工作表保护

1、问题描述:当我们需要保护我们的工作表不被别人编辑时:点击审阅-保护工作表-填入密码;
vba&python破解各种密码合集_第1张图片
vba&python破解各种密码合集_第2张图片
vba&python破解各种密码合集_第3张图片

二、VBA解除excel工作薄&工作表保护

1、密码解除(忘记密码时使用):工作表存在密码而且我们需要编辑工作表,会出现如下图示,需要我们去输入密码;
vba&python破解各种密码合集_第4张图片

代码一如下:

Sub PJ()

Dim sht As Worksheet

For Each sht In Worksheets

sht.Protect AllowFiltering:=True

sht.Unprotect

Next

End Sub

代码二如下:

Public Sub AllInternalPasswords()
' Breaks worksheet and workbook structure passwords. Bob McCormick
' probably originator of base code algorithm modified for coverage
' of workbook structure / windows passwords and for multiple passwords
'
' Norman Harker and JE McGimpsey 27-Dec-2002 (Version 1.1)
' Modified 2003-Apr-04 by JEM: All msgs to constants, and
' eliminate one Exit Sub (Version 1.1.1)
' Reveals hashed passwords NOT original passwords
Const DBLSPACE As String = vbNewLine & vbNewLine
Const AUTHORS As String = DBLSPACE & vbNewLine & _
"Adapted from Bob McCormick base code by" & _
"Norman Harker and JE McGimpsey"
Const HEADER As String = "AllInternalPasswords User Message"
Const VERSION As String = DBLSPACE & "Version 1.1.1 2003-Apr-04"
Const REPBACK As String = DBLSPACE & "Please report failure " & _
"to the microsoft.public.excel.programming newsgroup."
Const ALLCLEAR As String = DBLSPACE & "The workbook should " & _
"now be free of all password protection, so make sure you:" & _
DBLSPACE & "SAVE IT NOW!" & DBLSPACE & "and also" & _
DBLSPACE & "BACKUP!, BACKUP!!, BACKUP!!!" & _
DBLSPACE & "Also, remember that the password was " & _
"put there for a reason. Don't stuff up crucial formulas " & _
"or data." & DBLSPACE & "Access and use of some data " & _
"may be an offense. If in doubt, don't."
Const MSGNOPWORDS1 As String = "There were no passwords on " & _
"sheets, or workbook structure or windows." & AUTHORS & VERSION
Const MSGNOPWORDS2 As String = "There was no protection to " & _
"workbook structure or windows." & DBLSPACE & _
"Proceeding to unprotect sheets." & AUTHORS & VERSION
Const MSGTAKETIME As String = "After pressing OK button this " & _
"will take some time." & DBLSPACE & "Amount of time " & _
"depends on how many different passwords, the " & _
"passwords, and your computer's specification." & DBLSPACE & _
"Just be patient! Make me a coffee!" & AUTHORS & VERSION
Const MSGPWORDFOUND1 As String = "You had a Worksheet " & _
"Structure or Windows Password set." & DBLSPACE & _
"The password found was: " & DBLSPACE & "$$" & DBLSPACE & _
"Note it down for potential future use in other workbooks by " & _
"the same person who set this password." & DBLSPACE & _
"Now to check and clear other passwords." & AUTHORS & VERSION
Const MSGPWORDFOUND2 As String = "You had a Worksheet " & _
"password set." & DBLSPACE & "The password found was: " & _
DBLSPACE & "$$" & DBLSPACE & "Note it down for potential " & _
"future use in other workbooks by same person who " & _
"set this password." & DBLSPACE & "Now to check and clear " & _
"other passwords." & AUTHORS & VERSION
Const MSGONLYONE As String = "Only structure / windows " & _
"protected with the password that was just found." & _
ALLCLEAR & AUTHORS & VERSION & REPBACK
Dim w1 As Worksheet, w2 As Worksheet
Dim i As Integer, j As Integer, k As Integer, l As Integer
Dim m As Integer, n As Integer, i1 As Integer, i2 As Integer
Dim i3 As Integer, i4 As Integer, i5 As Integer, i6 As Integer
Dim PWord1 As String
Dim ShTag As Boolean, WinTag As Boolean
Application.ScreenUpdating = False
With ActiveWorkbook
WinTag = .ProtectStructure Or .ProtectWindows
End With
ShTag = False
For Each w1 In Worksheets
ShTag = ShTag Or w1.ProtectContents
Next w1
If Not ShTag And Not WinTag Then
MsgBox MSGNOPWORDS1, vbInformation, HEADER
Exit Sub
End If
MsgBox MSGTAKETIME, vbInformation, HEADER
If Not WinTag Then
MsgBox MSGNOPWORDS2, vbInformation, HEADER
Else
On Error Resume Next
Do 'dummy do loop
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
With ActiveWorkbook
.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If .ProtectStructure = False And _
.ProtectWindows = False Then
PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
MsgBox Application.Substitute(MSGPWORDFOUND1, _
"$$", PWord1), vbInformation, HEADER
Exit Do 'Bypass all for...nexts
End If
End With
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
Loop Until True
On Error GoTo 0
End If
If WinTag And Not ShTag Then
MsgBox MSGONLYONE, vbInformation, HEADER
Exit Sub
End If
On Error Resume Next
For Each w1 In Worksheets
'Attempt clearance with PWord1
w1.Unprotect PWord1
Next w1
On Error GoTo 0
ShTag = False
For Each w1 In Worksheets
'Checks for all clear ShTag triggered to 1 if not.
ShTag = ShTag Or w1.ProtectContents
Next w1
If ShTag Then
For Each w1 In Worksheets
With w1
If .ProtectContents Then
On Error Resume Next
Do 'Dummy do loop
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If Not .ProtectContents Then
PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
MsgBox Application.Substitute(MSGPWORDFOUND2, _
"$$", PWord1), vbInformation, HEADER
'leverage finding Pword by trying on other sheets
For Each w2 In Worksheets
w2.Unprotect PWord1
Next w2
Exit Do 'Bypass all for...nexts
End If
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
Loop Until True
On Error GoTo 0
End If
End With
Next w1
End If
MsgBox ALLCLEAR & AUTHORS & VERSION & REPBACK, vbInformation, HEADER
End Sub


备份代码如下:

Public Sub 工作表保护密码破解()
Const DBLSPACE As String = vbNewLine & vbNewLine
Const AUTHORS As String = DBLSPACE & vbNewLine & _
"作者:McCormick   JE McGimpsey "
Const HEADER As String = "工作表保护密码破解"
Const VERSION As String = DBLSPACE & "版本 Version 1.1.1"
Const REPBACK As String = DBLSPACE & ""
Const ZHENGLI As String = DBLSPACE & "                        FGHRSH 整理"
Const ALLCLEAR As String = DBLSPACE & "该工作簿中的工作表密码保护已全部解除!!" & DBLSPACE & "请记得另保存" _
& DBLSPACE & "注意:不要用在不当地方,要尊重他人的劳动成果!"
Const MSGNOPWORDS1 As String = "该文件工作表中没有加密"
Const MSGNOPWORDS2 As String = "该文件工作表中没有加密2"
Const MSGTAKETIME As String = "解密需花费一定时间,请耐心等候!" & DBLSPACE & "按确定开始破解!"
Const MSGPWORDFOUND1 As String = "密码重新组合为:" & DBLSPACE & "$$" & DBLSPACE & _
"如果该文件工作表有不同密码,将搜索下一组密码并修改清除"
Const MSGPWORDFOUND2 As String = "密码重新组合为:" & DBLSPACE & "$$" & DBLSPACE & _
"如果该文件工作表有不同密码,将搜索下一组密码并解除"
Const MSGONLYONE As String = "确保为唯一的?"
Dim w1 As Worksheet, w2 As Worksheet
Dim i As Integer, j As Integer, k As Integer, l As Integer
Dim m As Integer, n As Integer, i1 As Integer, i2 As Integer
Dim i3 As Integer, i4 As Integer, i5 As Integer, i6 As Integer
Dim PWord1 As String
Dim ShTag As Boolean, WinTag As Boolean
Application.ScreenUpdating = False
With ActiveWorkbook
WinTag = .ProtectStructure Or .ProtectWindows
End With
ShTag = False
For Each w1 In Worksheets
ShTag = ShTag Or w1.ProtectContents
Next w1
If Not ShTag And Not WinTag Then
MsgBox MSGNOPWORDS1, vbInformation, HEADER
Exit Sub
End If
MsgBox MSGTAKETIME, vbInformation, HEADER
If Not WinTag Then
Else
On Error Resume Next
Do 'dummy do loop
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
With ActiveWorkbook
.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If .ProtectStructure = False And _
.ProtectWindows = False Then
PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
MsgBox Application.Substitute(MSGPWORDFOUND1, _
"$$", PWord1), vbInformation, HEADER
Exit Do 'Bypass all for...nexts
End If
End With
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
Loop Until True
On Error GoTo 0
End If
If WinTag And Not ShTag Then
MsgBox MSGONLYONE, vbInformation, HEADER
Exit Sub
End If
On Error Resume Next
For Each w1 In Worksheets
'Attempt clearance with PWord1
w1.Unprotect PWord1
Next w1
On Error GoTo 0
ShTag = False
For Each w1 In Worksheets
'Checks for all clear ShTag triggered to 1 if not.
ShTag = ShTag Or w1.ProtectContents
Next w1
If ShTag Then
For Each w1 In Worksheets
With w1
If .ProtectContents Then
On Error Resume Next
Do 'Dummy do loop
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If Not .ProtectContents Then
PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
MsgBox Application.Substitute(MSGPWORDFOUND2, _
"$$", PWord1), vbInformation, HEADER
'leverage finding Pword by trying on other sheets
For Each w2 In Worksheets
w2.Unprotect PWord1
Next w2
Exit Do 'Bypass all for...nexts
End If
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
Loop Until True
On Error GoTo 0
End If
End With
Next w1
End If
MsgBox ALLCLEAR & AUTHORS & VERSION & REPBACK & ZHENGLI, vbInformation, HEADER
End Sub

三、修改源文件解除excel工作表保护

1、问题描述:
vba&python破解各种密码合集_第5张图片
分析:
Office07 或以上版本中微软采用的xlsx,docx,pptx,本质上是一个压缩包,构成excel的所有源码都在这个压缩包中,后缀改为.zip或.rar即可;
2、解除方法:
2.1、将文件后缀修改为.zip或.rar;
在这里插入图片描述
在这里插入图片描述
2.2、双击用压缩软件打开(不要关闭解压软件,直到破解完成),进入xl文件夹,这里使用bandzip;
vba&python破解各种密码合集_第6张图片
2.3、进入worksheets文件夹;
vba&python破解各种密码合集_第7张图片
2.4、鼠标右键点击编辑,删除括号内的内容;
vba&python破解各种密码合集_第8张图片
vba&python破解各种密码合集_第9张图片
2.5、另存为到电脑桌面,在如下对话框内点击添加,将桌面的sheet1.xml文件拖进来,点击开始,选择覆盖,点击确定;
vba&python破解各种密码合集_第10张图片
vba&python破解各种密码合集_第11张图片

vba&python破解各种密码合集_第12张图片
2.6、到这一步已完成,关闭解压软件,将后缀名重新修改为.xlsx;
在这里插入图片描述

vba&python破解各种密码合集_第13张图片

备注:需要删除的代码如下。

<sheetProtection algorithmName="SHA-512" hashValue="SYSZ4+9ohVwscBEiA31JeqVPcjjY9XaMg5OsClaBP34UbCSVxPYvTXxevW42hPizsL2Qcdwcx0KUzw8WWh78QA==" saltValue="uEIfq5SxKr2ChQKR7bJkVg==" spinCount="100000" sheet="1" objects="1" scenarios="1"/>

四、excel设置VBA Project保护

1、问题描述:当我们需要保护我们编写的宏不被别人查看和编辑时:菜单栏中选择开发工具-点击Visual Basic打开编辑器-鼠标右键点击属性-保护-填写密码;(不想让别人看源码的时候需要把查看时锁定工程勾选上)
vba&python破解各种密码合集_第14张图片
vba&python破解各种密码合集_第15张图片

vba&python破解各种密码合集_第16张图片
vba&python破解各种密码合集_第17张图片

五、VBA解除VBA Project密码

1、密码解除(忘记密码时使用):VBA Project存在密码而且我们需要编辑宏,会出现如下图示,需要我们去输入密码;
vba&python破解各种密码合集_第18张图片
2、代码如下:

Sub MoveProtect()
    Dim FileName As String
    FileName = Application.GetOpenFilename("Excel文件(*.xls & *.xla),*.xls;*.xla", , "VBA破解")
    If FileName = CStr(False) Then
       Exit Sub
    Else
       VBAPassword FileName, False
    End If
End Sub
 
'设置VBA编码保护
Sub SetProtect()
    Dim FileName As String
    FileName = Application.GetOpenFilename("Excel文件(*.xls & *.xla),*.xls;*.xla", , "VBA破解")
    If FileName = CStr(False) Then
       Exit Sub
    Else
       VBAPassword FileName, True
    End If
End Sub
 
Private Function VBAPassword(FileName As String, Optional Protect As Boolean = False)
      If Dir(FileName) = "" Then
         Exit Function
      Else
         FileCopy FileName, FileName & ".bak"
      End If
 
      Dim GetData As String * 5
      Open FileName For Binary As #1
      Dim CMGs As Long
      Dim DPBo As Long
      For i = 1 To LOF(1)
          Get #1, i, GetData
          If GetData = "CMG=""" Then CMGs = i
          If GetData = "[Host" Then DPBo = i - 2: Exit For
      Next
      If CMGs = 0 Then
         MsgBox "请先对VBA编码设置一个保护密码...", 32, "提示"
         Exit Function
      End If
      If Protect = False Then
         Dim St As String * 2
         Dim s20 As String * 1
         '取得一个0D0A十六进制字串
         Get #1, CMGs - 2, St
         '取得一个20十六制字串
         Get #1, DPBo + 16, s20
         '替换加密部份机码
         For i = CMGs To DPBo Step 2
             Put #1, i, St
         Next
         '加入不配对符号
         If (DPBo - CMGs) Mod 2 <> 0 Then
            Put #1, DPBo + 1, s20
         End If
         MsgBox "文件解密成功......", 32, "提示"
      Else
         Dim MMs As String * 5
         MMs = "DPB="""
         Put #1, CMGs, MMs
         MsgBox "对文件特殊加密成功......", 32, "提示"
      End If
      Close #1
End Function

3、使用方法

备注:如果是.xlsm文件,需要打开文件另存为97-03版的xls文件;
vba&python破解各种密码合集_第19张图片
vba&python破解各种密码合集_第20张图片

3.1、由于我们需要破解的excel表存在VBA Project密码,无法使用vba,因此我们需新建一个excel表,打开vba编辑器,将上述代码复制粘贴到编辑器中点击运行;
vba&python破解各种密码合集_第21张图片
3.2、在弹出的对话框中进入需要破解的文件路径,选择需要破解的文件;
vba&python破解各种密码合集_第22张图片
vba&python破解各种密码合集_第23张图片

六、修改源文件解除VBA Project密码

1、密码解除(忘记密码时使用):工作表存在密码而且我们需要编辑工作表,会出现如下图示,需要我们去输入密码;
vba&python破解各种密码合集_第24张图片

2、解除方法:
2.1、将文件后缀修改为.zip或.rar;(上面是以.zip为例,本节以.rar为例)
在这里插入图片描述
在这里插入图片描述
2.2、双击用压缩软件打开(不要关闭解压软件,直到破解完成),进入xl文件夹,这里使用bandzip;
vba&python破解各种密码合集_第25张图片
2.3、鼠标右键选择vbaProject.bin文件-编辑-另存为-保存到桌面;(也可以解压该文件到桌面;拖动到桌面也可以;)
vba&python破解各种密码合集_第26张图片
2.4、用notepad++或其它编辑器打开,找到下面一行代码,将DPB改为DPX并保存;

DPB="A4A60836088725872578DB88258804E725D56C6B4A82D38962C0F5054685AD2D6F989F9E7945"

vba&python破解各种密码合集_第27张图片
vba&python破解各种密码合集_第28张图片
2.5、用桌面的vbaProject.bin文件将压缩包内的文件进行替换;
vba&python破解各种密码合集_第29张图片
vba&python破解各种密码合集_第30张图片
vba&python破解各种密码合集_第31张图片
2.6、将后缀名重新改为.xlsm,打开excel文件,出现以下错误时,选择是,接着出现未知错误时点击确定;
vba&python破解各种密码合集_第32张图片

vba&python破解各种密码合集_第33张图片
2.7、在菜单栏中选择开发工具-Visual Basic(或Alt+F11)打开vba编辑器,工具-vbaproject工程属性-随便输入密码(例:123),即可查看原有vba代码;
vba&python破解各种密码合集_第34张图片
vba&python破解各种密码合集_第35张图片
2.8、取消密码。 重新打开这个文件,按“Alt + F11”进入“Visual Basic”代码页面,此时会提示输入密码。输入刚刚设置的新密码即可。然后回到 VBA 编辑器的 “工具” -> “**属性” -> “保护”,去掉 “查看时锁定工程”前面的标记,此时便已破解完成,可以查看源码了;
vba&python破解各种密码合集_第36张图片

七、excel设置进入密码

vba&python破解各种密码合集_第37张图片
vba&python破解各种密码合集_第38张图片

八、vba解除excel进入密码

问题描述:
vba&python破解各种密码合集_第39张图片

代码如下(仅测试了2-3位比较简单的进入密码):

Sub crack()
Dim i As Long
Dim FileName As String
i = 0
FileName = Application.GetOpenFilename("Excel文件(*.xls & *.xlsx),*.xls;*.xlsx", , "VBA破解")
FileName = Right(FileName, Len(FileName) - InStrRev(FileName, "\"))
Application.ScreenUpdating = False
line2: On Error GoTo line1
Workbooks.Open FileName, , True, , i
MsgBox "Password is " & i
Exit Sub
line1: i = i + 1
Resume line2
Application.ScreenUpdating = True
End Sub

九、python破解excel进入密码

1.配置python

参考链接

2.源码如下

在这里插入图片描述

generate_pass_word.py代码如下(示例):

from itertools import chain, product, permutations, combinations_with_replacement


class Generate_pass_word:
    def __init__(self):
        self.num = [str(i) for i in range(0, 10)]
        self.lower_letter = 'abcdefghijklmnopqrstuvwxyz'
        self.upper_letter = self.lower_letter.upper()
        self.simple_char = '@#$&%^*!'
        self.minority_char = ",./?;:\'\"\\|,。;:”“‘~·`()(){}[]【】"

    def num_password_1_6(self, n=6):  # 生成1~6位数的数字密码组合。
        num_password = ''
        for i in range(1, n + 1):
            num_password = chain(num_password, product(self.num, repeat=i))
        for i in num_password:
            yield ''.join(i)

    def num_password_n(self, n):  # 生成n位数的数字密码组合。
        num_password = product(self.num, repeat=n)
        for i in num_password:
            yield ''.join(i)

    def low_letter_password_1_6(self, n=6):  # 生成1~6位数的小写字母密码组合。
        low_letter_password = ''
        for i in range(1, n + 1):
            low_letter_password = chain(low_letter_password, product(self.lower_letter, repeat=i))
        for i in low_letter_password:
            yield ''.join(i)

    def low_letter_password_n(self, n):  # 生成n位数的小写字母密码组合。
        low_letter_password = product(self.lower_letter, repeat=n)
        for i in low_letter_password:
            yield ''.join(i)

    def upper_letter_password(self, n=6):  # 生成1~6位数的大写字母密码组合。
        for i in self.low_letter_password_1_6(n):
            yield i.upper()

# =================  所有字符的密码组合  ======================
    def all_letter_password_1_4(self, simple=True):      # 由数字、字母、字符组成的 14位 密码。
        dic = {'num': self.num,
               "lower_letter": self.lower_letter,
               "upper_letter": self.upper_letter,
               "char": self.simple_char,
               }
        if simple:
            pass
        else:
            dic['char'] = self.simple_char + self.minority_char

        gene_password_1 = self.simple_char + self.minority_char     # 1 位数密码

        gene_password_2 = ''                                    # 2 位数密码
        group = []         # 存放密码类型的组成
        for i in combinations_with_replacement(dic.keys(), 2):  # 先有放回的取出两种类型的密码。
            for r in permutations(i):                           # 对取出的密码进行排列组合。
                group.append(r)
        pass_group = set(group)                                 # 去除重复项。
        pass_group.discard(('num', 'num'))                      # 去除纯数字和纯字母的。
        pass_group.discard(('lower_letter', 'lower_letter'))
        pass_group.discard(('upper_letter', 'upper_letter'))
        for i in pass_group:
            x, y = i
            gene_password_2 = chain(gene_password_2, product(dic[x],dic[y]))

        gene_password_3 = ''
        group = []  # 存放密码类型的组成
        for i in combinations_with_replacement(dic.keys(), 3):  # 先有放回的取出两种类型的密码。
            for r in permutations(i):  # 对取出的密码进行排列组合。
                group.append(r)
        pass_group = set(group)  # 去除重复项。
        pass_group.discard(('num',)*3)  # 去除纯数字和纯字母的。
        pass_group.discard(('lower_letter',)*3)
        pass_group.discard(('upper_letter',)*3)
        for i in pass_group:
            x, y, z = i
            gene_password_3 = chain(gene_password_3, product(dic[x], dic[y], dic[z]))

        gene_password_4 = ''
        group = []  # 存放密码类型的组成
        for i in combinations_with_replacement(dic.keys(), 4):  # 先有放回的取出两种类型的密码。
            for r in permutations(i):  # 对取出的密码进行排列组合。
                group.append(r)
        pass_group = set(group)  # 去除重复项。
        pass_group.discard(('num',)*4)  # 去除纯数字和纯字母的。
        pass_group.discard(('lower_letter',)*4)
        pass_group.discard(('upper_letter',)*4)
        for i in pass_group:
            x, y, z, n = i
            gene_password_4 = chain(gene_password_4, product(dic[x], dic[y], dic[z], dic[n]))

        for i in chain(gene_password_1,gene_password_2,gene_password_3,gene_password_4):
            yield ''.join(i)

    def all_letter_password_n(self, n, simple=True,):      # 由数字、字母、字符组成的 n 位密码。
        dic = {'num': self.num,
               "lower_letter": self.lower_letter,
               "upper_letter": self.upper_letter,
               "char": self.simple_char,
               }
        if simple:
            pass
        else:
            dic['char'] = self.simple_char + self.minority_char
        for i in combinations_with_replacement(dic.keys(), n):         # 先有放回的取出 n 种类型的密码。
            g = set(permutations(i))               # 先对n种类型密码排列组合,然后去除排列组合中的重复项。
            g.discard(('num',) * n)                   # 去除纯数字的组合,discard()即使去除元素不存在,也不报错。
            g.discard(('lower_letter',) * n)       # 去除纯小写字母的组合。
            g.discard(('upper_letter',) * n)      # 去除纯大写字母的组合。
            for i in g:                                        # 历遍组合。
                for r in product(*[dic[d] for d in i]):  
                    yield ''.join(r)

unpack.py代码如下(示例):

from zipfile import is_zipfile, ZipFile
from rarfile import is_rarfile, RarFile, RarCRCError
from multiprocessing import Manager, Pool, freeze_support, cpu_count
from itertools import chain, cycle
from threading import Thread
import os, time
from generate_pass_word import Generate_pass_word


def unpack(flg, password, read_path, save_path):
    if is_zipfile(read_path):                                  # 先判断该文件是不是zip压缩文件。
        compress_file = ZipFile(read_path)
        while flg.run:
            try:    
                pwd = next(password)      # 获取一个密码。
                # print(pwd)
                compress_file.extractall(path=save_path, pwd=pwd.encode('utf-8'))      # 密码需要编码
                print(f'\n解压密码为:{pwd}')
                with open(os.path.join(save_path, '密码.txt'), 'w') as f:
                    f.write(f'解压密码为:{pwd}')
                compress_file.close()
                flg.run = False
            # except BadZipFile as e:
            #     flg.run = False
            #     print('11压缩文件损坏, 程序退出……', e)
            except StopIteration:
                break
            except Exception as e:
                pass
                #print(e, f'  {pwd}')
                
    else:                       # 判断该文件是不是rar压缩文件。
        compress_file = RarFile(read_path)
        while flg.run:
            try:    
                pwd = next(password)                          # 获取一个密码。
                # print(pwd)
                compress_file.extractall(path=save_path, pwd=pwd)        # 密码不需要编码。
                print(f'\n解压密码为:{pwd}')
                with open(os.path.join(save_path, '密码.txt'), 'w') as f:
                    f.write(f'解压密码为:{pwd}')
                compress_file.close()
                flg.run = False
            except RarCRCError:
                flg.run = False
                print('压缩文件损坏, 程序退出……')
            except StopIteration:
                break
            except Exception as e:
                pass
                # print(e)


def process_num(flg, dict_file):
    # print('process_num  已运行')
    read_path = dict_file['read_path']
    save_path = dict_file['save_path']
    g = Generate_pass_word()
    gen_pwd_1_6 = g.num_password_1_6()
    gen_pwd_7_8 = chain(g.num_password_n(7), g.num_password_n(8))
    gen_pwd_9 = g.num_password_n(9)
    gen_pwd_10 = g.num_password_n(10)
    t1 = Thread(target=unpack, args=(flg, gen_pwd_1_6, read_path, save_path))
    t2 = Thread(target=unpack, args=(flg, gen_pwd_7_8, read_path, save_path))
    t3 = Thread(target=unpack, args=(flg, gen_pwd_9, read_path, save_path))
    t4 = Thread(target=unpack, args=(flg, gen_pwd_10, read_path, save_path))
    t1.start()
    t2.start()
    t3.start()
    t4.start()
    t1.join()
    t2.join()
    t3.join()
    t4.join()


def process_letter(flg, dict_file):
    # print('process_letter  已运行')
    read_path = dict_file['read_path']
    save_path = dict_file['save_path']
    g = Generate_pass_word()
    let_pwd_1_6 = g.low_letter_password_1_6()
    let_pwd_7_8 = chain(g.low_letter_password_n(7), g.low_letter_password_n(8))
    let_pwd_9 = g.low_letter_password_n(9)
    let_pwd_10 = g.low_letter_password_n(10)
    let_up_pwd = g.upper_letter_password()
    t1 = Thread(target=unpack, args=(flg, let_pwd_1_6, read_path, save_path))
    t2 = Thread(target=unpack, args=(flg, let_pwd_7_8, read_path, save_path))
    t3 = Thread(target=unpack, args=(flg, let_pwd_9, read_path, save_path))
    t4 = Thread(target=unpack, args=(flg, let_pwd_10, read_path, save_path))
    t5 = Thread(target=unpack, args=(flg, let_up_pwd, read_path, save_path))
    t1.start()
    t2.start()
    t3.start()
    t4.start()
    t5.start()
    t1.join()
    t2.join()
    t3.join()
    t4.join()
    t5.join()


def process_mixture_1_8(flg, dict_file):
    # print('process_mixture_1_8  已运行')
    read_path = dict_file['read_path']
    save_path = dict_file['save_path']
    g = Generate_pass_word()
    pwd_1_4 = g.all_letter_password_1_4()
    pwd_5_6 = chain(g.all_letter_password_n(5), g.all_letter_password_n(6))
    pwd_7 = g.all_letter_password_n(7)
    pwd_8 = g.all_letter_password_n(8)
    t1 = Thread(target=unpack, args=(flg, pwd_1_4, read_path, save_path))
    t2 = Thread(target=unpack, args=(flg, pwd_5_6, read_path, save_path))
    t3 = Thread(target=unpack, args=(flg, pwd_7, read_path, save_path))
    t4 = Thread(target=unpack, args=(flg, pwd_8, read_path, save_path))
    t1.start()
    t2.start()
    t3.start()
    t4.start()
    t1.join()
    t2.join()
    t3.join()
    t4.join()


def process_mixture_9_10(flg, dict_file):
    # print('process_mixture_9_10  已运行')
    read_path = dict_file['read_path']
    save_path = dict_file['save_path']
    g = Generate_pass_word()
    pwd_9 = g.all_letter_password_n(9)
    pwd_10 = g.all_letter_password_n(10)
    t1 = Thread(target=unpack, args=(flg, pwd_9, read_path, save_path))
    t2 = Thread(target=unpack, args=(flg, pwd_10, read_path, save_path))
    t1.start()
    t2.start()
    t1.join()
    t2.join()


def schedule(flg):
    for i in cycle(['|', '/', '—', '\\']):
        if flg.run:
            print(f'{i} {i} {i} {i} {i} {i}', flush=True, end='')
            time.sleep(0.6)
            print('\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b', flush=True, end='')
        else:
            print('\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b', flush=True, end='')
            break


if __name__ == '__main__':
    freeze_support()
    print('该程序可破解10位之内的密码。\n联系:[email protected]')
    path = input('\n请输入压缩文件的完整路径名:\n>>> ')
    read_path = os.path.abspath(path)
    print(read_path)

    if not os.path.exists(read_path) or (not is_zipfile(read_path) and not is_rarfile(read_path)):
        print('该文件不是ZIP、ARA压缩文件!\n\
        程序 3秒后 退出……')
        time.sleep(3)
        os._exit(1)

    # 先创建一个存放的目录。
    dir_path = os.path.dirname(path)
    file_name = os.path.basename(path)
    save_path = os.path.join(dir_path, file_name.split('.')[0])
    dict_file = {'read_path': read_path,
                 'dir_path': dir_path,
                 'file_name': file_name,
                 'save_path': save_path}

    if not os.path.isdir(save_path):
        os.makedirs(save_path)

    Flg = Manager().Namespace()
    Flg.run = True
    print(f'{time.ctime()} 开始对 《{file_name}》 进行密码破解,请等待……')
    Thread(target=schedule, args=(Flg,), daemon=True).start()

    if cpu_count() <= 4:
        cpu_num = cpu_count()
    else:
        cpu_num = 4
    pool = Pool(processes=cpu_num)
    pool.apply_async(process_num, args=(Flg, dict_file))
    pool.apply_async(process_letter, args=(Flg, dict_file))
    pool.apply_async(process_mixture_1_8, args=(Flg, dict_file))
    pool.apply_async(process_mixture_9_10, args=(Flg, dict_file))
    pool.close()
    pool.join()
    if not Flg.run:
        print('\n解压密码保存在 密码.txt 文件中。\n')
        print('单击右上角 X,退出程序。')
        time.sleep(300)
    print('程序结束。')
    time.sleep(3)

十、vba自带密码展示

vba&python破解各种密码合集_第40张图片

密码如下(示例):

solver、atpvbean、eurotool密码:Wildebeest!!

十一、vba解除excel结构限制

代码如下(示例):

Sub test()
ActiveWorkbook.Sheets.Copy
For Each sh In ActiveWorkbooks.Sheets
sh.Visible = True
Next
End Sub

Private Sub VBAPassword()

    '你要解保护的Excel文件路径
    Filename = Application.GetOpenFilename("Excel文件(*.xlsx & *.xla & *.xlt),*.xlsx;*.xla;*.xlt", , "VBA破解")
    
    If Dir(Filename) = "" Then
    MsgBox "没找到相关文件,清重新设置。"
    Exit Sub
    Else
    FileCopy Filename, Filename & ".bak" '备份文件。
    End If
    
    Dim GetData As String * 5
    Open Filename For Binary As #1
    Dim CMGs As Long
    Dim DPBo As Long
    For i = 1 To LOF(1)
    Get #1, i, GetData
    If GetData = "CMG=""" Then CMGs = i
    If GetData = "[Host" Then DPBo = i - 2: Exit For
    Next
    
    If CMGs = 0 Then
    MsgBox "请先对VBA编码设置一个保护密码...", 32, "提示"
    Exit Sub
    End If
    
    Dim St As String * 2
    Dim s20 As String * 1
    
    '取得一个0D0A十六进制字串
    Get #1, CMGs - 2, St
    
    '取得一个20十六制字串
    Get #1, DPBo + 16, s20
    
    '替换加密部份机码
    For i = CMGs To DPBo Step 2
    Put #1, i, St
    Next
    
    '加入不配对符号
    If (DPBo - CMGs) Mod 2 <> 0 Then
    Put #1, DPBo + 1, s20
    End If
    MsgBox "文件解密成功......", 32, "提示"
    
    Close #1
    
End Sub

总结

分享:
谁不是一边受伤,一边学会坚强。成长就是这样,你得接受这个世界给你的所有伤害,然后无所畏惧的长大。

你可能感兴趣的:(Python,源码,VBA,python,vba,源码,excel)