Skip to content

Commit

Permalink
fall back to pyzipper for explicit passwords as well
Browse files Browse the repository at this point in the history
  • Loading branch information
huettenhain committed Nov 27, 2024
1 parent 4b061b1 commit 8ad1661
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions refinery/units/formats/archive/xtzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,42 @@ def _carver(cls):
return carve_zip

def unpack(self, data: bytearray):
from zipfile import ZipFile, ZipInfo
from zipfile import ZipInfo, ZipFile

def password_invalid(password: Optional[bytes]):
nonlocal archive, fallback
if password:
archive.setpassword(password)
try:
archive.testzip()
except NotImplementedError:
if fallback:
raise
self.log_debug('compression method unsupported, switching to pyzipper')
archive = self._pyzipper.AESZipFile(MemoryFile(data))
fallback = True
return password_invalid(password)
except RuntimeError as E:
if 'password' not in str(E):
raise
return True
else:
if password:
self.log_debug('using password:', password)
return False

password = bytes(self.args.pwd)
fallback = False
archive = ZipFile(MemoryFile(data))
passwords = [password]

if password:
archive.setpassword(password)
if not password:
passwords.extend(p.encode(self.codec) for p in self._COMMON_PASSWORDS)
for p in passwords:
if not password_invalid(p):
break
else:
def password_invalid(pwd: Optional[str], pyzipper=False):
nonlocal archive
if pwd is not None:
archive.setpassword(pwd.encode(self.codec))
try:
archive.testzip()
except NotImplementedError:
if pyzipper:
raise
self.log_debug('compression method unsupported, switching to pyzipper')
archive = self._pyzipper.AESZipFile(MemoryFile(data))
return password_invalid(pwd, True)
except RuntimeError as E:
if 'password' not in str(E):
raise
return True
else:
if pwd:
self.log_debug('using password:', pwd)
return False
for pwd in [None, *self._COMMON_PASSWORDS]:
if not password_invalid(pwd):
break
else:
raise RuntimeError('Archive is password-protected.')
raise RuntimeError('Archive is password-protected.')

for info in archive.infolist():
def xt(archive: ZipFile = archive, info: ZipInfo = info):
Expand Down

0 comments on commit 8ad1661

Please sign in to comment.