def switch_model(tablevel): if tablevel is None: tablevel = tab_level_1 param_name = randomvalue.string_value() param_value = randomvalue.int_value(switch_num_min, switch_num_max) param_name_selfid = randomvalue.string_value_num(3) switch_string = f"{tablevel}id {param_name_selfid} = self;\n{tablevel}int {param_name} = [{param_name_selfid} intValue];\n" # switch_string = f"{tablevel}int {param_name} = {param_value};\n" #switch的分支数 random_num = randomvalue.int_value(switch_num_min, switch_num_max) randstring1 = randomvalue.string_value() if random_num == switch_num_min: inline_string1 = inline_model_tablevel(tab_level_3) switch_string = f'{switch_string}{tablevel}switch ({param_name}) {{\n{tablevel}{tab_level_1}case 2:{{\n{inline_string1}{tablevel}{tab_level_2}}}break;\n{tablevel}{tab_level_1}default:{{\n{tablevel}{tab_level_2}NSLog(@\"do not catch anything\");\n{tablevel}{tab_level_2}}}break;\n\t}}\n' else: for num in range(switch_num_min, random_num + 1): if num == switch_num_min: inline_string1 = inline_model_tablevel(tab_level_3) switch_string = f'{switch_string}{tablevel}switch ({param_name}) {{\n{tablevel}{tab_level_1}case 2:{{\n{inline_string1}{tablevel}{tab_level_2}}}break;\n' elif num == random_num: switch_string = f'{switch_string}\n{tablevel}{tab_level_1}default:\n{tablevel}{tab_level_2}{{NSLog(@\"do not catch anything\");\n{tablevel}{tab_level_2}}}break;\n\t}}\n' else: inline_string1 = inline_model_tablevel(tab_level_3) num_str = str(num) switch_string = f'\n{switch_string}\n{tablevel}{tab_level_1}case {num_str}:{{\n{inline_string1}{tablevel}{tab_level_2}}}break;' return f'\n{switch_string}\n'
def newlajifile(lajifilenum): system_funcfile = open(config.system_func_path, 'r') system_func_lengh = len(system_funcfile.readlines()) system_funcfile.close() if os.path.exists(config.random_func_path): os.remove(config.random_func_path) if os.path.exists(config.random_class_name_path): os.remove(config.random_class_name_path) print(f'新建的文件个数:{str(lajifilenum)}') for index in range(0, lajifilenum): new_file_dic = filemanager.create_h_m( f'{config.get_xcodefile_path()}/{config.get_xcodefile_name()}', name=None) h_file = new_file_dic["h_path"] m_file = new_file_dic["m_path"] class_name = new_file_dic["class_name"] # 在project添加引用 subprocess.call( f'ruby {config.ruby_path} {config.get_xcodefile_project_path()} {config.get_xcodefile_name()} {class_name} {config.get_xcodefile_path()}/{config.get_xcodefile_name()}/', shell=True) classfile = open(config.random_class_name_path, 'a+') classfile.writelines(f'{class_name}\n') classfile.close() # 函数个数 func_num = randomvalue.int_value(config.m_h_num_min, config.m_h_num_max) for ii in range(1, func_num): func_arr = [] # 获取系统方法的字符串 for i in range(1, 3): random_row = randomvalue.int_value(1, system_func_lengh) random_line = linecache.getline(config.system_func_path, random_row) func_arr.append(random_line) # 创建随机函数并且写入txt文件,函数信息,包括返回类型、参数等 func_dic = randomvalue.func_create(config.random_func_path, 3, class_name, 1) # 函数声明(写进.h中) func_head_string = f'{codemodel.constom_func_head_model(func_dic)};' filemanager.write_string(h_file, f'{func_head_string}\n', line_num=None) # 函数实现(写进.m中) if_model_flag = randomvalue.half_probability() while_model_flag = randomvalue.half_probability() switch_model_flag = randomvalue.half_probability() funcwrite_string = codemodel.func_model(if_model_flag, while_model_flag, switch_model_flag, func_arr, 1, func_dic) filemanager.write_string(m_file, funcwrite_string, line_num=None)
def property_model(num_min, num_max): totle = randomvalue.int_value(num_min, num_max) property_string = "" for i in range(totle): index = randomvalue.int_value(0, len(property_list) - 1) property_name = randomvalue.string_value_num(2) property_string = f'{property_string}{property_list[index]}{property_name};\n' return property_string
def create_h_m(outpath, name): super_class = "NSObject" if name is None: index = randomvalue.int_value(0, len(file_type_pool) - 1) world = randomvalue.string_value() name = f'{world}{file_name_pool[index]}' super_class = f'{file_type_pool[index]}' newfile_h = open(f'{outpath}/{name}.h', 'w') h_string = f'#import <UIKit/UIKit.h>\nNS_ASSUME_NONNULL_BEGIN\n\n@interface {name} : {super_class}\n\n@end\n\nNS_ASSUME_NONNULL_END' newfile_h.write(h_string) newfile_h.close() newfile_m = open(f'{outpath}/{name}.m', 'w') m_string = f'#import \"{name}.h\"\n#import <objc/message.h>\n@implementation {name}\n\n@end' newfile_m.write(m_string) newfile_m.close() print(f'生成类:{name}') path_dir = { "m_path": f'{outpath}/{name}.m', "h_path": f'{outpath}/{name}.h', "class_name": f'{name}' } return path_dir
def if_model(tablevel): if tablevel is None: tablevel = tab_level_1 param_name = randomvalue.string_value_num(2) param_value = randomvalue.int_value(0, 1000) param_name_selfid = randomvalue.string_value_num(3) if_string = f"{tablevel}id {param_name_selfid} = self;\n{tablevel}int {param_name} = [{param_name_selfid} intValue];\n" #if的分支数 random_num = randomvalue.int_value(if_num_min, if_num_max) if random_num == 1: inline_string = inline_model_tablevel(tab_level_2) if_string = f'{if_string}{tablevel}if ({param_name} <= 1000){{\n{inline_string}\t}}\n' elif random_num == 2: inline_string1 = inline_model_tablevel(tab_level_2) inline_string2 = inline_model_tablevel(tab_level_2) if_string = f'{if_string}{tablevel}if ({param_name} <= 500){{\n{inline_string1}\t}}else{{\n{inline_string2}\t}}\n' else: level = 1000 / random_num for num in range(1, random_num + 1): if num == 1: inline_string = inline_model_tablevel(tab_level_2) level_num_str = str(level * num) if_string = f'{if_string}{tablevel}if ({param_name} <= {level_num_str}){{\n{inline_string}\t}}\n' elif num == random_num: inline_string = inline_model_tablevel(tab_level_2) if_string = f'{if_string}{tablevel}else{{\n{inline_string}\t}}\n' else: inline_string = inline_model_tablevel(tab_level_2) level_num_str = str(level * (num - 1)) level_num_str1 = str(level * num) if_string = f'{if_string}{tablevel}else if({param_name} > {level_num_str} && {param_name} <= {level_num_str1}){{\n{inline_string}\t}}\n' return f'\n{if_string}\n'
def func_model(if_model_flag, while_model_flag, switch_model_flag, func_call_string_array, tablevel, func_dic): if_string = "" if if_model_flag == 1: if_string = if_model(None) while_string = "" if while_model_flag == 1: while_string = while_model(None) switch_string = "" if switch_model_flag == 1: switch_string = switch_model(None) func_call_string = "" if len(func_call_string_array): for item in func_call_string_array: func_call_string_item = system_func_call_model( str(item).strip('\n'), tablevel) if func_call_string_item is None: func_call_string_item = " " func_call_string = f'{func_call_string}{func_call_string_item}' if func_call_string == "": func_call_string = " " func_head_string = constom_func_head_model(func_dic) # 每个模块随机乱序 random_sort = randomvalue.int_value(1, 4) if random_sort == 1: func_create_string = f'{func_head_string}{{\n{if_string}{while_string}{switch_string}{func_call_string}}}' elif random_sort == 2: func_create_string = f'{func_head_string}{{\n{while_string}{switch_string}{if_string}{func_call_string}}}' elif random_sort == 3: func_create_string = f'{func_head_string}{{\n{func_call_string}{while_string}{if_string}{switch_string}}}' else: func_create_string = f'{func_head_string}{{\n{switch_string}{func_call_string}{if_string}{while_string}}}' return f'\n{func_create_string}\n'
def writewhite(filepath, whitelist): write_file_list = filemanager.get_white_file(filepath, whitelist) for file_item in write_file_list: if os.path.splitext(file_item)[-1] == '.h': end_num = filemanager.find_key_line(file_item, "@interface") filemanager.write_string(file_item, codemodel.property_model( config.m_h_num_min, config.m_h_num_max), line_num=(int(end_num) + 1)) if os.path.splitext(file_item)[-1] == '.m': end_num = filemanager.find_key_line(file_item, "@implementation") randomint = randomvalue.int_value(config.m_h_num_min, config.m_h_num_max) for funcnum in range(randomint): func_dic = randomvalue.func_create(config.random_func_path, 3, None, None) filemanager.write_string(file_item, codemodel.func_model( 1, 1, 1, [], 1, func_dic), line_num=(int(end_num) + 1))
def main(argv): print("当前功能模块:【写入垃圾代码】") print(f'\n当前文件夹白名单:{config.file_while_list}') file_level = input(f'{OPTION}') file_num = randomvalue.int_value((int(file_level) * 10), (int(file_level) * 10 + 10)) property_flag = input(f"是否添加属性(是:y 默认:否)\n") white_write_flag = input(f"是否在白名单文件夹写入垃圾代码(是:y 默认:否)\n") while True: inpustring = input("工程文件路径\n") config.set_xcodefile_path(str(inpustring).strip()) if os.path.exists(config.get_xcodefile_path()) == True: break else: print("文件不存在,请重新输入") config.set_xcodefile_name(os.path.basename(config.get_xcodefile_path())) config.set_xcodefile_project_path( f'{config.get_xcodefile_path()}/{config.get_xcodefile_name()}.xcodeproj' ) if config.call_all_class == "": config.set_call_all_path( f'{config.get_xcodefile_path()}/{config.get_xcodefile_name()}') else: config.set_call_all_path(f'{config.get_xcodefile_path()}/HX') starttime = datetime.datetime.now() if white_write_flag == 'y' or white_write_flag == 'Y': writewhite(config.get_xcodefile_path(), config.file_while_list) if file_level != '0': newlajifile(file_num) creatswitch(property_flag) endtime = datetime.datetime.now() print(f'耗时:{(endtime - starttime).seconds}秒')