From b2f7d73998d65701ab09c0b05a020a30efd0240a Mon Sep 17 00:00:00 2001 From: bitmeta69 <206962233+bitmeta69@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:02:45 -0500 Subject: [PATCH] Preserve exact IOC checks for AndroidQF rooting packages --- .../android/modules/androidqf/aqf_packages.py | 96 +++++++++------ tests/android_androidqf/test_packages.py | 110 ++++++++++++++++++ 2 files changed, 171 insertions(+), 35 deletions(-) diff --git a/src/mvt/android/modules/androidqf/aqf_packages.py b/src/mvt/android/modules/androidqf/aqf_packages.py index 294ad530..bfbae51b 100644 --- a/src/mvt/android/modules/androidqf/aqf_packages.py +++ b/src/mvt/android/modules/androidqf/aqf_packages.py @@ -53,6 +53,7 @@ class AQFPackages(AndroidQFModule): "", result, ) + self._check_exact_indicators(result) continue # Detections for apps installed via unusual methods. @@ -93,47 +94,72 @@ class AQFPackages(AndroidQFModule): result, ) - if not self.indicators: - continue - - ioc_match = self.indicators.check_app_id(result.get("name") or "") - if ioc_match: - self.alertstore.critical( - ioc_match.message, "", result, matched_indicator=ioc_match.ioc - ) - - for package_file in result.get("files", []): - ioc_match = self.indicators.check_file_hash( - package_file.get("sha256") or "" - ) - if ioc_match: - self.alertstore.critical( - ioc_match.message, "", result, matched_indicator=ioc_match.ioc - ) - - if "certificate" not in package_file: - continue - - # The keys generated by AndroidQF have a leading uppercase character. - for hash_type in ["Md5", "Sha1", "Sha256"]: - certificate_hash = package_file["certificate"][hash_type] - ioc_match = self.indicators.check_app_certificate_hash( - certificate_hash - ) - if ioc_match: - self.alertstore.critical( - ioc_match.message, - "", - result, - matched_indicator=ioc_match.ioc, - ) - break + self._check_exact_indicators(result) if self.module_options.get("virustotal", False): self.check_virustotal( delay=self.module_options.get("virustotal_delay", 0) ) + def _check_exact_indicators(self, result: ModuleAtomicResult) -> None: + """Check exact matches independently of package heuristics.""" + if not self.indicators: + return + + ioc_match = self.indicators.check_app_id(result.get("name") or "") + if ioc_match: + self.alertstore.critical( + ioc_match.message, "", result, matched_indicator=ioc_match.ioc + ) + + package_files = result.get("files", []) + if not isinstance(package_files, list): + self.log.warning("Invalid file metadata for package %s", result.get("name")) + return + + for package_file in package_files: + if not isinstance(package_file, dict): + self.log.warning("Invalid file metadata for package %s", result.get("name")) + continue + file_hash = package_file.get("sha256") + if file_hash is not None and not isinstance(file_hash, str): + self.log.warning("Invalid SHA-256 metadata for package %s", result.get("name")) + file_hash = None + ioc_match = self.indicators.check_file_hash(file_hash or "") + if ioc_match: + self.alertstore.critical( + ioc_match.message, "", result, matched_indicator=ioc_match.ioc + ) + + if "certificate" not in package_file: + continue + + certificate = package_file["certificate"] + if not isinstance(certificate, dict): + self.log.warning("Invalid certificate metadata for package %s", result.get("name")) + continue + + # The keys generated by AndroidQF have a leading uppercase character. + for hash_type in ["Md5", "Sha1", "Sha256"]: + certificate_hash = certificate.get(hash_type) + if not isinstance(certificate_hash, str): + self.log.warning( + "Missing or invalid %s certificate metadata for package %s", + hash_type, result.get("name"), + ) + continue + ioc_match = self.indicators.check_app_certificate_hash( + certificate_hash + ) + if ioc_match: + self.alertstore.critical( + ioc_match.message, + "", + result, + matched_indicator=ioc_match.ioc, + ) + break + def check_virustotal(self, delay: int = 0) -> None: files_by_hash: dict[ str, list[tuple[ModuleAtomicResult, ModuleAtomicResult]] diff --git a/tests/android_androidqf/test_packages.py b/tests/android_androidqf/test_packages.py index a879e880..894d8d0a 100644 --- a/tests/android_androidqf/test_packages.py +++ b/tests/android_androidqf/test_packages.py @@ -187,3 +187,113 @@ def test_check_androidqf_rejects_negative_virustotal_delay(data_path): assert result.exit_code == 2 assert "Invalid value for '--delay'" in result.output + + +@pytest.mark.parametrize( + "indicator_kwargs, expected_value", + [ + ({"app_ids": ["com.topjohnwu.magisk"]}, "com.topjohnwu.magisk"), + ({"files_sha256": ["a" * 64]}, "a" * 64), + ({"app_cert_hashes": ["b" * 64]}, "b" * 64), + ], +) +def test_root_package_heuristic_preserves_exact_ioc_checks( + indicators_factory, indicator_kwargs, expected_value +): + module = AQFPackages( + results=[ + { + "name": "com.topjohnwu.magisk", + "installer": "com.android.vending", + "system": False, + "files": [ + { + "sha256": "a" * 64, + "certificate": { + "Md5": "c" * 32, + "Sha1": "d" * 40, + "Sha256": "b" * 64, + }, + } + ], + } + ] + ) + module.indicators = indicators_factory(**indicator_kwargs) + + module.check_indicators() + + matches = [a for a in module.alertstore.alerts if a.matched_indicator] + assert len(matches) == 1 + assert matches[0].matched_indicator.value == expected_value + assert any("rooting/jailbreaking" in a.message for a in module.alertstore.alerts) + + +@pytest.mark.parametrize("with_indicators", [False, True]) +def test_root_package_keeps_existing_heuristic_short_circuit( + indicators_factory, with_indicators +): + module = AQFPackages(results=[{ + "name": "com.topjohnwu.magisk", "installer": "null", "system": False, + "files": [], + }]) + if with_indicators: + module.indicators = indicators_factory(app_ids=["example.unrelated"]) + module.check_indicators() + assert len(module.alertstore.alerts) == 1 + assert "rooting/jailbreaking" in module.alertstore.alerts[0].message + + +@pytest.mark.parametrize( + "files", + [ + None, + {}, + [None], + [{"sha256": None, "certificate": None}], + [{"sha256": 123, "certificate": {}}], + [{"certificate": {"Md5": None, "Sha1": [], "Sha256": "b" * 64}}], + [None, {"sha256": "a" * 64, "certificate": {"Sha256": "b" * 64}}], + ], +) +def test_root_package_incomplete_metadata_preserves_later_matches( + indicators_factory, files, caplog +): + root_name = "com.topjohnwu.magisk" + module = AQFPackages(results=[ + {"name": root_name, "files": files}, + { + "name": "example.later", + "installer": "com.android.vending", + "system": False, + "files": [{ + "sha256": "a" * 64, + "certificate": {"Sha256": "b" * 64}, + }], + }, + ]) + module.indicators = indicators_factory( + app_ids=[root_name, "example.later"], + files_sha256=["a" * 64], + app_cert_hashes=["b" * 64], + ) + + module.check_indicators() + + matches = [alert for alert in module.alertstore.alerts if alert.matched_indicator] + assert {alert.matched_indicator.value for alert in matches} == { + root_name, "example.later", "a" * 64, "b" * 64, + } + assert any("rooting/jailbreaking" in alert.message for alert in module.alertstore.alerts) + assert "metadata" in caplog.text + if isinstance(files, list) and any( + isinstance(item, dict) + and isinstance(item.get("certificate"), dict) + and item["certificate"].get("Sha256") == "b" * 64 + for item in files + ): + assert any( + alert.event["name"] == root_name + and alert.matched_indicator.value == "b" * 64 + for alert in matches + )