Exemplo n.º 1
0
def mix_code_block(smali_file):
    """Print the code blocks in a mixed way"""
    edit_method = False  # Editing a method
    block_count = 0  # Current block index
    code_blocks = []  # Code blocks array
    for smali_line in u.open_file_input(smali_file):  # For each line
        if is_beg_not_abstract_method(
                smali_line) and not edit_method:  # Method start
            edit_method = True
            block_count = 0
            code_blocks = []
            print smali_line,  # Print the line unchanged
        elif is_end_method(smali_line) and edit_method:  # Method end
            edit_method = False
            u.shuffle_list(code_blocks)
            for code_block in code_blocks:
                print code_block.smali_string,
            print smali_line,  # Print the line unchanged
        elif edit_method:
            if is_block_sign(smali_line):  # Block signpost found
                block_count += 1  # Increment block index
                curr_code_block = Code_block(block_count,
                                             '')  # Read code block
                code_blocks.append(curr_code_block)  # Add to block list
            else:
                if block_count != 0:
                    curr_code_block.put_code(
                        smali_line)  # Add smali code to current block
                else:
                    print smali_line,  # Print the line unchanged
        else:
            print smali_line,  # Print the line unchanged
Exemplo n.º 2
0
def change_cfg(smali_file_list):
    """"""
    for smali_file in smali_file_list:  # For each smali file
        edit_method = False
        for smali_line in u.open_file_input(smali_file):  # For each line
            #At the beggining of non-abstract method
            if re.search(
                    r'^([ ]*?)\.method', smali_line) is not None and re.search(
                        r'abstract', smali_line) is None and re.search(
                            r'native', smali_line) is None and not edit_method:
                #Append at the beginning of the method a Goto to the label located at end of the method, and a label to the real first istruction of the method itself
                print smali_line,
                print '    goto/32 :CFGGoto2'  # Goto END
                print '    :CFGGoto1'  # Label INIT
                edit_method = True  # We are in a method, and we must edit it
            #At the end of a method
            elif re.search(r'^([ ]*?)\.end method',
                           smali_line) is not None and edit_method:
                #Append at the end of the method a Goto to the label located at beginning of the method, and a label to the real last istruction of the method itself
                print '    :CFGGoto2'  # Label END
                print '    goto/32 :CFGGoto1'  # Goto INIT
                print smali_line,
                edit_method = False  # Successefull exit from a method
            else:
                print smali_line,  # Otherwise print the line unchanged
Exemplo n.º 3
0
def load_code_block(smali_file):
    """Read and split smali code into code blocks"""
    edit_method = False  # Editing a method
    jump_count = 0  # Current jump index
    for smali_line in u.open_file_input(smali_file):  # For each line
        if is_beg_not_abstract_method(
                smali_line) and not edit_method:  # Method start
            edit_method = True
            jump_count = 0
            print smali_line,  # Print the line unchanged
        elif is_end_method(smali_line) and edit_method:  # Method end
            edit_method = False
            print smali_line,  # Print the line unchanged
        elif edit_method:  # Reading method
            if is_block_sign(smali_line):  # Block signpost
                jump_name = u.get_random(True, 15)  # Random jump name
                jump_count += 1  # Increment jump count
                print '    goto/32 :l_' + jump_name + '_' + str(jump_count)
                print '    nop'
                print smali_line,
                print '    :l_' + jump_name + '_' + str(jump_count)
            else:
                print smali_line,  # Print the line unchanged
        else:
            print smali_line,  # Print the line unchanged
Exemplo n.º 4
0
def append_defunct_method(defunct_str, smali_file_list):
    """Append to each smali class file the defunct method"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            print smali_line,
            if re.search(r'^([ ]*?)# direct methods', smali_line) is not None:  # At the top of the direct methods section
                print defunct_str  # Append the defunct method
Exemplo n.º 5
0
def mix_code_block(smali_file):
    """Print the code blocks in a mixed way"""
    edit_method = False  # Editing a method
    block_count = 0  # Current block index
    code_blocks = []  # Code blocks array
    for smali_line in u.open_file_input(smali_file):  # For each line
        if is_beg_not_abstract_method(smali_line) and not edit_method:  # Method start
            edit_method = True
            block_count = 0
            code_blocks = []
            print smali_line,  # Print the line unchanged
        elif is_end_method(smali_line) and edit_method:  # Method end
            edit_method = False
            u.shuffle_list(code_blocks)
            for code_block in code_blocks:
                print code_block.smali_string,
            print smali_line,  # Print the line unchanged
        elif edit_method:
            if is_block_sign(smali_line):  # Block signpost found
                block_count += 1  # Increment block index
                curr_code_block = Code_block(block_count, "")  # Read code block
                code_blocks.append(curr_code_block)  # Add to block list
            else:
                if block_count != 0:
                    curr_code_block.put_code(smali_line)  # Add smali code to current block
                else:
                    print smali_line,  # Print the line unchanged
        else:
            print smali_line,  # Print the line unchanged
Exemplo n.º 6
0
def change_all_field(edited_field, smali_file_list, class_landroid_java_over_list):
    """Search for a filed reference in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(r'^([ ]*?)(((i|s)get(\-)?)|((i|s)put(\-)?))', smali_line) is not None:  # If contains a field reference
                change_match_line(smali_line, edited_field, class_landroid_java_over_list)
            else:
                print smali_line,  # Print the line unchanged
Exemplo n.º 7
0
def add_all_method(smali_file, new_method):
    """Add the indirection methods"""
    for smali_line in u.open_file_input(smali_file):  # For each line
        if re.search(r'^([ ]*?)# direct methods', smali_line) is not None:   # Before the directs methods
            print smali_line,  # Print the line unchanged
            print new_method.getvalue()  # Print the method
        else:
            print smali_line,  # Print the line unchanged
Exemplo n.º 8
0
def add_crypt_method(smali_file_list, class_name):
    """Search for a string in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(r'^([ ]*?)const\-string', smali_line) is not None:
                crypt_string(smali_line, class_name)
            else:
                print smali_line,  # Print back the line unchanged
Exemplo n.º 9
0
def find_all_final_string_field(smali_file_list):
    """Search for a field definition in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(r'^([ ]*?)\.field', smali_line) is not None:  # If this line contains a field definition
                get_match_line(smali_line)
            else:
                print smali_line,  # Print back the line unchanged
Exemplo n.º 10
0
def change_all_direct_method(smali_file_list, class_name):
    """Search for a method reference in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(r'^([ ]*?)invoke\-', smali_line) is not None:  # If contains a method reference
                change_match_line(smali_line, class_name)
            else:
                print smali_line,  # Print the line unchanged
Exemplo n.º 11
0
def add_crypt_method(smali_file_list, class_name):
    """Search for a string in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(r'^([ ]*?)const\-string', smali_line) is not None:
                crypt_string(smali_line, class_name)
            else:
                print smali_line,  # Print back the line unchanged
Exemplo n.º 12
0
def change_all_res_file(res_file_list, edited_res):
    """Search in all the resource XML files a resource reference"""
    for res_file in res_file_list:  # For each XML file
        if u.base_name(res_file) != 'public.xml':  # Do not edit the 'public.xml' file
            for res_line in u.open_file_input(res_file):  # For each line
                if re.search(r'@|name=|\:', res_line) is not None:  # If a resource reference is found
                    change_match_res_file(res_line, edited_res)
                else:  # Otherwise print back the line to file unchanged
                    print res_line,
