Merge pull request #933 from besendorf/fix/androidqf-root-zip-931

Fix AndroidQF artifact discovery in root-level ZIP archives
This commit is contained in:
besendorf authored and GitHub committed 2026-11-25 12:41:54 +01:00
commit 829becf8e7
5 files changed
+147 -4

No files matched your search

+3 -1
View File
@@ -95,7 +95,9 @@ class CmdAndroidCheckAndroidQF(Command):
elif os.path.isfile(self.target_path):
self.__format = "zip"
self.__zip = zipfile.ZipFile(self.target_path)
self.__files = self.__zip.namelist()
self.__files = [
entry.filename for entry in self.__zip.infolist() if not entry.is_dir()
]
self._load_acquisition_context()
@@ -34,7 +34,9 @@ class AQFProcesses(ProcessesArtifact, AndroidQFModule):
)
def run(self) -> None:
ps_files = self._get_files_by_pattern("*/ps.txt")
ps_files = self._get_files_by_pattern(
"*/processes.txt"
) or self._get_files_by_pattern("*/ps.txt")
if not ps_files:
return
+16 -2
View File
@@ -46,8 +46,22 @@ class AndroidQFModule(MVTModule):
self.archive = archive
self.files = files
def _get_files_by_pattern(self, pattern: str):
return fnmatch.filter(self.files, pattern)
def _get_files_by_pattern(self, pattern: str) -> List[str]:
pattern = pattern.replace("\\", "/")
patterns = [pattern]
# ZIPs may store artifacts at their root, without the acquisition
# directory prefix present in paths collected by from_dir().
if pattern.startswith("*/"):
patterns.append(pattern[2:])
return [
file_path
for file_path in self.files
if any(
fnmatch.fnmatch(file_path.replace("\\", "/"), candidate)
for candidate in patterns
)
]
def _get_device_timezone(self):
"""
+60
View File
@@ -0,0 +1,60 @@
# Mobile Verification Toolkit (MVT)
# Copyright (c) 2026 The MVT Authors.
# Use of this software is governed by the MVT License 1.1 that can be found at
# https://license.mvt.re/1.1/
import zipfile
import pytest
from mvt.android.modules.androidqf.base import AndroidQFModule
@pytest.mark.parametrize("prefix", ["", "./", "acquisition/", "outer/acquisition/"])
@pytest.mark.parametrize(
"pattern, filename",
[
("*/packages.json", "packages.json"),
("*/getprop.txt", "getprop.txt"),
("*/ps.txt", "ps.txt"),
("*/settings_*.txt", "settings_global.txt"),
("*/files.json", "files.json"),
("*/logs/*", "logs/logcat.txt"),
("*/mounts.json", "mounts.json"),
("*/root_binaries.json", "root_binaries.json"),
],
)
def test_artifact_lookup_preserves_archive_names(tmp_path, prefix, pattern, filename):
member = prefix + filename
with zipfile.ZipFile(tmp_path / "acquisition.zip", "w") as archive:
archive.writestr(member, b"artifact")
archive.writestr(prefix + "unrelated.txt", b"unrelated")
module = AndroidQFModule()
module.from_zip(archive, archive.namelist())
assert module._get_files_by_pattern(pattern) == [member]
assert module._get_file_content(member) == b"artifact"
def test_artifact_lookup_preserves_windows_paths():
module = AndroidQFModule()
module.from_dir("parent", [r"acquisition\logs\logcat.txt"])
assert module._get_files_by_pattern("*/logs/*") == [r"acquisition\logs\logcat.txt"]
def test_artifact_lookup_preserves_order_without_duplicates():
module = AndroidQFModule()
module.files = [
"acquisition/settings_global.txt",
"settings_secure.txt",
"./settings_system.txt",
"not_settings_global.txt",
"settings_global.txt.bak",
]
assert module._get_files_by_pattern("*/settings_*.txt") == module.files[:3]
assert module._get_files_by_pattern("settings_secure.txt") == [
"settings_secure.txt"
]
assert module._get_files_by_pattern("*/missing.txt") == []
+65
View File
@@ -10,6 +10,7 @@ import shutil
import tempfile
import zipfile
import pytest
from click.testing import CliRunner
from mvt.android.cli import check_androidqf
@@ -23,7 +24,71 @@ from .utils import get_artifact_folder
TEST_BACKUP_PASSWORD = "123456"
@pytest.mark.parametrize(
"layout", ["directory", "", "./", "acquisition/", "outer/acquisition/"]
)
def test_androidqf_property_detection_across_layouts(
tmp_path, indicators_factory, monkeypatch, layout
):
monkeypatch.setattr(settings, "NETWORK_ACCESS_ALLOWED", False)
content = (
"[persist.sys.timezone]: [Europe/Berlin]\n"
"[test.suspicious.property]: [enabled]\n"
)
if layout == "directory":
target = tmp_path / "acquisition"
target.mkdir()
(target / "getprop.txt").write_text(content)
else:
target = tmp_path / "acquisition.zip"
with zipfile.ZipFile(target, "w") as archive:
archive.writestr(layout + "getprop.txt", content)
command = CmdAndroidCheckAndroidQF(
target_path=str(target),
module_name="AQFGetProp",
iocs=indicators_factory(android_property_names=["test.suspicious.property"]),
)
command.run()
assert len(command.executed) == 1
module = command.executed[0]
assert module.results == [
{"name": "persist.sys.timezone", "value": "Europe/Berlin"},
{"name": "test.suspicious.property", "value": "enabled"},
]
assert module._get_device_timezone() == "Europe/Berlin"
assert len(command.alertstore.alerts) == 1
assert command.alertstore.alerts[0].event == module.results[1]
class TestCheckAndroidqfCommand:
@pytest.mark.parametrize("use_zip", [False, True], ids=["folder", "zip"])
def test_androidqf_process_list_and_log_files(self, tmp_path, use_zip):
archive_path = tmp_path / "acquisition.zip"
with zipfile.ZipFile(archive_path, "w") as archive:
archive.writestr("processes.txt", "USER PID NAME\nroot 1 init\n")
archive.writestr("logs/proc/kmsg", "")
archive.writestr("logs/data/anr/", "")
target = archive_path if use_zip else tmp_path / "acquisition"
if not use_zip:
archive.extractall(target)
command = CmdAndroidCheckAndroidQF(
target_path=str(target),
disable_version_check=True,
disable_indicator_check=True,
)
command.run()
records = {type(module).__name__: module.results for module in command.executed}
assert records["AQFProcesses"] == [
{"user": "root", "pid": 1, "command": "init"}
]
timestamps = records["AQFLogTimestamps"]
assert len(timestamps) == 1
assert timestamps[0]["path"].replace("\\", "/").endswith("logs/proc/kmsg")
def test_log_timestamps_module_is_registered(self):
assert AQFLogTimestamps in ANDROIDQF_MODULES