Remove excel vb password

本文介绍了一种破解Excel中VBA保护密码的方法,通过修改底层API调用来绕过密码验证过程。此方法适用于Windows 64位及32位系统,并提供了详细的源代码实现。

Remove excel vb password

New a excel and open Visual Basic edit window, then insert a module. Put below code in module.

===============Source Code==========================

Option Explicit
#If Win64 Then
Private Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (Destination As LongLong, Source As LongLong, ByVal Length As LongLong)
Private Declare PtrSafe Function VirtualProtect Lib "kernel32" (lpAddress As LongLong, _
        ByVal dwSize As LongLong, ByVal flNewProtect As LongLong, lpflOldProtect As LongLong) As LongLong
           
Private Declare PtrSafe Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As LongLong
      
Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongLong, _
        ByVal lpProcName As String) As LongLong
      
Private Declare PtrSafe Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" (ByVal hInstance As LongLong, _
        ByVal pTemplateName As LongLong, ByVal hWndParent As LongLong, _
        ByVal lpDialogFunc As LongLong, ByVal dwInitParam As LongLong) As Integer
Dim HookBytes(0 To 5) As Byte
Dim OriginBytes(0 To 5) As Byte
Dim pFunc As LongLong
Dim Flag As Boolean
#Else
Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (Destination As Long, Source As Long, ByVal Length As Long)
Private Declare Function VirtualProtect Lib "kernel32" (lpAddress As Long, _
        ByVal dwSize As Long, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long
           
Private Declare Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As Long
      
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, _
        ByVal lpProcName As String) As Long
      
Private Declare Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" (ByVal hInstance As Long, _
        ByVal pTemplateName As Long, ByVal hWndParent As Long, _
        ByVal lpDialogFunc As Long, ByVal dwInitParam As Long) As Integer
Dim HookBytes(0 To 5) As Byte
Dim OriginBytes(0 To 5) As Byte
Dim pFunc As Long
Dim Flag As Boolean
        
#End If
#If Win64 Then
Private Function GetPtr(ByVal Value As LongLong) As LongLong
#Else
Private Function GetPtr(ByVal Value As Long) As Long
#End If
    'Get func pointer
    GetPtr = Value
End Function
  
Public Sub RecoverBytes()
    'If hooked then recover pre-API 6byte that alse is recover pre-func's fucntion
    If Flag Then MoveMemory ByVal pFunc, ByVal VarPtr(OriginBytes(0)), 6
End Sub
  
#If Win64 Then
Public Function Hook() As Boolean
    Dim TmpBytes(0 To 5) As Byte
    Dim p As LongLong
    Dim OriginProtect As LongLong
#Else
Public Function Hook() As Boolean
    Dim TmpBytes(0 To 5) As Byte
    Dim p As Long
    Dim OriginProtect As Long
#End If
    Hook = False
    'VBE6.dll call DialogBoxParamA to show VB6INTL.dll resource of dialogbox 4070(that is password dialogbox)
    'If DialogBoxParamA return true, then VBE will belived password correct, so we will hook DialogBoxParamA func
    pFunc = GetProcAddress(GetModuleHandleA("user32.dll"), "DialogBoxParamA")
    'Standand api hook 1: change memory attribute, make sure that will writeable
    If VirtualProtect(ByVal pFunc, 6, &H40, OriginProtect) <> 0 Then
        'Standand api hook 2: Judge whether hook, look out first byte if is &H68,
        MoveMemory ByVal VarPtr(TmpBytes(0)), ByVal pFunc, 6
        If TmpBytes(0) <> &H68 Then
            'Standand api hook 3: Save pre-func's first 6bytes for recovery
            MoveMemory ByVal VarPtr(OriginBytes(0)), ByVal pFunc, 6
            'Get MyDialogBoxParam pointer with AddressOf
            p = GetPtr(AddressOf MyDialogBoxParam)
            'Standand api hook 4: Construct new API enter
            HookBytes(0) = &H68
            MoveMemory ByVal VarPtr(HookBytes(1)), ByVal VarPtr(p), 4
            HookBytes(5) = &HC3
               
            'standand API hook 5: replace API first 6bytes by HookBytes cotent
            MoveMemory ByVal pFunc, ByVal VarPtr(HookBytes(0)), 6
            'set hook done flag
            Flag = True
            Hook = True
        End If
    End If
End Function
  
#If Win64 Then
Private Function MyDialogBoxParam(ByVal hInstance As LongLong, _
        ByVal pTemplateName As LongLong, ByVal hWndParent As LongLong, _
        ByVal lpDialogFunc As LongLong, ByVal dwInitParam As LongLong) As Integer
#Else
Private Function MyDialogBoxParam(ByVal hInstance As Long, _
        ByVal pTemplateName As Long, ByVal hWndParent As Long, _
        ByVal lpDialogFunc As Long, ByVal dwInitParam As Long) As Integer
#End If
    If pTemplateName = 4070 Then
        'someone call Dialogbox with 4070 then return 1,that make VBE believe the password is correct
        MyDialogBoxParam = 1
    Else
        'some call Dialogbox without 4070, we call RecoverBytes to recover pre-func's fucntion, then go pre-func
        RecoverBytes
        MyDialogBoxParam = DialogBoxParam(hInstance, pTemplateName, _
                           hWndParent, lpDialogFunc, dwInitParam)
        'pre-func done, call hook again
        Hook
    End If
End Function

=================================================

Go to Sheet1, and put below code in Sheet1 code window.

=============== Source Code ==========================

Sub crack()
If Hook Then
MsgBox "crack done"
End If
End Sub
 
Sub recover()
RecoverBytes
MsgBox "recover done"
End Sub
===================================================


At the end you need open your target excel and run macro of "Sheet1.crack".


All done.

### 如何在 Excel VBA 中使用集合 #### 集合对象简介 集合(Collection)是一个用于存储一组项目的内置对象,在VBA中非常有用。与数组不同的是,集合中的项目可以通过键名或索引来访问[^1]。 #### 创建集合对象实例 创建一个新的集合对象实例通常通过 `New` 关键字来完成: ```vba Dim myCollection As New Collection ``` 如果未设置引用,则可以在过程内初始化该变量: ```vba Dim myCollection As Collection Set myCollection = New Collection ``` #### 向集合添加成员 向集合中添加项的方法如下所示: ```vba myCollection.Add Item:= &quot;Item1&quot;, Key:=&quot;First&quot; myCollection.Add &quot;Item2&quot;, &quot;Second&quot; ``` 这里展示了两种不同的方式指定参数名称与否都可以实现相同的效果;另外还可以给每件物品分配唯一的字符串作为其标识符即Key属性值以便后续检索操作更加便捷高效。 #### 访问集合内的元素 有两种方法可以获取已存入的数据:一种是指定位置序号(从1开始),另一种则是利用之前定义好的关键字来进行定位读取: ```vba Debug.Print myCollection(1) &#39; 输出:&quot;Item1&quot; Debug.Print myCollection(&quot;Second&quot;) &#39; 输出:&quot;Item2&quot; ``` #### 移除特定条目 当不再需要某些数据时可以从列表里将其删除掉,同样支持按Index或者key的方式执行此动作. ```vba myCollection.Remove 1 &#39; 删除第一个加入的对象 &#39; 或者 myCollection.Remove &quot;Second&quot; &#39; 使用关键词移除对应的记录 ``` #### 获取集合大小 要了解当前容器中有多少个项目,可以直接调用Count属性获得整数值表示的数量统计结果。 ```vba MsgBox &quot;The collection contains &quot; &amp; myCollection.Count &amp; &quot; items.&quot; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值