Python作为一门脚本语言,我们可以用它做很多事,比如数据分析、多媒体处理、web开发、网络编程、游戏编程、人工智能等等方面都被广泛的应用;如果我们用Python来开发商业化项目的时候,难免会遇到一些闲得蛋疼的小人,总想着来破解你的项目或者软件,来破坏别人的劳动成果;所以针对商业化项目做一些必要的加密和混淆是在所难免的!
网上有很多混淆的方法和技术,混淆都是为了增加代码的阅读难度;还有就是加密了,最基本的方法是发布pyc文件, 也就是将所有的.py源文件转换成pyc对外发布;还有一种方案是方案是借助cython. cython可以将python文件转换成c, 并编译成pyd文件;这里我们主要讲的是通过ZFJPyMix来混淆Python源码,以达到增加代码的阅读难度的目的!
ZFJPyMix具有混淆文件名、混淆类名、混淆属性变量名、混淆方法名和删除注释来混淆Python源码,具体功能结构图如下:
Gitee: https://gitee.com/zfj1128/ZFJPyMix
链接:https://pan.baidu.com/s/1MEq-vyt_CMSLBdrI3PT6Yg 密码:tvb
混淆的部分日志,混淆的时候回通过import logging自动生成混淆日志《ZFJPyMix.log》,便于开发者修改部分报错:
如果你在混淆过程中不想某个文件或者文件夹不希望被混淆,可以设置忽略文件或者文件夹,多个文件或者文件夹以‘,’进行分割就行了!
ZFJPyMix的所以功能中我需要说明的是变量过滤、方法过滤、类名过滤,我们在混淆过程中如果一些命名会导致系统的方法被混淆掉,或者直接混淆了系统的方法,比如方法:__ init __我们可以通过方法过滤来过滤这个方法,防止被混淆;大家也可以自己添加需要忽略混淆的方法!
原代码:
# -\*- coding: utf-8 -\*-
# @Author: zhangfujie
# @Date: 2019/10/22
# @Last Modified by: zhangfujie
# @Last Modified time: 2019/10/22
# @ ---------- DFA过滤算 ----------
import time
time1 = time.time()
class DFAFilter(object):
"""DFA过滤算法"""
def \_\_init\_\_(self):
super(DFAFilter, self).\_\_init\_\_()
self.keyword\_chains = {}
self.delimit = '\x00'
# 读取解析敏感词
def parseSensitiveWords(self, path):
ropen = open(path,'r')
text = ropen.read()
keyWordList = text.split(',')
for keyword in keyWordList:
self.addSensitiveWords(str(keyword).strip())
# 生成敏感词树
def addSensitiveWords(self, keyword):
keyword = keyword.lower()
chars = keyword.strip()
if not chars:
return
level = self.keyword\_chains
for i in range(len(chars)):
if chars[i] in level:
level = level[chars[i]]
else:
if not isinstance(level, dict):
break
for j in range(i, len(chars)):
level[chars[j]] = {}
last\_level, last\_char = level, chars[j]
level = level[chars[j]]
last\_level[last\_char] = {self.delimit: 0}
break
if i == len(chars) - 1:
level[self.delimit] = 0
# 过滤敏感词
def filterSensitiveWords(self, message, repl="\*"):
message = message.lower()
ret = []
start = 0
while start < len(message):
level = self.keyword\_chains
step\_ins = 0
message\_chars = message[start:]
for char in message\_chars:
if char in level:
step\_ins += 1
if self.delimit not in level[char]:
level = level[char]
else:
ret.append(repl \* step\_ins)
start += step\_ins - 1
break
else:
ret.append(message[start])
break
start += 1
return ''.join(ret)
if \_\_name\_\_ == "\_\_main\_\_":
gfw = DFAFilter()
gfw.parseSensitiveWords('shieldwords.txt')
text = "小明骂小王是个王八蛋,小王骂小明是个王八羔子!"
result = gfw.filterSensitiveWords(text)
print(result)
time2 = time.time()
print('总共耗时:' + str(time2 - time1) + 's')
混淆后的代码:
import time
OOO00OOOO0OOOO00O000O = time.time()
class O00000O00OOOOO00O0OO(object):
def \_\_init\_\_(self):
super(O00000O00OOOOO00O0OO, self).\_\_init\_\_()
self.OOOO0O00OO0OO00OOO0OO = {}
self.O0O0OO00O0OO0OO0OOOO0 = '\x00'
def O0O00OO0O0OO00O000O0OO(self, path):
OO0OO0O0OOOOOO0OOOOO0 = open(path,'r')
text = OO0OO0O0OOOOOO0OOOOO0.read()
OOOOO00000000OOOO00OO = text.split(',')
for O00O0OO0OO0OO0OOOOO0O in OOOOO00000000OOOO00OO:
self.O0OOOO0OO0OOOOOOO00OO0(str(O00O0OO0OO0OO0OOOOO0O).strip())
def O0OOOO0OO0OOOOOOO00OO0(self, O00O0OO0OO0OO0OOOOO0O):
O00O0OO0OO0OO0OOOOO0O = O00O0OO0OO0OO0OOOOO0O.lower()
OO0OOO0OOOO0O0OOO0O00 = O00O0OO0OO0OO0OOOOO0O.strip()
if not OO0OOO0OOOO0O0OOO0O00:
return
O0OOOO00OO0OOO0000O0O = self.OOOO0O00OO0OO00OOO0OO
for i in range(len(OO0OOO0OOOO0O0OOO0O00)):
if OO0OOO0OOOO0O0OOO0O00[i] in O0OOOO00OO0OOO0000O0O:
O0OOOO00OO0OOO0000O0O = O0OOOO00OO0OOO0000O0O[OO0OOO0OOOO0O0OOO0O00[i]]
else:
if not isinstance(O0OOOO00OO0OOO0000O0O, dict):
break
for j in range(i, len(OO0OOO0OOOO0O0OOO0O00)):
O0OOOO00OO0OOO0000O0O[OO0OOO0OOOO0O0OOO0O00[j]] = {}
last\_level, last\_char = O0OOOO00OO0OOO0000O0O, OO0OOO0OOOO0O0OOO0O00[j]
O0OOOO00OO0OOO0000O0O = O0OOOO00OO0OOO0000O0O[OO0OOO0OOOO0O0OOO0O00[j]]
last\_level[last\_char] = {self.O0O0OO00O0OO0OO0OOOO0: 0}
break
if i == len(OO0OOO0OOOO0O0OOO0O00) - 1:
O0OOOO00OO0OOO0000O0O[self.O0O0OO00O0OO0OO0OOOO0] = 0
def OO0000OOOO0OO00OOOO000(self, O0OOO00O0OO00OOO0O0O0, repl="\*"):
O0OOO00O0OO00OOO0O0O0 = O0OOO00O0OO00OOO0O0O0.lower()
O0OO0OOOOO00OOOOO0000 = []
OOOOO0OO000OO0O0O00O0 = 0
while OOOOO0OO000OO0O0O00O0 < len(O0OOO00O0OO00OOO0O0O0):
O0OOOO00OO0OOO0000O0O = self.OOOO0O00OO0OO00OOO0OO
OOOO000O0OO0O000OO00O = 0
OO0O000O00O00OO00OO00 = O0OOO00O0OO00OOO0O0O0[OOOOO0OO000OO0O0O00O0:]
for O0OOOO00OO0OOOOOO0O0O in OO0O000O00O00OO00OO00:
if O0OOOO00OO0OOOOOO0O0O in O0OOOO00OO0OOO0000O0O:
OOOO000O0OO0O000OO00O += 1
if self.O0O0OO00O0OO0OO0OOOO0 not in O0OOOO00OO0OOO0000O0O[O0OOOO00OO0OOOOOO0O0O]:
O0OOOO00OO0OOO0000O0O = O0OOOO00OO0OOO0000O0O[O0OOOO00OO0OOOOOO0O0O]
else:
O0OO0OOOOO00OOOOO0000.append(repl \* OOOO000O0OO0O000OO00O)
OOOOO0OO000OO0O0O00O0 += OOOO000O0OO0O000OO00O - 1
break
else:
O0OO0OOOOO00OOOOO0000.append(O0OOO00O0OO00OOO0O0O0[OOOOO0OO000OO0O0O00O0])
break
OOOOO0OO000OO0O0O00O0 += 1
return ''.join(O0OO0OOOOO00OOOOO0000)
if \_\_name\_\_ == "\_\_main\_\_":
OO000OO0O0OO00000OOO0 = O00000O00OOOOO00O0OO()
OO000OO0O0OO00000OOO0.O0O00OO0O0OO00O000O0OO('shieldwords.txt')
text = "小明骂小王是个王八蛋,小王骂小明是个王八羔子!"
OOOOOO00000OO0O0OO00O = OO000OO0O0OO00000OOO0.OO0000OOOO0OO00OOOO000(text)
print(OOOOOO00000OO0O0OO00O)
OO000OO0OOOOO00OOOO00 = time.time()
print('总共耗时:' + str(OO000OO0OOOOO00OOOO00 - OOO00OOOO0OOOO00O000O) + 's')
欢迎过往大牛针对ZFJPyMix提供宝贵的建议和意见!也欢迎大家进QQ群交流学习!
最后我想说,我们作为程序员应该尊重别人的劳动成果,尊重版权!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。