我和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 =密碼)下一行不應該被執行... –