Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cracking Sheet Password with VBA

Tags:

excel

vba

I found this VBA code to unlock sheets without knowing the password:

Sub PasswordBreaker()

  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

My question is: What kind of exploit does it use to work?

In other words, how come this generated string of A's and B's can be used as the password to a sheet inside a particular workbook ?

like image 973
Mariusz Górski Avatar asked Nov 13 '13 12:11

Mariusz Górski


People also ask

How do I bypass a password in Excel VBA?

Use Alt+F11 to enter the macro editor. Once in VBA double click the sheet you need to unlock from the menu listing on the left. This will open the general declarations page for the sheet. Sub PasswordBreaker() 'Breaks worksheet password protection.

Can you crack a VBA password?

#3 Method – Break VBA Code Password in Excel Using Workbook Open password-protected Excel Workbook VBA files. Press Alt + F11 to open visual basic editor. Click on Inset Module option and paste the given code into VBA module. Press the F5 button and run the code to break VBA password in Excel.

Can Excel passwords be cracked?

A password protected excel file can sometimes be recovered using specific tools such as HashCat or John the Ripper. These are quite technical password "cracking" programs. Older versions of office (prior to office 2010) had quite weak password hashing algorithms but the newer versions are much harder to recover.


2 Answers

The Excel worksheet password protection works by converting the input password to a hash and stores it. A hash is a one-way algorithm that crunches up the bits, losing some information along the way, but generating a fingerprint of the original data. Because of the loss of data, it is impossible to reverse a hash to get the original password, but in the future if someone types in a password it can be hashed and compared against the stored hash. This (usually) makes it more secure than simply storing the password as a string to compare against.

The best description by far I've encountered of how brute forcing the Excel hashing algorithm works is on the page @mehow links to, posted by Torben Klein. His answer can be summed up as:

  1. The Excel hash function maps the large space of possible passwords to the small space of possible hashes.
  2. Because the hashing algorithm generates such small hashes, 15 bits, the number of possible hashes is 2^15 = 32768 hashes.
  3. 32768 is a tiny number of things to try when computing power is applied. Klein derives a subset of the input passwords that cover all of the possible hashes.

Based on this description of Excel's hashing function, the following code generates the same hash as Excel which you can use to test Klein's function.

Option Explicit
'mdlExcelHash

Public Function getExcelPasswordHash(Pass As String)
    Dim PassBytes() As Byte
    PassBytes = StrConv(Pass, vbFromUnicode)
    Dim cchPassword As Long
    cchPassword = UBound(PassBytes) + 1
    Dim wPasswordHash As Long
    If cchPassword = 0 Then
        getExcelPasswordHash = wPasswordHash
        Exit Function
    End If

    Dim pch As Long
    pch = cchPassword - 1
    While pch >= 0
        wPasswordHash = wPasswordHash Xor PassBytes(pch)
        wPasswordHash = RotateLeft_15bit(wPasswordHash, 1)
        pch = pch - 1
    Wend

    wPasswordHash = wPasswordHash Xor cchPassword
    wPasswordHash = wPasswordHash Xor &HCE4B&
    getExcelPasswordHash = wPasswordHash
End Function

Private Function RotateLeft_15bit(num As Long, Count As Long) As Long
    Dim outLong As Long
    Dim i As Long
    outLong = num
    For i = 0 To Count - 1
        outLong = ((outLong \ 2 ^ 14) And &H1) Or ((outLong * 2) And &H7FFF) 'Rotates left around 15 bits, kind of a signed rotateleft
    Next
    RotateLeft_15bit = outLong
End Function
like image 69
Blackhawk Avatar answered Sep 20 '22 01:09

Blackhawk


The accepted answer doesn't work for worksheets protected on Excel >2016 with SHA-512, but it's very easy to work around considering excel 2016 uses office openxml specification which is open source.

This method is also backwards compatible so it's another way to break the older proprietary md5 sheet protection rather than cracking it. Simply save-as from a .xls version to a .xlsx version before you try it.

Method

  1. Rename your .xlsx/.xlsm file to .zip.
  2. Right click-Extract all to a folder using windows explorer.
  3. Open the xl\worksheets folder and open the file with the correct sheet name.
  4. Search for "sheetProtection":

<sheetProtection algorithmName="SHA-512" hashValue="j1woDldvfHE8IVB1F82CN/pmfOdOkpxkkZURiZJSGISjkJRIfM1G7EFwJsEeE1H+sf7s6sLIYSCuHPJG5Tpozw==" saltValue="QX8YeX/qfspqhDemAUEwSw==" spinCount="100000" sheet="1" objects="1" scenarios="1"/>

  1. Delete the whole node and save the file.
  2. Select all of the files at the root of the folder that you extracted the files to, right click it and click Send to -> Compressed (zipped) folder.
  3. Renamed the resulting file to it's original .xlsx/.xlsm extension

Open the file, and the protection on that sheet will be gone.

like image 40
Alexander Don'valderath Avatar answered Sep 20 '22 01:09

Alexander Don'valderath