我和zipfile
类有很奇怪的行为,我希望有人能帮我解决困扰我的问题。zipfile几个密码匹配
我写了尝试打开由密码(这是被WinRAR ziped)加密的压缩文件很短的剧本,但原来这zipfile
类不涨异常其他几个这是不正确的密码。
所以我对zip文件的密码是 '恶',但zFile.extractall
没涨execpction时的密码是的
- '结账' 之一,
- 'disannuller',
- 'euornithes' 或
- 'yamaltu'。
使用任何列出的密码zipfile
类中提取出后附加内容是不正确的。即使WinRar也不允许使用这些密码进行unziping。
我的Python代码如下:
import zipfile
diffrentPass = [
'wrongpass1',
'wrongpass2',
'checkouts',
'disannuller',
'euornithes',
'evil',
'yamaltu']
def extractFile(zFile, password):
try:
answer= zFile.extractall(pwd=password)
print 'Fount password : ', password
except:
pass
def main():
zFile = zipfile.ZipFile("evil.zip")
for password in diffrentPass:
extractFile(zFile, password)
if __name__ == '__main__':
main()
UPDATE:
我知道,我跳过异常,但请从程序看的出来:
wrongpass1 was incorrect
wrongpass2 was incorrect
Fount password : checkouts
Fount password : disannuller
Fount password : euornithes
Fount password : evil
Fount password : yamaltu
Process finished with exit code 0
线:
Fount password : checkouts
Fount password : disannuller
Fount password : euornithes
Fount password : yamaltu
不应该在所有
出现添加例如:
def extractFile(zFile, password):
try:
answer= zFile.extractall(pwd=password)
print 'Fount password : ', password
except Exception, e:
print password + " was incorrect"
没有什么改变输出
UPDATE +发生了什么
@Phil冰霜Explain what happened
是确定它实际上是我添加的问题点o使用一些调试打印来比较密码和文件中的check_byte。
输出示例:
#!! Wrong pass, check_byte are diffrent
# raised RuntimeError("Bad password for file", name)
Checking bytes for : wrongpass1
pass check_byte : 47
file check_byte 112
Pass is correct for zipfile class : False
#!! wrong password but for zipFile is ok , check_byte are the same
# but file will be the unpacked incorrectly
# RuntimeError("Bad password for file", name) will be not rise
Checking bytes for : checkouts
pass check_byte : 112
file check_byte 112
Pass is correct for zipfile class : True
Fount password : checkouts
#!! password ok
Checking bytes for : evil
pass check_byte : 112
file check_byte 112
Pass is correct for zipfile class : True
Fount password : evil
代码:
import zipfile, zlib, binascii, struct
from zipfile import _ZipDecrypter
diffrentPass = [
'wrongpass1',
'wrongpass2',
'checkouts',
'disannuller',
'euornithes',
'evil',
'yamaltu',
'wrongpass1',]
def extractFile(zFile, password, bytes):
print '\nChecking bytes for : ', password
zd = _ZipDecrypter(password)
h = map(zd, bytes[0:12])
print 'pass check_byte :', ord(h[11])
for item in zFile.infolist():
if item.flag_bits & 0x8:
check_byte = (item._raw_time >> 8) & 0xff
else:
check_byte = (item.CRC >> 24) & 0xff
print 'file check_byte ',check_byte
print "Pass is correct for zipfile class : " , ord(h[11]) == check_byte
try:
answer= zFile.extractall(pwd=password)
print 'Fount password : ', password
except Exception, e:
pass
def main():
# begining of ziped file must be cut off dummy method works ony on this specific zip file
# ....20111126036.jpg
bytes = open('evil.zip', 'rb').read(45+12)[-12:]
zFile = zipfile.ZipFile("evil.zip")
for password in diffrentPass:
extractFile(zFile, password,bytes)
if __name__ == '__main__':
main()
它通过调用通 –
确保为UKU写给使用(我认为这将是)除了NameError提高你只是忽略它的异常:然后做的东西,而不是传球。一个例子可能是:messagebox.showerror(“错误”,“错误的密码”)。 – Evilunclebill
但如果exepction在行回答惜售= zFile.extractall(PWD =密码)下一行不应该被执行... –