diff --git a/src/mvt/android/cmd_check_androidqf.py b/src/mvt/android/cmd_check_androidqf.py index e079807..98eacc8 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,44 @@ 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()) + return + if self.format == "zip": module.from_zip_file(self.archive, self.files) else: