By Jonathan Sayce


2009-06-22 10:37:13 8 Comments

I've been asked to update some Excel 2003 macros, but the VBA projects are password protected, and it seems there's a lack of documentation... no-one knows the passwords.

Is there a way of removing or cracking the password on a VBA project?

22 comments

@Đức Thanh Nguyễn 2014-12-16 15:32:01

You can try this direct VBA approach which doesn't require HEX editing. It will work for any files (*.xls, *.xlsm, *.xlam ...).

Tested and works on

Excel 2007
Excel 2010
Excel 2013 - 32 bit version.
Excel 2016 - 32 bit version.

Looking for 64 bit version? See https://stackoverflow.com/a/31005696/4342479

how it works

I will try my best to explain how it works - please excuse my english.

  1. The VBE will call a system function to create the password dialog box.
  2. If user enters the right password and click OK, this function returns 1. If user enters the wrong password or click Cancel, this function returns 0.
  3. After the dialog box is closed, the VBE checks the returned value of the system function
  4. if this value is 1, the VBE will "think" that the password is right, hence the locked VBA project will be opened.
  5. The code below swaps the memory of the original function used to display the password dialog with a user defined function that will always return 1 when being called.

using the code

Please backup your files first!

  1. Open the file(s) that contain your locked VBA Projects
  2. Create a new xlsm file and store this code in Module1

    code credited to Siwtom (nick name), a Vietnamese developer

    Option Explicit
    
    Private Const PAGE_EXECUTE_READWRITE = &H40
    
    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
    
    Private Function GetPtr(ByVal Value As Long) As Long
        GetPtr = Value
    End Function
    
    Public Sub RecoverBytes()
        If Flag Then MoveMemory ByVal pFunc, ByVal VarPtr(OriginBytes(0)), 6
    End Sub
    
    Public Function Hook() As Boolean
        Dim TmpBytes(0 To 5) As Byte
        Dim p As Long
        Dim OriginProtect As Long
    
        Hook = False
    
        pFunc = GetProcAddress(GetModuleHandleA("user32.dll"), "DialogBoxParamA")
    
    
        If VirtualProtect(ByVal pFunc, 6, PAGE_EXECUTE_READWRITE, OriginProtect) <> 0 Then
    
            MoveMemory ByVal VarPtr(TmpBytes(0)), ByVal pFunc, 6
            If TmpBytes(0) <> &H68 Then
    
                MoveMemory ByVal VarPtr(OriginBytes(0)), ByVal pFunc, 6
    
                p = GetPtr(AddressOf MyDialogBoxParam)
    
                HookBytes(0) = &H68
                MoveMemory ByVal VarPtr(HookBytes(1)), ByVal VarPtr(p), 4
                HookBytes(5) = &HC3
    
                MoveMemory ByVal pFunc, ByVal VarPtr(HookBytes(0)), 6
                Flag = True
                Hook = True
            End If
        End If
    End Function
    
    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
        If pTemplateName = 4070 Then
            MyDialogBoxParam = 1
        Else
            RecoverBytes
            MyDialogBoxParam = DialogBoxParam(hInstance, pTemplateName, _
                               hWndParent, lpDialogFunc, dwInitParam)
            Hook
        End If
    End Function
    
  3. Paste this code under the above code in Module1 and run it

    Sub unprotected()
        If Hook Then
            MsgBox "VBA Project is unprotected!", vbInformation, "*****"
        End If
    End Sub
    
  4. Come back to your VBA Projects and enjoy.

@Chris 2015-02-25 21:17:21

Doesn't seem to work on 64Bit versions of office.

@Đức Thanh Nguyễn 2015-02-25 21:21:01

@Chris you are absolutely right. Because the Windows API functions are defined for win 32 in this code.

@Tom 2015-03-19 01:30:30

Instead of running this from a separate workbook; could you run if from the PERSONAL file instead?

@Dennis G 2015-03-21 12:53:11

Some explanation would be nice of how this is working.

@kaybee99 2015-07-03 23:36:46

@Chris see my answer below for a version that works on 64-bit Office

@EranG 2016-01-25 12:01:19

Now the only question left (after seeing this impressive method works perfectly), is how the hell can I make my VBA project protected stronger to prevent others from using this hack on it :)

@ant1j 2016-01-27 13:07:51

This is also working perfectly well for PowerPoint and .pptm .ppam files

@Đức Thanh Nguyễn 2016-02-03 13:41:14

@EranG , sorry to get you wait for my comment. You may have a look at this link us9.campaign-archive1.com/…

@brettdj 2016-05-08 00:14:27

@ĐứcThanhNguyễn great post. You may want to write up the follow-up question and answer to have here as a reference as well (post a question and then answer it).

@boogiehound 2016-09-29 09:42:14

Tested with a docm in Word 2016, works like a charm!

@Matthew Bond 2016-11-21 12:49:20

This code works perfectly in unlocking the VBA code although each time I have used this it prevents me from re-protecting the project with a different password, has anyone else had this problem?

@Rafiki 2017-02-17 10:43:17

Also works for CATIA projects (CATvba macros) with abolutely no adapatations, which surprised me a lot! simply do the same, paste in different modules and that will work

@user6632933 2017-08-23 06:44:37

@ĐứcThanhNguyễn This line Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _ (Destination As Long, Source As Long, ByVal Length As Long) returns an error saying that I have to update the version to a 64-bit one. I'm using excel 2016 64-bit. Any help? Thanks

@Đức Thanh Nguyễn 2017-08-25 17:21:27

@ramedju see this one: stackoverflow.com/a/31005696/4342479

@B H 2018-01-10 16:25:00

I found it corrupts the VBA project in the Excel file so I had to export all of the modules/classes, then save the file as xlsx (non-macro), then CLOSE the file (stupid Excel), then re-open, then import modules and copy code from class files. At this point, I could save file as xlsm with my own password on the VBA project.

@Abhi O. 2018-01-18 15:27:31

Thank You!!! This worked like a charm. You sir are a life saver. Yesterday I copied over my own code from another workbook and modified it for a new process we were trying to automate and protected it but had no idea what I was thinking when i did it, because none of my usual passwords worked on this and I would have lost a whole days worth of work if not for this code.

@brymck 2018-02-21 08:54:06

This is amazing... but the fact that all you need to do to unprotect a VBA project is replace a system call to open a dialog is even more amazing.

@Tikkaty 2018-10-19 02:25:56

Excellent job with this! Great job - works well

@stinkyjak 2019-04-25 14:36:02

As of the time of this comment, this still works for me V1808 10730.20334 32-bit. Thanks.

@VePe 2018-11-18 08:03:29

With my turn, this is built upon kaybee99's excellent answer which is built upon Đức Thanh Nguyễn's fantastic answer to allow this method to work with both x86 and amd64 versions of Office.

An overview of what is changed, we avoid push/ret which is limited to 32bit addresses and replace it with mov/jmp reg.

Tested and works on

Word/Excel 2016 - 32 bit version.
Word/Excel 2016 - 64 bit version.

how it works

  1. Open the file(s) that contain your locked VBA Projects.
  2. Create a new file with the same type as the above and store this code in Module1

    Option Explicit
    
    Private Const PAGE_EXECUTE_READWRITE = &H40
    
    Private Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (Destination As LongPtr, Source As LongPtr, ByVal Length As LongPtr)
    
    Private Declare PtrSafe Function VirtualProtect Lib "kernel32" (lpAddress As LongPtr, _
    ByVal dwSize As LongPtr, ByVal flNewProtect As LongPtr, lpflOldProtect As LongPtr) As LongPtr
    
    Private Declare PtrSafe Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As LongPtr
    
    Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongPtr, _
    ByVal lpProcName As String) As LongPtr
    
    Private Declare PtrSafe Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" (ByVal hInstance As LongPtr, _
    ByVal pTemplateName As LongPtr, ByVal hWndParent As LongPtr, _
    ByVal lpDialogFunc As LongPtr, ByVal dwInitParam As LongPtr) As Integer
    
    Dim HookBytes(0 To 11) As Byte
    Dim OriginBytes(0 To 11) As Byte
    Dim pFunc As LongPtr
    Dim Flag As Boolean
    
    Private Function GetPtr(ByVal Value As LongPtr) As LongPtr
        GetPtr = Value
    End Function
    
    Public Sub RecoverBytes()
        If Flag Then MoveMemory ByVal pFunc, ByVal VarPtr(OriginBytes(0)), 12
    End Sub
    
    Public Function Hook() As Boolean
        Dim TmpBytes(0 To 11) As Byte
        Dim p As LongPtr, osi As Byte
        Dim OriginProtect As LongPtr
    
        Hook = False
    
        #If Win64 Then
            osi = 1
        #Else
            osi = 0
        #End If
    
        pFunc = GetProcAddress(GetModuleHandleA("user32.dll"), "DialogBoxParamA")
    
        If VirtualProtect(ByVal pFunc, 12, PAGE_EXECUTE_READWRITE, OriginProtect) <> 0 Then
    
            MoveMemory ByVal VarPtr(TmpBytes(0)), ByVal pFunc, osi+1
            If TmpBytes(osi) <> &HB8 Then
    
                MoveMemory ByVal VarPtr(OriginBytes(0)), ByVal pFunc, 12
    
                p = GetPtr(AddressOf MyDialogBoxParam)
    
                If osi Then HookBytes(0) = &H48
                HookBytes(osi) = &HB8
                osi = osi + 1
                MoveMemory ByVal VarPtr(HookBytes(osi)), ByVal VarPtr(p), 4 * osi
                HookBytes(osi + 4 * osi) = &HFF
                HookBytes(osi + 4 * osi + 1) = &HE0
    
                MoveMemory ByVal pFunc, ByVal VarPtr(HookBytes(0)), 12
                Flag = True
                Hook = True
            End If
        End If
    End Function
    
    Private Function MyDialogBoxParam(ByVal hInstance As LongPtr, _
    ByVal pTemplateName As LongPtr, ByVal hWndParent As LongPtr, _
    ByVal lpDialogFunc As LongPtr, ByVal dwInitParam As LongPtr) As Integer
    
        If pTemplateName = 4070 Then
            MyDialogBoxParam = 1
        Else
            RecoverBytes
            MyDialogBoxParam = DialogBoxParam(hInstance, pTemplateName, _
                       hWndParent, lpDialogFunc, dwInitParam)
            Hook
        End If
    End Function
    
  3. Paste this code in Module2 and run it

    Sub unprotected()
        If Hook Then
            MsgBox "VBA Project is unprotected!", vbInformation, "*****"
        End If
    End Sub
    

