Commit 36bb9e41 by sikang

bug fix

parent 76a5c4f7
...@@ -3,6 +3,7 @@ import random ...@@ -3,6 +3,7 @@ import random
import re import re
import os import os
class JavaGarble: class JavaGarble:
__used_names = [] __used_names = []
__variables = [] __variables = []
...@@ -18,12 +19,13 @@ class JavaGarble: ...@@ -18,12 +19,13 @@ class JavaGarble:
self.__used_names.append(result) self.__used_names.append(result)
return result return result
# 生成变量 # 生成变量
def __generate_variable(self): def __generate_variable(self):
var_scope = ["public", "private", "protected"] var_scope = ["public", "private", "protected"]
var_types = ["String", "int", "double", "float"] var_types = ["String", "int", "double", "float"]
var_count = random.randint(random.randint(3,10), random.randint(20,30)) var_count = random.randint(
random.randint(3, 10), random.randint(20, 30))
for i in range(0, var_count): for i in range(0, var_count):
code = "%s %s %s = " % ( code = "%s %s %s = " % (
var_scope[random.randint(0, 2)], var_scope[random.randint(0, 2)],
...@@ -91,100 +93,103 @@ class JavaGarble: ...@@ -91,100 +93,103 @@ class JavaGarble:
for i in range(0, random.randint(0, 5)): for i in range(0, random.randint(0, 5)):
log_code += "android.util.Log.%s(\"%s\",\"%s\");\n" % ( log_code += "android.util.Log.%s(\"%s\",\"%s\");\n" % (
''.join(random.sample("idew", random.randint(1, 1))), ''.join(random.sample("idew", random.randint(1, 1))),
''.join(random.sample("ABCDEFGHIJKLMNOPQRSTUVWXYZ", random.randint(3, 10))), ''.join(random.sample(
''.join(random.sample("ABCDEFGHIJKLMNOPQRSTUVWXYZ", random.randint(5, 15))) "ABCDEFGHIJKLMNOPQRSTUVWXYZ", random.randint(3, 10))),
''.join(random.sample(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", random.randint(5, 15)))
) )
fun_content = fun_content.replace("${func_content}", log_code + return_value) fun_content = fun_content.replace(
"${func_content}", log_code + return_value)
self.__functions.append(fun_content) self.__functions.append(fun_content)
if len(self.__functions) < random.randint(3, 10): if len(self.__functions) < random.randint(3, 10):
self.__generate_function(model_path) self.__generate_function(model_path)
#插入代码 # 插入代码
def __insert_code(self): def __insert_code(self):
codes = self.__logic_model.split("//sep") codes = self.__logic_model.split("//sep")
code = codes[random.randint(0,len(codes)-1)] code = codes[random.randint(0, len(codes)-1)]
code.replace("${string}",self.__gen_rand_str()) code = code.replace("${string}", self.__gen_rand_str())\
code.replace("${name}",self.__gen_rand_str()) .replace("${name}", self.__gen_rand_str())\
code.replace("${number}",str(random.randint(1,10000))) .replace("${number}", str(random.randint(1, 10000)))
vars_size = len(self.__variables)-1 vars_size = len(self.__variables)-1
items = self.__variables[random.randint(0,vars_size)].split(" ") items = self.__variables[random.randint(0, vars_size)].split(" ")
if "String" in items[1]: if "String" in items[1]:
condition = "%s.length()%s%s"%(items[2],random.sample("><", 1), items[4]) condition = "%s.length()%s%s" % (
items[2], random.sample("><", 1), items[4])
else: else:
condition = "%s%s%s"%(items[2],random.sample("><", 1), items[4]) condition = "%s%s%s" % (items[2], random.sample("><", 1), items[4])
code.replace("${condition}",condition) code = code.replace("${condition}", condition)
content = "" content = ""
for i in range(0,random.randint(0,vars_size)): for i in range(0, random.randint(0, vars_size)):
var = self.__variables[random.randint(0,vars_size)] var = self.__variables[random.randint(0, vars_size)]
content += "%s + "% var.split(" ")[2] content += "%s + " % var.split(" ")[2]
content = "System.out.println(len(%s+\"\"));"%content content = "System.out.println(len(%s+\"\"));" % content
code.replace("${content}",content)
return code
code = code.replace("${content}", content)
return code
def __garble_java_file(self,file): def __garble_java_file(self, file):
self.__used_names = [] self.__used_names = []
self.__variables = [] self.__variables = []
self.__functions = [] self.__functions = []
with open(file,"r",encoding="utf-8") as fin: with open(file, "r", encoding="utf-8") as fin:
lines = fin.readlines() lines = fin.readlines()
#生成全局变量 # 生成全局变量
self.__generate_variable() self.__generate_variable()
#生成方法 # 生成方法
self.__generate_function("tools/garble/model/function_model.java") self.__generate_function("tools/garble/model/function_model.java")
ignore = False ignore = False
#花括号 # 花括号
curly_braces=0 curly_braces = 0
with open(file,"w+",encoding="utf-8") as fout: with open(file, "w+", encoding="utf-8") as fout:
for line in lines: for line in lines:
# interface 不混淆 # interface 不混淆
if "interface" in line and "{\n" in line or ignore: if "interface" in line and "{\n" in line or ignore:
ignore = True ignore = True
fout.write(line) fout.write(line)
continue continue
if (line.startswith("class ") or " class " in line) and "{\n" in line: if (line.startswith("class ") or " class " in line) and "{\n" in line:
#插入全局变量 # 插入全局变量
for var in self.__variables: for var in self.__variables:
line +="%s\n"% var line += "%s\n" % var
#插入方法 # 插入方法
for fun in self.__functions: for fun in self.__functions:
line +="%s\n"% fun line += "%s\n" % fun
curly_braces += line.count("{")
curly_braces -= line.count("}")
#定位1个缩进开头的类方法 # 定位1个缩进开头的类方法
if (line.startswith(" ") and not line.startswith(" ")) and ("){\n" in line or ") {\n" in line): if (line.startswith(" ") and not line.startswith(" ")) and ("){\n" in line or ") {\n" in line):
curly_braces = line.count("{") curly_braces = line.count("{")
curly_braces -= line.count("}") curly_braces -= line.count("}")
#一个花括号表示在方法的直接作用域内 # 一个花括号表示在方法的直接作用域内
if curly_braces == 1: if curly_braces == 1:
curly_braces += line.count("{")
curly_braces -= line.count("}")
if "return" in line: if "return" in line:
curly_braces = -10000 curly_braces = -10000
continue continue
if ";\n" in line: if ";\n" in line:
line +="\n%s"% self.__insert_code() line += "\n%s" % self.__insert_code()
fout.write(line) fout.write(line)
def java_garble(self,config): def java_garble(self, config):
self.__config = config self.__config = config
with open("tools/garble/model/logic_model.java", "r", encoding="utf-8") as fin: with open("tools/garble/model/logic_model.java", "r", encoding="utf-8") as fin:
self.__logic_model = fin.read() self.__logic_model = fin.read()
for main_dir in config['lib_main']: for main_dir in config['lib_main']:
for root, dirs, files in os.walk("%s/%s"%(config["root"],main_dir)): for root, dirs, files in os.walk("%s/%s" % (config["root"], main_dir)):
for file in files: for file in files:
if file.endswith(".java"): if file.endswith(".java"):
self.__garble_java_file(os.path.join(root,file)) self.__garble_java_file(os.path.join(root, file))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment