Python code encryption confusion

python an interpreted language, the encrypted source code itself is difficult. But sometimes we have to consider the time and to launch a python products encryption of the code, the source code in order to avoid leaks. To this end, I looked up some information, studied several common ways python code encryption in this record it.

Source code encryption

(A) py scripts compiled into a binary file pyc

Compile command:

    python -m py_compile file.py

pyc file is a binary file, but can be easily be the reverse, decompile online tool: https://tool.lu/pyc/. Of course there are solutions to this problem, the solution is possible by modifying the python source code opcode, then recompile py code that can prevent reverse to some extent, because the reverse need to know to be modified opcode in order to restore them. If you use a private Bytecode instruction set, so the usual Python disassembler and decompiler not work on your pyc files generated by the private Python compiler, also equivalent to protect your Python code. But the cost of doing so is your Python application runs only on your private Python interpreter. (Actually in the release of a product, do not apply)

(B) py script packaged into exe file

exe files for the windows platform, generally using packers (py2exe, PyInstaller, etc.) packaged into exe, these tools are used to package a Python project executable files into a single, easy to use (on a machine without Python environment). But it may conveniently be obtained by compressing all packets pyc or source file, and the C / C ++ compiler generates executable file has essential difference, is substantially zero protection, it is necessary to operate exe packers.

(C) py c compiled script file (cython)

The core code with cython py module file is converted into a .c file, then compiled into gcc so (unix) files, or compiling it into pyd (windows) files.

Compilation process:

1, server install dependencies

pip install python
yum install python-devel gcc

2, write setup.py file, as follows:

from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("test.py",language_level=2)
)
# 批量编译
setup(
ext_modules = cythonize(["test.py","test2.py".......],language_level=2)
)

3, run the following command

python setup.py build_ext —inplace

Will generate a test.so , delete the remaining files can be directly referenced test.so (py like reference file)

Source code obfuscation

In addition to encryption, can also be confusion as to the source code, increasing the difficulty of reading the source code. This has many third-party libraries, I have listed a few:
https://pypi.org/project/pyminifier/
https://github.com/astrand/pyobfuscate
http://pyob.oxyry.com/

pyminifier library usage:

-----------------------------------------------------
注:我这有个学习基地,里面有很多学习资料,感兴趣的+Q群:895817687
-----------------------------------------------------

pyminifier -O test.py >> test_py.py
pyminifier --replacement-length=1 --obfuscate-builtins --obfuscate-import-methods --obfuscate-variables test.py

Guess you like

Origin blog.csdn.net/weixin_42625143/article/details/95068612