2016-08-24 8 views
2

私はPythonの初心者で、パスワードクラッカーを作成したことがあります。これはブルートフォース攻撃を使用しています。プログラムの実行中にプログレスバーを出力しようとしています。私がこれまで持っている:出力の進捗バーを使用する

import zipfile 
import sys 
import time 


def progress_bar(sleep_time): 
    for i in range(101): 
     time.sleep(sleep_time) 
     sys.stdout.write("\r[{0}] {1}%".format('#'*(i/10), i)) 
     sys.stdout.flush() 


def obtain_password(path_to_zip_file): 
    password = None 

    zip_file = zipfile.ZipFile(path_to_zip_file) 

    with open('wordlist.txt', 'r') as dict: 
     for line in dict.readlines(): 
      possible = line.strip("\n") 
      try: 
       zip_file.extractall(pwd=possible) 
       password = "Password found {}".format(possible) 
      except: 
        pass 

    return password 

は、だから私の質問はobtain_passwordメソッドが実行されている間、私は出力にプログレスバーを得ることができる方法ですか?私はプログレスバーの方法を少し変更する必要がありますか?

+1

をプログレスバーを更新してみましょう'obtain_password'関数用です。 http://www.tutorialspoint.com/python/python_multithreading.htmまた、 'obtain_password'関数がプログレスバーをしばらくの間ペイントするようにすることもできます。 – grael

+1

@graelそれは、この時点で私のために少し進んだようだ、私はそれが好きだが、ありがとう。 –

答えて

2

あなたがしようとしていることはうまくいかないので、スレッドが1つしかないことに注意してください。

あなたができることは、あなたのワードリストの行数を取得し、数学を行うことです。タイマーよりはるかに正確です。

また
import zipfile 
import sys 
import time 

def obtain_password(path_to_zip_file): 
    password = None 
    zip_file = zipfile.ZipFile(path_to_zip_file) 
    with open('wordlist.txt', 'r') as f: 
     lines = f.readlines() 
     total = len(lines) # get number of lines 
     current = 0 
     for line in lines: 
      current += 1 
      if current % 1000 == 0: # every 1000 lines, shows the progress 
       print('%.2f %%' % float(current/total * 100)) 
      possible = line.strip("\n") 
      try: 
       zip_file.extractall(pwd=possible) 
       #password = "Password found {}".format(possible) 
       print(possible) 
       sys.exit() 
      except: 
       pass 

は、私はあなたが、例外がextractallによって提起されているものを取得し、それらをキャッチお勧めします:

私はこれらの線に沿って何かをあなたが欲しいものがあるでしょうけれども、コードをテストしませんでした正しく。 そのようなものをすべてキャッチ:except:は良い習慣ではありません。

+0

偶然、スレッディングによってそれを行う例を教えてください。 –

+1

母、私ははいかもしれませんが、あなたは学ばず、あなたのハッキングスキルは上がらないでしょう。あなた自身が一発撃ってください。問題がある場合は、新しい質問を投稿してください:-) –

+0

ところで、プログレスバーにスレッドを使用するのは役に立たないです。 「unzip testing part」に時間がかかる場合は、スレッディングが面白いかもしれません。 –

2

あなたが望むことをする方法があります。

  1. あなたはプログレスバーと別の1のための1つのスレッドを持って試みることができるあなたのパスワードクラッカーはたまに

    import time 
    
    # Stores the time between updates in seconds. 
    time_between_updates = 10 
    last_update = 0 
    
    def your_expensive_operation(): 
        for i in range(10000000): 
         time.sleep(1)   # Emulate an expensive operation 
         if time.time() - last_update > time_between_updates: 
          print("\r" + (int(i/10000000.0 * 79) * "#"), end='') 
    
    your_expensive_operation() 
    
  2. スレッドを使用し

    import time 
    import threading 
    
    # Stores your current position in percent. 
    current_position = 0 
    done = False 
    
    def paint_thread(): 
        while not done: 
         print("\r" + (int(current_position * 79) * "#"), end='') 
         # Make it update once a second. 
         time.sleep(1) 
    
    thread = threading.Thread(target=paint_thread) 
    thread.start() 
    
    for i in range(10000000): 
        time.sleep(1)   # Emulate an expensive operation 
        current_position = i/10000000.0 
    
    done = True 
    
+0

私は本当にスレッドインスタンスを理解していません。何が起こっているのかご説明できますか? –

+1

このソーセージは何ですか? –

+0

@Loïcこれで私の一日が笑われた –

関連する問題