From 355850bd5cec50eb358fa8233ec919445573e0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donncha=20=C3=93=20Cearbhaill?= Date: Mon, 28 Oct 2024 11:12:20 +0100 Subject: [PATCH 1/2] WIP: Run bugreport modules against bugreport.zip in AndroidQF extraction --- src/mvt/android/cmd_check_androidqf.py | 39 +++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/mvt/android/cmd_check_androidqf.py b/src/mvt/android/cmd_check_androidqf.py index e079807..a0210f2 100644 --- a/src/mvt/android/cmd_check_androidqf.py +++ b/src/mvt/android/cmd_check_androidqf.py @@ -12,6 +12,8 @@ from typing import List, Optional from mvt.common.command import Command from .modules.androidqf import ANDROIDQF_MODULES +from .modules.bugreport import BUGREPORT_MODULES +from .modules.bugreport.base import BugReportModule log = logging.getLogger(__name__) @@ -39,7 +41,11 @@ class CmdAndroidCheckAndroidQF(Command): ) self.name = "check-androidqf" - self.modules = ANDROIDQF_MODULES + + # We can load AndroidQF and bugreport modules here, as + # AndroidQF dump will contain a bugreport. + self.modules = ANDROIDQF_MODULES + BUGREPORT_MODULES + # TODO: Check how to namespace and deduplicate modules. self.format: Optional[str] = None self.archive: Optional[zipfile.ZipFile] = None @@ -54,12 +60,43 @@ class CmdAndroidCheckAndroidQF(Command): for fname in subfiles: file_path = os.path.relpath(os.path.join(root, fname), parent_path) self.files.append(file_path) + elif os.path.isfile(self.target_path): self.format = "zip" self.archive = zipfile.ZipFile(self.target_path) self.files = self.archive.namelist() + def load_bugreport(self): + # Refactor this file list loading + # First we need to find the bugreport file location + bugreport_zip_path = None + for file_name in self.files: + if file_name.endswith("bugreport.zip"): + bugreport_zip_path = file_name + break + else: + self.log.warning("No bugreport.zip found in the AndroidQF dump") + return None + + if self.format == "zip": + # Create handle to the bugreport.zip file inside the AndroidQF dump + handle = self.archive.open(bugreport_zip_path) + bugreport_zip = zipfile.ZipFile(handle) + else: + # Load the bugreport.zip file from the extracted AndroidQF dump on disk. + parent_path = Path(self.target_path).absolute().parent.as_posix() + bug_report_path = os.path.join(parent_path, bugreport_zip_path) + bugreport_zip = zipfile.ZipFile(bug_report_path) + + return bugreport_zip + def module_init(self, module): + if isinstance(module, BugReportModule): + bugreport_archive = self.load_bugreport() + if not bugreport_archive: + return + module.from_zip(bugreport_archive, bugreport_archive.namelist()) + if self.format == "zip": module.from_zip_file(self.archive, self.files) else: From 2bb613fe09a42047a5a2a8317616d7ae386c883b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donncha=20=C3=93=20Cearbhaill?= Date: Mon, 28 Oct 2024 11:19:45 +0100 Subject: [PATCH 2/2] Return after loading bugreport module --- src/mvt/android/cmd_check_androidqf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mvt/android/cmd_check_androidqf.py b/src/mvt/android/cmd_check_androidqf.py index a0210f2..98eacc8 100644 --- a/src/mvt/android/cmd_check_androidqf.py +++ b/src/mvt/android/cmd_check_androidqf.py @@ -96,6 +96,7 @@ class CmdAndroidCheckAndroidQF(Command): if not bugreport_archive: return module.from_zip(bugreport_archive, bugreport_archive.namelist()) + return if self.format == "zip": module.from_zip_file(self.archive, self.files)