导入 pwn 包时,pyinstaller 不转换(或生成损坏的 exe)

pyinstaller isn't converting (or generate a corrupted exe) when pwn package is imported

我正在反向 shell 项目中使用 pwn 作为 Client/Server 而不是使用套接字,因为它更容易接收 (recvuntil ) 消息,但是当我尝试使用 pyinstaller 将其转换为 EXEonefile 时,它最终失败了,生成了一个损坏的 exe 或生成了一个不断出错的 exe,我什至有首先在控制台上测试这个程序。

那么有没有办法让它工作,甚至让我自己 pyinstaller(如果我必须)?

我想不出比这个更好的标题了,所以如果你有更好的,请评论它

my venv conda python version is 3.9.7

这里有一点Server代码(这只是为了测试):

import threading
from pwn import *
def tryThreading():
    l=listen(5005)
    client=l.wait_for_connection()
    def tryRecv():
        while True:
            if client.can_recv():
                print('\n-Bob: '+client.recv().decode())
    recv=threading.Thread(target=tryRecv)
    recv.start()
    while True:
        client.send(input('-Sam: ').encode())
tryThreading()

下面是对上述程序的测试运行:

(pyins) C:\Users\YCS\Desktop>python tryPwn.py
[x] Trying to bind to :: on port 5005
[x] Trying to bind to :: on port 5005: Trying ::
[+] Trying to bind to :: on port 5005: Done
[x] Waiting for connections on :::5005
[+] Waiting for connections on :::5005: Got connection from ::1 on port 60340
-Sam: Hello
-Sam:
-Bob: Hello World

这里是 pyinstaller 输出:

