web-dev-qa-db-ja.com

マルチプロセスしようとするときに「TypeError: '_io.BufferedReader'オブジェクトをシリアル化できません」エラーを修正するにはどうすればよいですか

私のプログラムはパスワードで保護された.Zipファイルをブルートフォースすることを目的としているため、コードのスレッドをマルチプロセッシングに切り替えてパフォーマンスを測定し、ブルートフォースの可能性を高めようとしています。しかし、プログラムを実行しようとするたびに、次のようになります。

_BruteZIP2.py -z "Generic Zip.zip" -f  Worm.txt
Traceback (most recent call last):
  File "C:\Users\User\Documents\Jetbrains\PyCharm\BruteZIP\BruteZIP2.py", line 40, in <module>
    main(args.Zip, args.file)
  File "C:\Users\User\Documents\Jetbrains\PyCharm\BruteZIP\BruteZIP2.py", line 34, in main
    p.start()
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 112, in start
self._popen = self._Popen(self)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
reduction.dump(process_obj, to_child)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: cannot serialize '_io.BufferedReader' object
_

私と同じ問題のあるスレッドを見つけましたが、どちらも未回答/未解決でした。私はPoolp.start()の上に挿入してみました。これは、Windowsベースのマシンを使用しているためと思われますが、役に立ちませんでした。私のコードは次のとおりです:

_  import argparse
  from multiprocessing import Process
  import zipfile

  parser = argparse.ArgumentParser(description="Unzips a password protected .Zip by performing a brute-force attack using either a Word list, password list or a dictionary.", usage="BruteZIP.py -z Zip.zip -f file.txt")
  # Creates -z arg
  parser.add_argument("-z", "--Zip", metavar="", required=True, help="Location and the name of the .Zip file.")
  # Creates -f arg
  parser.add_argument("-f", "--file", metavar="", required=True, help="Location and the name of the Word list/password list/dictionary.")
  args = parser.parse_args()


  def extract_Zip(zip_file, password):
      try:
          Zip_file.extractall(pwd=password)
          print(f"[+] Password for the .Zip: {password.decode('utf-8')} \n")
      except:
          # If a password fails, it moves to the next password without notifying the user. If all passwords fail, it will print nothing in the command Prompt.
          print(f"Incorrect password: {password.decode('utf-8')}")
          # pass


  def main(Zip, file):
      if (Zip == None) | (file == None):
          # If the args are not used, it displays how to use them to the user.
          print(parser.usage)
          exit(0)
      Zip_file = zipfile.ZipFile(Zip)
      # Opens the Word list/password list/dictionary in "read binary" mode.
      txt_file = open(file, "rb")
      for line in txt_file:
          password = line.strip()
          p = Process(target=extract_Zip, args=(Zip_file, password))
          p.start()
          p.join()


  if __name__ == '__main__':
      # BruteZIP.py -z Zip.zip -f file.txt.
      main(args.Zip, args.file)
_

前に述べたように、これは主に私が現在Windowsベースのマシンを使用しているために起こっていると思います。 Linuxベースのマシンを使用している他の数人とコードを共有したところ、上記のコードを実行しても問題はありませんでした。

ここでの私の主な目標は、スレッド化と比較して実行される試行回数を最大化するために8つのプロセス/プールを開始することですが、_TypeError: cannot serialize '_io.BufferedReader' object_メッセージの修正を取得できないため、ここで何をすればよいかわからず、どうすれば修正できますか?どんな援助もいただければ幸いです。

3
Arszilla

ファイルハンドルがうまくシリアル化されません...しかし、Zipfilehandleの代わりにZipファイルのnameを送信できます(文字列はプロセス間で正常にシリアル化されます)。また、組み込みなので、ファイル名にZipを使用しないでください。 Zip_filenameを選択しました

p = Process(target=extract_Zip, args=(Zip_filename, password))

次に:

def extract_Zip(zip_filename, password):
      try:
          Zip_file = zipfile.ZipFile(Zip_filename)
          Zip_file.extractall(pwd=password)

もう1つの問題は、次の理由でコードが並列実行されないことです。

      p.start()
      p.join()

p.joinは、プロセスが完了するまで待機します...ほとんど役に立ちません。最後にjoinにプロセス識別子を保存する必要があります。

これにより、他の問題が発生する可能性があります。並行して作成するプロセスが多すぎる場合は、マシンの問題である可能性があり、しばらくするとあまり役に立ちません。代わりに multiprocessing.Pool を検討して、ワーカーの数を制限します。

簡単な例は次のとおりです。

with multiprocessing.Pool(5) as p:
    print(p.map(f, [1, 2, 3, 4, 5, 6, 7]))

あなたの例に適応:

with multiprocessing.Pool(5) as p:
    p.starmap(extract_Zip, [(Zip_filename,line.strip()) for line in txt_file])

starmapPython multiprocessing pool.map for multiple arguments )で説明されているように、タプルを2つの個別の引数として展開してextract_Zipメソッドに適合させます