Exemplo n.º 13
0
def append_defunct_method(defunct_str, smali_file_list):
    """Append to each smali class file the defunct method"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            print smali_line,
            if re.search(
                    r'^([ ]*?)# direct methods', smali_line
            ) is not None:  # At the top of the direct methods section
                print defunct_str  # Append the defunct method
Exemplo n.º 14
0
def add_all_method(smali_file, new_method):
    """Add the indirection methods"""
    for smali_line in u.open_file_input(smali_file):  # For each line
        if re.search(r'^([ ]*?)# direct methods',
                     smali_line) is not None:  # Before the directs methods
            print smali_line,  # Print the line unchanged
            print new_method.getvalue()  # Print the method
        else:
            print smali_line,  # Print the line unchanged
Exemplo n.º 15
0
def find_all_landroid_ljava_over(smali_file_list):
    """Find all the class definition subclasses of an SDK class"""
    for smali_file in smali_file_list:   # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            class_match = re.search(r'^([ ]*?)\.class(.*?)(?P<className>L([^;]*?);)', smali_line)  # Match the class definition
            if class_match is not None:
                class_name = class_match.group('className')  # Recover the class name
                if re.search(r'Landroid|Ljava', class_name):  # If the class is a subclass of an SDK class
                    yield class_name  # Return the class name
            print smali_line,  # Print back the line unchanged
Exemplo n.º 16
0
def find_all_native_method(smali_file_list):
    """Search for a method definition in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(r'^([ ]*?)\.method', smali_line) is not None and re.search(r' native ', smali_line) is not None:
                method_name = get_match_line(smali_line, [], False)
                if method_name is not None:
                    yield method_name  # Return the method name
            else:
                print smali_line,  # Print back the line unchanged
Exemplo n.º 17
0
def find_all_activity_field(smali_file_list):
    """Search for an activity definition in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(r'^\.super Landroid/app/Activity\;$', smali_line) is not None:  # If this line contains a field definition
                print '.super Landroid/app/ActivityOb;'
            elif re.search(r'Landroid/app/Activity\;\-\>', smali_line) is not None:
                print smali_line.replace('Landroid/app/Activity;-><', 'Landroid/app/ActivityOb;-><')
            else:
                print smali_line,  # Print back the line unchanged
Exemplo n.º 18
0
def find_all_final_string_field(smali_file_list):
    """Search for a field definition in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(
                    r'^([ ]*?)\.field', smali_line
            ) is not None:  # If this line contains a field definition
                get_match_line(smali_line)
            else:
                print smali_line,  # Print back the line unchanged
Exemplo n.º 19
0
def add_nop_in_method(smali_file, valid_op_code):
    """Remove the debug info from the file"""
    for smali_line in u.open_file_input(smali_file):  # For each line
        line_op_code = re.search(r'^([ ]*)(?P<opCode>([^ ]+)) ', smali_line)
        if line_op_code is not None:
            op_code = line_op_code.group('opCode')
            if op_code not in valid_op_code:  # If the istruction is not a debug information
                print smali_line,  # Print the original instruction
        else:
            print smali_line,  # Print the original instruction
Exemplo n.º 20
0
def add_nop_in_method(smali_file, valid_op_code):
    """Add multiple nop sequence of random lenght (1-3) between two nop-valid istruction"""
    for smali_line in u.open_file_input(smali_file):  # For each line
        print smali_line,  # Print the original instruction
        line_op_code = re.search(r'^([ ]*)(?P<opCode>([^ ]+)) ', smali_line)
        if line_op_code is not None:
            op_code = line_op_code.group('opCode')
            if op_code in valid_op_code:
                nop_count = u.random_nop_interval()  # Randomize the number of nop(s)
                print '    nop\n' * nop_count  # Print the nop(s)
Exemplo n.º 21
0
def purge_xml_tag_file(file_name):
    for xml_line in u.open_file_input(file_name):  # For each line
        xml_line = xml_line.replace('<b>', '\u003Cb\u003E')
        xml_line = xml_line.replace('</b>', '\u003C/b\u003E')
        xml_line = xml_line.replace('<i>', '\u003Ci\u003E')
        xml_line = xml_line.replace('</i>', '\u003C/i\u003E')
        xml_line = xml_line.replace('<u>', '\u003Cu\u003E')
        xml_line = xml_line.replace('</u>', '\u003C/u\u003E')
        xml_line = xml_line.replace('<font', '\u003Cfont')
        xml_line = xml_line.replace('</font>', '\u003C/font\u003E')
        print xml_line,  # Print back the line unchanged
