python打包和反编译一条龙

python打包和反编译


从py到exe

打包

  1. 安装Pyinstaller
pip install pyinstaller
//太慢可切源
pip install -i https://pypi.douban.com/simple/ pyinstaller #豆瓣源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller #清华源
  1. 在文件所在的位置启动cmd,命令如下
Pyinstaller -F -w -i chengzi.ico py_word.py

其中-F参数代表制作独立的可执行程序。

  • w是指程序启动的时候不会打开命令行。如果不加-w的参数,就会有黑洞洞的控制台窗口出来。比如在刚才的脚本里我加一行print('Hello World!'),那么就不要放-w参数了,不然运行会报错,毕竟Hello World!需要在命令行里打印出来。此外,w参数在GUI界面时非常有用。
  • 但是!!!注意,如果这里python文件有input的话,就会报错(报错的框框标题是Unhandled exception in script),所以要把w给去掉。

最后的-i chengzi.ico就是指设置自己的图标图案,因为默认打包图片是下图这样的。这个参数也可以写成--icon=chengzi.ico

  1. 然后就在disk下面找到

ps: Linux同,指令一样的,生成elf文件

  • 直接把exe拖进ida

    可以在strings里面发现__main____file__py开头的东西

    这个是python打包之后的exe文件的特征


从exe到pyc

pyi-archieve_viewer

直接在文件所在的地方启动cmd,键入即可

(前面下好了Pyinstaller就可以直接用哇)

pyi-archive_viewer abc.exe

下面的是pyi-archieve_viewer的其他选项:

-h, --help
Show help. 显示帮助。

-l, --log
Quick contents log. 快速内容日志。

-b, --brief
Print a python evaluable list of contents filenames.
打印 python 可评估的内容文件名列表。

-r, --recursive
Used with -l or -b, applies recursive behaviour.
与 -l 或 -b 一起使用,应用递归行为。

在键入pyi-archive_viewer abc.exe之后会在最后面有一个?,在?后面键入以下指令就好。

?
U: go up one level
//返回上一级,返回查看包含的文档
O <name>: open embedded archive with given name
//打开嵌入的压缩包的名称(如果省略会提示,列如当查看单文件可执行文件时,可以打开其中的PYZ-00.pyz存档
X <name>: extract file with given name
//提取,省略同样会提示。会提示输入输出名,如果没有给出,则将该文件提取到标准输出。
S: list the contents of current archive again
//再列举一遍之前出现的
Q: quit
//退出然后到普通的命令行

然后就大概这样哇

D:\aDate\2024-1\春节\123\open it!>pyi-archive_viewer begin~.exe
Contents of 'begin~.exe' (PKG/CArchive):
 position, length, uncompressed_length, is_compressed, typecode, name
 0, 248, 344, 1, 'm', 'struct'
//上面这个就是其中一个可以提取出来的pyc文件(一般第一个就是可以提取的pyc文件。都是叫struct)
 248, 4015, 8269, 1, 'm', 'pyimod01_archive'
 4263, 10363, 24574, 1, 'm', 'pyimod02_importers'
 14626, 2863, 7035, 1, 'm', 'pyimod03_ctypes'
 17489, 1087, 2017, 1, 'm', 'pyimod04_pywin32'
 18576, 1064, 1840, 1, 's', 'pyiboot01_bootstrap'
 19640, 970, 1679, 1, 's', 'pyi_rth_inspect'
 20610, 1242, 3625, 1, 's', '1'
