2012-12-30 54 views
0

我和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() 
+3

它通過調用通 –

+0

確保爲UKU寫給使用(我認爲這將是)除了NameError提高你只是忽略它的異常:然後做的東西,而不是傳球。一個例子可能是:messagebox.showerror(「錯誤」,「錯誤的密碼」)。 – Evilunclebill

+0

但如果exepction在行回答惜售= zFile.extractall(PWD =密碼)下一行不應該被執行... –

回答

1

我猜這個計劃的目標是暴力破解密碼了一個zip文件。我猜你已經嘗試了更長的密碼列表,只有少數列出的問題沒有引發異常,而你有更多的行爲像預期的那樣。

在我看來,隨着zipfile的實施,你有一個接受密碼的概率爲1/255,因此不會引發異常。這是因爲密碼是通過檢查一個字節的校驗和驗證:

if ord(h[11]) != check_byte: 
    raise RuntimeError("Bad password for file", name) 

full source

編輯:經過進一步的研究,這可能是a duplicate question

據我所知,壓縮加密薄弱,通過比蠻力等手段容易破碎。然而,我從來沒有理由自己去做,我也不知道你想要完成什麼的細節,所以我不能提出更具體的解決方案。

+0

你是對的,我做了更多的教育目的比具體使用 –

+0

我寫了一些代碼,檢查你已經解釋(更新的第一篇文章),是的檢查通過zipFile類的方式是不可靠的,謝謝你的幫幫我! –

3

將引發異常,但你「吞嚥」,因爲你不顯示它的任何錯誤,只是忽略它稱呼「通過」。

此外,您的代碼還有另一個問題,主模塊部分縮進不正確。 在當前的代碼在main()不會被調用,因爲調用主模塊是你忽略了在代碼中異常的main()的

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: 
     print password + " was incorrect" 


def main(): 
    zFile = zipfile.ZipFile("evil.zip") 

    for password in diffrentPass: 
     extractFile(zFile, password) 

if __name__ == '__main__': 
    main() 
+0

...我知道了這個代碼... wrongpass1是不正確的 wrongpass2是不正確的潤版液 密碼:檢出 潤版液密碼:disannuller 潤版液密碼:euornithes 潤版液密碼:邪惡 潤版液密碼:yamaltu 進程退出代碼爲0完成工作 –

+0

罰款除了這個事實,它應該實際上停止時,它已經找到了正確的:) –

+0

@UkuLoskit當然你可以停下來找到一個匹配的密碼,但是不管怎樣,它不應該開始報告不正確的密碼。 –

0

定義的一部分。如果你想讓它引發異常,則:

def extractFile(zFile, password): 
    try: 
     answer= zFile.extractall(pwd=password) 
     print 'Fount password : ', password 
    except Exception, e: 
     # do something here before raising exception 
     raise e 

或者根本就不使用tryexcept塊。

評論後編輯

如果你想向大家報告,密碼是錯誤的,那麼你需要做的是,在except塊,因爲當執行zFile.extractall(pwd=password)異常已經拋出了錯誤的密碼,所以print會從未執行過。

def extractFile(zFile, password): 
    try: 
     answer= zFile.extractall(pwd=password) 
     print 'Password "%s" is correct' % password 
    except Exception, e: 
     print 'Password "%s" is incorrect' % password 
     pass 
+0

當最初拋出excetion時,'print'不會執行。不報告*期望*錯誤有什麼問題? –

+0

@ Anony-Mousse請檢查最新的答案。謝謝 –

+0

你最後除了有什麼問題。在你的例子中除了密碼是inccorect時將打印'fount密碼'? – Evilunclebill

0

它看起來像你明確放棄可能被提出,這裏的任何異常:

try: 
    answer= zFile.extractall(pwd=password) 
    print 'Fount password : ', password 
except: 
    pass 

如果zFile.extractall()拋出一個異常,則執行except:塊。您的除外塊包含pass,這在Python中意味着「什麼都不做」或「無操作」。

+0

的'print'當EXCETION最初拋出將不會執行。不報告*期望*錯誤有什麼問題? –

+0

@ Anony-Mousse好點。我現在意識到代碼的意圖必須是嘗試許多密碼並打印出正確的密碼。我會建議以不同的方式來構建代碼,以便清楚地表明這一點,但這會導致無關緊要的問題。 –

0
try: 
    zFile.extractall(pwd=password) 
    print 'Fount password : ', password 
except: 
    pass 

我刪除了「答案=」,它沒有任何意義。 另外如果你想出路的異常處理程序循環,你可以這樣做:

def zipExit(password): 
    print 'Password found: '+password 
    exit(0) 

def extractFile(zFile, password) 
    try:  
     zFile.extractall(pwd=password) 
     zipExit(password) 
    except: 
     pass 

def main(): 
    ....the rest of your code here. 

而對於一個密碼破解(如果你使用Linux,但我不asuming因爲你使用WinRAR)你不想在屏幕上輸出失敗的嘗試,這就是爲什麼異常處理程序通過而不是打印某些東西。 因爲您可以將程序指向輸出到文件。

〜/蟒蛇zipcrack.py> cracked.password

這樣的事情。