Exemplo n.º 22
0
def find_all_direct_method(android_method_list, smali_file_list):
    """Search for a method definition in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
        #If this line contains a non constructor method definition
            if re.search(r'^([ ]*?)\.method', smali_line) is not None and re.search(r' constructor |\<init\>|\<clinit\>', smali_line) is None:
                method_name = get_match_line(smali_line, android_method_list, True)
                if method_name is not None:
                    yield method_name  # Return the method name
            else:
                print smali_line,  # Print back the line unchanged
Exemplo n.º 23
0
def purge_xml_tag_file(file_name):
    for xml_line in u.open_file_input(file_name):  # For each line
        xml_line = xml_line.replace('<b>', '\u003Cb\u003E')
        xml_line = xml_line.replace('</b>', '\u003C/b\u003E')
        xml_line = xml_line.replace('<i>', '\u003Ci\u003E')
        xml_line = xml_line.replace('</i>', '\u003C/i\u003E')
        xml_line = xml_line.replace('<u>', '\u003Cu\u003E')
        xml_line = xml_line.replace('</u>', '\u003C/u\u003E')
        xml_line = xml_line.replace('<font', '\u003Cfont')
        xml_line = xml_line.replace('</font>', '\u003C/font\u003E')
        print xml_line,  # Print back the line unchanged
Exemplo n.º 24
0
def add_nop_in_method(smali_file, valid_op_code):
    """Add multiple nop sequence of random lenght (1-3) between two nop-valid istruction"""
    for smali_line in u.open_file_input(smali_file):  # For each line
        print smali_line,  # Print the original instruction
        line_op_code = re.search(r'^([ ]*)(?P<opCode>([^ ]+)) ', smali_line)
        if line_op_code is not None:
            op_code = line_op_code.group('opCode')
            if op_code in valid_op_code:
                nop_count = u.random_nop_interval(
                )  # Randomize the number of nop(s)
                print '    nop\n' * nop_count  # Print the nop(s)
Exemplo n.º 25
0
def change_all_res_file(res_file_list, edited_res):
    """Search in all the resource XML files a resource reference"""
    for res_file in res_file_list:  # For each XML file
        if u.base_name(
                res_file) != 'public.xml':  # Do not edit the 'public.xml' file
            for res_line in u.open_file_input(res_file):  # For each line
                if re.search(r'@|name=|\:', res_line
                             ) is not None:  # If a resource reference is found
                    change_match_res_file(res_line, edited_res)
                else:  # Otherwise print back the line to file unchanged
                    print res_line,
Exemplo n.º 26
0
def change_all_field(edited_field, smali_file_list,
                     class_landroid_java_over_list):
    """Search for a filed reference in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(
                    r'^([ ]*?)(((i|s)get(\-)?)|((i|s)put(\-)?))',
                    smali_line) is not None:  # If contains a field reference
                change_match_line(smali_line, edited_field,
                                  class_landroid_java_over_list)
            else:
                print smali_line,  # Print the line unchanged
Exemplo n.º 27
0
def change_all_res_file(res_file_list, edited_class, package_name):
    """"Search in all the resource XML files a class reference"""
    for res_file in res_file_list:  # For each XML resource file
        for res_line in u.open_file_input(res_file):  # For each line
            if re.search(r'(\"|\<|\/)' + get_main_exec_dir() + '\.',
                         res_line) is not None:  # If contain a class signpost
                res_line = change_match_res_file(res_line, edited_class)
            if re.search(r'(\"|\<|\/)\.',
                         res_line) is not None:  # If contain a class signpost
                res_line = change_match_res_file_package(
                    res_line, edited_class, package_name)
            print res_line,  # Print the line back  unchanged
Exemplo n.º 28
0
def change_all_class(edited_class, smali_file_list):
    """Search for a class reference in all the the smali file"""
    for smali_file in smali_file_list:  # For each smali file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(
                    r'L([^;\(\) ]*?);',
                    smali_line) is not None:  # If contains a class reference
                change_match_line(smali_line, edited_class)
            elif re.search(r'\;\|Sign\|', smali_line) is not None:
                print smali_line.replace(';|Sign|', ''),
            else:
                print smali_line,  # Print the line unchanged
