-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathlocationd.py
174 lines (151 loc) · 6.13 KB
/
locationd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Mobile Verification Toolkit (MVT)
# Copyright (c) 2021-2023 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 base64
import logging
import plistlib
from typing import Optional, Union
from mvt.common.utils import convert_mactime_to_iso
from ..base import IOSExtraction
LOCATIOND_BACKUP_IDS = [
"a690d7769cce8904ca2b67320b107c8fe5f79412",
]
LOCATIOND_ROOT_PATHS = [
"private/var/mobile/Library/Caches/locationd/clients.plist",
"private/var/root/Library/Caches/locationd/clients.plist",
]
class LocationdClients(IOSExtraction):
"""Extract information from apps who used geolocation."""
def __init__(
self,
file_path: Optional[str] = None,
target_path: Optional[str] = None,
results_path: Optional[str] = None,
module_options: Optional[dict] = None,
log: logging.Logger = logging.getLogger(__name__),
results: Optional[list] = None,
) -> None:
super().__init__(
file_path=file_path,
target_path=target_path,
results_path=results_path,
module_options=module_options,
log=log,
results=results,
)
self.timestamps = [
"ConsumptionPeriodBegin",
"ReceivingLocationInformationTimeStopped",
"VisitTimeStopped",
"LocationTimeStopped",
"BackgroundLocationTimeStopped",
"SignificantTimeStopped",
"NonPersistentSignificantTimeStopped",
"FenceTimeStopped",
"BeaconRegionTimeStopped",
]
def serialize(self, record: dict) -> Union[dict, list]:
records = []
for timestamp in self.timestamps:
if timestamp in record.keys():
records.append(
{
"timestamp": record[timestamp],
"module": self.__class__.__name__,
"event": timestamp,
"data": f"{timestamp} from {record['package']}",
}
)
return records
def check_indicators(self) -> None:
if not self.indicators:
return
for result in self.results:
parts = result["package"].split("/")
proc_name = parts[len(parts) - 1]
ioc = self.indicators.check_process(proc_name)
if ioc:
self.log.warning(
"Found a suspicious process name in LocationD entry %s",
result["package"],
)
result["matched_indicator"] = ioc
self.detected.append(result)
continue
if "BundleId" in result:
ioc = self.indicators.check_process(result["BundleId"])
if ioc:
self.log.warning(
"Found a suspicious process name in LocationD entry %s",
result["package"],
)
result["matched_indicator"] = ioc
if "BundlePath" in result:
ioc = self.indicators.check_file_path(result["BundlePath"])
if ioc:
self.log.warning(
"Found a suspicious file path in Location D: %s",
result["BundlePath"],
)
result["matched_indicator"] = ioc
self.detected.append(result)
continue
if "Executable" in result:
ioc = self.indicators.check_file_path(result["Executable"])
if ioc:
self.log.warning(
"Found a suspicious file path in Location D: %s",
result["Executable"],
)
result["matched_indicator"] = ioc
self.detected.append(result)
continue
if "Registered" in result:
# Sometimes registered is a bool
if isinstance(result["Registered"], bool):
continue
ioc = self.indicators.check_file_path(result["Registered"])
if ioc:
self.log.warning(
"Found a suspicious file path in Location D: %s",
result["Registered"],
)
result["matched_indicator"] = ioc
self.detected.append(result)
continue
def _extract_locationd_entries(self, file_path):
with open(file_path, "rb") as handle:
file_plist = plistlib.load(handle)
for key, _ in file_plist.items():
# Some migration information are int and not dicts
if not isinstance(file_plist[key], dict):
continue
# FIXME: unclear key format in iOS 17
result = file_plist[key]
result["package"] = key.rstrip(":")
for timestamp in self.timestamps:
if timestamp in result.keys():
result[timestamp] = convert_mactime_to_iso(result[timestamp])
if "ClientStorageToken" in result:
result["ClientStorageToken"] = base64.b64encode(
result["ClientStorageToken"]
)
self.results.append(result)
def run(self) -> None:
if self.is_backup:
self._find_ios_database(backup_ids=LOCATIOND_BACKUP_IDS)
self.log.info("Found Locationd Clients plist at path: %s", self.file_path)
self._extract_locationd_entries(self.file_path)
elif self.is_fs_dump:
for locationd_path in self._get_fs_files_from_patterns(
LOCATIOND_ROOT_PATHS
):
self.file_path = locationd_path
self.log.info(
"Found Locationd Clients plist at path: %s", self.file_path
)
self._extract_locationd_entries(self.file_path)
self.log.info(
"Extracted a total of %d Locationd Clients entries", len(self.results)
)