2

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111

2 Answers2

6

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))
Mike
  • 1,080
  • 1
  • 9
  • 25
Thomas Vélez
  • 61
  • 1
  • 2
4

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.

Picture

Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29