收到了客户两张电子表格,需要修改几项数据,其中一张表格是直接可以修改的,但同一压缩包里的另一个文件则显示收保护,读取没问题,修改就不行了,询问客户密码也是一问三不知。无奈在51CTO找了这么一个暴力测试的方案,程序会在成功后弹窗提示该文件可能的密码。类似的代码在知乎也看到过,51CTO的更为简洁但删除了一些作者注释(包含代码作者信息),于是重新翻了一下知乎,加上了代码作者的名字。
Sub PasswordBreaker()
'作者:McCormick JE McGimpsey
Dim i As Integer, j As Integer, k As Integer
Dim l As Integer, m As Integer, n As Integer
Dim i1 As Integer, i2 As Integer, i3 As Integer
Dim i4 As Integer, i5 As Integer, i6 As Integer
On Error Resume Next
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
ActiveSheet.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 ActiveSheet.ProtectContents = False Then
MsgBox "One usable password is " & Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
ActiveWorkbook.Sheets(1).Select
Range("a1").FormulaR1C1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
Exit Sub
End If
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
End Sub
程序会尝试一些可能的密码组合,因为组合较多,所以过于复杂的密码会耗费较长时间,耐心等待。当然一般表格保护只是用于避免用户误操作,极少会设置复杂度爆表的密码。这里的多重循环,实际上前面几段都是在重复取A或者B,仅For n = 32 To 126
这一句测试了多个字符,博主测试了一下密码12345
,破解后提示可用密码为AABBAABBBAAW
,感觉这段程序并不是单纯的暴力破解,而是利用了EXCEL的密码生成机制。