• 实现对python源码加密的方法



    前言

    由于项目的保密性需求,对已开发完成的项目需要进行加密处理,本文给出两种方法。


    一、将py文件编译成pyc

    加密py代码,虽然编译为pyc作用不大(很容易被反编译出来源码),但还是有一定加密的效果,如果项目比较时间紧,可以用此方法应急处理。

    python -m compileall -f -q -b "py文件夹"
    
    • 1

    该命令会把目录下的py文件都会生成相应的pyc文件
    然后执行以下命令,删除掉py文件,只保留pyc文件

     find . -name "*.py" -type f -print -exec rm -rf {} \;
    
    • 1

    最后执行运行pyc文件就可以了
    如:python ****.pyc

    注意:此方法加密与使用的python版本要一致,否则会报错。即运行python -m compileall -f -q -b "py文件夹"python ****.pyc的版本要保持一致。


    二、将py转化成so文件

    2.1准备工作

    pip3 install Cython
    
    sudo apt-get update
    sudo apt-get install python-devel 
    sudo apt-get install gcc
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.2新建py2so.py文件

    填写如下内容:

    #-* -coding: UTF-8 -* -
    
    """
    执行前提:
        系统安装python-devel 和 gcc
        Python安装cython
    编译某个文件夹:
        python py2so.py BigoModel
    生成结果:
        目录 build 下
    生成完成后:
        启动文件还需要py/pyc担当,须将启动的py/pyc拷贝到编译目录并删除so文件
    """
    
    import sys, os, shutil, time
    from distutils.core import setup
    from Cython.Build import cythonize
    
    starttime = time.time()
    setupfile= os.path.join(os.path.abspath('.'), __file__)
    
    def getpy(basepath=os.path.abspath('.'), parentpath='', name='', build_dir="build", 
              excepts=(), copyOther=False, delC=False):
        """
        获取py文件的路径
        :param basepath: 根路径
        :param parentpath: 父路径
        :param name: 文件/夹
        :param excepts: 排除文件
        :param copy: 是否copy其他文件
        :return: py文件的迭代器
        """
        fullpath = os.path.join(basepath, parentpath, name)
        for fname in os.listdir(fullpath):
            ffile = os.path.join(fullpath, fname)
            if os.path.isdir(ffile) and ffile != os.path.join(basepath, build_dir) and not fname.startswith('.'):
                for f in getpy(basepath, os.path.join(parentpath, name), fname, build_dir, excepts, copyOther, delC):
                    yield f
            elif os.path.isfile(ffile):
                # print("\t", basepath, parentpath, name, ffile)
                ext = os.path.splitext(fname)[1]
                if ext == ".c":
                    if delC and os.stat(ffile).st_mtime > starttime:
                        os.remove(ffile)
                elif ffile not in excepts and ext not in('.pyc', '.pyx'):
                    # print("\t\t", basepath, parentpath, name, ffile)
                    if ext in('.py', '.pyx') and not fname.startswith('__'):
                        yield os.path.join(parentpath, name, fname)
                    elif copyOther:
                            dstdir = os.path.join(basepath, build_dir, parentpath, name)
                            if not os.path.isdir(dstdir): os.makedirs(dstdir)
                            shutil.copyfile(ffile, os.path.join(dstdir, fname))
            else:
                pass
    
    if __name__ == "__main__":
        currdir = os.path.abspath('.')
        parentpath = sys.argv[1] if len(sys.argv)>1 else "."
    
        currdir, parentpath = os.path.split(currdir if parentpath == "." else os.path.abspath(parentpath))
        build_dir = os.path.join(parentpath, "build")
        build_tmp_dir = os.path.join(build_dir, "temp")
        print("start:", currdir, parentpath, build_dir)
        os.chdir(currdir)
        try:
            #获取py列表
            module_list = list(getpy(basepath=currdir,parentpath=parentpath, build_dir=build_dir, excepts=(setupfile)))
            print(module_list)
            setup(ext_modules = cythonize(module_list),script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir])
            module_list = list(getpy(basepath=currdir, parentpath=parentpath, build_dir=build_dir, excepts=(setupfile), copyOther=True))
        except Exception as ex:
            print("error! ", ex)
        finally:
            print("cleaning...")
            module_list = list(getpy(basepath=currdir, parentpath=parentpath, build_dir=build_dir, excepts=(setupfile), delC=True))
            if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir)
    
        print("complate! time:", time.time()-starttime, 's')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    2.3编译项目文件夹

    python py2so.py project_dir_path
    
    • 1

    运行结束后,project_dir_path会生成一个build文件夹,将该文件夹下的.os文件全部移动到被加密的文件夹对应的位置,删除原文件夹下的py文件,若为启动文件,建议保留,可通过启动文件来运行程序。


    总结

    将py文件编译成pyc:

    Python 标准库中提供了一个名为 compileall 的库,可以轻松地进行编译。简单方便,提高了一点源码破解门槛
    平台兼容性好,py 能在哪里运行,pyc 就能在哪里运行。但是有现成的反编译工具(python-uncompyle6 ),破解成本低。建议在项目时间紧,应急时使用。

    将py转化成so文件:

    Cython方法加密就是将py文件转为so文件,用so文件替换py文件,执行速率会比python快一些,但是偶尔会遇到部分代码不好使,只能针对进行测试,将失效的不进行加密或者采取其他方式加密。加密效果好,不易被反编译,但是对于项目需要一步步测试。

    参考文档:
    https://www.jianshu.com/p/166b5dc1e9b9
    https://blog.csdn.net/qq_43076825/article/details/108516876
    https://github.com/ArvinMei/py2so
    如果阅读本文对你有用,欢迎一键三连呀!!!
    2022年7月1日09:09:37
    在这里插入图片描述

  • 相关阅读:
    zabbix监控多实例redis
    如何理解高新认定中的核心自主知识产权条件?
    【供应链】供应链的四个流通和供应链发展趋势
    wxWidgets(1):在Ubuntu 环境中搭建wxWidgets 库环境,安装库和CodeBlocks的IDE,可以运行demo界面了,继续学习中
    从李佳琦到背后的商业逻辑再到游戏行业
    io,nio,aio总结
    记一次To B开发普通的性能优化历程......报表优化
    bsdtar 归档程序在保留文件特殊属性上比 GNU tar 更全面和简便
    卓望数码--2023.10.16
    看源码方法
  • 原文地址:https://blog.csdn.net/JulyLi2019/article/details/125534454