def duplicate_function(program_tree, verbose=0, numbermax=NUMBERMAX): if verbose > 1: print "apply duplicate_function transformation", "with", numbermax p = deepcopy(program_tree) ret = get_all_ident(program_tree) arg = {"ident_present": ret, "numbermax": numbermax} arg["size"] = count_object(p, [Statements]) walker(p, postfunction=duplicate_function_func_post, arg=arg) return p
def outlining(program_tree, verbose=0, numbermax=NUMBERMAX): if verbose > 1: print "apply outlining transformation" p = deepcopy(program_tree) ret = get_all_ident(program_tree) arg = {"ident_present": ret, "encountered": [], "numbermax": numbermax} arg["size"] = count_object(p, [Statements]) walker(p, postfunction=outlining_post_func, arg=arg) return p
def modify_data_flow_1(program_tree,verbose=0,numbermax=NUMBERMAX): if verbose >1: print "apply modify_data_flow_1 transformation" p = deepcopy(program_tree) ret = get_all_ident(program_tree) arg={"ident_present":ret,"numbermax":numbermax} arg["size"]=count_object(p,[Assignment]) walker(p,postfunction=modify_data_flow_1_post_func,arg=arg) return p
def change_str(program_tree, verbose=0, numbermax=NUMBERMAX): if verbose > 1: print "apply change_str transformation", "with", numbermax p = deepcopy(program_tree) ret = get_all_ident(program_tree) arg = {"numbermax": numbermax, "ident_present": ret} arg["size"] = count_object(p, [String]) walker(p, postfunction=change_str_func, arg=arg) return p
def try_eval(program_tree, verbose=False, numbermax=NUMBERMAX): if verbose: print "apply try_eval transformation" p = deepcopy(program_tree) memory = [] walker(p, function=try_eval_func, postfunction=try_eval_post_func, arg=memory) return p
def evalification(program_tree, verbose=False, numbermax=NUMBERMAX): if verbose: print "apply evalification transformation" p = deepcopy(program_tree) arg = {"numbermax": numbermax} arg["size"] = count_object(p, [Statements]) #~ ret =get_all_ident(program_tree) walker(p, evalification_func, arg=arg) return p
def rename_variables(program_tree, verbose=0, numbermax=NUMBERMAX): if verbose > 1: print "apply rename_variables transformation" p = deepcopy(program_tree) ret = get_all_ident(program_tree) if "eval" in ret: return program_tree arg = {"idents": ret, "switcher": [], "numbermax": numbermax} arg["size"] = count_object(p, [Ident]) walker(p, rename_variables_func, rename_variables_post_func, arg=arg) return p
def add_dummy_variables(parse_tree, verbose=0, numbermax=NUMBERMAX): """This function takes in arpument parse_tree as a parse tree, and will add some dummy variables, it will return a new parse tree, with these dummy variables the verbose argument allows to increase verbosity""" if verbose > 1: print "apply add_dummy_var transformation" p = deepcopy(parse_tree) ret = get_all_ident(parse_tree) arg = {"ident_present": ret, "numbermax": numbermax} arg["size"] = count_object(p, [Statements]) walker(p, postfunction=add_dummy_var_func_post, arg=arg) return p
def add_if_statement_2_func_post(program, arg): if program.__class__ == Statements: if shouldi(arg=arg) and not is_function_inside(program): o = program.statements_list if len(o) > 0: ifrom = jshadobf_random.randint(0, len(o) - 1) ito = jshadobf_random.randint(ifrom, len(o)) s1 = deepcopy(o[ifrom:ito]) s2 = deepcopy(o[ifrom:ito]) for i in range(len(s2), 0, -1): if jshadobf_random.randint(0, 4) == 0: del s2[i - 1] flipcoin = jshadobf_random.randint(0, 1) if not flipcoin: s1, s2 = s2, s1 ie = If(genPredicate(program, flipcoin), Statements(s1), Statements(s2)) program.replaceall(o[:ifrom]) program.append(ie) program.append_list(o[ito:]) return [], arg
def aggregate_data(program_tree, verbose=0, numbermax=NUMBERMAX): if verbose > 1: print "apply aggregate_data transformation" p = deepcopy(program_tree) ret = get_all_ident(program_tree) arg = { "ident_present": ret, "numbermax": numbermax, "list_aggregation": [] } arg["size"] = count_object(p, [Number]) walker(p, aggregate_data_func, aggregate_data_post_func, arg=arg) return p
def modify_data_flow_2_func(program,arg): if isinstance(program,Number): if shouldi(arg=arg): s,pos = get_upper_statement_and_pos(program) if s != None: whereto = jshadobf_random.randint(0,pos) newp = deepcopy(program) vardeclist,name=genVarNotIn(arg["ident_present"],newp) program.parent.replace_item(program, Ident(name)) s.insert(whereto,vardeclist) if isinstance(program,List): if shouldi(arg=arg): s,pos = get_upper_statement_and_pos(program) if s != None and not is_functioncall_inside(program): whereto = jshadobf_random.randint(0,pos) if count_object(program,[Ident] ): whereto = pos newp = deepcopy(program) vardeclist,name=genVarNotIn(arg["ident_present"],newp) program.parent.replace_item(program, Ident(name)) s.insert(whereto,vardeclist) return [] , arg
def aggregate_data_post_func(program, arg): if program.__class__ in [Program, Function]: l = arg["list_aggregation"].pop() perm = random_list_int_exclusive(0, len(l) - 1) # perm = range(0,len(l)) newl = deepcopy(l) newl = permute_list(newl, perm) vardeclist, name = genVarNotIn(arg["ident_present"], List(newl)) i = 0 for item in l: item.parent.replace_item( item, Expr([Ident(name), Index(Number(perm.index(i)))])) i += 1 program.statements.insert(0, vardeclist) # # if vardeclist != None: # # s.statements_list.insert(pos,Statement_plus_ending(Statements([Assignment(Var(Ident(name)),"=",program.expr)]))) # s.insert(whereto,vardeclist) # if isinstance(program.parent,Expr): # program.parent.replace_expr(program, Expr([Ident(name)])) return [], arg
def findrefvar(program, arg): #~ print program.__class__ if arg == None: return [], None if isinstance(program, VarDeclaration): arg["var_dec_in"].append(program.var.name) if isinstance(program, Assignment): pv = deepcopy(program.var) removeindex(pv) arg["assignment"].append(str(pv)) if isinstance(program, This): return [], None if isinstance(program, Break): return [], None if isinstance(program, Ident): if program.name == "eval": print RED, "FIND EVAL OR THIS", BLACK return [], None if not ident_is_property( program): #and not ident_is_function(program) : a = ident_is_in_left_handside_assignment(program) if not a or a.typeof != "=": arg["ref"].append(program.name) return [], arg
def remove_empty_statement(program_tree, verbose=False, numbermax=NUMBERMAX): if verbose: print "apply remove_empty_statement transformation" p = deepcopy(program_tree) walker(p, remove_empty_statement_func, arg=[]) return p
def outlining_post_func(program, arg): if isinstance(program, Statements): if shouldi(arg=arg) and len(program.statements_list) != 0: f = jshadobf_random.randint(0, len(program.statements_list) - 1) t = jshadobf_random.randint(f + 1, len(program.statements_list)) outline = program.statements_list[f:t] if not is_return_inside(outline) and not is_function_inside( outline): funcobj, funcname = genVarNotIn(arg["ident_present"]) presence = detect_presence(outline) if not (presence["arguments"] or presence["this_present"] or presence["break_present"] or presence["eval_present"]): examine_outline = examine_function_vars(outline) upfunc = get_upper_function(program) examine_before = examine_function_vars( upfunc, program.statements_list[f]) examine_func = examine_function_vars(upfunc) if isinstance(upfunc, Function): for v in upfunc.args_dec.args_list: include_ident(examine_before["local_var"], v) nameargs ,varout,vardecout , vardec,todec= [],[],[],[],[] if all_function_call_are_functionnal(outline): for k in examine_outline["local_var"].keys(): if examine_before["local_var"].has_key( k) and examine_before["local_var"][k][0][ "has_been_assigned"]: nameargs.append(k) for k in examine_outline["var_global_ref"].keys(): if examine_before["local_var"].has_key(k): nameargs.append(k) elif examine_func["local_var"].has_key(k): todec.append(k) elif examine_before["var_global_set"].has_key(k): nameargs.append(k) for k in examine_outline["var_global_set"].keys(): if examine_before["local_var"].has_key(k): nameargs.append(k) elif examine_func["local_var"].has_key(k): todec.append(k) for k in examine_outline["local_var"].keys(): vardecout.append(k) for k in examine_outline["local_var"].keys(): if examine_outline["local_var"][k][0][ "has_been_assigned"]: varout.append(k) for k in examine_outline["var_global_set"].keys(): varout.append(k) movable = all([ k in nameargs + vardecout for k in examine_outline["local_var"].keys() + examine_outline["var_global_ref"].keys() ] + [examine_outline["var_global_set"].keys() == []]) elif len(examine_outline["local_var"]) == 0: nameargs, varout, vardecout = [], [], [] movable = False else: return [], arg vardecout = list(set(vardecout)) varout = list(set(varout)) nameargs = list(set(nameargs)) rem = range(f, t) rem.reverse() for i in rem: del program.statements_list[i] outline = [ Var([VarDeclaration(Ident(n))]) for n in vardec ] + outline outline += [ Return(Expr([List([Ident(n) for n in varout])])) ] listarg = Listarguments([Ident(n) for n in nameargs]) listretobj, listretname = genVarNotIn(arg["ident_present"]) i = 0 for n in varout: program.insert( f, Assignment( Ident(n), "=", MemberExpr(Expr([Ident(listretname)]), [Index(Expr([Number(str(i))]))]))) i = i + 1 for n in vardecout: program.insert(f, Var([VarDeclaration(Ident(n))])) i = i + 1 for n in todec: outline.insert(0, Var([VarDeclaration(Ident(n))])) program.insert( f, Var([ VarDeclaration( Ident(listretname), Functioncall(Ident(funcname), listarg)) ])) uppers = [] if movable: uppers = get_all_upper_statement_and_pos(program) uppers = uppers + [(program, f)] upper, pos = uppers[jshadobf_random.randint( 0, len(uppers) - 1)] outline_statement = Statements(outline) l1 = deepcopy(listarg) upper.insert( pos, Function(Ident(funcname), l1, outline_statement)) return [], arg
def simplify_if(program_tree, verbose=False, numbermax=NUMBERMAX): if verbose: print "apply simplify_if transformation" p = deepcopy(program_tree) walker(p, simplify_if_func, arg=[]) return p
tree=None, path=option.file, strict=False, test_n_times=1, numbermax=1000, transformations_available=transfomations_available, objectives=["mu1", "mu2", "mu3", "mu4", "mu5", "exectime"], verbose=option.verbose) # print to_apply adam.convert() print yellow(adam) testnum = 0 cont = True while cont: individual = deepcopy(adam) files = [] testnum += 1 print yellow("Start %d" % testnum) for i in range(20): individual.mutate() individual.evaluate() files.append(individual.runner.modified_executable) print individual.objectives if individual.worst: sum_file_name = tempfile.mktemp(prefix="summary_", suffix=".txt", dir=option.output_dir) with open(sum_file_name, "w") as sum_file: sum_file.write("TRANSFORMATIONS:\n") sum_file.write("\n".join(individual.transformations))