一、用Pyinstaller打包python代碼 1. 安裝Pyinstaller 安裝過程非常簡單,,在命令行中運行: pip install pyinstaller
# main.py#import mylibimport osif __name__ == '__main__': mylib.myfunc() os.system('pause') 這時只需要在命令行中運行:
即可,。會看到一下輸出: PS D:\文檔\tmp\test> pyinstaller.exe -F main.py580 INFO: PyInstaller: 3.6582 INFO: Python: 3.7.3585 INFO: Platform: Windows-10-10.0.18362-SP0592 INFO: wrote D:\文檔\tmp\test\main.spec596 INFO: UPX is not available.611 INFO: Extending PYTHONPATH with paths['D:\\文檔\\tmp\\test', 'D:\\文檔\\tmp\\test']612 INFO: checking Analysis614 INFO: Building Analysis because Analysis-00.toc is non existent614 INFO: Initializing module dependency graph...620 INFO: Caching module graph hooks...657 INFO: Analyzing base_library.zip ...13893 INFO: Caching module dependency graph...14161 INFO: running Analysis Analysis-00.toc14233 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by d:\programfiles\python\python.exe15748 INFO: Analyzing D:\文檔\tmp\test\main.py15751 INFO: Processing module hooks...15752 INFO: Loading module hook 'hook-encodings.py'...16003 INFO: Loading module hook 'hook-pydoc.py'...16011 INFO: Loading module hook 'hook-xml.py'...16916 INFO: Looking for ctypes DLLs16917 INFO: Analyzing run-time hooks ...16925 INFO: Looking for dynamic libraries17373 INFO: Looking for eggs17374 INFO: Using Python library d:\programfiles\python\python37.dll17374 INFO: Found binding redirects:[]17377 INFO: Warnings written to D:\文檔\tmp\test\build\main\warn-main.txt17447 INFO: Graph cross-reference written to D:\文檔\tmp\test\build\main\xref-main.html17506 INFO: checking PYZ17507 INFO: Building PYZ because PYZ-00.toc is non existent17508 INFO: Building PYZ (ZlibArchive) D:\文檔\tmp\test\build\main\PYZ-00.pyz18600 INFO: Building PYZ (ZlibArchive) D:\文檔\tmp\test\build\main\PYZ-00.pyz completed successfully.18637 INFO: checking PKG18639 INFO: Building PKG because PKG-00.toc is non existent18640 INFO: Building PKG (CArchive) PKG-00.pkg22329 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.22332 INFO: Bootloader d:\programfiles\python\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe22334 INFO: checking EXE22335 INFO: Building EXE because EXE-00.toc is non existent22336 INFO: Building EXE from EXE-00.toc22416 INFO: Appending archive to EXE D:\文檔\tmp\test\dist\main.exe22641 INFO: Building EXE from EXE-00.toc completed successfully. 你將在當前文件夾下看到如下文件:
其中dist文件夾中就是生成的exe文件,。直接雙擊exe文件就能正常運行了。 但這篇文章的重點并不是介紹如何使用pyinstaller,。此時,,我又在思考,如此簡單的打包過程究竟安全嗎,?打包成的exe文件會不會輕而易舉地被反編譯?查閱了相關資料后發(fā)現(xiàn),,確實可能,。 二、pyinstaller 的反編譯過程 1. 下載并使用pyinstxtractor解包 pyinstxtractor.py ,,可以將pyinstaller 生成的exe文件解包成pyc文件,。https:///projects/pyinstallerextractor/ python pyinstxtractor.py xx.exe xx.exe_extracted 文件夾 ,,里面有一堆dll ,pyd等文件,,我們需要注意的是里面有一個xxx.exe.manifest 文件,xxx可能與你的exe文件名不同,,但這才是它的真實名字,。.py 文件對應的pyc文件,。PYZ-00.pyz_extracted 文件夾,,里面都是引入的依賴庫,當然,我們自己寫的mylib.py 也在其中,,它也是我們反編譯的對象,。2. 反編譯pyc文件 http://tools./decompyle/ 但直接將我們找到的pyc文件上傳會發(fā)現(xiàn)無法反編譯。main.pyc 的區(qū)別,,左邊是我們解包出來的,右邊是運行生成的,。xx.exe_extracted 文件夾里找一找,。會發(fā)現(xiàn)有一個叫struct的文件,,我們給他加上后綴.pyc 反編譯試試。發(fā)現(xiàn)成功反編譯出如下內容:main.py 中的內容被成功反編譯出來了,。mylib.py 等依賴庫中的內容,不過值得注意的是,,網(wǎng)上很多教程都沒有提到依賴庫的pyc文件缺少的字節(jié)數(shù)與主程序的不同,!mylib.pyc 反編譯內容反編譯成功,!三,、使用pyinstaller加密打包exe 其實只要在打包時加個key參數(shù)就能加密
1. 安裝pycrypto包 原本安裝過程應該很簡單,通過pip就能安裝,。 pip install pycrypto 不過安裝過程好像要調用VS編譯器編譯,,這就造成了莫名其妙的問題,如果你在安裝過程中沒有報錯,,那么恭喜你,,你可以跳過這部分了。我在網(wǎng)上找了很多解決方法都沒效,,最后終于在StackOverflow上找到了一篇回答,,完美解決了這個問題。 原答案地址: 以我的vs2015為例 1,、在開始菜單中找到VS文件夾,用管理員身份運行這個”兼容工具命令提示符“ 2,、在你的VS安裝目錄下找到 3,、輸入 4,、然后再執(zhí)行 2. 使用pyinstaller加密打包 現(xiàn)在執(zhí)行如下命令就能加密打包了。key后面為密鑰可以隨便輸,。
3. 反編譯測試 那么我們再來測試一下加密打包的exe還能不能被反編譯,。再次執(zhí)行 PS > python pyinstxtractor.py .\main-encrypt.exe import imp[*] Processing .\main-encrypt.exe[*] Pyinstaller version: 2.1+[*] Python version: 37[*] Length of package: 5787283 bytes[*] Found 63 files in CArchive[*] Beginning extraction...please standby[+] Possible entry point: pyiboot01_bootstrap[+] Possible entry point: main[*] Found 136 files in PYZ archive[!] Error: Failed to decompress Crypto, probably encrypted. Extracting as is.[!] Error: Failed to decompress Crypto.Cipher, probably encrypted. Extracting as is.[!] Error: Failed to decompress __future__, probably encrypted. Extracting as is.[!] Error: Failed to decompress _compat_pickle, probably encrypted. Extracting as is.[!] Error: Failed to decompress argparse, probably encrypted. Extracting as is.[!] Error: Failed to decompress ast, probably encrypted. Extracting as is.[!] Error: Failed to decompress base64, probably encrypted. Extracting as is.[!] Error: Failed to decompress bdb, probably encrypted. Extracting as is.[!] Error: Failed to decompress bisect, probably encrypted. Extracting as is.[!] Error: Failed to decompress bz2, probably encrypted. Extracting as is.[!] Error: Failed to decompress calendar, probably encrypted. Extracting as is.[!] Error: Failed to decompress cmd, probably encrypted. Extracting as is.[!] Error: Failed to decompress code, probably encrypted. Extracting as is.[!] Error: Failed to decompress codeop, probably encrypted. Extracting as is. 這次下面輸出了一長串Error,看來確實是被加密了,。我們再來看一看文件夾,。
看來這個加密只針對依賴庫。 四,、總結 如果你不希望別人得到你的源碼,,建議將你程序的入口函數(shù)寫在一個單獨的文件里,并采用加密方式打包exe,。這樣的話,,就算別人嘗試反編譯也只能得到你的入口函數(shù)。 原文地址:https://blog./archives/155 作者:江風引雨 |
|
來自: 我本無我O > 《Python 知識》