Skip to content

Instantly share code, notes, and snippets.

@briancohan
Last active May 6, 2023 05:46
Show Gist options
  • Save briancohan/49306996fbce6a01018c9546c17bfd19 to your computer and use it in GitHub Desktop.
Save briancohan/49306996fbce6a01018c9546c17bfd19 to your computer and use it in GitHub Desktop.
Unlock Excel Files
Attribute VB_Name = "PasswordBreaker"
Sub PasswordBreaker()
'Breaks worksheet password protection.
Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer, f As Integer
Dim g As Integer, h As Integer, i As Integer, j As Integer, k As Integer, l As Integer
On Error Resume Next
For a = 65 To 66: For b = 65 To 66: For c = 65 To 66: For d = 65 To 66: For e = 65 To 66: For f = 65 To 66
For g = 65 To 66: For h = 65 To 66: For i = 65 To 66: For j = 65 To 66: For k = 65 To 66: For l = 32 To 126
ActiveSheet.Unprotect Chr(a) & Chr(b) & Chr(c) & Chr(d) & Chr(e) & Chr(f) & Chr(g) & Chr(h) & Chr(i) & Chr(j) & Chr(k) & Chr(l)
If ActiveSheet.ProtectContents = False Then
degub.Print "pw: " & Chr(a) & Chr(b) & Chr(c) & Chr(d) & Chr(e) & Chr(f) & Chr(g) & Chr(h) & Chr(i) & Chr(j) & Chr(k) & Chr(l)
Exit Sub
End If
Next: Next: Next: Next: Next: Next: Next: Next: Next: Next: Next: Next
End Sub
Sub PasswordBreakAllSheets()
'Loops through all sheets and breaks worksheet password protection
Dim wb As Workbook: Set wb = ActiveWorkbook
Dim ws As Worksheet
For Each ws In wb.Worksheets
ws.Activate
PasswordBreaker
Next
End Sub
#!/bin/bash
# This will have issues if there are spaces in the file name. To replace the spaces with underscores, run:
## find -name "* *" -type f | rename 's/ /_/g'
# If this file is saved in your path as unlockxl, you can loop through all files in your directory with
## for i in ./*.xls*; do unlockxl.sh "$i"; done
# Parse out file name and extension
filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"
# Determine if the file is an excel file
xlfile=0
if [ $extension == "xls" ]; then
xlfile=1
elif [ $extension == "xlsx" ]; then
xlfile=1
fi
# Quit if not an excel file
if [ $xlfile -eq 0 ]; then
echo "That's not an excel file, must be: .xls, .xlsx"
exit 0
fi
# Convert to 2007 format if .xls
if [ $extension == "xls" ]; then
echo "Converting from .xls to .xlsx"
libreoffice --convert-to xlsx $filename".xls" --headless
fi
echo "Moving .xlxs to .zip"
mv $filename".xlsx" $filename".zip"
echo "Unzipping .zip file"
unzip $filename".zip"
echo "Deleting the .zip file"
rm $filename".zip"
# Where the magic happens
echo "Removing workbook passwords"
sed -i 's/<workbookProtection[^>]*>//' xl/workbook.xml
echo "Removing worksheet passwords"
sed -i 's/<sheetProtection[^>]*>//' xl/worksheets/*.xml
echo "Ensuring all sheets are visible"
sed -i 's/state="hidden"/state="visible"/g' xl/workbook.xml
# Cleaning up
echo "Rezipping excel parts"
zip -r $filename".zip" \[Content_Types\].xml docProps/ _rels/ xl/
echo "Cleanup the trash"
rm -R \[Content_Types\].xml docProps/ _rels/ xl/
echo "Moving .zip back to .xlsx"
mv $filename".zip" $filename".xlsx"
echo "Done"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment