天天看点

python代码打包编译与python代码反编译,一文带你了解

本文最后会附加所有脚本和程序的获取方式;

PS:在我的测试过程中,​

​python3.9​

​版本不支持反编译,其他的使用的暂时没发现问题,都可以反编译成功。

情景对话一:

A:给我写个程序,我要实现什么什么

B:写好了,给你

A:运行失败了啊。。。

B:我看看

B:你这里没有安装xxx\xxx\xxx模块,你安装下就好了

A:这么麻烦?我这是内网,下载老麻烦了

B:那我不管,我给你实现了哈

A:你这技术不行啊~~~

B:???

情景对话二:

A:哇塞,你这个代码好厉害啊,直接运行就好了;

B:哎呀,一般般了;

A:你是怎么实现的啊,源代码发我一下我看看吧;

B:要什么源代码,很简单了,不是不给你看,主要是我代码丢了;

A:~~~

以上两种情况,不知道你遇到过没有,如果有的话,看完这个文章,再遇到这样的问题,分分钟搞定了就。

1、python代码打包编译

​python​

​​打包成各种平台上的可执行文件,其实用到了​

​pyinstaller​

​模块;

他可以让你的​

​python​

​​代码打包成各种类型的,比如运行在​

​windows​

​​上的​

​.exe​

​​格式文件,亦或者运行在​

​linux​

​上的可执行文件;这样子做有什么好处呢?

我们都知道,写​

​python​

​​代码的时候往往需要安装很多模块,那么后期运行的服务器上经常会出现缺少模块的事情发生,但是我们通过​

​pyinstaller​

​模块打包之后,就可以在没有安装这些模块的机器上运行了,那么看下如何操作吧。

1.1、安装

pip install pyinstaller      

1.2、​

​python​

​代码打包成可执行文件

想打包成可以在​

​windows​

​​下运行的​

​.exe​

​​文件的话,就在​

​windows​

​​平台下打包;​

​linux​

​亦然;也就是说后期你想在什么平台执行,那就需要在什么平台打包;

例如我们的源代码如下:

#!/usr/bin/python
#coding:utf-8
from datetime import datetime
now_date = datetime.now()
print('现在的时间为:' + str(now_date))      

打包命令:

PS C:\Users\22768\Desktop\yunwei_ceshi> ls


    目录: C:\Users\22768\Desktop\yunwei_ceshi


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2022-06-15     21:43            138 ceshi.py


PS C:\Users\22768\Desktop\yunwei_ceshi> pyinstaller -F .\ceshi.py  # 命令在这里哈
PS C:\Users\22768\Desktop\yunwei_ceshi> ls


    目录: C:\Users\22768\Desktop\yunwei_ceshi


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2022-06-15     21:44                build
d-----        2022-06-15     21:44                dist
-a----        2022-06-15     21:43            138 ceshi.py
-a----        2022-06-15     21:44            854 ceshi.spec


PS C:\Users\22768\Desktop\yunwei_ceshi>      

执行完之后可以看到输出了几个目录,那么我们打包的内容在哪里呢?在​

​dist​

​目录下,我们进去看一下;

PS C:\Users\22768\Desktop\yunwei_ceshi> cd .\dist\
PS C:\Users\22768\Desktop\yunwei_ceshi\dist> ls


    目录: C:\Users\22768\Desktop\yunwei_ceshi\dist


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2022-06-15     21:44        7864211 ceshi.exe


PS C:\Users\22768\Desktop\yunwei_ceshi\dist>      

由于我是在​

​windows​

​​平台上运行的,所以就是一个​

​ceshi.exe​

​​文件,你如果想让后面的程序在​

​linux​

​​平台上运行,那么你就需要在​

​linux​

​平台上进行打包;

1.3、运行

在​

​windows​

​​平台上打包的​

​python​

​代码,双击即可运行;

在​

​linux​

​​平台上打包的​

​python​

​​代码,只能使用​

​./​

​来执行,例如:

./ceshi      

2、python代码反编译

如果我们拿到了​

​python​

​打包后的代码,想看下源代码,亦或者是提供程序包的人离职了,没人有源代码,这种情况下我们该如何查看源代码呢?且往下看;

例如我们拿到的就是上一步打包之后的​

​ceshi.exe​

​文件,如何看到源代码呢?

2.1、第一步

这里需要使用到一个脚本​

​pyinstxtractor.py​

​,本文最后会附加所有脚本和程序的获取方式;