(pyins) C:\Users\YCS\Desktop>pyinstaller --onefile --noconfirm tryPwn.py
344 INFO: PyInstaller: 4.5.1
344 INFO: Python: 3.9.7 (conda)
712 INFO: Platform: Windows-10-10.0.19042-SP0
714 INFO: wrote C:\Users\YCS\Desktop\tryPwn.spec
720 INFO: UPX is not available.
728 INFO: Extending PYTHONPATH with paths
['C:\Users\YCS\Desktop', 'C:\Users\YCS\Desktop']
1044 INFO: checking Analysis
1044 INFO: Building Analysis because Analysis-00.toc is non existent
1045 INFO: Initializing module dependency graph...
1049 INFO: Caching module graph hooks...
1084 INFO: Analyzing base_library.zip ...
4147 INFO: Processing pre-find module path hook distutils from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
4150 INFO: distutils: retargeting to non-venv dir 'C:\ProgramData\Anaconda3\envs\pyins\lib'
6770 INFO: Caching module dependency graph...
7030 INFO: running Analysis Analysis-00.toc
7051 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\ProgramData\Anaconda3\envs\pyins\python.exe
7126 WARNING: lib not found: api-ms-win-core-path-l1-1-0.dll dependency of C:\ProgramData\Anaconda3\envs\pyins\python39.dll
7350 INFO: Analyzing C:\Users\YCS\Desktop\tryPwn.py
7569 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-urllib3.packages.six.moves.py'.
9896 INFO: Processing pre-find module path hook site from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-site.py'.
9898 INFO: site: retargeting to fake-dir 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\fake-modules'
16082 INFO: Processing pre-safe import module hook six.moves from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-six.moves.py'.
20863 INFO: Processing module hooks...
20863 INFO: Loading module hook 'hook-bcrypt.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
20865 INFO: Loading module hook 'hook-certifi.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
20874 INFO: Loading module hook 'hook-cryptography.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
21219 INFO: Loading module hook 'hook-mako.codegen.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
21222 INFO: Loading module hook 'hook-nacl.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
21256 INFO: Loading module hook 'hook-pycparser.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
21258 INFO: Loading module hook 'hook-pywintypes.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
47967 INFO: Loading module hook 'hook-difflib.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
47973 INFO: Loading module hook 'hook-distutils.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
47975 INFO: Loading module hook 'hook-distutils.util.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
47981 INFO: Loading module hook 'hook-encodings.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48088 INFO: Loading module hook 'hook-heapq.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48093 INFO: Loading module hook 'hook-lib2to3.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48158 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48164 INFO: Loading module hook 'hook-numpy.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48277 WARNING: Conda distribution 'numpy', dependency of 'numpy', was not found. If you installed this distribution with pip then you may ignore this warning.
48279 INFO: Import to be excluded not found: 'f2py'
48318 INFO: Loading module hook 'hook-numpy._pytesttester.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48322 INFO: Loading module hook 'hook-pickle.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48327 INFO: Loading module hook 'hook-PIL.Image.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48670 INFO: Loading module hook 'hook-PIL.ImageFilter.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48676 INFO: Loading module hook 'hook-PIL.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48697 INFO: Loading module hook 'hook-PIL.SpiderImagePlugin.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
48702 INFO: Loading module hook 'hook-pkg_resources.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
49362 INFO: Processing pre-safe import module hook win32com from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\pre_safe_import_module\hook-win32com.py'.
49452 WARNING: Hidden import "pkg_resources.py2_warn" not found!
49751 WARNING: Hidden import "pkg_resources.markers" not found!
49757 INFO: Loading module hook 'hook-pygments.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
51370 INFO: Loading module hook 'hook-setuptools.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
65132 INFO: Loading module hook 'hook-sysconfig.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
65135 INFO: Loading module hook 'hook-win32ctypes.core.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
65331 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
65333 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
65334 INFO: Loading module hook 'hook-xml.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
65335 INFO: Loading module hook 'hook-_tkinter.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
65502 INFO: checking Tree
65502 INFO: Building Tree because Tree-00.toc is non existent
65502 INFO: Building Tree Tree-00.toc
65661 INFO: checking Tree
65662 INFO: Building Tree because Tree-01.toc is non existent
65662 INFO: Building Tree Tree-01.toc
65769 INFO: checking Tree
65769 INFO: Building Tree because Tree-02.toc is non existent
65769 INFO: Building Tree Tree-02.toc
65780 INFO: Loading module hook 'hook-pythoncom.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
91530 INFO: Loading module hook 'hook-win32com.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
92036 INFO: Loading module hook 'hook-packaging.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
92039 INFO: Loading module hook 'hook-setuptools.msvc.py' from 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks'...
92118 INFO: Looking for ctypes DLLs
92237 INFO: Analyzing run-time hooks ...
92249 INFO: Including run-time hook 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgutil.py'
92253 INFO: Including run-time hook 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_multiprocessing.py'
92258 INFO: Including run-time hook 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_inspect.py'
92264 INFO: Including run-time hook 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgres.py'
92271 INFO: Including run-time hook 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_win32api.py'
92273 INFO: Including run-time hook 'C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\_pyinstaller_hooks_contrib\hooks\rthooks\pyi_rth_certifi.py'
92300 INFO: Looking for dynamic libraries
92819 INFO: Looking for eggs
92819 INFO: Using Python library C:\ProgramData\Anaconda3\envs\pyins\python39.dll
92820 INFO: Found binding redirects:
[]
92834 INFO: Warnings written to C:\Users\YCS\Desktop\build\tryPwn\warn-tryPwn.txt
93034 INFO: Graph cross-reference written to C:\Users\YCS\Desktop\build\tryPwn\xref-tryPwn.html
93088 INFO: checking PYZ
93088 INFO: Building PYZ because PYZ-00.toc is non existent
93089 INFO: Building PYZ (ZlibArchive) C:\Users\YCS\Desktop\build\tryPwn\PYZ-00.pyz
95011 INFO: Building PYZ (ZlibArchive) C:\Users\YCS\Desktop\build\tryPwn\PYZ-00.pyz completed successfully.
95064 INFO: checking PKG
95065 INFO: Building PKG because PKG-00.toc is non existent
95065 INFO: Building PKG (CArchive) PKG-00.pkg
103176 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
103182 INFO: Bootloader C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
103182 INFO: checking EXE
103183 INFO: Building EXE because EXE-00.toc is non existent
103183 INFO: Building EXE from EXE-00.toc
103187 INFO: Copying icons from ['C:\ProgramData\Anaconda3\envs\pyins\lib\site-packages\PyInstaller\bootloader\images\icon-console.ico']
103341 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
103342 INFO: Writing RT_ICON 1 resource with 3752 bytes
103342 INFO: Writing RT_ICON 2 resource with 2216 bytes
103342 INFO: Writing RT_ICON 3 resource with 1384 bytes
103342 INFO: Writing RT_ICON 4 resource with 37019 bytes
103342 INFO: Writing RT_ICON 5 resource with 9640 bytes
103343 INFO: Writing RT_ICON 6 resource with 4264 bytes
103343 INFO: Writing RT_ICON 7 resource with 1128 bytes
103347 INFO: Updating manifest in C:\Users\YCS\Desktop\build\tryPwn\run.exe.33xsdso9
103420 INFO: Updating resource type 24 name 1 language 0
103423 INFO: Appending archive to EXE C:\Users\YCS\Desktop\dist\tryPwn.exe
111875 INFO: Building EXE from EXE-00.toc completed successfully.

这是tryPwn(目标程序)输出:

(pyins) C:\Users\YCS\Desktop\dist>tryPwn.exe
Traceback (most recent call last):
  File "tryPwn.py", line 2, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "pwn\__init__.py", line 4, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "pwn\toplevel.py", line 24, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "pwnlib\asm.py", line 58, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "pwnlib\shellcraft\__init__.py", line 171, in <module>
  File "pwnlib\shellcraft\__init__.py", line 37, in __init__
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\YCS\AppData\Local\Temp\_MEI10682\pwnlib\shellcraft\templates\__doc__'
[13272] Failed to execute script 'tryPwn' due to unhandled exception!

有关发生的事情的更多信息HERE

经过三天的调试,我可以看出解决方案有两个步骤:

首先: 尝试通过先删除 pwntools 软件包然后再安装来重新安装它

pip uninstall pwn
pip uninstall pwntools
pip uninstall pwnlib
pip install pwn
pip install pwntools

其次: 在你得到你的 spec 文件后打开它并添加这些行:

from PyInstaller.utils.hooks import collect_all
    
datas=collect_all('pwn')[0]
datas+=collect_all('pwnlib')[0]

并将空列表([])改成datas里面的分析:

a = Analysis(['tryPwn.py'],
         pathex=['C:\Users\YCS\Desktop'],
         binaries=[],
         datas=datas,
         hiddenimports=[],
         hookspath=[],
         hooksconfig={},
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher,
         noarchive=False)

然后使用您的选项和 spec 文件转换它:

pyinstaller --onefile --noconfirm tryPwn.spec

享受