@Matt 2015-06-26 12:21:37

For a .xlsm or .dotm file type you need to do it a slightly different way.

  1. Change the extension of the .xlsm file to .zip.
  2. Open the .zip file (with WinZip or WinRar etc) and go to the xl folder.
  3. Extract the vbaProject.bin file and open it in a Hex Editor (I use HxD, its completely free and lightweight.)
  4. Search for DPB and replace with DPx and save the file.
  5. Replace the old vbaProject.bin file with this new on in the zipped file.
  6. Change the file extension back to .xlsm.
  7. Open workbook skip through the warning messages.
  8. Open up Visual Basic inside Excel.
  9. Go to Tools > VBAProject Properties > Protection Tab.
  10. Put in a new password and save the .xlsm file.
  11. Close and re open and your new password will work.

@LimaNightHawk 2016-10-11 13:16:45

Worked in Excel 2016, Windows 10 64bit. (xlsm files)

@NBajanca 2017-09-08 13:11:04

Worked in Word 2016, Windows 10 64bit (dotm files)

@nkatsar 2018-06-20 09:31:26

Great solution, worked for me in Excel 2013 64 bit. You may skip changing of file extensions to .zip if you have 7-Zip installed. In this case, you can just right-click on the .xlsm file and choose "7-Zip -> Open Archive"

@Berry Tsakala 2018-10-18 17:42:23

