-
Notifications
You must be signed in to change notification settings - Fork 20
/
mru.py
475 lines (368 loc) · 15.8 KB
/
mru.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# -*- coding: utf-8 -*-
"""Most Recently Used (MRU) collector."""
from dtfabric.runtime import data_maps as dtfabric_data_maps
from winregrc import data_format
from winregrc import errors
class MostRecentlyUsedEntry(object):
"""Most Recently Used (MRU) entry.
Attributes:
key_path (str): path of the Windows Registry key.
shell_item_data (bytes): Shell Item data.
shell_item_list_data (bytes): Shell Item list data.
string (str): string.
value_name (str): name of the Windows Registry value.
"""
def __init__(
self, key_path=None, shell_item_data=None, shell_item_list_data=None,
string=None, value_name=None):
"""Initializes a Most Recently Used (MRU) entry.
Args:
key_path (Optional[str]): path of the Windows Registry key.
shell_item_data (Optional[bytes]): Shell Item data.
shell_item_list_data (Optional[bytes]): Shell Item list data.
string (Optional[str]): string.
value_name (Optional[str]): name of the Windows Registry value.
"""
super(MostRecentlyUsedEntry, self).__init__()
self.key_path = key_path
self.shell_item_data = shell_item_data
self.shell_item_list_data = shell_item_list_data
self.string = string
self.value_name = value_name
class MostRecentlyUsedCollector(data_format.BinaryDataFormat):
"""Most Recently Used (MRU) collector.
Attributes:
mru_entries (list[MostRecentlyUsedEntry]): most recently used (MRU) entries.
"""
_DEFINITION_FILE = 'mru.yaml'
_OPENSAVE_MRU_KEY_PATH = (
'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\'
'Explorer\\ComDlg32\\OpenSaveMRU').upper()
_SHELL_ITEM_MRU_KEY_PATHS = [
('HKEY_CURRENT_USER\\Local Settings\\Software\\Microsoft\\Windows\\'
'Shell\\BagMRU'),
('HKEY_CURRENT_USER\\Local Settings\\Software\\Microsoft\\Windows\\'
'ShellNoRoam\\BagMRU'),
'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\BagMRU',
('HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\ShellNoRoam\\'
'BagMRU'),
('HKEY_CURRENT_USER\\Software\\Classes\\Local Settings\\Software\\'
'Microsoft\\Windows\\Shell\\BagMRU'),
('HKEY_CURRENT_USER\\Software\\Classes\\Local Settings\\Software\\'
'Microsoft\\Windows\\ShellNoRoam\\BagMRU'),
('HKEY_CURRENT_USER\\Software\\Classes\\Wow6432Node\\Local Settings\\'
'Software\\Microsoft\\Windows\\Shell\\BagMRU'),
('HKEY_CURRENT_USER\\Software\\Classes\\Wow6432Node\\Local Settings\\'
'Software\\Microsoft\\Windows\\ShellNoRoam\\BagMRU')]
_SHELL_ITEM_MRU_KEY_PATHS = [
key_path.upper() for key_path in _SHELL_ITEM_MRU_KEY_PATHS]
_SHELL_ITEM_LIST_MRU_KEY_PATHS = [
('HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\'
'Explorer\\DesktopStreamMRU'),
('HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\'
'Explorer\\ComDlg32\\OpenSavePidlMRU'),
('HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\'
'Explorer\\StreamMRU'),
('HKEY_CURRENT_USER\\Software\\Classes\\Software\\Microsoft\\Windows\\'
'CurrentVersion\\Explorer\\StreamMRU')]
_SHELL_ITEM_LIST_MRU_KEY_PATHS = [
key_path.upper() for key_path in _SHELL_ITEM_LIST_MRU_KEY_PATHS]
_STRING_AND_SHELL_ITEM_MRU_KEY_PATHS = [
('HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\'
'Explorer\\RecentDocs')]
_STRING_AND_SHELL_ITEM_MRU_KEY_PATHS = [
key_path.upper() for key_path in _STRING_AND_SHELL_ITEM_MRU_KEY_PATHS]
_STRING_AND_SHELL_ITEM_LIST_MRU_KEY_PATHS = [
('HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\'
'Explorer\\ComDlg32\\LastVisitedPidlMRU')]
_STRING_AND_SHELL_ITEM_LIST_MRU_KEY_PATHS = [
key_path.upper()
for key_path in _STRING_AND_SHELL_ITEM_LIST_MRU_KEY_PATHS]
def __init__(self, debug=False, output_writer=None):
"""Initializes a Most Recently Used (MRU) collector.
Args:
debug (Optional[bool]): True if debug information should be printed.
output_writer (Optional[OutputWriter]): output writer.
"""
super(MostRecentlyUsedCollector, self).__init__(debug=debug)
self._output_writer = output_writer
self.mru_entries = []
def _InKeyPaths(self, key_path, key_paths):
"""Checks if a specific key path is defined in a list of key paths.
Args:
key_path (str): Windows Registry key path.
key_paths (list[str]): list of Windows Registry key paths in upper case.
Returns:
bool: True if the key path is defined in the list of key paths.
"""
key_path = key_path.upper()
for matching_key_path in key_paths:
if key_path.startswith(matching_key_path):
return True
return False
def _ProcessKey(self, registry_key):
"""Processes a Windows Registry key.
Args:
registry_key (dfwinreg.WinRegistryKey): Windows Registry key.
Returns:
bool: True if a Most Recently Used (MRU) key was found, False if not.
"""
result = False
value_names = [
registry_value.name for registry_value in registry_key.GetValues()]
if 'MRUList' in value_names:
if self._ProcessKeyWithMRUListValue(registry_key):
result = True
elif 'MRUListEx' in value_names:
if self._ProcessKeyWithMRUListExValue(registry_key):
result = True
for subkey in registry_key.GetSubkeys():
if self._ProcessKey(subkey):
result = True
return result
def _ProcessKeyWithMRUListValue(self, registry_key):
"""Processes a Windows Registry key that contains a MRUList value.
Args:
registry_key (dfwinreg.WinRegistryKey): Windows Registry key.
Returns:
bool: True if a Most Recently Used (MRU) key was found, False if not.
Raises:
ParseError: if the MRUList value could not be parsed.
"""
registry_value = registry_key.GetValueByName('MRUList')
data_type_map = self._GetDataTypeMap('mrulist_entries')
context = dtfabric_data_maps.DataTypeMapContext(values={
'data_size': len(registry_value.data)})
try:
mrulist_entries = self._ReadStructureFromByteStream(
registry_value.data, 0, data_type_map, 'MRUList entries',
context=context)
except (ValueError, errors.ParseError) as exception:
raise errors.ParseError(
f'Unable to parse MRUList entries with error: {exception!s}')
mrulist = set([])
recovered_mrulist = set([])
is_recovered = False
for entry_letter in mrulist_entries:
if entry_letter == 0:
is_recovered = True
entry_letter = chr(entry_letter)
if is_recovered:
recovered_mrulist.add(entry_letter)
else:
mrulist.add(entry_letter)
result = False
for registry_value in registry_key.GetValues():
if registry_value.name in (
'MRUList', 'NodeSlot', 'NodeSlots', 'ViewStream'):
continue
if self._debug:
self._output_writer.WriteText(
f'Key: {registry_key.path:s}\nValue: {registry_value.name:s}\n')
if self._InKeyPaths(registry_key.path, self._SHELL_ITEM_MRU_KEY_PATHS):
self._ProcessMRUEntryShellItem(
registry_key.path, registry_value.name, registry_value.data)
elif self._InKeyPaths(
registry_key.path, self._SHELL_ITEM_LIST_MRU_KEY_PATHS):
self._ProcessMRUEntryShellItemList(
registry_key.path, registry_value.name, registry_value.data)
elif self._InKeyPaths(
registry_key.path, self._STRING_AND_SHELL_ITEM_MRU_KEY_PATHS):
self._ProcessMRUEntryStringAndShellItem(
registry_key.path, registry_value.name, registry_value.data)
elif self._InKeyPaths(
registry_key.path, self._STRING_AND_SHELL_ITEM_LIST_MRU_KEY_PATHS):
self._ProcessMRUEntryStringAndShellItemList(
registry_key.path, registry_value.name, registry_value.data)
else:
self._ProcessMRUEntryString(
registry_key.path, registry_value.name, registry_value.data)
result = True
return result
def _ProcessKeyWithMRUListExValue(self, registry_key):
"""Processes a Windows Registry key that contains a MRUListEx value.
Args:
registry_key (dfwinreg.WinRegistryKey): Windows Registry key.
Returns:
bool: True if a Most Recently Used (MRU) key was found, False if not.
Raises:
ParseError: if the MRUListEx value could not be parsed.
"""
# TODO: determine what trailing data is in:
# HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\
# ComDlg32\CIDSizeMRU
registry_value = registry_key.GetValueByName('MRUListEx')
data_type_map = self._GetDataTypeMap('mrulistex_entries')
context = dtfabric_data_maps.DataTypeMapContext(values={
'data_size': len(registry_value.data)})
try:
mrulistex_entries = self._ReadStructureFromByteStream(
registry_value.data, 0, data_type_map, 'MRUListEx entries',
context=context)
except (ValueError, errors.ParseError) as exception:
raise errors.ParseError(
f'Unable to parse MRUListEx entries with error: {exception!s}')
mrulistex = set([])
recovered_mrulistex = set([])
is_recovered = False
for entry_number in mrulistex_entries:
if entry_number == 0:
is_recovered = True
if is_recovered:
recovered_mrulistex.add(entry_number)
else:
mrulistex.add(entry_number)
result = False
for registry_value in registry_key.GetValues():
if registry_value.name in (
'MRUListEx', 'NodeSlot', 'NodeSlots', 'ViewStream'):
continue
if self._debug:
self._output_writer.WriteText(
f'Key: {registry_key.path:s}\nValue: {registry_value.name:s}\n')
if self._InKeyPaths(registry_key.path, self._SHELL_ITEM_MRU_KEY_PATHS):
self._ProcessMRUEntryShellItem(
registry_key.path, registry_value.name, registry_value.data)
elif self._InKeyPaths(
registry_key.path, self._SHELL_ITEM_LIST_MRU_KEY_PATHS):
self._ProcessMRUEntryShellItemList(
registry_key.path, registry_value.name, registry_value.data)
elif self._InKeyPaths(
registry_key.path, self._STRING_AND_SHELL_ITEM_MRU_KEY_PATHS):
self._ProcessMRUEntryStringAndShellItem(
registry_key.path, registry_value.name, registry_value.data)
elif self._InKeyPaths(
registry_key.path, self._STRING_AND_SHELL_ITEM_LIST_MRU_KEY_PATHS):
self._ProcessMRUEntryStringAndShellItemList(
registry_key.path, registry_value.name, registry_value.data)
else:
self._ProcessMRUEntryString(
registry_key.path, registry_value.name, registry_value.data)
result = True
return result
def _ProcessMRUEntryShellItem(self, key_path, value_name, value_data):
"""Processes a shell item MRUEntry.
Args:
key_path (str): path of the Windows Registry key.
value_name (str): name of the Windows Registry value.
value_data (bytes): Windows Registry value data.
"""
if self._debug:
self._output_writer.WriteDebugData('Shell item data', value_data)
mru_entry = MostRecentlyUsedEntry(
key_path=key_path, shell_item_data=value_data, value_name=value_name)
self.mru_entries.append(mru_entry)
def _ProcessMRUEntryShellItemList(self, key_path, value_name, value_data):
"""Processes a shell item list MRUEntry.
Args:
key_path (str): path of the Windows Registry key.
value_name (str): name of the Windows Registry value.
value_data (bytes): Windows Registry value data.
"""
if self._debug:
self._output_writer.WriteDebugData('Shell item list data', value_data)
mru_entry = MostRecentlyUsedEntry(
key_path=key_path, shell_item_list_data=value_data,
value_name=value_name)
self.mru_entries.append(mru_entry)
def _ProcessMRUEntryString(self, key_path, value_name, value_data):
"""Processes a string MRUEntry.
Args:
key_path (str): path of the Windows Registry key.
value_name (str): name of the Windows Registry value.
value_data (bytes): Windows Registry value data.
"""
value_data_size = len(value_data)
data_offset = 0
for data_offset in range(0, value_data_size, 2):
if value_data[data_offset:data_offset + 2] == b'\0\0':
data_offset += 2
break
if self._debug:
self._output_writer.WriteDebugData(
'String data', value_data[0:data_offset])
string = value_data[0:data_offset - 2].decode('utf-16-le')
if self._debug:
self._output_writer.WriteValue('String', string)
if self._debug and data_offset < value_data_size:
self._output_writer.WriteDebugData(
'Trailing data', value_data[data_offset:])
mru_entry = MostRecentlyUsedEntry(
key_path=key_path, string=string, value_name=value_name)
self.mru_entries.append(mru_entry)
def _ProcessMRUEntryStringAndShellItem(
self, key_path, value_name, value_data):
"""Processes a string and shell item MRUEntry.
Args:
key_path (str): path of the Windows Registry key.
value_name (str): name of the Windows Registry value.
value_data (bytes): Windows Registry value data.
"""
value_data_size = len(value_data)
data_offset = 0
for data_offset in range(0, value_data_size, 2):
if value_data[data_offset:data_offset + 2] == b'\0\0':
data_offset += 2
break
if self._debug:
self._output_writer.WriteDebugData(
'String data', value_data[0:data_offset])
string = value_data[0:data_offset - 2].decode('utf-16-le')
if self._debug:
self._output_writer.WriteValue('String', string)
if data_offset < value_data_size:
if self._debug:
self._output_writer.WriteDebugData(
'Shell item data', value_data[data_offset:])
mru_entry = MostRecentlyUsedEntry(
key_path=key_path, shell_item_data=value_data[data_offset:],
string=string, value_name=value_name)
self.mru_entries.append(mru_entry)
def _ProcessMRUEntryStringAndShellItemList(
self, key_path, value_name, value_data):
"""Processes a string and shell item list MRUEntry.
Args:
key_path (str): path of the Windows Registry key.
value_name (str): name of the Windows Registry value.
value_data (bytes): Windows Registry value data.
"""
value_data_size = len(value_data)
data_offset = 0
for data_offset in range(0, value_data_size, 2):
if value_data[data_offset:data_offset + 2] == b'\0\0':
data_offset += 2
break
if self._debug:
self._output_writer.WriteDebugData(
'String data', value_data[0:data_offset])
string = value_data[0:data_offset - 2].decode('utf-16-le')
if self._debug:
self._output_writer.WriteValue('String', string)
if data_offset < value_data_size:
if self._debug:
self._output_writer.WriteDebugData(
'Shell item list data', value_data[data_offset:])
mru_entry = MostRecentlyUsedEntry(
key_path=key_path, shell_item_list_data=value_data[data_offset:],
string=string, value_name=value_name)
self.mru_entries.append(mru_entry)
def Collect(self, registry): # pylint: disable=arguments-differ
"""Collects Most Recently Used (MRU) entries.
Args:
registry (dfwinreg.WinRegistry): Windows Registry.
Returns:
bool: True if a Most Recently Used (MRU) key was found, False if not.
"""
result = False
current_user_key = registry.GetKeyByPath('HKEY_CURRENT_USER')
if current_user_key:
if self._ProcessKey(current_user_key):
result = True
if not result:
# Fallback for if source is a single UsrClass.dat file.
current_user_classes_key = registry.GetKeyByPath(
'HKEY_CURRENT_USER\\Software\\Classes')
if current_user_classes_key:
if self._ProcessKey(current_user_classes_key):
result = True
return result