2017-09-03 88 views
0

我一直在參加道德黑客課程。本課程的一部分內容是創建一個Python腳本,用於從密碼列表文本文件中找到鎖定zip文件的密碼(希望這很有意義!) - 基本上是通過嘗試每個密碼的文本文件進行迭代。該腳本不起作用,不會出錯,並且講師說「好吧,它適用於我」 - 沒用。這裏的腳本:道德黑客腳本不工作 - 沒有錯誤信息

import optparse 
import zipfile 
from threading import Thread 

def extract_zip(zfile, password): 
    try: 
     zfile.extractall(pwd=password) 
     print("[+] Password Found: " + password + '\n') 
    except: 
     pass 

def main(): 
    parser = optparse.OptionParser("usage %prog "+\ 
            "-f <zipfile> -d <dictionary>") 
    parser.add_option('-f', dest='zname', type='string',\ 
         help='specify zip file') 
    parser.add_option('-d', dest='dname', type='string',\ 
         help='specify dictionary file') 
    (options, arg) = parser.parse_args() 
    if (options.zname == None) | (options.dname == None): 
     print(parser.usage) 
     exit(0) 
    else: 
     zname = options.zname 
     dname = options.dname 

    zFile = zipfile.ZipFile(zname) 
    passFile = open(dname) 

    for line in passFile.readlines(): 
     password = line.strip('\n') 
     t = Thread(target=extract_zip, args=(zFile, password)) 
     t.start() 

if __name__ == '__main__': 
    main() 

其他兩個文件與密碼列表的文本文件,密碼保護的zip文件,其中從文本文件密碼的人會解鎖。

在課程中有一個線程提到optparse是depracated的,argparse是它的替代品 - 但是即使用這種方法重寫腳本也行不通。

對於想要關閉這部分課程,我正在尋找幫助,爲什麼這不起作用。

在此先感謝您的幫助。

+0

您是否在負責提取密碼的if條件下調試了您的代碼調試,並發現您可能在讀取文件時發生錯誤。 –

+0

我不知道該怎麼做 - 但。我嘗試過使用調試器,但現在超出了我的知識範圍。看代碼,是(options,arg)的問題? – Steve

+1

安裝pycharm並轉到YouTube,以便在Python中進行調試。 –

回答

-1

我運行使用python3它excuted沒有問題我這樣做你的代碼前不久 它是一個叫猛烈一些書

的password.txt應該包含這一行

受害者:HX9LLTdc /基德:503 :100:IAMA受害者:/家庭/受害者:/ bin/sh的 根:DFNFxgW7C05fo:504:100:馬庫斯·赫斯:/根目錄:/斌/慶典

和命令應該是 蟒蛇stack.py - f evil.zip -d passwords.txt

0

每上面我的意見 - 我添加的代碼下面正下方的 「嘗試」 的聲明:

password = bytes(password.encode('utf-8'))

...再變

print('[+] Password Found: ' + password + '\n')

print("[+] Password Found: " + (password.decode("utf-8")) + '\n')

現在我得到密碼打印到控制檯,並解壓縮zip文件。這是最後的工作代碼。

import optparse 
import zipfile 
from threading import Thread 


def extract_zip(zfile, password): 
    try: 
     password = bytes(password.encode('utf-8')) 
     zfile.extractall(pwd=password) 
     print("[+] Password Found: " + (password.decode("utf-8")) + '\n') 
    except: 
     pass 


def main(): 
    parser = optparse.OptionParser("usage %prog " + '-f <zipfile> -d <dictionary>') 
    parser.add_option('-f', dest='zname', type='string', help='specify zip file') 
    parser.add_option('-d', dest='dname', type='string', help='specify dictionary file') 
    (options, args) = parser.parse_args() 
    if (options.zname is None) | (options.dname is None): 
     print(parser.usage) 
     exit(0) 
    else: 
     zname = options.zname 
     dname = options.dname 

    zFile = zipfile.ZipFile(zname) 
    passFile = open(dname) 

    for line in passFile.readlines(): 
     password = line.strip('\n') 
     t = Thread(target=extract_zip, args=(zFile, password)) 
     t.start() 


if __name__ == '__main__': 
    main() 

,我發現這個問題的方法是通過改變「除了」語句打印例外控制檯:

except Exception as e: print(e) 

從那裏我有幾個問題需要解決,但至少我有錯誤工作。一旦密碼成功記錄到控制檯,我將懷疑語句更改回「通過」 - 不需要查看失敗的密碼!

我希望這可以幫助別人遇到同樣的問題。