works with Word 2013, win7x64. (It's very sad, being so easily tricked to believe the code is somewhat secure).

@ThierryMichel 2018-11-15 01:03:17

WAO!!! Hats off @Matt how do you even come up with that stuff?

@Matt 2018-11-15 09:36:42

@ThierryMichel Combination of previous solutions and trial and error!

@Grez 2018-04-08 13:37:56

If you work in Java you may try VBAMacroExtractor. After extracting VBA scripts from .xlsm I've found there password in plaintext.

@ashleedawg 2018-02-10 14:57:49

VBA Project Passwords on Access, Excel, Powerpoint, or Word documents (2007, 2010, 2013 or 2016 versions with extensions .ACCDB .XLSM .XLTM .DOCM .DOTM .POTM .PPSM) can be easily removed.

It's simply a matter of changing the filename extension to .ZIP, unzipping the file, and using any basic Hex Editor (like XVI32) to "break" the existing password, which "confuses" Office so it prompts for a new password next time the file is opened.

A summary of the steps:

  • rename the file so it has a .ZIP extension.
  • open the ZIP and go to the XL folder.
  • extract vbaProject.bin and open it with a Hex Editor
  • "Search & Replace" to "replace all" changing DPB to DPX.
  • Save changes, place the .bin file back into the zip, return it to it's normal extension and open the file like normal.
  • ALT+F11 to enter the VB Editor and right-click in the Project Explorer to choose VBA Project Properties.
  • On the Protection tab, Set a new password.
  • Click OK, Close the file, Re-open it, hit ALT+F11.
  • Enter the new password that you set.

At this point you can remove the password completely if you choose to.

Complete instructions with a step-by-step video I made "way back when" are on YouTube here.

It's kind of shocking that this workaround has been out there for years, and Microsoft hasn't fixed the issue.


The moral of the story?

Microsoft Office VBA Project passwords are not to be relied upon for security of any sensitive information. If security is important, use third-party encryption software.

@Edwin van der V 2018-01-29 09:09:30

For Excel 2016 64-bit on a Windows 10 machine, I have used a hex editor to be able to change the password of a protected xla (have not tested this for any other extensions). Tip: create a backup before you do this.

The steps I took:

  1. Open the vba in the hex editor (for example XVI)
  2. Search on this DPB
  3. Change DPB to something else, like DPX
  4. Save it!
  5. Reopen the .xla, an error message will appear, just continue.
  6. You can now change the password of the .xla by opening the properties and go to the password tab.

I hope this helped some of you!

@Developer33 2017-06-05 10:43:17

your excel file's extension change to xml. And open it in notepad. password text find in xml file.

you see like below line;

Sheets("Sheet1").Unprotect Password:="blabla"

(sorry for my bad english)

@Noel Widmer 2017-06-05 11:09:59

Can you explain how your answer is better than the very good ones already provided?

@Developer33 2017-06-06 11:32:28

my solution has not a code. so very compact solution other than.

@Pieter 2010-11-05 15:25:55

There is another (somewhat easier) solution, without the size problems. I used this approach today (on a 2003 XLS file, using Excel 2007) and was successful.

  1. Backup the xls file
  2. Open the file in a HEX editor and locate the DPB=... part
  3. Change the DPB=... string to DPx=...
  4. Open the xls file in Excel
  5. Open the VBA editor (ALT + F11)
  6. the magic: Excel discovers an invalid key (DPx) and asks whether you want to continue loading the project (basically ignoring the protection)
  7. You will be able to overwrite the password, so change it to something you can remember
  8. Save the xls file*
  9. Close and reopen the document and work your VBA magic!

*NOTE: Be sure that you have changed the password to a new value, otherwise the next time you open the spreadsheet Excel will report errors (Unexpected Error), then when you access the list of VBA modules you will now see the names of the source modules but receive another error when trying to open forms/code/etc. To remedy this, go back to the VBA Project Properties and set the password to a new value. Save and re-open the Excel document and you should be good to go!

@jtolle 2010-11-30 01:29:14

This method worked great on an Excel 2000 .xls file.

@Joe Carroll 2012-12-15 10:49:20

Unfortunately, this didn't work for me with Excel for Mac 2011 v14.2.5. I got the option to repair the file, not to reset the password, and the effect was losing all the VBA scripts.

@Chris W 2013-02-04 14:26:41

Perfect solution - I did this with a 2003 file using HxD Hex Editor

@KekuSemau 2013-06-22 17:07:58

I just tried it (.xls, Excel 2007) and it did not work. Result is: The Modules are visible, code does indeed seem to work, but when opening a module, it says unexpected error (40230).

@Owen B 2013-08-15 11:08:41

Same error here (Excel 2010) - but then I realised I'd skipped the 'set a new password and save/reopen' (steps 7-9) from Pieter.

@E-r Gabriel Doronila 2014-06-06 05:34:24

+1 This method also worked on our poorly developed access (.mdb) file! Now we can make that thing better, thanks for this!

@Leo 2014-09-14 04:38:07

This method WFM on Excel 2013, on an .xls file. I don't remember what version of Office I used it last on, many years ago. When I first opened the file (step 4), the macro content was disabled. When I enabled it, I got several unexpected error messages. I clicked through all of them, then followed the rest of the recipe. After saving the file seems to work.

@Wernfried Domscheit 2014-10-20 13:43:29

Yes, also for for me it said unexpected error, however you can change the password (or set it to empty string) and save the excel sheet. Then it worked for me.

@dberm22 2014-10-29 12:30:07

This worked for me on a ppt Add-in file, too. (*.ppa)

@kevinarpe 2015-06-02 01:31:42

This works for XLAs also. The NOTE is very important. Do not skip this important step.

@user658182 2016-05-03 19:58:46

As another user pointed out, on Mac 2011, the option to repair the file comes up. Once you do that, it deletes all the scripts. Has anyone found a work around for Mac? @JoeCarroll

@Stephen Jacob 2017-05-07 06:09:46

@Pieter This worked with Excel 2007. I was wondering how you figured this out, is their some logic behind this?

@Chris Spicer 2011-05-03 10:02:39

My tool, VbaDiff, reads VBA directly from the file, so you can use it to recover protected VBA code from most office documents without resorting to a hex editor.

@Luboš Suk 2015-11-20 11:52:39

I tried some of solutions above and none of them works for me (excel 2007 xlsm file). Then i found another solution that even retrieve password, not just crack it.

Insert this code into module, run it and give it some time. It will recover your password by brute force.

Sub PasswordBreaker()

'Breaks worksheet password protection.

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)
Exit Sub
End If
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
End Sub

@PBD10017 2016-04-16 18:05:07

Nice! I think you got one downvote because your solution unlocks the worksheet rather than the VBA module. Nevertheless I found it helpful - so thanks!

@brettdj 2016-05-08 00:12:30

