Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - the zipfile module doesn't seem to work with passwords

Tags:

python

I've been trying to implement a very simple script, extracting zip files that are password protected. I have created a simple zip file (test.zip) with the password "1234" containing 2 text files (1.txt, 2.txt) and i wrote this script:

import zipfile

PASSWORD = "1234"

zip = zipfile.ZipFile("test.zip", "r")
zip.setpassword(PASSWORD)
zip.extractall()
zip.close()

and i'm getting the following Runtime error:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    zip.extractall()
  File "/usr/lib/python2.7/zipfile.py", line 962, in extractall
    self.extract(zipinfo, path, pwd)
  File "/usr/lib/python2.7/zipfile.py", line 950, in extract
    return self._extract_member(member, path, pwd)
  File "/usr/lib/python2.7/zipfile.py", line 993, in _extract_member
    source = self.open(member, pwd=pwd)
  File "/usr/lib/python2.7/zipfile.py", line 934, in open
    raise RuntimeError("Bad password for file", name)
RuntimeError: ('Bad password for file', <zipfile.ZipInfo object at 0x1f3f2a8>)

I've tried iterating using "zip.namelist()" and the "extract()" method + specifying the exact parameters as follows:

zip.extract(<file_name>, path=<path>, pwd=<password>)

with no luck :( I know about the security issue with "extractall()" and in my complete code i will have verification prior to the extracting process, i'm just trying to figure out what am i doing wrong?

Thanks for the help in advance!

like image 417
Blaland Avatar asked Sep 20 '11 09:09

Blaland


1 Answers

As indicated in a comment it could be a problem with your encryption mode. Using 7-zip to create the zip file using AES-256 I get the same error as yours. With ZypCrypto encryption it works OK.

PyCrust 0.9.8 - The Flakiest Python Shell
Python 2.6.7 (r267:88850, Jun 27 2011, 13:20:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import zipfile
>>> psw = "abcd"

#with zipcrypto encryption

>>> path= "C:/Users/joaquin/Desktop/zipcrypto.zip"
>>> zip = zipfile.ZipFile(path, "r")
>>> zip.setpassword(psw)
>>> zip.extractall("C:/Python26")
>>> zip.close()

#with AES-256 encryption

>>> path= "C:/Users/joaquin/Desktop/aes256.zip"
>>> zip = zipfile.ZipFile(path, "r")
>>> zip.setpassword(psw)
>>> zip.extractall("C:/Python26")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Python26\lib\zipfile.py", line 938, in extractall
    self.extract(zipinfo, path, pwd)
  File "C:\Python26\lib\zipfile.py", line 926, in extract
    return self._extract_member(member, path, pwd)
  File "C:\Python26\lib\zipfile.py", line 969, in _extract_member
    source = self.open(member, pwd=pwd)
  File "C:\Python26\lib\zipfile.py", line 901, in open
    raise RuntimeError("Bad password for file", name)
RuntimeError: ('Bad password for file', <zipfile.ZipInfo object at 0x00000000042B3948>)
>>> 

This problem (zipfile only supporting traditional PKWARE encryption method) has been reported as a feature request for python 3.2

like image 89
joaquin Avatar answered Oct 25 '22 08:10

joaquin