//这个是另外一个可以提取出来的pyc文件(这个是py文件打包前的名字,如果不知道的也可以通过其前后文件的特征来判断,前面的是一堆pyi_文件开头,后面的就是VCR文件)
 21852, 56219, 109440, 1, 'b', 'VCRUNTIME140.dll'
 78071, 45317, 84760, 1, 'b', '_bz2.pyd'
 123388, 121913, 253720, 1, 'b', '_decimal.pyd'
 245301, 29903, 65304, 1, 'b', '_hashlib.pyd'
 275204, 88053, 159000, 1, 'b', '_lzma.pyd'
 363257, 38299, 79640, 1, 'b', '_socket.pyd'
 401556, 70493, 176920, 1, 'b', '_ssl.pyd'
 472049, 6321, 12240, 1, 'b', 'api-ms-win-core-console-l1-1-0.dll'
 478370, 6121, 11736, 1, 'b', 'api-ms-win-core-datetime-l1-1-0.dll'
 484491, 6159, 11728, 1, 'b', 'api-ms-win-core-debug-l1-1-0.dll'
 490650, 6209, 11728, 1, 'b', 'api-ms-win-core-errorhandling-l1-1-0.dll'
 496859, 7254, 15328, 1, 'b', 'api-ms-win-core-file-l1-1-0.dll'
 504113, 6177, 11728, 1, 'b', 'api-ms-win-core-file-l1-2-0.dll'
 510290, 6288, 11744, 1, 'b', 'api-ms-win-core-file-l2-1-0.dll'
 516578, 6156, 11736, 1, 'b', 'api-ms-win-core-handle-l1-1-0.dll'
 522734, 6333, 12256, 1, 'b', 'api-ms-win-core-heap-l1-1-0.dll'
 529067, 6203, 11728, 1, 'b', 'api-ms-win-core-interlocked-l1-1-0.dll'
 535270, 6513, 12752, 1, 'b', 'api-ms-win-core-libraryloader-l1-1-0.dll'
 541783, 7050, 14800, 1, 'b', 'api-ms-win-core-localization-l1-2-0.dll'
 548833, 6351, 12240, 1, 'b', 'api-ms-win-core-memory-l1-1-0.dll'
 555184, 6277, 11728, 1, 'b', 'api-ms-win-core-namedpipe-l1-1-0.dll'
 561461, 6431, 12752, 1, 'b', 'api-ms-win-core-processenvironment-l1-1-0.dll'
 567892, 6939, 14288, 1, 'b', 'api-ms-win-core-processthreads-l1-1-0.dll'
 574831, 6377, 12240, 1, 'b', 'api-ms-win-core-processthreads-l1-1-1.dll'
 581208, 6139, 11728, 1, 'b', 'api-ms-win-core-profile-l1-1-0.dll'
 587347, 6363, 12240, 1, 'b', 'api-ms-win-core-rtlsupport-l1-1-0.dll'
 593710, 6225, 11728, 1, 'b', 'api-ms-win-core-string-l1-1-0.dll'
 599935, 6758, 13776, 1, 'b', 'api-ms-win-core-synch-l1-1-0.dll'
 606693, 6412, 12240, 1, 'b', 'api-ms-win-core-synch-l1-2-0.dll'
 613105, 6456, 12768, 1, 'b', 'api-ms-win-core-sysinfo-l1-1-0.dll'
 619561, 6339, 12240, 1, 'b', 'api-ms-win-core-timezone-l1-1-0.dll'
 625900, 6160, 11728, 1, 'b', 'api-ms-win-core-util-l1-1-0.dll'
 632060, 6507, 12752, 1, 'b', 'api-ms-win-crt-conio-l1-1-0.dll'
 638567, 7566, 15824, 1, 'b', 'api-ms-win-crt-convert-l1-1-0.dll'
 646133, 6341, 12240, 1, 'b', 'api-ms-win-crt-environment-l1-1-0.dll'
 652474, 6964, 13776, 1, 'b', 'api-ms-win-crt-filesystem-l1-1-0.dll'
 659438, 6489, 12752, 1, 'b', 'api-ms-win-crt-heap-l1-1-0.dll'
 665927, 6445, 12240, 1, 'b', 'api-ms-win-crt-locale-l1-1-0.dll'
 672372, 9429, 20944, 1, 'b', 'api-ms-win-crt-math-l1-1-0.dll'
 681801, 6544, 12752, 1, 'b', 'api-ms-win-crt-process-l1-1-0.dll'
 688345, 7743, 16336, 1, 'b', 'api-ms-win-crt-runtime-l1-1-0.dll'
 696088, 8182, 17872, 1, 'b', 'api-ms-win-crt-stdio-l1-1-0.dll'
 704270, 8354, 18384, 1, 'b', 'api-ms-win-crt-string-l1-1-0.dll'
 712624, 7051, 14288, 1, 'b', 'api-ms-win-crt-time-l1-1-0.dll'
 719675, 6486, 12240, 1, 'b', 'api-ms-win-crt-utility-l1-1-0.dll'
 726161, 1847308, 5157656, 1, 'b', 'libcrypto-3.dll'
 2573469, 283575, 789784, 1, 'b', 'libssl-3.dll'
 2857044, 2278651, 5765912, 1, 'b', 'python311.dll'
 5135695, 17464, 30488, 1, 'b', 'select.pyd'
 5153159, 479880, 1035728, 1, 'b', 'ucrtbase.dll'
 5633039, 417379, 1141016, 1, 'b', 'unicodedata.pyd'
 6050418, 566204, 1847837, 1, 'x', 'base_library.zip'
 6616622, 1356103, 1356103, 0, 'z', 'PYZ-00.pyz'
? x struct
//这里直接输入目录里面显示的名字就好,不用加后缀
Output filename? struct.pyc
//然后就会显示让你取一个名字,这里就要加上我们的后缀了
? x 1
Output filename? 1.py
? q

结束之后就可以在启动的文件夹里面找到提取成功的pyc文件,另外如果是上面列出来没有的,会有如下的报错:

? x index
Failed to extract data for entry 'index' from 'begin~.exe': 'No entry named index found in the archive!'
Output filename?

ps: 但是在同目录下还会照样生成错误的输入的文件。

https://pyinstaller.org/en/stable/advanced-topics.html#using-pyi-archive-viewer

具体用法在官方pyinstaller文档说明里面。

PyInstaller Extractor

这是一个可以直接提取exe文件里面的脚本,用于提取 PyInstaller 生成的可执行文件的内容。

用法:(在该脚本所在文件夹启动cmd,然后就可以在该文件夹下面发现一个以提取exe命名的文件夹,打开就是全部提取好的)

D:\re\python\pyinstxtractor-master>python .\pyinstxtractor.py .\begin~.exe
[+] Processing .\begin~.exe
[+] Pyinstaller version: 2.1+
//pyinstaller版本
[+] Python version: 3.11
//python版本
[+] Length of package: 7976141 bytes
//存档长度
[+] Found 61 files in CArchive
//在CArchive里找到61个文件
[+] Beginning extraction...please standby
[+] Possible entry point: pyiboot01_bootstrap.pyc
[+] Possible entry point: pyi_rth_inspect.pyc
[+] Possible entry point: 1.pyc
//可能的入口点
[+] Found 102 files in PYZ archive
[+] Successfully extracted pyinstaller archive: .\begin~.exe

You can now use a python decompiler on the pyc files within the extracted directory

从pyc到py

修补文件头

使用pyinstaller打包的文件,文件头会被去掉。再还原的过程中,我们需要手动进行修补,这个文件头长度一般为16字节。用pyi-archieve_viewer来提取的pyc都会缺,但是PyInstaller Extractor的不会缺(怪)所以这里还是推荐用后者。

然后提取出来的pyc有一个是和原本py文件同名的,那个点击后会执行和原exe一样的行为。

还原pyc

posted on 2024-02-19 02:10  aster_ist  阅读(100)  评论(0编辑  收藏  举报

导航