Ejemplo n.º 1
0
def get_cmd_args(args: list = None):
    """
    Parse and return the command line parameters needed for the script execution.

    :param args: List of arguments to be parsed (by default sys.argv is used).
    :return: The command line needed parameters.
    """

    obfuscators = ObfuscatorManager().get_obfuscators_names()

    parser = argparse.ArgumentParser(
        prog="python3 -m obfuscapk.cli",
        description=
        "Obfuscate an application (.apk) without needing its source code.",
    )
    parser.add_argument(
        "apk_file",
        type=str,
        metavar="<APK_FILE>",
        help="The path to the application (.apk) to obfuscate",
    )
    parser.add_argument(
        "-o",
        "--obfuscator",
        action="append",
        metavar="OBFUSCATOR",
        choices=obfuscators,
        help=
        "The name of the obfuscator to use. Can be specified multiple times to "
        "use more obfuscators (in sequence). Allowed values are: {0}".format(
            ", ".join(obfuscators)),
        required=True,
    )
    parser.add_argument(
        "-w",
        "--working-dir",
        type=str,
        metavar="DIR",
        help=
        "The working directory that will contain the intermediate files. By "
        "default a directory will be created in the same directory as the input "
        "application. If the specified directory doesn't exist, it will be created",
    )
    parser.add_argument(
        "-d",
        "--destination",
        type=str,
        metavar="OUT_APK",
        help=
        "The path where to save the obfuscated .apk file. By default the file "
        "will be saved in the working directory",
    )
    parser.add_argument(
        "-i",
        "--ignore-libs",
        action="store_true",
        help=
        "Ignore known third party libraries during the obfuscation operations",
    )
    parser.add_argument(
        "-p",
        "--show-progress",
        action="store_true",
        dest="interactive",
        help="Show obfuscation progress (as a progress bar)",
    )
    parser.add_argument(
        "-k",
        "--virus-total-key",
        action="append",
        metavar="VT_API_KEY",
        help=
        "When using Virus Total obfuscator, a valid API key has to be provided. "
        "Can be specified multiple times to use a different API key for each request "
        "(cycling through the API keys)",
    )
    return parser.parse_args(args)
Ejemplo n.º 2
0
def perform_obfuscation(
    input_apk_path: str,
    obfuscator_list: List[str],
    working_dir_path: str = None,
    obfuscated_apk_path: str = None,
    interactive: bool = False,
    ignore_libs: bool = False,
    virus_total_api_key: List[str] = None,
):
    """
    Apply the obfuscation techniques to an input application and generate an obfuscated
    apk file.

    :param input_apk_path: The path to the input application file to obfuscate.
    :param obfuscator_list: A list containing the names of the obfuscation techniques
                            to apply.
    :param working_dir_path: The working directory where to store the intermediate
                             files. By default a directory will be created in the same
                             directory as the input application. If the specified
                             directory doesn't exist, it will be created.
    :param obfuscated_apk_path: The path where to save the obfuscated apk file. By
                                default the file will be saved in the working directory.
    :param interactive: If True, show a progress bar with the obfuscation progress.
    :param ignore_libs: If True, exclude known third party libraries from the
                        obfuscation operations.
    :param virus_total_api_key: A list containing Virus Total API keys, needed only
                                when using Virus Total obfuscator.
    """

    check_external_tool_dependencies()

    if not os.path.isfile(input_apk_path):
        logger.critical(
            'Unable to find application file "{0}"'.format(input_apk_path))
        raise FileNotFoundError(
            'Unable to find application file "{0}"'.format(input_apk_path))

    obfuscation = Obfuscation(
        input_apk_path,
        working_dir_path,
        obfuscated_apk_path,
        interactive=interactive,
        ignore_libs=ignore_libs,
        virus_total_api_key=virus_total_api_key,
    )

    manager = ObfuscatorManager()
    obfuscator_name_to_obfuscator_object = {
        ob.name: ob.plugin_object
        for ob in manager.get_all_obfuscators()
    }
    obfuscator_name_to_function = {
        ob.name: ob.plugin_object.obfuscate
        for ob in manager.get_all_obfuscators()
    }
    valid_obfuscators = manager.get_obfuscators_names()

    # Check how many obfuscators in list will add new fields/methods.
    for obfuscator_name in obfuscator_list:
        # Make sure all the provided obfuscator names are valid.
        if obfuscator_name not in valid_obfuscators:
            raise ValueError(
                'There is no obfuscator named "{0}"'.format(obfuscator_name))
        if obfuscator_name_to_obfuscator_object[
                obfuscator_name].is_adding_fields:
            obfuscation.obfuscators_adding_fields += 1
        if obfuscator_name_to_obfuscator_object[
                obfuscator_name].is_adding_methods:
            obfuscation.obfuscators_adding_methods += 1

    obfuscator_progress = util.show_list_progress(
        obfuscator_list,
        interactive=interactive,
        unit="obfuscator",
        description="Running obfuscators",
    )

    for obfuscator_name in obfuscator_progress:
        try:
            if interactive:
                obfuscator_progress.set_description(
                    "Running obfuscators ({0})".format(obfuscator_name))
            (obfuscator_name_to_function[obfuscator_name])(obfuscation)
        except Exception as e:
            logger.critical("Error during obfuscation: {0}".format(e),
                            exc_info=True)
            raise
