2021-07-16 06:05:01 +00:00
|
|
|
# Mobile Verification Toolkit (MVT)
|
2022-01-30 19:15:01 +00:00
|
|
|
# Copyright (c) 2021-2022 The MVT Project Authors.
|
2021-08-01 19:11:08 +00:00
|
|
|
# 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
|
|
|
|
2021-07-30 09:40:09 +00:00
|
|
|
import logging
|
2021-07-16 06:05:01 +00:00
|
|
|
import os
|
|
|
|
import sqlite3
|
2022-02-17 17:17:38 +00:00
|
|
|
import tarfile
|
|
|
|
import io
|
|
|
|
import zlib
|
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
import datetime
|
2021-07-30 09:40:09 +00:00
|
|
|
|
|
|
|
from mvt.common.utils import check_for_links, convert_timestamp_to_iso
|
2022-02-17 17:17:38 +00:00
|
|
|
from mvt.common.module import InsufficientPrivileges
|
2021-07-16 06:05:01 +00:00
|
|
|
|
|
|
|
from .base import AndroidExtraction
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2021-07-30 16:08:52 +00:00
|
|
|
SMS_BUGLE_PATH = "data/data/com.google.android.apps.messaging/databases/bugle_db"
|
|
|
|
SMS_BUGLE_QUERY = """
|
2021-11-19 14:27:51 +00:00
|
|
|
SELECT
|
2022-02-17 17:17:38 +00:00
|
|
|
ppl.normalized_destination AS address,
|
2021-07-25 13:06:22 +00:00
|
|
|
p.timestamp AS timestamp,
|
2021-11-19 14:27:51 +00:00
|
|
|
CASE WHEN m.sender_id IN
|
2021-07-25 13:06:22 +00:00
|
|
|
(SELECT _id FROM participants WHERE contact_id=-1)
|
2022-02-17 17:17:38 +00:00
|
|
|
THEN 2 ELSE 1 END incoming, p.text AS body
|
2021-07-25 13:06:22 +00:00
|
|
|
FROM messages m, conversations c, parts p,
|
|
|
|
participants ppl, conversation_participants cp
|
|
|
|
WHERE (m.conversation_id = c._id)
|
|
|
|
AND (m._id = p.message_id)
|
|
|
|
AND (cp.conversation_id = c._id)
|
|
|
|
AND (cp.participant_id = ppl._id);
|
|
|
|
"""
|
2021-07-30 16:08:52 +00:00
|
|
|
|
|
|
|
SMS_MMSSMS_PATH = "data/data/com.android.providers.telephony/databases/mmssms.db"
|
|
|
|
SMS_MMSMS_QUERY = """
|
2021-11-19 14:27:51 +00:00
|
|
|
SELECT
|
2022-02-17 17:17:38 +00:00
|
|
|
address AS address,
|
2021-07-25 13:06:22 +00:00
|
|
|
date_sent AS timestamp,
|
|
|
|
type as incoming,
|
2022-02-17 17:17:38 +00:00
|
|
|
body AS body
|
2021-07-25 13:06:22 +00:00
|
|
|
FROM sms;
|
|
|
|
"""
|
2021-07-16 06:05:01 +00:00
|
|
|
|
2021-11-19 14:27:51 +00:00
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
class SMS(AndroidExtraction):
|
|
|
|
"""This module extracts all SMS messages containing links."""
|
|
|
|
|
|
|
|
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):
|
2022-02-17 17:17:38 +00:00
|
|
|
body = record["body"].replace("\n", "\\n")
|
2021-07-16 06:05:01 +00:00
|
|
|
return {
|
|
|
|
"timestamp": record["isodate"],
|
|
|
|
"module": self.__class__.__name__,
|
|
|
|
"event": f"sms_{record['direction']}",
|
2022-02-17 17:17:38 +00:00
|
|
|
"data": f"{record['address']}: \"{body}\""
|
2021-07-16 06:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def check_indicators(self):
|
|
|
|
if not self.indicators:
|
|
|
|
return
|
|
|
|
|
|
|
|
for message in self.results:
|
2022-02-17 17:17:38 +00:00
|
|
|
if "body" not in message:
|
2021-07-16 06:05:01 +00:00
|
|
|
continue
|
|
|
|
|
2022-02-17 17:17:38 +00:00
|
|
|
message_links = check_for_links(message["body"])
|
2021-07-16 06:05:01 +00:00
|
|
|
if self.indicators.check_domains(message_links):
|
|
|
|
self.detected.append(message)
|
|
|
|
|
|
|
|
def _parse_db(self, db_path):
|
|
|
|
"""Parse an Android bugle_db SMS database file.
|
2021-09-10 13:18:13 +00:00
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
:param db_path: Path to the Android SMS database file to process
|
2021-09-10 13:18:13 +00:00
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
"""
|
|
|
|
conn = sqlite3.connect(db_path)
|
|
|
|
cur = conn.cursor()
|
2021-11-19 14:27:51 +00:00
|
|
|
|
2021-07-25 13:06:22 +00:00
|
|
|
if (self.SMS_DB_TYPE == 1):
|
2021-07-30 16:08:52 +00:00
|
|
|
cur.execute(SMS_BUGLE_QUERY)
|
2021-07-25 13:06:22 +00:00
|
|
|
elif (self.SMS_DB_TYPE == 2):
|
2021-07-30 16:08:52 +00:00
|
|
|
cur.execute(SMS_MMSMS_QUERY)
|
2021-07-25 13:06:22 +00:00
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
names = [description[0] for description in cur.description]
|
|
|
|
|
|
|
|
for item in cur:
|
2021-08-15 17:05:15 +00:00
|
|
|
message = {}
|
2021-07-16 06:05:01 +00:00
|
|
|
for index, value in enumerate(item):
|
|
|
|
message[names[index]] = value
|
|
|
|
|
|
|
|
message["direction"] = ("received" if message["incoming"] == 1 else "sent")
|
|
|
|
message["isodate"] = convert_timestamp_to_iso(message["timestamp"])
|
|
|
|
|
|
|
|
# If we find links in the messages or if they are empty we add
|
|
|
|
# them to the list of results.
|
2022-02-17 17:17:38 +00:00
|
|
|
if check_for_links(message["body"]) or message["body"].strip() == "":
|
2021-07-16 06:05:01 +00:00
|
|
|
self.results.append(message)
|
|
|
|
|
|
|
|
cur.close()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
log.info("Extracted a total of %d SMS messages containing links", len(self.results))
|
|
|
|
|
2022-02-17 17:17:38 +00:00
|
|
|
def _extract_sms_from_backup_tar(self, tar_data):
|
|
|
|
# Extract data from generated tar file
|
|
|
|
tar_bytes = io.BytesIO(tar_data)
|
|
|
|
tar = tarfile.open(fileobj=tar_bytes, mode='r')
|
|
|
|
for member in tar.getmembers():
|
|
|
|
if not member.name.endswith("_sms_backup"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.log.debug("Extracting SMS messages from backup file %s", member.name)
|
|
|
|
sms_part_zlib = zlib.decompress(tar.extractfile(member).read())
|
|
|
|
json_data = json.loads(sms_part_zlib)
|
|
|
|
|
|
|
|
# TODO: Copied from SMS module. Refactor to avoid duplication
|
|
|
|
for message in json_data:
|
|
|
|
utc_timestamp = datetime.datetime.utcfromtimestamp(int(message["date"]) / 1000)
|
|
|
|
message["isodate"] = convert_timestamp_to_iso(utc_timestamp)
|
|
|
|
message["direction"] = ("sent" if int(message["date_sent"]) else "received")
|
|
|
|
|
|
|
|
message_links = check_for_links(message["body"])
|
|
|
|
if message_links or message["body"].strip() == "":
|
|
|
|
self.results.append(message)
|
|
|
|
|
|
|
|
log.info("Extracted a total of %d SMS messages containing links", len(self.results))
|
|
|
|
|
|
|
|
def _extract_sms_adb(self):
|
|
|
|
"""Use the Android backup command to extract SMS data from the native SMS app
|
|
|
|
|
|
|
|
It is crucial to use the under-documented "-nocompress" flag to disable the non-standard Java compression
|
|
|
|
algorithim. This module only supports an unencrypted ADB backup.
|
|
|
|
"""
|
|
|
|
self.log.warning("Please check phone and accept Android backup prompt. Do not set an encryption password. \a")
|
|
|
|
|
2022-02-18 14:09:08 +00:00
|
|
|
# Run ADB command to create a backup of SMS app
|
2022-02-17 17:17:38 +00:00
|
|
|
# TODO: Base64 encoding as temporary fix to avoid byte-mangling over the shell transport...
|
|
|
|
backup_output_b64 = self._adb_command("/system/bin/bu backup -nocompress com.android.providers.telephony | base64")
|
|
|
|
backup_output = base64.b64decode(backup_output_b64)
|
|
|
|
if not backup_output.startswith(b"ANDROID BACKUP"):
|
|
|
|
self.log.error("Extracting SMS via Android backup failed. No valid backup data found.")
|
|
|
|
return
|
|
|
|
|
|
|
|
[magic_header, version, is_compressed, encryption, tar_data] = backup_output.split(b"\n", 4)
|
|
|
|
if encryption != b"none" or int(is_compressed):
|
|
|
|
self.log.error("The backup is encrypted or compressed and cannot be parsed. "
|
|
|
|
"[version: %s, encryption: %s, compression: %s]", version, encryption, is_compressed)
|
|
|
|
return
|
|
|
|
|
|
|
|
self._extract_sms_from_backup_tar(tar_data)
|
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
def run(self):
|
2022-02-17 17:17:38 +00:00
|
|
|
try:
|
|
|
|
if (self._adb_check_file_exists(os.path.join("/", SMS_BUGLE_PATH))):
|
|
|
|
self.SMS_DB_TYPE = 1
|
|
|
|
self._adb_process_file(os.path.join("/", SMS_BUGLE_PATH), self._parse_db)
|
|
|
|
elif (self._adb_check_file_exists(os.path.join("/", SMS_MMSSMS_PATH))):
|
|
|
|
self.SMS_DB_TYPE = 2
|
|
|
|
self._adb_process_file(os.path.join("/", SMS_MMSSMS_PATH), self._parse_db)
|
|
|
|
return
|
|
|
|
except InsufficientPrivileges:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.log.warn("No SMS database found. Trying extraction of SMS data using Android backup feature.")
|
|
|
|
self._extract_sms_adb()
|