Exemplo n.º 29
0
def find_all_method(smali_file_list):
    """Match all methods declarations"""
    for smali_file in smali_file_list:  # For all smali file
        for smali_line in u.open_file_input(smali_file):  # For each line
            print smali_line,
            class_match = re.search(r'^([ ]*?)\.class(.*?)(?P<className>L([^;]*?);)', smali_line)  # Match class declaration
            if class_match is not None:
                class_name = class_match.group('className')  # Match class name
            if re.search(r'^([ ]*?)\.method', smali_line) is not None:  # Method delcaration
                method_name = get_match_line(smali_line, class_name)
                if method_name is not None:
                    yield method_name  # Return the method name
Exemplo n.º 30
0
def fix_safe_test(smali_file_list):
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(r'safetest', smali_line) is not None:
                class_match = re.search(
                    r'(?P<className>Lcom/safetest/[^;]*?;)', smali_line)
                if class_match is None:
                    print smali_line,
                else:
                    class_name = class_match.group(
                        'className')  # Recover the old class name
                    change_match_line(smali_line, [class_name])
            else:
                print smali_line,
Exemplo n.º 31
0
def find_all_landroid_ljava_over(smali_file_list):
    """Find all the class definition subclasses of an SDK class"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            class_match = re.search(
                r'^([ ]*?)\.class(.*?)(?P<className>L([^;]*?);)',
                smali_line)  # Match the class definition
            if class_match is not None:
                class_name = class_match.group(
                    'className')  # Recover the class name
                if re.search(r'Landroid|Ljava', class_name
                             ):  # If the class is a subclass of an SDK class
                    yield class_name  # Return the class name
            print smali_line,  # Print back the line unchanged
Exemplo n.º 32
0
def change_all_method(smali_file, new_method, all_method_list):
    """Redirect all the method calls"""
    for smali_line in u.open_file_input(smali_file):  # For each line
        class_match = re.search(r'^([ ]*?)\.class(.*?)(?P<className>L([^;]*?);)', smali_line)  # Match the class declaration
        if class_match is not None:
            class_name = class_match.group('className')  # Find the class name
        invoke_match = re.search(r'^([ ]*?)(?P<invokeType>invoke\-([^ ]*?)) {(?P<invokeParam>([vp0-9,. ]*?))}, (?P<invokeObject>L(.*?);|\[L(.*?);)->(?P<invokeMethod>(.*?))\((?P<invokePass>(.*?))\)(?P<invokeReturn>(.*?))$', smali_line)
        if invoke_match is not None:
            if not is_init(invoke_match.group('invokeMethod')):
                change_match_line(smali_line, invoke_match.group('invokeType'), invoke_match.group('invokeParam'), invoke_match.group('invokeObject'), invoke_match.group('invokeMethod'), invoke_match.group('invokePass'), invoke_match.group('invokeReturn'), class_name, new_method, all_method_list)
            else:
                print smali_line,  # Print the line unchanged
        else:
            print smali_line,  # Print the line unchanged
Exemplo n.º 33
0
def find_all_activity_field(smali_file_list):
    """Search for an activity definition in all the the smali file"""
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(
                    r'^\.super Landroid/app/Activity\;$', smali_line
            ) is not None:  # If this line contains a field definition
                print '.super Landroid/app/ActivityOb;'
            elif re.search(r'Landroid/app/Activity\;\-\>',
                           smali_line) is not None:
                print smali_line.replace('Landroid/app/Activity;-><',
                                         'Landroid/app/ActivityOb;-><')
            else:
                print smali_line,  # Print back the line unchanged
Exemplo n.º 34
0
def find_all_method(smali_file_list):
    """Match all methods declarations"""
    for smali_file in smali_file_list:  # For all smali file
        for smali_line in u.open_file_input(smali_file):  # For each line
            print smali_line,
            class_match = re.search(
                r'^([ ]*?)\.class(.*?)(?P<className>L([^;]*?);)',
                smali_line)  # Match class declaration
            if class_match is not None:
                class_name = class_match.group('className')  # Match class name
            if re.search(r'^([ ]*?)\.method',
                         smali_line) is not None:  # Method delcaration
                method_name = get_match_line(smali_line, class_name)
                if method_name is not None:
                    yield method_name  # Return the method name
Exemplo n.º 35
0
def define_code_block(smali_file, valid_op_code):
    """Try to define a code block"""
    edit_method = False  # Editing a method
    in_try = False  # In a try-catch
    for smali_line in u.open_file_input(smali_file):  # For each line
        if is_beg_not_abstract_method(
                smali_line) and not edit_method:  # Method start
            edit_method = True
            in_try = False
            print smali_line,  # Print the line unchanged
        elif is_end_method(smali_line) and edit_method:  # Method end
            edit_method = False
            print smali_line,  # Print the line unchanged
        elif edit_method:
            line_op_code = re.search(r'^([ ]*)(?P<opCode>([^ \n]+))([ ]|$)',
                                     smali_line)  # Match a line
            if line_op_code is not None:
                op_code = line_op_code.group('opCode')
                if re.search(r'^([ ]*?):try_start',
                             op_code) is not None:  # Try start
                    in_try = True  # In try
                if re.search(r'^([ ]*?):try_end_',
                             op_code) is not None:  # Try end
                    in_try = False  # Out try
                if op_code in valid_op_code and not in_try:
                    print '#!Block!#'  # Print block signpost
                    new_if = if_mapping.get(op_code, None)
                    if new_if is not None:
                        line_op_code = re.search(
                            r'^([ ]*)(?P<opCode>([^ ]+)) (?P<regGo>[^:]*?):(?P<labelGo>[^ ]*?)$',
                            smali_line)  # Match a line
                        if line_op_code is not None:
                            regGo = line_op_code.group('regGo')
                            labelGo = line_op_code.group('labelGo')
                            goto32_name = u.get_random(True,
                                                       15)  # Random jump name
                            print '    ' + new_if + ' ' + regGo + ':gl_' + goto32_name
                            print '    goto/32 :' + labelGo
                            print '    :gl_' + goto32_name
                    else:
                        print smali_line,
                else:
                    print smali_line,  # Print the line unchanged
            else:
                print smali_line,  # Print the line unchanged
        else:
            print smali_line,  # Print the line unchanged
Exemplo n.º 36
0
def add_arithmetic_dranch_in_method(smali_file):
    """Add a fake arithmetic branch near each valid istruction"""
    edit_method = False  # Out Method
    junk_name = None
    this_name = None
    for smali_line in u.open_file_input(smali_file):  # For each line
        #Entering non abstract method
        if re.search(r'^([ ]*?)\.method', smali_line) is not None and re.search(r' abstract ', smali_line) is None and re.search(r' native ', smali_line) is None and not edit_method:
            print smali_line,
            edit_method = True  # In method
        #Exiting method
        elif re.search(r'^([ ]*?)\.end method', smali_line) is not None and edit_method:
            if junk_name is not None and this_name is not None:
                print '    :' + junk_name
                print '    goto/32 :' + this_name
            print smali_line,
            edit_method = False  # Out Method
            junk_name = None
            this_name = None
        elif edit_method:  # If in method
            print smali_line,
            locals_match = re.search(r'^([ ]*?)\.locals (?P<localCount>([0-9]+))$', smali_line)
            if locals_match is not None:
                local_count = locals_match.group('localCount')
                if int(local_count) >= 2:  # If exist at least 2 register
                    rand_int_v0 = u.get_random_int(1, 32)  # Random integer in the first one
                    rand_int_v1 = u.get_random_int(1, 32)  # Random integer in the second one
                    #Add the fake branch
                    print ''
                    print '    const v0, ' + str(rand_int_v0)
                    print '    const v1, ' + str(rand_int_v1)
                    print '    add-int v0, v0, v1'
                    print '    add-int v0, v0, v1'
                    print '    rem-int v0, v0, v1'
                    junk_name = u.get_random(True, 15)
                    this_name = u.get_random(True, 15)
                    goto32_name = u.get_random(True, 15)
                    print '    if-gtz v0, :' + goto32_name
                    print '    goto/32 :' + junk_name
                    print '    :' + goto32_name
                    print '    :' + this_name
        else:
            print smali_line,
Exemplo n.º 37
0
def define_code_block(smali_file, valid_op_code):
    """Try to define a code block"""
    edit_method = False  # Editing a method
    in_try = False  # In a try-catch
    for smali_line in u.open_file_input(smali_file):  # For each line
        if is_beg_not_abstract_method(smali_line) and not edit_method:  # Method start
            edit_method = True
            in_try = False
            print smali_line,  # Print the line unchanged
        elif is_end_method(smali_line) and edit_method:  # Method end
            edit_method = False
            print smali_line,  # Print the line unchanged
        elif edit_method:
            line_op_code = re.search(r"^([ ]*)(?P<opCode>([^ \n]+))([ ]|$)", smali_line)  # Match a line
            if line_op_code is not None:
                op_code = line_op_code.group("opCode")
                if re.search(r"^([ ]*?):try_start", op_code) is not None:  # Try start
                    in_try = True  # In try
                if re.search(r"^([ ]*?):try_end_", op_code) is not None:  # Try end
                    in_try = False  # Out try
                if op_code in valid_op_code and not in_try:
                    print "#!Block!#"  # Print block signpost
                    new_if = if_mapping.get(op_code, None)
                    if new_if is not None:
                        line_op_code = re.search(
                            r"^([ ]*)(?P<opCode>([^ ]+)) (?P<regGo>[^:]*?):(?P<labelGo>[^ ]*?)$", smali_line
                        )  # Match a line
                        if line_op_code is not None:
                            regGo = line_op_code.group("regGo")
                            labelGo = line_op_code.group("labelGo")
                            goto32_name = u.get_random(True, 15)  # Random jump name
                            print "    " + new_if + " " + regGo + ":gl_" + goto32_name
                            print "    goto/32 :" + labelGo
                            print "    :gl_" + goto32_name
                    else:
                        print smali_line,
                else:
                    print smali_line,  # Print the line unchanged
            else:
                print smali_line,  # Print the line unchanged
        else:
            print smali_line,  # Print the line unchanged
Exemplo n.º 38
0
def find_all_class(smali_file_list):
    """Search for a class definition in all the the smali file"""
    annotation_flag = False
    signature_flag = False
    for smali_file in smali_file_list:  # For each file
        for smali_line in u.open_file_input(smali_file):  # For each line
            if re.search(
                    r'^([ ]*?)\.source', smali_line
            ) is not None:  # If this line contains a class definition
                get_match_source_line(smali_line)
            elif re.search(
                    r'^([ ]*?)\.class', smali_line
            ) is not None:  # If this line contains a class definition
                class_name = get_match_line(smali_line)
                if class_name is not None:
                    yield class_name
            elif re.search(
                    r'^([ ]*?)\.annotation system Ldalvik/annotation/InnerClass;',
                    smali_line) is not None:
                annotation_flag = True
                print smali_line,
            elif re.search(
                    r'^([ ]*?)\.annotation system Ldalvik/annotation/Signature;',
                    smali_line) is not None:
                signature_flag = True
                print smali_line,
            elif re.search(r'^([ ]*?)\.end annotation',
                           smali_line) is not None and annotation_flag is True:
                annotation_flag = False
                print smali_line,
            elif re.search(r'^([ ]*?)\.end annotation',
                           smali_line) is not None and signature_flag is True:
                signature_flag = False
                print smali_line,
            elif annotation_flag is True and re.search(r'^([ ]*?)name = \"',
                                                       smali_line):
                get_match_subclass_annotation(smali_line)
            elif signature_flag is True and re.search(r'^([ ]*?)\"(.*)\"',
                                                      smali_line):
                get_match_subclass_signature(smali_line)
            else:
                print smali_line,  # Print the line unchanged
Exemplo n.º 39
0
def change_cfg(smali_file_list):
    """"""
    for smali_file in smali_file_list:  # For each smali file
        edit_method = False
        for smali_line in u.open_file_input(smali_file):  # For each line
            #At the beggining of non-abstract method
            if re.search(r'^([ ]*?)\.method', smali_line) is not None and re.search(r'abstract', smali_line) is None and re.search(r'native', smali_line) is None and not edit_method:
                #Append at the beginning of the method a Goto to the label located at end of the method, and a label to the real first istruction of the method itself
                print smali_line,
                print '    goto/32 :CFGGoto2'  # Goto END
                print '    :CFGGoto1'  # Label INIT
                edit_method = True  # We are in a method, and we must edit it
            #At the end of a method
            elif re.search(r'^([ ]*?)\.end method', smali_line) is not None and edit_method:
                #Append at the end of the method a Goto to the label located at beginning of the method, and a label to the real last istruction of the method itself
                print '    :CFGGoto2'  # Label END
                print '    goto/32 :CFGGoto1'  # Goto INIT
                print smali_line,
                edit_method = False  # Successefull exit from a method
            else:
                print smali_line,  # Otherwise print the line unchanged
Exemplo n.º 40
0
def load_code_block(smali_file):
    """Read and split smali code into code blocks"""
    edit_method = False  # Editing a method
    jump_count = 0  # Current jump index
    for smali_line in u.open_file_input(smali_file):  # For each line
        if is_beg_not_abstract_method(smali_line) and not edit_method:  # Method start
            edit_method = True
            jump_count = 0
            print smali_line,  # Print the line unchanged
        elif is_end_method(smali_line) and edit_method:  # Method end
            edit_method = False
            print smali_line,  # Print the line unchanged
        elif edit_method:  # Reading method
            if is_block_sign(smali_line):  # Block signpost
                jump_name = u.get_random(True, 15)  # Random jump name
                jump_count += 1  # Increment jump count
                print "    goto/32 :l_" + jump_name + "_" + str(jump_count)
                print "    nop"
                print smali_line,
                print "    :l_" + jump_name + "_" + str(jump_count)
            else:
                print smali_line,  # Print the line unchanged
        else:
            print smali_line,  # Print the line unchanged
Exemplo n.º 41
0
def change_all_method(smali_file, new_method, all_method_list):
    """Redirect all the method calls"""
    for smali_line in u.open_file_input(smali_file):  # For each line
        class_match = re.search(
            r'^([ ]*?)\.class(.*?)(?P<className>L([^;]*?);)',
            smali_line)  # Match the class declaration
        if class_match is not None:
            class_name = class_match.group('className')  # Find the class name
        invoke_match = re.search(
            r'^([ ]*?)(?P<invokeType>invoke\-([^ ]*?)) {(?P<invokeParam>([vp0-9,. ]*?))}, (?P<invokeObject>L(.*?);|\[L(.*?);)->(?P<invokeMethod>(.*?))\((?P<invokePass>(.*?))\)(?P<invokeReturn>(.*?))$',
            smali_line)
        if invoke_match is not None:
            if not is_init(invoke_match.group('invokeMethod')):
                change_match_line(smali_line, invoke_match.group('invokeType'),
                                  invoke_match.group('invokeParam'),
                                  invoke_match.group('invokeObject'),
                                  invoke_match.group('invokeMethod'),
                                  invoke_match.group('invokePass'),
                                  invoke_match.group('invokeReturn'),
                                  class_name, new_method, all_method_list)
            else:
                print smali_line,  # Print the line unchanged
        else:
            print smali_line,  # Print the line unchanged
Exemplo n.º 42
0
def change_all_res_file_package(res_file_list, rename_list):
    """"Search in all the resource XML files a reference"""
    for res_file in res_file_list:  # For each XML resource file
        for res_line in u.open_file_input(res_file):  # For each line
            change_match_res_file_of_package(res_line, rename_list)