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 = solidityobf_random.randint(0, len(o) - 1)
                ito = solidityobf_random.randint(ifrom, len(o))
                s1 = deepcopy(o[ifrom:ito])
                s2 = deepcopy(o[ifrom:ito])
                for i in range(len(s2), 0, -1):
                    if solidityobf_random.randint(0, 4) == 0:
                        del s2[i - 1]
                flipcoin = solidityobf_random.randint(0, 1)
                if not flipcoin:
                    s1, s2 = s2, s1
                if not flipcoin:
                    ie = If([genPredicate(program, flipcoin)],
                            [Statements([EmptyStatement()]),
                             Statements(s2)])
                else:
                    ie = If([genPredicate(program, flipcoin)],
                            [Statements(s1),
                             Statements([EmptyStatement()])])
                program.replaceall(o[:ifrom])
                program.append(ie)
                program.append_list(o[ito:])
    return [], arg
def duplicate_function_func_post(program, arg):
    if program.__class__ == Function:
        if shouldi(arg=arg):
            if program.ident is not None:
                newfunc = deepcopy(program)
                statement, pos = get_upper_statement_and_pos(program)
                statement.insert(
                    solidityobf_random.randint(
                        0,
                        len(statement.statements_list) - 1), newfunc)
                sl = newfunc.statements.statements_list
                if len(sl) > 0:
                    for i in range(max(1, len(sl) / 3)):
                        del sl[solidityobf_random.randint(0, len(sl) - 1)]
                i = 0
                newname = program.ident.name
                n = newname
                while newname in arg["ident_present"]:
                    i = i + 1
                    newname = program.ident.name + format(i)
                    n += " " + newname
                print red(n)
                arg["ident_present"].append(newname)
                newfunc.replace_ident(Ident(newname))
    return [], arg
def add_if_statement(program_tree, verbose=0, numbermax=NUMBERMAX):
    if verbose > 1:
        print "apply add_if_statement transformation", "with", numbermax
    p = deepcopy(program_tree)
    arg = {"numbermax": numbermax}
    arg["size"] = count_object(p, [Statements])
    walker(p, postfunction=add_if_statement_func_post, 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 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 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 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
예제 #8
0
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 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 aggregate_data_post_func(program, arg):
    if program.__class__ in [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
예제 #12
0
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
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