来了,Python终于可以不用源码发布了,代码加密2种思想
off999 2024-10-18 08:02 29 浏览 0 评论
我们在使用Python做完项目后,给客户去部署,但是又不想让客户看到自己的代码,这时我们怎么办?
下面就来介绍不使用Python源码发布的两种思想:
发布编译版本
我们可以让Python的源码直接生成2进制的文件,从而达到避免源码暴露的问题,编译方式有以下几种:
pyc文件
.pyc文件是.py文件动态编译后生成的能够被Cpython解释器解释的二进制代码,可以直接在.py引入,就像我们引用模块一样使用,非常简单方便,使用方法:
$ python3 -m compileall -b . // -b添加生成.pyc文件具有版本号
$ python3 -O -m compileall -b . // 进行优化生成.pyc后我们可以直接将.py的源码文件删除掉了
find <src> -name '*.py' -type f -print -exec rm {} \;优点
- 为破解提高了一点点门槛儿
- 兼容性好,.py能运行,.pyc就能运行
缺点
- 解释器兼容性差,.pyc 只能在特定版本的解释器上运行
- 有现成的反编译工具,破解成本低
python-uncompyle6 工具可以将.pyc文件反向编译成.py文件,效果很好。
生成可执行文件
我们可以利用pyinstall和py2exe将python代码直接编译成可执行文件,具体方法可以自行百度查询。
优点
- 破解难度增加
- 方便目标机器运行
缺点
- 兼容性差,容易引起.so文件未找到的问题
- 可以按照规则找到.pyc文件进行反编译
编译生成.so文件
我们可以将python代码翻译成C代码,并且进一步进行编译生成.so动态链接文件,我们可以使用.py文件直接引用这个文件中的模块儿。
这个我们需要更换另一种解释器Cython解释器,Cython使用方法请大家自行百度,这里篇幅有限,不再赘述。
优点
- 二进制文件难以破解
- 可以突破python中的GIL的限制
- 引用方便
缺点
- 兼容性差,更换平台需要重新进行编译
- 对于python代码的支持不是100%的完美,可能会有方法未定义的问题
利用其他虚拟机
Jython和IronPython两种解释器,利用了Java和.net的平台,编译生成的包可以直接使用jvm和.net进行运行,除了这两个平台还有其他的第三方解释器。
优点
- 文件破解难
- 可以突破GIL的限制
缺点
- 支持的第三方包少,如果要使用可能会进行二次开发
- 需要借助其他平台
代码混淆
代码混淆的本质就是让代码变得谁都看不懂,但是代码逻辑还是之前的逻辑,其中比较出色的代码混淆工具给大家介绍两款。
具体命令我就不给大家介绍了,主要贴出混淆前后的代码对比。
- oxyry
源代码
"""The n queens puzzle.
https://github.com/sol-prog/N-Queens-Puzzle/blob/master/nqueens.py
"""
__all__ = []
class NQueens:
"""Generate all valid solutions for the n queens puzzle"""
def __init__(self, size):
# Store the puzzle (problem) size and the number of valid solutions
self.__size = size
self.__solutions = 0
self.__solve()
def __solve(self):
"""Solve the n queens puzzle and print the number of solutions"""
positions = [-1] * self.__size
self.__put_queen(positions, 0)
print("Found", self.__solutions, "solutions.")
def __put_queen(self, positions, target_row):
"""
Try to place a queen on target_row by checking all N possible cases.
If a valid place is found the function calls itself trying to place a queen
on the next row until all N queens are placed on the NxN board.
"""
# Base (stop) case - all N rows are occupied
if target_row == self.__size:
self.__show_full_board(positions)
self.__solutions += 1
else:
# For all N columns positions try to place a queen
for column in range(self.__size):
# Reject all invalid positions
if self.__check_place(positions, target_row, column):
positions[target_row] = column
self.__put_queen(positions, target_row + 1)
def __check_place(self, positions, ocuppied_rows, column):
"""
Check if a given position is under attack from any of
the previously placed queens (check column and diagonal positions)
"""
for i in range(ocuppied_rows):
if positions[i] == column or \
positions[i] - i == column - ocuppied_rows or \
positions[i] + i == column + ocuppied_rows:
return False
return True
def __show_full_board(self, positions):
"""Show the full NxN board"""
for row in range(self.__size):
line = ""
for column in range(self.__size):
if positions[row] == column:
line += "Q "
else:
line += ". "
print(line)
print("\n")
def __show_short_board(self, positions):
"""
Show the queens positions on the board in compressed form,
each number represent the occupied column position in the corresponding row.
"""
line = ""
for i in range(self.__size):
line += str(positions[i]) + " "
print(line)
def main():
"""Initialize and solve the n queens puzzle"""
NQueens(8)
if __name__ == "__main__":
# execute only if run as a script
main()
混淆后
""#line:4
__all__ =[]#line:6
class OOO000O0OOOO0O0O0 :#line:8
""#line:9
def __init__ (O0OO0O00OOOO00OO0 ,O0000OO0OO0OO0OOO ):#line:11
O0OO0O00OOOO00OO0 .__OO0O0O000OO00O000 =O0000OO0OO0OO0OOO #line:13
O0OO0O00OOOO00OO0 .__OOOOOOO0O0OOO00O0 =0 #line:14
O0OO0O00OOOO00OO0 .__OO000OO0O00OOOOOO ()#line:15
def __OO000OO0O00OOOOOO (OO00OOO000O0OOO0O ):#line:17
""#line:18
OO00OO00O000000O0 =[-1 ]*OO00OOO000O0OOO0O .__OO0O0O000OO00O000 #line:19
OO00OOO000O0OOO0O .__OO0O000OO0OOO0O0O (OO00OO00O000000O0 ,0 )#line:20
print ("Found",OO00OOO000O0OOO0O .__OOOOOOO0O0OOO00O0 ,"solutions.")#line:21
def __OO0O000OO0OOO0O0O (O0O00OOOOOOO00OO0 ,OOOOO0O0O0OOOO0OO ,OOO00O00O00OOO0O0 ):#line:23
""#line:28
if OOO00O00O00OOO0O0 ==O0O00OOOOOOO00OO0 .__OO0O0O000OO00O000 :#line:30
O0O00OOOOOOO00OO0 .__O0O0O0O0O0O00O0OO (OOOOO0O0O0OOOO0OO )#line:31
O0O00OOOOOOO00OO0 .__OOOOOOO0O0OOO00O0 +=1 #line:32
else :#line:33
for OOO00O0O0OOOOO0O0 in range (O0O00OOOOOOO00OO0 .__OO0O0O000OO00O000 ):#line:35
if O0O00OOOOOOO00OO0 .__O0O000O0O0O0OOO00 (OOOOO0O0O0OOOO0OO ,OOO00O00O00OOO0O0 ,OOO00O0O0OOOOO0O0 ):#line:37
OOOOO0O0O0OOOO0OO [OOO00O00O00OOO0O0 ]=OOO00O0O0OOOOO0O0 #line:38
O0O00OOOOOOO00OO0 .__OO0O000OO0OOO0O0O (OOOOO0O0O0OOOO0OO ,OOO00O00O00OOO0O0 +1 )#line:39
def __O0O000O0O0O0OOO00 (OOO0000O0OOO00O00 ,O0000OO00O0OOOOO0 ,O0O00000OOO0OOOO0 ,OO0OOO00OO000OO0O ):#line:42
""#line:46
for OOO00O0OO0O00000O in range (O0O00000OOO0OOOO0 ):#line:47
if O0000OO00O0OOOOO0 [OOO00O0OO0O00000O ]==OO0OOO00OO000OO0O or O0000OO00O0OOOOO0 [OOO00O0OO0O00000O ]-OOO00O0OO0O00000O ==OO0OOO00OO000OO0O -O0O00000OOO0OOOO0 or O0000OO00O0OOOOO0 [OOO00O0OO0O00000O ]+OOO00O0OO0O00000O ==OO0OOO00OO000OO0O +O0O00000OOO0OOOO0 :#line:50
return False #line:52
return True #line:53
def __O0O0O0O0O0O00O0OO (OOO0O0O000O0OOOO0 ,O0OOOO00000000O0O ):#line:55
""#line:56
for O0O00O000O0O00O0O in range (OOO0O0O000O0OOOO0 .__OO0O0O000OO00O000 ):#line:57
OOO0O00OOOOO0O00O =""#line:58
for OOOOOO00OO0O00O00 in range (OOO0O0O000O0OOOO0 .__OO0O0O000OO00O000 ):#line:59
if O0OOOO00000000O0O [O0O00O000O0O00O0O ]==OOOOOO00OO0O00O00 :#line:60
OOO0O00OOOOO0O00O +="Q "#line:61
else :#line:62
OOO0O00OOOOO0O00O +=". "#line:63
print (OOO0O00OOOOO0O00O )#line:64
print ("\n")#line:65
def __O000OOOO0OO000O00 (OOOO00O0000O00000 ,OOOO0O0OO0O00O00O ):#line:67
""#line:71
O0OO0OO0000O00OO0 =""#line:72
for OO0O000O0O000OO00 in range (OOOO00O0000O00000 .__OO0O0O000OO00O000 ):#line:73
O0OO0OO0000O00OO0 +=str (OOOO0O0OO0O00O00O [OO0O000O0O000OO00 ])+" "#line:74
print (O0OO0OO0000O00OO0 )#line:75
def O000O0O00O00OO0OO ():#line:77
""#line:78
OOO000O0OOOO0O0O0 (8 )#line:79
if __name__ =="__main__":#line:81
O000O0O00O00OO0OO ()#line:83
看到代码我们是这样的表情
- pyminifier
源代码
#!/usr/bin/env python
"""
tumult.py - Because everyone needs a little chaos every now and again.
"""
try:
import demiurgic
except ImportError:
print("Warning: You're not demiurgic. Actually, I think that's normal.")
try:
import mystificate
except ImportError:
print("Warning: Dark voodoo may be unreliable.")
# Globals
ATLAS = False # Nothing holds up the world by default
class Foo(object):
"""
The Foo class is an abstract flabbergaster that when instantiated
represents a discrete dextrogyratory inversion of a cattywompus
octothorp.
"""
def __init__(self, *args, **kwargs):
"""
The initialization vector whereby the ineffably obstreperous
becomes paramount.
"""
# TODO. BTW: What happens if we remove that docstring? :)
def demiurgic_mystificator(self, dactyl):
"""
A vainglorious implementation of bedizenment.
"""
inception = demiurgic.palpitation(dactyl) # Note the imported call
demarcation = mystificate.dark_voodoo(inception)
return demarcation
def test(self, whatever):
"""
This test method tests the test by testing your patience.
"""
print(whatever)
if __name__ == "__main__":
print("Forming...")
f = Foo("epicaricacy", "perseverate")
f.test("Codswallop")混淆后
#!/usr/bin/env python
?=ImportError
?=print
?=False
搓=object
try:
import demiurgic
except ?:
?("Warning: You're not demiurgic. Actually, I think that's normal.")
try:
import mystificate
except ?:
?("Warning: Dark voodoo may be unreliable.")
?=?
class ?(搓):
def __init__(self,*args,**kwargs):
pass
def (self,dactyl):
?=demiurgic.palpitation(dactyl)
?=mystificate.dark_voodoo(?)
return ?
def (self,whatever):
?(whatever)
if __name__=="__main__":
?("Forming...")
?=?("epicaricacy","perseverate")
?.("Codswallop")看到代码后
无论哪种方案都是不十全十美的,在以后的项目开发过程中,我们还需要加强网络安全方面的意识,防患于未然。
相关推荐
- 阿里云国际站ECS:阿里云ECS如何提高网站的访问速度?
-
TG:@yunlaoda360引言:速度即体验,速度即业务在当今数字化的世界中,网站的访问速度已成为决定用户体验、用户留存乃至业务转化率的关键因素。页面加载每延迟一秒,都可能导致用户流失和收入损失。对...
- 高流量大并发Linux TCP性能调优_linux 高并发网络编程
-
其实主要是手里面的跑openvpn服务器。因为并没有明文禁p2p(哎……想想那么多流量好像不跑点p2p也跑不完),所以造成有的时候如果有比较多人跑BT的话,会造成VPN速度急剧下降。本文所面对的情况为...
- 性能测试100集(12)性能指标资源使用率
-
在性能测试中,资源使用率是评估系统硬件效率的关键指标,主要包括以下四类:#性能测试##性能压测策略##软件测试#1.CPU使用率定义:CPU处理任务的时间占比,计算公式为1-空闲时间/总...
- Linux 服务器常见的性能调优_linux高性能服务端编程
-
一、Linux服务器性能调优第一步——先搞懂“看什么”很多人刚接触Linux性能调优时,总想着直接改配置,其实第一步该是“看清楚问题”。就像医生看病要先听诊,调优前得先知道服务器“哪里...
- Nginx性能优化实战:手把手教你提升10倍性能!
-
关注△mikechen△,十余年BAT架构经验倾囊相授!Nginx是大型架构而核心,下面我重点详解Nginx性能@mikechen文章来源:mikechen.cc1.worker_processe...
- 高并发场景下,Spring Cloud Gateway如何抗住百万QPS?
-
关注△mikechen△,十余年BAT架构经验倾囊相授!大家好,我是mikechen。高并发场景下网关作为流量的入口非常重要,下面我重点详解SpringCloudGateway如何抗住百万性能@m...
- Kubernetes 高并发处理实战(可落地案例 + 源码)
-
目标场景:对外提供HTTPAPI的微服务在短时间内收到大量请求(例如每秒数千至数万RPS),要求系统可弹性扩容、限流降级、缓存减压、稳定运行并能自动恢复。总体思路(多层防护):边缘层:云LB...
- 高并发场景下,Nginx如何扛住千万级请求?
-
Nginx是大型架构的必备中间件,下面我重点详解Nginx如何实现高并发@mikechen文章来源:mikechen.cc事件驱动模型Nginx采用事件驱动模型,这是Nginx高并发性能的基石。传统...
- Spring Boot+Vue全栈开发实战,中文版高清PDF资源
-
SpringBoot+Vue全栈开发实战,中文高清PDF资源,需要的可以私我:)SpringBoot致力于简化开发配置并为企业级开发提供一系列非业务性功能,而Vue则采用数据驱动视图的方式将程序...
- Docker-基础操作_docker基础实战教程二
-
一、镜像1、从仓库获取镜像搜索镜像:dockersearchimage_name搜索结果过滤:是否官方:dockersearch--filter="is-offical=true...
- 你有空吗?跟我一起搭个服务器好不好?
-
来人人都是产品经理【起点学院】,BAT实战派产品总监手把手系统带你学产品、学运营。昨天闲的没事的时候,随手翻了翻写过的文章,发现一个很严重的问题。就是大多数时间我都在滔滔不绝的讲理论,却很少有涉及动手...
- 部署你自己的 SaaS_saas如何部署
-
部署你自己的VPNOpenVPN——功能齐全的开源VPN解决方案。(DigitalOcean教程)dockovpn.io—无状态OpenVPNdockerized服务器,不需要持久存储。...
- Docker Compose_dockercompose安装
-
DockerCompose概述DockerCompose是一个用来定义和管理多容器应用的工具,通过一个docker-compose.yml文件,用YAML格式描述服务、网络、卷等内容,...
- 京东T7架构师推出的电子版SpringBoot,从构建小系统到架构大系统
-
前言:Java的各种开发框架发展了很多年,影响了一代又一代的程序员,现在无论是程序员,还是架构师,使用这些开发框架都面临着两方面的挑战。一方面是要快速开发出系统,这就要求使用的开发框架尽量简单,无论...
- Kubernetes (k8s) 入门学习指南_k8s kubeproxy
-
Kubernetes(k8s)入门学习指南一、什么是Kubernetes?为什么需要它?Kubernetes(k8s)是一个开源的容器编排系统,用于自动化部署、扩展和管理容器化应用程序。它...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
python入门到脱坑 输入与输出—str()函数
-
宝塔面板如何添加免费waf防火墙?(宝塔面板开启https)
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
慕ke 前端工程师2024「完整」
-
失业程序员复习python笔记——条件与循环
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