Ejemplo n.º 3
0
def get_cmd_args(args: list = None):
    """
    Parse and return the command line parameters needed for the script execution.

    :param args: List of arguments to be parsed (by default sys.argv is used).
    :return: The command line needed parameters.
    """

    obfuscators = ObfuscatorManager().get_obfuscators_names()

    parser = argparse.ArgumentParser(
        prog="python3 -m obfuscapk.cli",
        description=
        "Obfuscate an application (.apk) without needing its source code.",
    )
    parser.add_argument(
        "apk_file",
        type=str,
        metavar="<APK_FILE>",
        help="The path to the application (.apk) to obfuscate",
    )
    parser.add_argument(
        "-o",
        "--obfuscator",
        action="append",
        metavar="OBFUSCATOR",
        choices=obfuscators,
        help=
        "The name of the obfuscator to use. Can be specified multiple times to "
        "use more obfuscators (in sequence). Allowed values are: {0}".format(
            ", ".join(obfuscators)),
        required=True,
    )
    parser.add_argument(
        "-w",
        "--working-dir",
        type=str,
        metavar="DIR",
        help=
        "The working directory that will contain the intermediate files. By "
        "default a directory will be created in the same directory as the input "
        "application. If the specified directory doesn't exist, it will be created",
    )
    parser.add_argument(
        "-d",
        "--destination",
        type=str,
        metavar="OUT_APK",
        help=
        "The path where to save the obfuscated .apk file. By default the file "
        "will be saved in the working directory",
    )
    parser.add_argument(
        "-i",
        "--ignore-libs",
        action="store_true",
        help=
        "Ignore known third party libraries during the obfuscation operations",
    )
    parser.add_argument(
        "-p",
        "--show-progress",
        action="store_true",
        dest="interactive",
        help="Show obfuscation progress (as a progress bar)",
    )
    parser.add_argument("--use-aapt2",
                        action="store_true",
                        help="Use aapt2 for rebuild app")
    parser.add_argument(
        "-k",
        "--virus-total-key",
        type=str,
        metavar="VT_API_KEY",
        help=
        "When using Virus Total obfuscator, a valid API key has to be provided",
    )
    parser.add_argument(
        "--keystore-file",
        type=str,
        metavar="KEYSTORE_FILE",
        help=
        "The path to a custom keystore file to be used for signing the obfuscated "
        ".apk file. By default a keystore bundled with this tool will be used",
    )
    parser.add_argument(
        "--keystore-password",
        type=str,
        metavar="KEYSTORE_PASSWORD",
        help=
        "The password of the custom keystore used for signing the obfuscated .apk "
        "file (needed only when specifying a custom keystore file)",
    )
    parser.add_argument(
        "--key-alias",
        type=str,
        metavar="KEY_ALIAS",
        help=
        "The key alias for signing the obfuscated .apk file (needed only when "
        "specifying a custom keystore file)",
    )
    parser.add_argument(
        "--key-password",
        type=str,
        metavar="KEY_PASSWORD",
        help=
        "The key password for signing the obfuscated .apk file (needed only when "
        "specifying a custom keystore file)",
    )
    parser.add_argument(
        "--ignore-packages-file",
        type=str,
        metavar="IGNORE_PACKAGES_FILE",
        help="The file containing the package names to be ignored during the "
        "obfuscation (one package name per line)",
    )
    return parser.parse_args(args)