コード例 #1
0
    def obfuscate(self, obfuscation_info: Obfuscation):
        self.logger.info('Running "{0}" obfuscator'.format(
            self.__class__.__name__))

        try:
            # Change default namespace.
            Xml.register_namespace(
                "obfuscation", "http://schemas.android.com/apk/res/android")

            xml_parser = Xml.XMLParser(encoding="utf-8")
            manifest_tree = Xml.parse(obfuscation_info.get_manifest_file(),
                                      parser=xml_parser)
            manifest_root = manifest_tree.getroot()
            self.remove_xml_duplicates(manifest_root)
            self.scramble_xml_element(manifest_root)
            self.indent_xml(manifest_root)

            # Write the changes into the manifest file.
            manifest_tree.write(obfuscation_info.get_manifest_file(),
                                encoding="utf-8")

        except Exception as e:
            self.logger.error(
                'Error during execution of "{0}" obfuscator: {1}'.format(
                    self.__class__.__name__, e))
            raise

        finally:
            obfuscation_info.used_obfuscators.append(self.__class__.__name__)
コード例 #2
0
 def test_get_manifest_file(self, tmp_demo_apk_v10_original_path: str):
     obfuscation = Obfuscation(tmp_demo_apk_v10_original_path)
     manifest = obfuscation.get_manifest_file()
     assert os.path.isfile(manifest)
コード例 #3
0
    def obfuscate(self, obfuscation_info: Obfuscation):
        self.logger.info('Running "{0}" obfuscator'.format(self.__class__.__name__))

        try:
            Xml.register_namespace('android', 'http://schemas.android.com/apk/res/android')

            xml_parser = Xml.XMLParser(encoding='utf-8')
            manifest_tree = Xml.parse(obfuscation_info.get_manifest_file(), parser=xml_parser)
            manifest_root = manifest_tree.getroot()

            self.package_name = manifest_root.get('package')
            if not self.package_name:
                raise Exception('Unable to extract package name from application manifest')

            # Get a mapping between class name and smali file path.
            for smali_file in util.show_list_progress(obfuscation_info.get_smali_files(),
                                                      interactive=obfuscation_info.interactive,
                                                      description='Class name to smali file mapping'):
                with open(smali_file, 'r', encoding='utf-8') as current_file:
                    class_name = None
                    for line in current_file:
                        if not class_name:
                            # Every smali file contains a class.
                            class_match = util.class_pattern.match(line)
                            if class_match:
                                self.class_name_to_smali_file[class_match.group('class_name')] = smali_file
                                break

            self.transform_package_name(manifest_root)

            # Write the changes into the manifest file.
            manifest_tree.write(obfuscation_info.get_manifest_file(), encoding='utf-8')

            xml_files: Set[str] = set(
                os.path.join(root, file_name)
                for root, dir_names, file_names in os.walk(obfuscation_info.get_resource_directory())
                for file_name in file_names if file_name.endswith('.xml') and 'layout' in root  # Only layout files.
            )
            xml_files.add(obfuscation_info.get_manifest_file())

            # TODO: use the following code to rename only the classes declared in application's package.
            # package_smali_files: Set[str] = set(
            #     smali_file for class_name, smali_file in self.class_name_to_smali_file.items()
            #     if class_name[1:].startswith(self.package_name.replace('.', '/'))
            # )
            #
            # # Rename the classes declared in the application's package.
            # class_rename_transformations = self.rename_class_declarations(list(package_smali_files),
            #                                                               obfuscation_info.interactive)

            # Rename all classes declared in smali files.
            class_rename_transformations = self.rename_class_declarations(obfuscation_info.get_smali_files(),
                                                                          obfuscation_info.interactive)

            # Update renamed classes through all the smali files.
            self.rename_class_usages_in_smali(obfuscation_info.get_smali_files(), class_rename_transformations,
                                              obfuscation_info.interactive)

            # Update renamed classes through all the xml files.
            self.rename_class_usages_in_xml(list(xml_files), class_rename_transformations,
                                            obfuscation_info.interactive)

        except Exception as e:
            self.logger.error('Error during execution of "{0}" obfuscator: {1}'.format(self.__class__.__name__, e))
            raise

        finally:
            obfuscation_info.used_obfuscators.append(self.__class__.__name__)