mirror of
https://github.com/mvt-project/mvt.git
synced 2026-11-25 13:20:54 +01:00
Merge pull request #992 from besendorf/fix/androidqf-apk-sha256
Validate AndroidQF APK SHA-256 values before lookup
This commit is contained in:
2 files changed
+105
-40
No files matched your search
@@ -5,8 +5,9 @@
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
from typing import Optional
|
||||
from typing import Optional, TypeGuard
|
||||
|
||||
from rich.progress import track
|
||||
|
||||
@@ -24,6 +25,12 @@ from mvt.common.virustotal import VTNoKey, VTQuotaExceeded, virustotal_lookup
|
||||
from .base import AndroidQFModule
|
||||
|
||||
|
||||
def _is_sha256(value: object) -> TypeGuard[str]:
|
||||
return (
|
||||
isinstance(value, str) and re.fullmatch(r"[0-9a-fA-F]{64}", value) is not None
|
||||
)
|
||||
|
||||
|
||||
class AQFPackages(AndroidQFModule):
|
||||
"""This module examines the installed packages in packages.json"""
|
||||
|
||||
@@ -97,9 +104,7 @@ class AQFPackages(AndroidQFModule):
|
||||
self._check_exact_indicators(result)
|
||||
|
||||
if self.module_options.get("virustotal", False):
|
||||
self.check_virustotal(
|
||||
delay=self.module_options.get("virustotal_delay", 0)
|
||||
)
|
||||
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."""
|
||||
@@ -119,16 +124,20 @@ class AQFPackages(AndroidQFModule):
|
||||
|
||||
for package_file in package_files:
|
||||
if not isinstance(package_file, dict):
|
||||
self.log.warning("Invalid file metadata for package %s", result.get("name"))
|
||||
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 _is_sha256(file_hash):
|
||||
ioc_match = self.indicators.check_file_hash(file_hash)
|
||||
if ioc_match:
|
||||
self.alertstore.critical(
|
||||
ioc_match.message, "", result, matched_indicator=ioc_match.ioc
|
||||
)
|
||||
elif file_hash not in (None, ""):
|
||||
self.log.warning(
|
||||
"Invalid SHA-256 metadata for package %s", result.get("name")
|
||||
)
|
||||
|
||||
if "certificate" not in package_file:
|
||||
@@ -136,7 +145,9 @@ class AQFPackages(AndroidQFModule):
|
||||
|
||||
certificate = package_file["certificate"]
|
||||
if not isinstance(certificate, dict):
|
||||
self.log.warning("Invalid certificate metadata for package %s", result.get("name"))
|
||||
self.log.warning(
|
||||
"Invalid certificate metadata for package %s", result.get("name")
|
||||
)
|
||||
continue
|
||||
|
||||
# The keys generated by AndroidQF have a leading uppercase character.
|
||||
@@ -145,12 +156,11 @@ class AQFPackages(AndroidQFModule):
|
||||
if not isinstance(certificate_hash, str):
|
||||
self.log.warning(
|
||||
"Missing or invalid %s certificate metadata for package %s",
|
||||
hash_type, result.get("name"),
|
||||
hash_type,
|
||||
result.get("name"),
|
||||
)
|
||||
continue
|
||||
ioc_match = self.indicators.check_app_certificate_hash(
|
||||
certificate_hash
|
||||
)
|
||||
ioc_match = self.indicators.check_app_certificate_hash(certificate_hash)
|
||||
if ioc_match:
|
||||
self.alertstore.critical(
|
||||
ioc_match.message,
|
||||
@@ -170,7 +180,7 @@ class AQFPackages(AndroidQFModule):
|
||||
|
||||
for package_file in package.get("files", []):
|
||||
file_hash = package_file.get("sha256")
|
||||
if not file_hash:
|
||||
if not _is_sha256(file_hash):
|
||||
continue
|
||||
|
||||
files_by_hash.setdefault(file_hash, []).append((package, package_file))
|
||||
|
||||
@@ -115,6 +115,43 @@ class TestAndroidqfPackages:
|
||||
== "31037a27af59d4914906c01ad14a318eee2f3e31d48da8954dca62a99174e3fa"
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"collection, invalid_hash",
|
||||
[
|
||||
("files_md5", "a" * 32),
|
||||
("files_sha1", "a" * 40),
|
||||
("files_sha256", "g" * 64),
|
||||
],
|
||||
)
|
||||
def test_invalid_apk_sha256_preserves_other_matches(
|
||||
self, indicators_factory, collection, invalid_hash
|
||||
):
|
||||
cert_hash = "b" * 64
|
||||
valid_hash = "abcd" * 16
|
||||
module = AQFPackages(
|
||||
results=[
|
||||
{
|
||||
"name": "com.example.app",
|
||||
"installer": "com.android.vending",
|
||||
"files": [
|
||||
{"sha256": invalid_hash, "certificate": {"Sha256": cert_hash}},
|
||||
{"sha256": valid_hash.upper()},
|
||||
],
|
||||
}
|
||||
]
|
||||
)
|
||||
module.indicators = indicators_factory(
|
||||
files_sha256=[valid_hash], app_cert_hashes=[cert_hash]
|
||||
)
|
||||
module.indicators.ioc_collections[0][collection].append(invalid_hash)
|
||||
|
||||
module.check_indicators()
|
||||
|
||||
assert [a.matched_indicator.value for a in module.alertstore.alerts] == [
|
||||
cert_hash,
|
||||
valid_hash,
|
||||
]
|
||||
|
||||
def test_packages_certificate_hash_ioc(self, module, indicators_factory):
|
||||
module.indicators = indicators_factory(
|
||||
app_cert_hashes=[
|
||||
@@ -139,10 +176,12 @@ class TestAndroidqfPackages:
|
||||
def test_virustotal_delays_after_missing_result(self, monkeypatch):
|
||||
lookups = []
|
||||
sleeps = []
|
||||
missing_hash = "a" * 64
|
||||
found_hash = "b" * 64
|
||||
|
||||
def fake_virustotal_lookup(file_hash):
|
||||
lookups.append(file_hash)
|
||||
if file_hash == "missing_hash":
|
||||
if file_hash == missing_hash:
|
||||
return None
|
||||
return {
|
||||
"attributes": {
|
||||
@@ -165,8 +204,9 @@ class TestAndroidqfPackages:
|
||||
"disabled": False,
|
||||
"system": False,
|
||||
"files": [
|
||||
{"path": "/data/app/missing.apk", "sha256": "missing_hash"},
|
||||
{"path": "/data/app/found.apk", "sha256": "found_hash"},
|
||||
{"path": "/data/app/missing.apk", "sha256": missing_hash},
|
||||
{"path": "/data/app/found.apk", "sha256": found_hash},
|
||||
{"path": "/data/app/invalid.apk", "sha256": "c" * 32},
|
||||
],
|
||||
}
|
||||
],
|
||||
@@ -174,7 +214,7 @@ class TestAndroidqfPackages:
|
||||
|
||||
module.check_indicators()
|
||||
|
||||
assert lookups == ["missing_hash", "found_hash"]
|
||||
assert lookups == [missing_hash, found_hash]
|
||||
assert sleeps == [16]
|
||||
assert module.results[0]["files"][1]["virustotal"] == "1/1"
|
||||
assert len(module.alertstore.alerts) == 1
|
||||
@@ -233,10 +273,16 @@ def test_root_package_heuristic_preserves_exact_ioc_checks(
|
||||
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": [],
|
||||
}])
|
||||
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()
|
||||
@@ -260,18 +306,22 @@ 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 = 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],
|
||||
@@ -282,9 +332,14 @@ def test_root_package_incomplete_metadata_preserves_later_matches(
|
||||
|
||||
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,
|
||||
root_name,
|
||||
"example.later",
|
||||
"a" * 64,
|
||||
"b" * 64,
|
||||
}
|
||||
assert any("rooting/jailbreaking" in alert.message for alert in module.alertstore.alerts)
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user