Repository: Gallopsled/pwntools

Issue:
muitiple thread bryteforce have bugs on Macos 10.15.5 with python3.8.5
##  summary
system: Macos 10.15
python:3.8.5

in this util 
util.iters.mbruteforce(f, string.ascii_letters + string.digits, 3, 'fixed')

works well with python 3.7.4 both on ubuntu and macos
works well on Ubuntu with python 3.8.5,but have bugs on Macos with python 3.8.5 . 

## Error Output
```
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 263, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/fjh1997/Desktop/bank.py", line 36, in <module>
    prefix = proof_of_work()
  File "/Users/fjh1997/Desktop/bank.py", line 31, in proof_of_work
    prefix = util.iters.mbruteforce(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pwnlib/util/iters.py", line 891, in mbruteforce
    shareddata[i] = multiprocessing.Manager().dict()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 57, in Manager
    m.start()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/managers.py", line 579, in start
    self._process.start()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 283, in _Popen
    return Popen(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "bank.py", line 36, in <module>
    prefix = proof_of_work()
  File "bank.py", line 31, in proof_of_work
    prefix = util.iters.mbruteforce(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pwnlib/util/iters.py", line 891, in mbruteforce
    shareddata[i] = multiprocessing.Manager().dict()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 57, in Manager
    m.start()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/managers.py", line 583, in start
    self._address = reader.recv()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/connection.py", line 250, in recv
    buf = self._recv_bytes()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/connection.py", line 414, in _recv_bytes
    buf = self._recv(4)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/connection.py", line 383, in _recv
    raise EOFError
EOFError
```
and then I try to put code under  ```if __name__ == '__main__:'```
the error become this
```
Traceback (most recent call last):
  File "bc1.py", line 40, in <module>
    prefix = proof_of_work()
  File "bc1.py", line 35, in proof_of_work
    prefix = util.iters.mbruteforce(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pwnlib/util/iters.py", line 902, in mbruteforce
    processes[i].start()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 283, in _Popen
    return Popen(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'mbruteforce.<locals>.bruteforcewrap'
```


Discussion:
same problem on MacOS 10.15.6 with python3.8

```python
[<] MBruteforcing
Traceback (most recent call last):
  File "exp.py", line 34, in <module>
    main()
  File "exp.py", line 31, in main
    proof_of_work(suffix, _hexdigest)
  File "exp.py", line 9, in proof_of_work
    prefix = mbruteforce(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pwnlib/util/iters.py", line 902, in mbruteforce
    processes[i].start()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 283, in _Popen
    return Popen(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'mbruteforce.<locals>.bruteforcewrap'
```

Resolving patch:
diff --git a/pwnlib/util/iters.py b/pwnlib/util/iters.py
--- a/pwnlib/util/iters.py
+++ b/pwnlib/util/iters.py
@@ -843,6 +843,13 @@ def bruteforce(func, alphabet, length, method = 'upto', start = None, databag =
     h.failure('No matches found')
 
 
+def _mbruteforcewrap(func, alphabet, length, method, start, databag):
+    oldloglevel = context.log_level
+    context.log_level = 'critical'
+    res = bruteforce(func, alphabet, length, method=method, start=start, databag=databag)
+    context.log_level = oldloglevel
+    databag["result"] = res
+
 
 def mbruteforce(func, alphabet, length, method = 'upto', start = None, threads = None):
     """mbruteforce(func, alphabet, length, method = 'upto', start = None, threads = None)
@@ -864,13 +871,6 @@ def mbruteforce(func, alphabet, length, method = 'upto', start = None, threads =
       '9999'
     """
 
-    def bruteforcewrap(func, alphabet, length, method, start, databag):
-        oldloglevel = context.log_level
-        context.log_level = 'critical'
-        res = bruteforce(func, alphabet, length, method=method, start=start, databag=databag)
-        context.log_level = oldloglevel
-        databag["result"] = res
-
     if start is None:
         start = (1, 1)
 
@@ -896,7 +896,7 @@ def bruteforcewrap(func, alphabet, length, method, start, databag):
 
         chunkid = (i2-1) + (i * N2) + 1
 
-        processes[i] = multiprocessing.Process(target=bruteforcewrap,
+        processes[i] = multiprocessing.Process(target=_mbruteforcewrap,
                 args=(func, alphabet, length, method, (chunkid, totalchunks),
                         shareddata[i]))
         processes[i].start()


Test patch: