程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python code packaging and compilation and Python code Decompilation

編輯:Python

At the end of this article, we will attach the access methods of all scripts and programs ;

PS: During my testing ,python3.9 Version does not support decompilation , I haven't found any problems with other applications for the time being , Can be decompiled successfully .

Situational dialogue 1 :

A: Write me a program , What I want to achieve

B: Well done. , Here you are.

A: The operation failed ...

B: Let me see

B: You have not installed xxx\xxx\xxx modular , Just install it

A: So much trouble ? This is the intranet , Download the old trouble

B: I don't care , I realized it for you

A: Your skill is not good ~~~

B:???

Situational dialogue 2 :

A: wow , Your code is so awesome , Just run it directly ;

B: Oh dear , Just so so ;

A: How did you achieve it , Send me the source code and let me have a look ;

B: What source code do you want , It's easy , It's not that I won't show you , I lost my code ;

A:~~~

In both cases , I don't know if you have ever met , If any , After reading this article , If you encounter such a problem again , It's done in minutes .

1、python Code packaging and compilation

python Packaged into executable files on various platforms , In fact, it uses pyinstaller modular ;

He can make you python The code is packaged into various types of , For example, running in windows Upper .exe Format file , Or run on linux The executable on ; What's the advantage of doing this ?

We all know , Write python Many modules need to be installed in the code , Then the late running servers often lack modules , But we go through pyinstaller After the module is packaged , You can run on a machine that does not have these modules installed , Let's see how to operate it .

1.1、 install

pip install pyinstaller

1.2、python Package the code into an executable file

I want to pack it up in windows Running down .exe Word of the file , It's just windows Packaging under the platform ;linux also ; In other words, what platform do you want to execute in the later stage , Then you need to package on what platform ;

For example, our source code is as follows :

#!/usr/bin/python
#coding:utf-8
from datetime import datetime
now_date = datetime.now()
print(' The current time is :' + str(now_date))

Packing command :

PS C:\Users\22768\Desktop\yunwei_ceshi> ls
Catalog : 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 # The order is here
PS C:\Users\22768\Desktop\yunwei_ceshi> ls
Catalog : 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>

After execution, you can see that several directories have been output , So where is the content we packed ? stay dist Under the table of contents , Let's go in and have a look ;

PS C:\Users\22768\Desktop\yunwei_ceshi> cd .\dist\
PS C:\Users\22768\Desktop\yunwei_ceshi\dist> ls
Catalog : 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>

Because I am in windows Running on the platform , So it's just one ceshi.exe file , If you want to make the following program in linux Run on the platform , Then you need to be in linux Package on the platform ;

1.3、 function

stay windows Packaged on the platform python Code , Double click to run ;

stay linux Packaged on the platform python Code , Only use ./ To execute , for example :

./ceshi

2、python Code decompilation

If we get python Packaged code , Want to see the source code , Or the person who provided the package left , No source code , How do we look at the source code in this case ? And look down ;

For example, what we get is what we got after packaging in the previous step ceshi.exe file , How to see the source code ?

2.1、 First step

You need to use a script here pyinstxtractor.py, At the end of this article, we will attach the access methods of all scripts and programs ;

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
Catalog : 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>

You can see one more ceshi.exe_extracted Catalog , Go in and have a look :

PS C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted> ls
Catalog : 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>

You can see that many software packages have been generated ;

2.2、 The second step

Among the many files newly generated in the first step , We need two files , One is struct.pyc, The other is ceshi.pyc;

PS C:\Users\22768\Desktop\yunwei_ceshi\dist\ceshi.exe_extracted> ls .\struct.pyc
Catalog : 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
Catalog : 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、 The third step

Use imhex Software , This software is a hexadecimal compiler , The software installation is simple , Just go to the next step in turn , No special operation is required ; Open after installation struct.pyc file , Here's the picture :

We need to record the value of the first line , Here's the picture :

Then close the file , Open another ceshi.pyc file , Here's the picture :

Modify the values of the first row and struct.pyc The first line of the file has the same value , Here's the picture :

Then save the contents of the file ;

2.4、 Step four

Here we need to use another module , namely uncompyle6, Get the software from the end of the question , After unpacking, use the following command to install ;

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

After installation , We go back to having ceshi.pyc Under the directory of this file ;

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

This nihao.txt Is our source code .

3、 Software download

A script is used in this article pyinstxtractor.py, A software imhex, One python package uncompyle6, I've packed it all , WeChat official account back office :python Decompile , You can get the download address ;

thus , In this paper, the end .

For more information, go to VX official account “ Operation and maintenance home ” , Get the latest article .

------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------

linux Under the system ,mknodlinux,linux Directory write permission , Chinese cabbage can be installed linux Do you ,linux How the system creates files , Led the g linux How to install software in the system ,linux Text positioning ;
ocr distinguish linux,linux Anchoring suffix ,linux System usage records ,u Dish has linux Image file , Fresh students will not Linux,linux kernel 64 position ,linux Self starting management service ;
linux Calculate folder size ,linux What are the equipment names ,linux Can I use a virtual machine ,linux The system cannot enter the command line , How to create kalilinux,linux Follow so Are the documents the same .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved