前言
作为一门脚本语言,我们可以用它做很多事,比如数据分析、多媒体处理、web开发、网络编程、游戏编程、人工智能等等方面都被广泛的应用;如果我们用Python来开发商业化项目的时候,难免会遇到一些闲得蛋疼的,总想着来破解你的项目或者软件,来破坏别人的劳动成果;所以针对商业化项目做一些必要的加密和混淆是在所难免的!
混淆加密
网上有很多混淆的方法和技术,混淆都是为了增加代码的阅读难度;还有就是加密了,最基本的方法是发布pyc文件, 也就是将所有的.py源文件转换成pyc对外发布;还有一种方案是方案是借助cython. cython可以将python文件转换成c, 并编译成pyd文件;这里我们主要讲的是通过来混淆Python源码,以达到增加代码的阅读难度的目的!
混淆工具
具有混淆文件名、混淆类名、混淆属性变量名、混淆方法名和删除注释来混淆Python源码,具体功能结构图如下:
软件界面
下载地址
Gitee: https://gitee.com/zfj1128/ZFJPyMix
教学视频
链接:https://pan.baidu.com/s/1MEq-vyt_CMSLBdrI3PT6Yg 密码:tvbr
界面视图
混淆日志
混淆的部分日志,混淆的时候回通过import logging自动生成混淆日志《ZFJPyMix.log》,便于开发者修改部分报错:
混淆忽略
如果你在混淆过程中不想某个文件或者文件夹不希望被混淆,可以设置忽略文件或者文件夹,多个文件或者文件夹以‘,’进行分割就行了!
特别说明
部分功能
的所以功能中我需要说明的是变量过滤、方法过滤、类名过滤,我们在混淆过程中如果一些命名会导致系统的方法被混淆掉,或者直接混淆了系统的方法,比如方法:__ 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')
结束语
欢迎过往大牛针对提供宝贵的建议和意见!也欢迎大家进QQ群交流学习!