'Python Zipfile compression method is not supported
The times when this worked were when I used ZIPCrypto compression. It's with AES-256 that it fails. How to get around this please?
I previously had success using the following Python code to open a password protected .zip file created with 7-Zip:
import zipfile
zip_file = zipfile.ZipFile('crack_me.zip')
output_verbose = 1 # increase that for long password list
with open('passwords.txt', 'rb') as password_list:
for index, line in enumerate(password_list):
try:
pwd = line.strip(b'\r\n')
zip_file.extractall(pwd=pwd)
except RuntimeError as e:
print(e)
if index % output_verbose == 0:
print('{}. The {} word not matched.'.format(index + 1, pwd))
else:
print('{}. Wow ! found the password: {}'.format(index + 1, pwd))
break
zip_file.close()
However, for no understandable reason, it has only worked a couple of times out of many attempts. Most times is gives "That compression method is not supported" for the exception.
I've tried deleting, renaming, re-creating the .zip file but no success. It makes no sense to me that it works occasionally.
I tried to simplify the issue as below:
import zipfile
zip_file = zipfile.ZipFile('crack_me.zip')
try:
zip_file.extractall(pwd=b"password")
print('Opened')
except RuntimeError as e:
print(e)
But I get the same exception. I've tried variations of pwd
such as bytes("password", "utf-8) and others.
The provided password opens the .zip file when opening with 7-Zip.
What is going on here please?
Solution 1:[1]
Try using pyzipper
. Here is a sample script:
import pyzipper
password = 'abc'
with pyzipper.AESZipFile('yourdocument.zip', 'r', compression=pyzipper.ZIP_DEFLATED, encryption=pyzipper.WZ_AES) as extracted_zip:
extracted_zip.extractall(pwd=str.encode(password))
Solution 2:[2]
In order for the zipfile library to work on a password protected zip you need to have ticked the "Zip legacy encryption" option when setting up the password.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mike |
Solution 2 | Freddy Mcloughlan |