通过生成.so文件可以加密python项目,破解难度较大,但是会有代码兼容问题,并且在新系统下需要重新编译
整理一些python中执行exec
函数编译为.so
动态库之后碰到的问题
测试代码执行环境:
centos:centos7 镜像
Python 3.6.8
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
因为我这用的空的centos7环境所以安装东西比较多,正常比如使用macOS可能只需要安装Cython
就可以了
安装:
yum -y install python3
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
pip3 install Cython
yum -y install gcc
yum -y install python3-devel
生成.so的代码(gen-so.py):
#!/usr/bin/env python3
import sys
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize([sys.argv[1]], compiler_directives={'language_level' : "3"}), script_args=['build_ext', '-b', './build', '-t', './tmp'])
举一个简单的例子:
python文件(demo.py):
def run():
print('run')
生成.so文件, 运行命令:
python3 gen-so.py demo.py
生成后文件结构:
.
|-- build
| `-- demo.cpython-36m-x86_64-linux-gnu.so
|-- demo.c
|-- demo.py
|-- gen-so.py
`-- tmp
`-- demo.o
2 directories, 5 files
调用.so文件,这里方便起见就直接通过python -c
调用了:
python3 -c "from build import demo;demo.run()"
会输出"run",没问题
调用exec
函数编译为动态库后表现不一致的情况
1. exec中使用实例变量
demo.py
class Demo():
def __init__(self):
self.a = 1
exec('self.b=2')
print(self.b)
def run():
Demo()
生成.so文件后,分别执行Python和.so的代码:
[root@6d819d145bf6 demo]# python3 -c "import demo; demo.run()"
2
[root@6d819d145bf6 demo]# python3 -c "from build import demo; demo.run()"
Traceback (most recent call last):
File "", line 1, in
File "demo.py", line 8, in demo.run
Demo()
File "demo.py", line 4, in demo.Demo.__init__
exec('self.b="2"')
File "", line 1, in
NameError: name 'self' is not defined
修改exec('self.b=2')
为self.__dict__['b'] = 2
或者exec('self.b=2',locals())
2. exec中使用局部变量
demo.py
class Demo():
def __init__(self):
self.a = 1
local_c = 'lc'
exec('local_d = "ld"')
print(locals())
def run():
Demo()
分别执行python和.so:
[root@6d819d145bf6 demo]# python3 -c "import demo; demo.run()"
{'local_c': 'lc', 'self': , 'local_d': 'ld'}
[root@6d819d145bf6 demo]# python3 -c "from build import demo; demo.run()"
{'local_c': 'lc', 'self': }
可以看到Python能拿到exec中的局部变量local_d
,但是.so动态库中不能拿到
3. exec中动态调用方法
demo.py
class Demo():
def __init__(self):
self.a = 1
import math
func_name = "log"
exec("log10 = math."+ func_name + "(10)")
print(log10)
def run():
Demo()
编译失败:
[root@6d819d145bf6 demo]# python3 gen-so.py demo.py
Compiling demo.py because it changed.
[1/1] Cythonizing demo.py
Error compiling Cython file:
------------------------------------------------------------
...
def __init__(self):
self.a = 1
import math
func_name = "log"
exec("log10 = math."+ func_name + "(10)")
print(log10)
^
------------------------------------------------------------
demo.py:7:14: undeclared name not builtin: log10
Traceback (most recent call last):
File "gen-so.py", line 7, in
setup(ext_modules = cythonize([sys.argv[1]], compiler_directives={'language_level' : "3"}), script_args=['build_ext', '-b', './build', '-t', './tmp'])
File "/usr/local/lib64/python3.6/site-packages/Cython/Build/Dependencies.py", line 1102, in cythonize
cythonize_one(*args)
File "/usr/local/lib64/python3.6/site-packages/Cython/Build/Dependencies.py", line 1225, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: demo.py
因为上面的log10
是局部变量,所以通过locals()
中也获取不到
修改exec("log10 = math."+ func_name + "(10)")
为log10 = getattr(math, func_name)(10)
直接运行.so:
[root@6d819d145bf6 demo]# python3 -c "from build import demo; demo.run()"
2.302585092994046
运行正确
4. exec代码内list使用局部变量
参考: listcomp unable to access locals defined in code called by exec if nested in function
这段代码在python和.so方式下都正常执行:
code = """
g = 5
x = [g for i in range(5)]
print(x)
"""
exec(code)
def run():
pass
执行后正确输出[5, 5, 5, 5, 5]
下面这段代码在Python版执行报错,但是.so动态库执行成功:
def run():
code = """
g = 5
x = [g for i in range(5)]
print(x)
"""
exec(code)
python版报错,.so执行成功:
[root@6d819d145bf6 demo]# python3 -c "import demo; demo.run()"
Traceback (most recent call last):
File "", line 1, in
File "/opt/demo/demo.py", line 7, in run
exec(code)
File "", line 3, in
File "", line 3, in
NameError: name 'g' is not defined
[root@6d819d145bf6 demo]# python3 -c "from build import demo; demo.run()"
[5, 5, 5, 5, 5]
修改exec(code)
为exec(code, globals())
后,python也运行正确,和.so表现一致