This is the right answer to the wrong question.

@Charles Byrne 2018-04-18 19:53:13

I have this one it my Personal Workbook. Authors cited Bob McCormick as original author later modified by Norman Harker and JE McGimpsey 2002.

@Colin Pickard 2009-06-22 10:58:24

Yes there is, as long as you are using a .xls format spreadsheet (the default for Excel up to 2003). For Excel 2007 onwards, the default is .xlsx, which is a fairly secure format, and this method will not work.

As Treb says, it's a simple comparison. One method is to simply swap out the password entry in the file using a hex editor (see Hex editors for Windows). Step by step example:

  1. Create a new simple excel file.
  2. In the VBA part, set a simple password (say - 1234).
  3. Save the file and exit. Then check the file size - see Stewbob's gotcha
  4. Open the file you just created with a hex editor.
  5. Copy the lines starting with the following keys:

    CMG=....
    DPB=...
    GC=...
    
  6. FIRST BACKUP the excel file you don't know the VBA password for, then open it with your hex editor, and paste the above copied lines from the dummy file.

  7. Save the excel file and exit.
  8. Now, open the excel file you need to see the VBA code in. The password for the VBA code will simply be 1234 (as in the example I'm showing here).

If you need to work with Excel 2007 or 2010, there are some other answers below which might help, particularly these: 1, 2, 3.

EDIT Feb 2015: for another method that looks very promising, look at this new answer by Đức Thanh Nguyễn.

@systemovich 2009-09-02 13:03:16

What if there are no lines that start with CMG=...?

@Colin Pickard 2009-09-02 13:43:49

In the blank excel file, or the locked one? Check the file size of the blank file. If its the locked file, make sure your backup is safe, then try changing just the other two lines. You sure it's encrypted file?

@systemovich 2009-09-02 13:51:36

In the blank file. The other two lines do not appear either. Does this also work in Excel 2007? I used HEdit.

@Colin Pickard 2009-09-02 14:16:59

I think there might be changes for Excel 2007, so this might not work. I don't have a copy of 2007 to try on this machine I'm afraid.

@Colin Pickard 2009-09-02 14:17:30

Is the protected file a .xlsx?

@systemovich 2009-09-02 14:25:56

It is xlsm, macro-enabled.

@Colin Pickard 2009-09-02 15:09:02

I can't confirm this, but I suspect that this trick will not work on the newer format. You may be reduced to brute-forcing the password. There are various sketchy-looking tools on the web for this. Good luck!

@Stewbob 2010-09-10 17:42:53

Excel 2007 password protection (and file format) is radically different than Excel 2003. I included some specifics about it in my answer below. In my opinion, the password protected option on an Excel 2007 file is the first time in Microsoft Office history that they have produced a reasonably secure file.

@tobriand 2013-07-20 11:13:53

I've recently had to do this in a project at work to pull out and archive source code for a variety of Excel applications (for which this post was invaluable). Two clarifications: (1) The minimum size appears to be 133 bytes for a 1-character password. It's pretty useful to have a pre-calculated 133-byte set of values too, since (2) Whilst padding the individual hashes works, there's no need. You can simply pad the end of the whole block after the GC="..." entry with null characters until you reach the length of the original file.

@SheetJS 2013-10-08 02:26:54

@tobriand or just replace the existing characters with spaces

@tobriand 2013-10-09 16:38:38

Spaces might work too - at the time I was having to use an ADO stream in order to replicate a hex editor in VBA, since at least if you try and do this with notepad as your editor, the file becomes corrupt (probably due to line break conversions, I'd guess, but not sure). Given that, I used Nulls, since in my experience they're more reliable (and either way, need to use a character), but if you have access to a proper editing tool, spaces might work just as well...

@inetphantom 2015-10-28 11:44:59

What is about *.xlsm

@Eric K. 2015-12-08 16:14:52

Is there anyway to retrieve the VBA project password while break it ?

@Mescalito 2016-02-28 23:58:14

I wasn't able to set vba password on an excel 2016 new file. Could someone simply share the HEX to replace with 1234? Or can it change from machine to machine?

@Drew Chapin 2016-05-10 16:33:20

This approach worked for me on a .xlsm file. I saved it as a .xls, did this, and then converted it back to .xlsm. It should be noted you can safely increase the length of the file if the new CMG... string is longer than the original.

@kaybee99 2015-06-23 14:32:21

I've built upon Đức Thanh Nguyễn's fantastic answer to allow this method to work with 64-bit versions of Excel. I'm running Excel 2010 64-Bit on 64-Bit Windows 7.

  1. Open the file(s) that contain your locked VBA Projects.
  2. Create a new xlsm file and store this code in Module1

    Option Explicit
    
    Private Const PAGE_EXECUTE_READWRITE = &H40
    
    Private Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (Destination As LongPtr, Source As LongPtr, ByVal Length As LongPtr)
    
    Private Declare PtrSafe Function VirtualProtect Lib "kernel32" (lpAddress As LongPtr, _
    ByVal dwSize As LongPtr, ByVal flNewProtect As LongPtr, lpflOldProtect As LongPtr) As LongPtr
    
    Private Declare PtrSafe Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As LongPtr
    
    Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongPtr, _
    ByVal lpProcName As String) As LongPtr
    
    Private Declare PtrSafe Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" (ByVal hInstance As LongPtr, _
    ByVal pTemplateName As LongPtr, ByVal hWndParent As LongPtr, _
    ByVal lpDialogFunc As LongPtr, ByVal dwInitParam As LongPtr) As Integer
    
    Dim HookBytes(0 To 5) As Byte
    Dim OriginBytes(0 To 5) As Byte
    Dim pFunc As LongPtr
    Dim Flag As Boolean
    
    Private Function GetPtr(ByVal Value As LongPtr) As LongPtr
        GetPtr = Value
    End Function
    
    Public Sub RecoverBytes()
        If Flag Then MoveMemory ByVal pFunc, ByVal VarPtr(OriginBytes(0)), 6
    End Sub
    
    Public Function Hook() As Boolean
        Dim TmpBytes(0 To 5) As Byte
        Dim p As LongPtr
        Dim OriginProtect As LongPtr
    
        Hook = False
    
        pFunc = GetProcAddress(GetModuleHandleA("user32.dll"), "DialogBoxParamA")
    
    
        If VirtualProtect(ByVal pFunc, 6, PAGE_EXECUTE_READWRITE, OriginProtect) <> 0 Then
    
            MoveMemory ByVal VarPtr(TmpBytes(0)), ByVal pFunc, 6
            If TmpBytes(0) <> &H68 Then
    
                MoveMemory ByVal VarPtr(OriginBytes(0)), ByVal pFunc, 6
    
                p = GetPtr(AddressOf MyDialogBoxParam)
    
                HookBytes(0) = &H68
                MoveMemory ByVal VarPtr(HookBytes(1)), ByVal VarPtr(p), 4
                HookBytes(5) = &HC3
    
                MoveMemory ByVal pFunc, ByVal VarPtr(HookBytes(0)), 6
                Flag = True
                Hook = True
            End If
        End If
    End Function
    
    Private Function MyDialogBoxParam(ByVal hInstance As LongPtr, _
    ByVal pTemplateName As LongPtr, ByVal hWndParent As LongPtr, _
    ByVal lpDialogFunc As LongPtr, ByVal dwInitParam As LongPtr) As Integer
    
        If pTemplateName = 4070 Then
            MyDialogBoxParam = 1
        Else
            RecoverBytes
            MyDialogBoxParam = DialogBoxParam(hInstance, pTemplateName, _
                       hWndParent, lpDialogFunc, dwInitParam)
            Hook
        End If
    End Function
    
  3. Paste this code in Module2 and run it

    Sub unprotected()
        If Hook Then
            MsgBox "VBA Project is unprotected!", vbInformation, "*****"
        End If
    End Sub
    

DISCLAIMER This worked for me and I have documented it here in the hope it will help someone out. I have not fully tested it. Please be sure to save all open files before proceeding with this option.

@Ashish Uttam 2015-10-04 07:52:04

I used this code to remove VBA project password from one of my very old excel add-in file (.xla) and it worked very well. Thanks a lot!

@James Wilkins 2016-04-26 17:16:16

Works great, thanks! ;) For others, be sure not to read too quickly and miss the step "and run it", lol. :P (I didn't have my coffee yet, so give me a break lol)

@GodEater 2016-08-20 14:34:15

Shame - but this causes Excel 2016 64 bit to crash when I try it.

@Maxime 2017-01-10 20:26:37

This causes my Excel 2013 64 bit to crash too. :-(

@Fixer 2017-02-14 16:15:48

I used this on an .xlsm document I had forgotten which password I had applied to it and it worked wonderfully. My thanks, sir!

@OfficialBenWhite 2017-02-21 18:45:06

worked on 64bit

@user6632933 2017-08-23 07:03:51

@kaybee99 Do you have any revision that works in an Excel 2016 64-bit? Thanks

@Mundi 2017-10-27 19:48:06

Not working, I get an out of memory message.

@Nam Vu 2017-11-14 17:24:53

@Mundi I have the same problems, any solution?

@Repose 2017-12-13 18:23:42

I get "out of memory" message when trying to run on Mac (2016 x64)

@Charles Byrne 2018-04-23 13:51:36

Instead of having two modules (64 and 32 bit) just use the Conditional Compilation and have the 64 bit declarations then the 32 bit declarations: #If Win64 Then Private Declare PtrSafe Sub... (etc) 'Remaining 64 Bit Declarations #Else Private Declare Sub... 'Remaining 64 Bit Declarations #End If

@David Hirst 2018-05-25 15:45:03

I used this in an .xls file rather a than new .xlms and it worked fine under office 2016 64 bit

@Zev Spitz 2018-07-10 12:47:43

I can confirm that this works with a Word macro-enabled template.

@Marcucciboy2 2018-07-18 13:41:59

@ZevSpitz what version of word are you using?

@Zev Spitz 2018-07-18 13:50:20

@Marcucciboy2 2010 64-bit.

@barfuin 2018-08-15 12:06:09

Worked on 2016 (365) 64bit

@jkpieterse 2018-09-05 16:12:29

I expect it will not work on files protected using spreadsheet1.com/unviewable-vba-project-app-for-excel.html

@user3761175 2014-06-20 18:11:07

For Excel 2007 onward you need to change your file extension to .zip In the archive there is a subfolder xl, in there you will find vbaProject.bin. Follow the step above with vbaProject.bin then save it back in the archive. Modify back your extension and voilà! (meaning follow steps above)

@Gimelist 2014-10-27 14:17:48

I can confirm this works for .xlam files with Excel 2010 as well. +1!

@SheetJS 2013-10-08 02:32:30

If the file is a valid zip file (the first few bytes are 50 4B -- used in formats like .xlsm), then unzip the file and look for the subfile xl/vbaProject.bin. This is a CFB file just like the .xls files. Follow the instructions for the XLS format (applied to the subfile) and then just zip the contents.

For the XLS format, you can follow some of the other methods in this post. I personally prefer searching for the DPB= block and replacing the text

CMG="..."
DPB="..."
GC="..."

with blank spaces. This obviates CFB container size issues.

@Stewbob 2009-06-24 14:36:12

Colin Pickard has an excellent answer, but there is one 'watch out' with this. There are instances (I haven't figured out the cause yet) where the total length of the "CMG=........GC=...." entry in the file is different from one excel file to the next. In some cases, this entry will be 137 bytes, and in others it will be 143 bytes. The 137 byte length is the odd one, and if this happens when you create your file with the '1234' password, just create another file, and it should jump to the 143 byte length.

If you try to paste the wrong number of bytes into the file, you will lose your VBA project when you try to open the file with Excel.

EDIT

This is not valid for Excel 2007/2010 files. The standard .xlsx file format is actually a .zip file containing numerous sub-folders with the formatting, layout, content, etc, stored as xml data. For an unprotected Excel 2007 file, you can just change the .xlsx extension to .zip, then open the zip file and look through all the xml data. It's very straightforward.

However, when you password protect an Excel 2007 file, the entire .zip (.xlsx) file is actually encrypted using RSA encryption. It is no longer possible to change the extension to .zip and browse the file contents.

@Anonymous Type 2010-09-27 22:37:20

Then you need to use standard zip hacking tools. Its no longer a "how do i back an excel file" problem.

@Treb 2010-09-28 06:58:21

@Anonymous Type: I think a zip cracking tool won't help. As I understand Stewbob, it's not the file entries in the zip file that are encrypted, but the whole zip file itself, which should include the header and the central directory.

@kizzx2 2011-02-02 18:58:10

Just curious: how could it be RSA when I just enter one password (symmetric)?

@onlynone 2015-10-05 19:43:54

How about when the file you want to get into has the shorter keys? Just keep creating vba docs until you get one that has 137?

@Nigel Heffernan 2017-10-19 15:10:39

Are you sure that the entire zipfile is encrypted when you lock the VBA project? I can still open the zipfile and see the file structure.... And subfolder xl\ contains the file vbaProject.bin which has the familiar "CMG=... GC=" hashing block.

@Spangen 2011-04-06 14:21:38

In the event that your block of CMG="XXXX"\r\nDPB="XXXXX"\r\nGC="XXXXXX" in your 'known password' file is shorter than the existing block in the 'unknown password' file, pad your hex strings with trailing zeros to reach the correct length.

e.g.

CMG="xxxxxx"\r\nDPB="xxxxxxxx"\r\nGC="xxxxxxxxxx"

in the unknown password file, should be set to

CMG="XXXX00"\r\nDPB="XXXXX000"\r\nGC="XXXXXX0000" to preserve file length.

I have also had this working with .XLA (97/2003 format) files in office 2007.

@tobriand 2013-07-20 11:21:14

This works, but as I've recently discovered (commented above) you can also simply add null characters after the final close quote in the GC="..." block until you hit the same length.

@Andy 2011-03-25 01:29:54

It's worth pointing out that if you have an Excel 2007 (xlsm) file, then you can simply save it as an Excel 2003 (xls) file and use the methods outlined in other answers.

@Qbik 2014-06-17 07:02:27

that is not true, I've worked with files for which conversion to xls/xla from xlsm was impossible, Excel 2007 and 2010 crashed each time, I've tried various instances, from one erros message - Kod wyjątku: c0000005 Przesunięcie wyjątku: 005d211d

@ZygD 2015-10-01 18:45:29

YES you can do it. I've done it many times. If there is something on sheets which is necessary and what is not transferred to the older version I do this: 1. convert .xlsm to .xls 2. crack the code of .xls 3. convert .xlsm to .xlsx 4. Put the code from modules in .xls to .xlsx and save that as .xlsm

@Purus 2017-02-03 07:08:52

It works after converting xlsm to xls as in the answer.

@Yuhong Bao 2010-11-30 00:41:16

Colin Pickard is mostly correct, but don't confuse the "password to open" protection for the entire file with the VBA password protection, which is completely different from the former and is the same for Office 2003 and 2007 (for Office 2007, rename the file to .zip and look for the vbaProject.bin inside the zip). And that technically the correct way to edit the file is to use a OLE compound document viewer like CFX to open up the correct stream. Of course, if you are just replacing bytes, the plain old binary editor may work.

BTW, if you are wondering about the exact format of these fields, they have it documented now:

http://msdn.microsoft.com/en-us/library/dd926151%28v=office.12%29.aspx

@JohnLBevan 2012-07-19 10:51:04

The following link gives details for the XSLM format files. gbanik.blogspot.co.uk/2010/08/… The solution's the same as the one outlined by Yuhong Bao above, but makes for interesting reading and includes screenshots.

@greg 2010-04-12 15:50:42

Have you tried simply opening them in OpenOffice.org?

I had a similar problem some time ago and found that Excel and Calc didn't understand each other's encryption, and so allowed direct access to just about everything.

This was a while ago, so if that wasn't just a fluke on my part it also may have been patched.

@Charles Duffy 2010-04-12 15:44:18

ElcomSoft makes Advanced Office Password Breaker and Advanced Office Password Recovery products which may apply to this case, as long as the document was created in Office 2007 or prior.

@Scoob 2010-04-12 15:35:20

Tom - I made a schoolboy error initially as I didn't watch the byte size and instead I copied and pasted from the "CMG" set up to the subsequent entry. This was two different text sizes between the two files, though, and I lost the VBA project just as Stewbob warned.

Using HxD, there is a counter tracking how much file you're selecting. Copy starting from CMG until the counter reads 8F (hex for 143) and likewise when pasting into the locked file - I ended up with twice the number of "..." at the end of the paste, which looked odd somehow and felt almost unnatural, but it worked.

I don't know if it is crucial, but I made sure I shut both the hex editor and excel down before reopening the file in Excel. I then had to go through the menus to open the VB Editor, into VBProject Properties and entered in the 'new' password to unlock the code.

I hope this helps.

@Treb 2009-06-22 10:54:40

The protection is a simple text comparison in Excel. Load Excel in your favourite debugger (Ollydbg being my tool of choice), find the code that does the comparison and fix it to always return true, this should let you access the macros.

@Anonymous Type 2010-09-27 22:37:57

No longer valid with new formats.

Related Questions

Sponsored Content

18 Answered Questions

[SOLVED] Why is char[] preferred over String for passwords?

25 Answered Questions

[SOLVED] Is it possible to force Excel recognize UTF-8 CSV files automatically?

  • 2011-05-14 13:53:39
  • Lyubomyr Shaydariv
  • 433107 View
  • 379 Score
  • 25 Answer
  • Tags:   excel csv utf-8

42 Answered Questions

[SOLVED] How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?

  • 2008-09-29 22:30:28
  • mistrmark
  • 998116 View
  • 1732 Score
  • 42 Answer
  • Tags:   c# .net excel file-io

9 Answered Questions

33 Answered Questions

[SOLVED] Disable browser 'Save Password' functionality

14 Answered Questions

[SOLVED] Secure hash and salt for PHP passwords

0 Answered Questions

Is there a way to crack the password on an Excel VBA Project? Restore functionality

  • 2017-11-01 17:47:24
  • Erick Frederick
  • 209 View
  • 0 Score
  • 0 Answer
  • Tags:   excel vba passwords

1 Answered Questions

How to crack the password for VBA modules in excel 2010

  • 2017-09-24 06:25:17
  • ANAND AGRAWAL
  • 2167 View
  • -1 Score
  • 1 Answer
  • Tags:   excel vba excel-vba

1 Answered Questions

[SOLVED] unlocking a vba project with vba, knowing the password?

0 Answered Questions

Sponsored Content