PS C:\Users\22768\Desktop\yunwei_ceshi\dist> python pyinstxtractor.py .\ceshi.exe
[+] Processing .\ceshi.exe
[+] Pyinstaller version: 2.1+
[+] Python version: 3.9
[+] Length of package: 7548819 bytes
[+] Found 71 files in CArchive
[+] Beginning extraction...please standby
[+] Possible entry point: pyiboot01_bootstrap.pyc
[+] Possible entry point: pyi_rth_subprocess.pyc
[+] Possible entry point: pyi_rth_pkgutil.pyc
[+] Possible entry point: pyi_rth_multiprocessing.pyc
[+] Possible entry point: pyi_rth_inspect.pyc
[+] Possible entry point: ceshi.pyc
[+] Found 227 files in PYZ archive
[+] Successfully extracted pyinstaller archive: .\ceshi.exe

You can now use a python decompiler on the pyc files within the extracted directory
PS C:\Users\22768\Desktop\yunwei_ceshi\dist> ls


    目录: C:\Users\22768\Desktop\yunwei_ceshi\dist


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2022-06-15     21:53                ceshi.exe_extracted
-a----        2022-06-15     21:44        7864211 ceshi.exe
-a----        2022-06-13     19:19          15336 pyinstxtractor.py


PS C:\Users\22768\Desktop\yunwei_ceshi\dist>      

可以看到多了一个​

​ceshi.exe_extracted​

​目录,进去看一下:

PS C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted> ls


    目录: C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2022-06-15     21:53                PYZ-00.pyz_extracted
-a----        2022-06-15     21:53          19864 api-ms-win-core-console-l1-1-0.dll
-a----        2022-06-15     21:53          19352 api-ms-win-core-datetime-l1-1-0.dll
-a----        2022-06-15     21:53          19352 api-ms-win-core-debug-l1-1-0.dll
-a----        2022-06-15     21:53          19376 api-ms-win-core-errorhandling-l1-1-0.dll
-a----        2022-06-15     21:53          22928 api-ms-win-core-file-l1-1-0.dll
-a----        2022-06-15     21:53          19344 api-ms-win-core-file-l1-2-0.dll
-a----        2022-06-15     21:53          19336 api-ms-win-core-file-l2-1-0.dll
-a----        2022-06-15     21:53          19352 api-ms-win-core-handle-l1-1-0.dll
-a----        2022-06-15     21:53          19856 api-ms-win-core-heap-l1-1-0.dll
-a----        2022-06-15     21:53          19368 api-ms-win-core-interlocked-l1-1-0.dll
-a----        2022-06-15     21:53          19888 api-ms-win-core-libraryloader-l1-1-0.dll
-a----        2022-06-15     21:53          21936 api-ms-win-core-localization-l1-2-0.dll
-a----        2022-06-15     21:53          19856 api-ms-win-core-memory-l1-1-0.dll
-a----        2022-06-15     21:53          19360 api-ms-win-core-namedpipe-l1-1-0.dll
-a----        2022-06-15     21:53          20424 api-ms-win-core-processenvironment-l1-1-0.dll
-a----        2022-06-15     21:53          21432 api-ms-win-core-processthreads-l1-1-0.dll
-a----        2022-06-15     21:53          19896 api-ms-win-core-processthreads-l1-1-1.dll
-a----        2022-06-15     21:53          18840 api-ms-win-core-profile-l1-1-0.dll
-a----        2022-06-15     21:53          19880 api-ms-win-core-rtlsupport-l1-1-0.dll
-a----        2022-06-15     21:53          19352 api-ms-win-core-string-l1-1-0.dll
-a----        2022-06-15     21:53          21392 api-ms-win-core-synch-l1-1-0.dll
-a----        2022-06-15     21:53          19856 api-ms-win-core-synch-l1-2-0.dll
-a----        2022-06-15     21:53          20376 api-ms-win-core-sysinfo-l1-1-0.dll
-a----        2022-06-15     21:53          19360 api-ms-win-core-timezone-l1-1-0.dll
-a----        2022-06-15     21:53          19336 api-ms-win-core-util-l1-1-0.dll
-a----        2022-06-15     21:53          20368 api-ms-win-crt-conio-l1-1-0.dll
-a----        2022-06-15     21:53          23448 api-ms-win-crt-convert-l1-1-0.dll
-a----        2022-06-15     21:53          19872 api-ms-win-crt-environment-l1-1-0.dll
-a----        2022-06-15     21:53          21408 api-ms-win-crt-filesystem-l1-1-0.dll
-a----        2022-06-15     21:53          20360 api-ms-win-crt-heap-l1-1-0.dll
-a----        2022-06-15     21:53          19856 api-ms-win-crt-locale-l1-1-0.dll
-a----        2022-06-15     21:53          28552 api-ms-win-crt-math-l1-1-0.dll
-a----        2022-06-15     21:53          20376 api-ms-win-crt-process-l1-1-0.dll
-a----        2022-06-15     21:53          23960 api-ms-win-crt-runtime-l1-1-0.dll
-a----        2022-06-15     21:53          25480 api-ms-win-crt-stdio-l1-1-0.dll
-a----        2022-06-15     21:53          25496 api-ms-win-crt-string-l1-1-0.dll
-a----        2022-06-15     21:53          21896 api-ms-win-crt-time-l1-1-0.dll
-a----        2022-06-15     21:53          19864 api-ms-win-crt-utility-l1-1-0.dll
-a----        2022-06-15     21:53         794260 base_library.zip
-a----        2022-06-15     21:53            211 ceshi.pyc
-a----        2022-06-15     21:53        3399200 libcrypto-1_1.dll
-a----        2022-06-15     21:53          32792 libffi-7.dll
-a----        2022-06-15     21:53         689184 libssl-1_1.dll
-a----        2022-06-15     21:53         190008 pyexpat.pyd
-a----        2022-06-15     21:53           1386 pyiboot01_bootstrap.pyc
-a----        2022-06-15     21:53           1728 pyimod01_os_path.pyc
-a----        2022-06-15     21:53           8809 pyimod02_archive.pyc
-a----        2022-06-15     21:53          17668 pyimod03_importers.pyc
-a----        2022-06-15     21:53           3495 pyimod04_ctypes.pyc
-a----        2022-06-15     21:53            712 pyi_rth_inspect.pyc
-a----        2022-06-15     21:53           2163 pyi_rth_multiprocessing.pyc
-a----        2022-06-15     21:53           1101 pyi_rth_pkgutil.pyc
-a----        2022-06-15     21:53            851 pyi_rth_subprocess.pyc
-a----        2022-06-15     21:53        4451896 python39.dll
-a----        2022-06-15     21:53        1735486 PYZ-00.pyz
-a----        2022-06-15     21:53          28216 select.pyd
-a----        2022-06-15     21:53            285 struct.pyc
-a----        2022-06-15     21:53        1144936 ucrtbase.dll
-a----        2022-06-15     21:53        1120824 unicodedata.pyd
-a----        2022-06-15     21:53          94088 VCRUNTIME140.dll
-a----        2022-06-15     21:53          64568 _asyncio.pyd
-a----        2022-06-15     21:53          86072 _bz2.pyd
-a----        2022-06-15     21:53         126520 _ctypes.pyd
-a----        2022-06-15     21:53         271416 _decimal.pyd
-a----        2022-06-15     21:53          65592 _hashlib.pyd
-a----        2022-06-15     21:53         162360 _lzma.pyd
-a----        2022-06-15     21:53          29752 _multiprocessing.pyd
-a----        2022-06-15     21:53          46136 _overlapped.pyd
-a----        2022-06-15     21:53          29240 _queue.pyd
-a----        2022-06-15     21:53          79928 _socket.pyd
-a----        2022-06-15     21:53         154168 _ssl.pyd


PS C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted>      

可以看到里面有生成了很多软件包;

2.2、第二步

在第一步新生成的诸多文件中,我们需要用到两个文件,一个是​

​struct.pyc​

​​,另一个是​

​ceshi.pyc​

​;

PS C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted> ls .\struct.pyc


    目录: C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2022-06-15     21:53            285 struct.pyc


PS C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted> ls .\ceshi.pyc


    目录: C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2022-06-15     21:53            211 ceshi.pyc


PS C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted>      

2.3、第三步

使用​

​imhex​

​​软件,这个软件是十六进制编译器,该软件安装就很简单了,直接依次下一步即可,并不需要什么特殊操作;安装好之后打开​

​struct.pyc​

​文件,如下图:

python代码打包编译与python代码反编译,一文带你了解

我们需要记录下第一行的值,如下图:

python代码打包编译与python代码反编译,一文带你了解

然后关闭该文件,打开另一个​

​ceshi.pyc​

​文件,如下图:

python代码打包编译与python代码反编译,一文带你了解

修改第一行的值和​

​struct.pyc​

​文件的第一行的值相同,如下图:

python代码打包编译与python代码反编译,一文带你了解

然后保存文件内容;

2.4、第四步

这里我们需要使用到另一个模块了,即​

​uncompyle6​

​,从该问末尾获取该软件,解压之后使用如下命令进行安装;

PS C:\Users\22768\Desktop\python-uncompyle6-master> python .\setup.py install      

安装完毕之后,我们重新回到有​

​ceshi.pyc​

​这个文件的目录下;

PS C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted> uncompyle6 .\ceshi.pyc > nihao.txt      

这个​

​nihao.txt​

​就是我们的源代码了。

3、软件下载

本文一共用到了一个脚本​

​pyinstxtractor.py​

​​,一个软件​

​imhex​

​​,一个​

​python​

​​包​

​uncompyle6​

​​,我都打包好了,微信公众号后台回复:​

​python反编译​

​,即可获取下载地址;

更多内容请转至VX公众号 “运维家” ,获取最新文章。

------ “运维家”  ------

------ “运维家”  ------

------ “运维家”  ------