Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot open password protected SHA1 encrypted files. #68

Closed
feinorgh opened this issue Sep 30, 2016 · 2 comments · May be fixed by #69
Closed

Cannot open password protected SHA1 encrypted files. #68

feinorgh opened this issue Sep 30, 2016 · 2 comments · May be fixed by #69

Comments

@feinorgh
Copy link

feinorgh commented Sep 30, 2016

I've been trying to get this module to work with XLSX files produced by Excel 14.0.7166.5000 (32-bit) (from the Microsoft Office Professional Plus 2010 package).

I tried to see what happened through the module, and realized the first problem is that it interprets the hashing algorithm as SHA1 (not SHA-1).

diff --git a/lib/Spreadsheet/ParseXLSX/Decryptor.pm b/lib/Spreadsheet/ParseXLSX/
Decryptor.pm
index 4968a7d..3a84e15 100644
--- a/lib/Spreadsheet/ParseXLSX/Decryptor.pm
+++ b/lib/Spreadsheet/ParseXLSX/Decryptor.pm
@@ -93,6 +93,8 @@ sub _standardDecryption {

     my ($cipherAlgorithm, $hashAlgorithm);

+    print "\$algID: $algID, \$algIDHash: $algIDHash\n";
+
     if ($algID == 0x0000660E || $algID == 0x0000660F || $algID == 0x0000660E) {
         $cipherAlgorithm = 'AES';
     } else {
@@ -194,7 +196,7 @@ sub new {

     if ($self->{hashAlgorithm} eq 'SHA512') {
         $self->{hashProc} = \&Digest::SHA::sha512;
-    } elsif ($self->{hashAlgorithm} eq 'SHA-1') {
+    } elsif ($self->{hashAlgorithm} eq 'SHA-1' || $self->{hashAlgorithm} eq 'SHA1') {
         $self->{hashProc} = \&Digest::SHA::sha1;
     } elsif ($self->{hashAlgorithm} eq 'SHA256') {
         $self->{hashProc} = \&Digest::SHA::sha256;

So after this, I got a new error, namely that the wrong password was given. So I compared the produced hashes and realized they are similar, but possibly with the wrong bit size, since the hash is zero padded when unpacked:

diff --git a/lib/Spreadsheet/ParseXLSX/Decryptor/Agile.pm b/lib/Spreadsheet/ParseXLSX/Decryptor/Agile.pm
index 0405a68..902c368 100644
--- a/lib/Spreadsheet/ParseXLSX/Decryptor/Agile.pm
+++ b/lib/Spreadsheet/ParseXLSX/Decryptor/Agile.pm
@@ -94,6 +94,11 @@ sub verifyPassword {
     my $encryptedVerifierHash0 = $self->{hashProc}->($self->decrypt($encryptedVerifier, "\xfe\xa7\xd2\x76\x3b\x4b\x9e\x79"));
     $encryptedVerifierHash = $self->decrypt($encryptedVerifierHash, "\xd7\xaa\x0f\x6d\x30\x61\x34\x4e");

+    use Data::Dump;
+    dd($encryptedVerifierHash0);
+    dd($encryptedVerifierHash);
+    print "'$encryptedVerifierHash0'\n";
+    print "'$encryptedVerifierHash'\n";
     die "Wrong password: $self" unless ($encryptedVerifierHash0 eq $encryptedVerifierHash);
 }

pack("H*","053129c83b159a03a74b824a5cfe1ed4690a52fe")
pack("H*","053129c83b159a03a74b824a5cfe1ed4690a52fe000000000000000000000000")
'1)▒;▒▒K▒J\▒▒i
R▒'
'1)▒;▒▒K▒J\▒▒i
R▒'

So they "look" similar produced as strings but do not compare to the same value (I suppose). I could not dig deeper into the issue currently, unfortunately, but there might be an easy fix. My knowledge of hashes and encryption routines is however limited, but it might be obvious to someone else.

Attached is the example Excel file protected by the password "foobar".

sha1-password-protected.xlsx

mazurin added a commit to mazurin/spreadsheet-parsexlsx that referenced this issue Oct 5, 2016
@mazurin mazurin mentioned this issue Oct 5, 2016
@Tux
Copy link

Tux commented May 22, 2017

I also hit this problem. Could you turn that into a PR please so that doy can release?

@feinorgh
Copy link
Author

There's an active pull request already: #69

MichaelDaum added a commit to MichaelDaum/spreadsheet-parsexlsx that referenced this issue Jan 2, 2024
Cannot open password protected SHA1 encrypted files. doy#68
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants