用python打开密码保护的zip文件

时间:2018-03-01 12:05:04

标签: python brute-force password-recovery

我正在尝试打开一个受保护的zip文件,我知道前5个字符是Super,密码是8个字符长,没有数字或符号我在python中使用此代码来帮助我,但它不是工作可以有人帮忙吗?

代码:

import zipfile
import itertools
import time

# Function for extracting zip files to test if the password works!
def extractFile(zip_file, password):
try:
    zip_file.extractall(pwd=password)
    return True
except KeyboardInterrupt:
    exit(0)
except Exception as e:
    pass

# The file name of the zip file.
zipfilename = 'planz.zip'
# The first part of the password.
first_half_password = 'Super'
# We don't know what characters they add afterwards...
alphabet = 'abcdefghijklmnopqrstuvwxyz'
zip_file = zipfile.ZipFile(zipfilename)

# For every possible combination of 3 letters from alphabet...
for c in itertools.product(alphabet, repeat=3):
   # Add the three letters to the first half of the password.
   password = first_half_password+''.join(c)
   # Try to extract the file.
   print("Trying: %s" % password)
   # If the file was extracted, you found the right password.

   if extractFile(zip_file, password):
       print('*' * 20)
       print('Password found: %s' % password)
       print('Files extracted...')
       exit(0)

# If no password was found by the end, let us know!
print('Password not found.')

2 个答案:

答案 0 :(得分:0)

Hy man!基本上,您可以添加字母变量以包含大写字母,密码是超人的播放,如果我没记错的话

答案 1 :(得分:0)

问题是,那 如果extractFile(zip_file,密码): 在许多情况下,错误密码也是如此。 (参见:https://bugs.python.org/issue18134)然后它会留下一个" unziiped文件"长度为0或某些字节。

您必须检查输出文件的大小是否合适。

例如通过找出zip中第一个文件的大小

zip_file = zipfile.ZipFile(zipfilename)

firstmember=zip_file.namelist()[0]
firstmembersize=zip_file.getinfo(firstmember).file_size

以后

if os.path.getsize(firstmember) == firstmembersize:

并且不要忘记在检查后为下一次尝试让路时删除错误大小的文件...