in shim/shim.py [0:0]
def main() -> None:
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
build_directory = Path(tempfile.mkdtemp())
try:
parser = argparse.ArgumentParser(
description="A security-focused static analyzer targeting Android."
)
parser.add_argument(
"--version",
action=VersionAction,
nargs=0,
help="Print the version and exit",
)
_add_target_arguments(parser)
_add_output_arguments(parser)
_add_binary_arguments(parser)
_add_configuration_arguments(parser)
_add_analysis_arguments(parser)
_add_metadata_arguments(parser)
_add_debug_arguments(parser)
arguments: argparse.Namespace = parser.parse_args()
if (
configuration.FACEBOOK_SHIM
and arguments.java_target is not None
and arguments.apk_path is not None
):
parser.error(
"The analysis target can either be a java target (--java-target)"
+ " or an apk file (--apk-path), but not both."
)
if (
configuration.FACEBOOK_SHIM
and arguments.java_target is None
and arguments.apk_path is None
):
parser.error(
"The analysis target should either be a java target (--java-target)"
+ " or an apk file (--apk-path)."
)
if not configuration.FACEBOOK_SHIM and arguments.apk_path is None:
parser.error("The argument --apk-path is required.")
# Build the vanilla java project.
if configuration.FACEBOOK_SHIM and arguments.java_target:
jar_file = _extract_jex_file_if_exists(
_build_target(arguments.java_target, mode=arguments.java_mode),
arguments.java_target,
build_directory,
)
desugared_jar_file = _desugar_jar_file(jar_file)
arguments.apk_path = os.fspath(_build_apk_from_jar(desugared_jar_file))
# Build the mariana trench binary if necessary.
binary = _get_analysis_binary(arguments)
LOG.info(f"Extracting `{arguments.apk_path}`...")
apk_directory = tempfile.mkdtemp(suffix="_apk")
dex_directory = tempfile.mkdtemp(suffix="_dex")
pyredex.utils.unzip_apk(arguments.apk_path, apk_directory)
dex_mode = pyredex.unpacker.detect_secondary_dex_mode(apk_directory)
dex_mode.unpackage(apk_directory, dex_directory)
LOG.info(f"Extracted APK into `{apk_directory}` and DEX into `{dex_directory}`")
options = _get_command_options(arguments, apk_directory, dex_directory)
command = [os.fspath(binary.resolve())] + options
if arguments.gdb:
command = ["gdb", "--args"] + command
elif arguments.lldb:
command = ["lldb", "--"] + command
LOG.info(f"Running Mariana Trench: {' '.join(command)}")
output = subprocess.run(command)
if output.returncode != 0:
LOG.fatal(f"Analysis binary exited with exit code {output.returncode}.")
sys.exit(output.returncode)
except (ClientError, configuration.Error) as error:
LOG.fatal(error.args[0])
sys.exit(1)
except Exception:
LOG.fatal(f"Unexpected error:\n{traceback.format_exc()}")
sys.exit(1)
finally:
try:
shutil.rmtree(build_directory)
except IOError:
pass # Swallow.