2021-07-16 06:05:01 +00:00
|
|
|
# Mobile Verification Toolkit (MVT)
|
2021-08-01 19:11:08 +00:00
|
|
|
# Copyright (c) 2021 The MVT Project Authors.
|
|
|
|
# Use of this software is governed by the MVT License 1.1 that can be found at
|
|
|
|
# https://license.mvt.re/1.1/
|
2021-07-16 06:05:01 +00:00
|
|
|
|
|
|
|
import logging
|
2021-07-30 09:40:09 +00:00
|
|
|
import os
|
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
import pkg_resources
|
|
|
|
|
2022-01-28 11:25:50 +00:00
|
|
|
from mvt.android.lookups.koodous import koodous_lookup
|
|
|
|
from mvt.android.lookups.virustotal import virustotal_lookup
|
2021-07-16 06:05:01 +00:00
|
|
|
|
2022-01-28 15:36:24 +00:00
|
|
|
from .base import AndroidExtraction
|
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2021-11-19 14:27:51 +00:00
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
class Packages(AndroidExtraction):
|
|
|
|
"""This module extracts the list of installed packages."""
|
|
|
|
|
|
|
|
def __init__(self, file_path=None, base_folder=None, output_folder=None,
|
2021-07-21 11:17:58 +00:00
|
|
|
serial=None, fast_mode=False, log=None, results=[]):
|
2021-07-16 06:05:01 +00:00
|
|
|
super().__init__(file_path=file_path, base_folder=base_folder,
|
2021-08-12 16:21:21 +00:00
|
|
|
output_folder=output_folder, fast_mode=fast_mode,
|
|
|
|
log=log, results=results)
|
2021-07-16 06:05:01 +00:00
|
|
|
|
|
|
|
def serialize(self, record):
|
|
|
|
records = []
|
|
|
|
|
|
|
|
timestamps = [
|
|
|
|
{"event": "package_install", "timestamp": record["timestamp"]},
|
|
|
|
{"event": "package_first_install", "timestamp": record["first_install_time"]},
|
|
|
|
{"event": "package_last_update", "timestamp": record["last_update_time"]},
|
|
|
|
]
|
|
|
|
|
|
|
|
for ts in timestamps:
|
|
|
|
records.append({
|
|
|
|
"timestamp": ts["timestamp"],
|
|
|
|
"module": self.__class__.__name__,
|
|
|
|
"event": ts["event"],
|
|
|
|
"data": f"{record['package_name']} (system: {record['system']}, third party: {record['third_party']})",
|
|
|
|
})
|
|
|
|
|
|
|
|
return records
|
|
|
|
|
|
|
|
def check_indicators(self):
|
|
|
|
root_packages_path = os.path.join("..", "..", "data", "root_packages.txt")
|
|
|
|
root_packages_string = pkg_resources.resource_string(__name__, root_packages_path)
|
|
|
|
root_packages = root_packages_string.decode("utf-8").split("\n")
|
2021-09-21 17:43:02 +00:00
|
|
|
root_packages = [rp.strip() for rp in root_packages]
|
2021-07-16 06:05:01 +00:00
|
|
|
|
2021-09-21 17:43:02 +00:00
|
|
|
for result in self.results:
|
|
|
|
if result["package_name"] in root_packages:
|
2021-07-16 06:05:01 +00:00
|
|
|
self.log.warning("Found an installed package related to rooting/jailbreaking: \"%s\"",
|
2021-11-19 14:27:51 +00:00
|
|
|
result["package_name"])
|
2021-09-21 17:43:02 +00:00
|
|
|
self.detected.append(result)
|
2022-01-27 11:50:37 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
ioc = self.indicators.check_app_id(result.get("package_name"))
|
|
|
|
if ioc:
|
|
|
|
result["matched_indicators"] = ioc
|
2021-09-21 17:43:02 +00:00
|
|
|
self.detected.append(result)
|
2022-01-27 11:50:37 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
for package_file in result["files"]:
|
|
|
|
ioc = self.indicators.check_file_hash(package_file["sha256"])
|
|
|
|
if ioc:
|
|
|
|
result["matched_indicators"] = ioc
|
|
|
|
self.detected.append(result)
|
2021-09-21 17:43:02 +00:00
|
|
|
|
2021-09-20 17:15:39 +00:00
|
|
|
def _get_files_for_package(self, package_name):
|
|
|
|
output = self._adb_command(f"pm path {package_name}")
|
|
|
|
output = output.strip().replace("package:", "")
|
|
|
|
if not output:
|
|
|
|
return []
|
|
|
|
|
|
|
|
package_files = []
|
|
|
|
for file_path in output.split("\n"):
|
|
|
|
file_path = file_path.strip()
|
|
|
|
|
|
|
|
md5 = self._adb_command(f"md5sum {file_path}").split(" ")[0]
|
|
|
|
sha1 = self._adb_command(f"sha1sum {file_path}").split(" ")[0]
|
|
|
|
sha256 = self._adb_command(f"sha256sum {file_path}").split(" ")[0]
|
|
|
|
sha512 = self._adb_command(f"sha512sum {file_path}").split(" ")[0]
|
|
|
|
|
|
|
|
package_files.append({
|
|
|
|
"path": file_path,
|
|
|
|
"md5": md5,
|
|
|
|
"sha1": sha1,
|
|
|
|
"sha256": sha256,
|
|
|
|
"sha512": sha512,
|
|
|
|
})
|
|
|
|
|
|
|
|
return package_files
|
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
def run(self):
|
|
|
|
self._adb_connect()
|
|
|
|
|
|
|
|
packages = self._adb_command("pm list packages -U -u -i -f")
|
2022-01-21 11:20:22 +00:00
|
|
|
if packages.strip() == "Error: Unknown option: -U":
|
|
|
|
packages = self._adb_command("pm list packages -u -i -f")
|
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
for line in packages.split("\n"):
|
|
|
|
line = line.strip()
|
|
|
|
if not line.startswith("package:"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
fields = line.split()
|
|
|
|
file_name, package_name = fields[0].split(":")[1].rsplit("=", 1)
|
2021-08-17 11:26:04 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
installer = fields[1].split("=")[1].strip()
|
|
|
|
except IndexError:
|
2021-07-16 06:05:01 +00:00
|
|
|
installer = None
|
2021-08-17 11:26:04 +00:00
|
|
|
else:
|
|
|
|
if installer == "null":
|
|
|
|
installer = None
|
|
|
|
|
2021-08-18 21:11:18 +00:00
|
|
|
try:
|
|
|
|
uid = fields[2].split(":")[1].strip()
|
|
|
|
except IndexError:
|
|
|
|
uid = None
|
2021-07-16 06:05:01 +00:00
|
|
|
|
|
|
|
dumpsys = self._adb_command(f"dumpsys package {package_name} | grep -A2 timeStamp").split("\n")
|
|
|
|
timestamp = dumpsys[0].split("=")[1].strip()
|
|
|
|
first_install = dumpsys[1].split("=")[1].strip()
|
|
|
|
last_update = dumpsys[2].split("=")[1].strip()
|
|
|
|
|
2021-09-20 17:15:39 +00:00
|
|
|
package_files = self._get_files_for_package(package_name)
|
|
|
|
|
2021-08-15 17:05:15 +00:00
|
|
|
self.results.append({
|
|
|
|
"package_name": package_name,
|
|
|
|
"file_name": file_name,
|
|
|
|
"installer": installer,
|
|
|
|
"timestamp": timestamp,
|
|
|
|
"first_install_time": first_install,
|
|
|
|
"last_update_time": last_update,
|
|
|
|
"uid": uid,
|
|
|
|
"disabled": False,
|
|
|
|
"system": False,
|
|
|
|
"third_party": False,
|
2021-09-20 17:15:39 +00:00
|
|
|
"files": package_files,
|
2021-08-15 17:05:15 +00:00
|
|
|
})
|
2021-07-16 06:05:01 +00:00
|
|
|
|
|
|
|
cmds = [
|
|
|
|
{"field": "disabled", "arg": "-d"},
|
|
|
|
{"field": "system", "arg": "-s"},
|
|
|
|
{"field": "third_party", "arg": "-3"},
|
|
|
|
]
|
|
|
|
for cmd in cmds:
|
|
|
|
output = self._adb_command(f"pm list packages {cmd['arg']}")
|
|
|
|
for line in output.split("\n"):
|
|
|
|
line = line.strip()
|
|
|
|
if not line.startswith("package:"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
package_name = line.split(":", 1)[1]
|
|
|
|
for i, result in enumerate(self.results):
|
|
|
|
if result["package_name"] == package_name:
|
|
|
|
self.results[i][cmd["field"]] = True
|
|
|
|
|
2022-01-28 11:25:50 +00:00
|
|
|
packages_to_lookup = []
|
2021-08-17 11:26:04 +00:00
|
|
|
for result in self.results:
|
|
|
|
if result["system"]:
|
|
|
|
continue
|
|
|
|
|
2022-01-28 11:25:50 +00:00
|
|
|
packages_to_lookup.append(result)
|
2021-08-17 11:26:04 +00:00
|
|
|
self.log.info("Found non-system package with name \"%s\" installed by \"%s\" on %s",
|
|
|
|
result["package_name"], result["installer"], result["timestamp"])
|
|
|
|
|
2022-01-28 11:25:50 +00:00
|
|
|
if not self.fast_mode:
|
|
|
|
virustotal_lookup(packages_to_lookup)
|
|
|
|
koodous_lookup(packages_to_lookup)
|
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
self.log.info("Extracted at total of %d installed package names",
|
|
|
|
len(self.results))
|
|
|
|
|
|
|
|
self._adb_disconnect()
|