-
Notifications
You must be signed in to change notification settings - Fork 15
/
yarp-print
executable file
·414 lines (320 loc) · 10.7 KB
/
yarp-print
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
#!/usr/bin/env python3
# yarp: yet another registry parser
# (c) Maxim Suhanov
from yarp import *
from yarp import __version__
import argparse
from collections import namedtuple
import os
import sys
PROGRAM_NAME = 'yarp-print'
PROGRAM_VERSION = __version__
Arguments = namedtuple('Arguments', [ 'primary_file', 'do_recovery', 'do_deleted' ])
def parse_args():
"""Parse command line arguments and return a named tuple (Arguments)."""
parser = argparse.ArgumentParser(prog = PROGRAM_NAME, description = 'Parse a Windows registry file (possibly a truncated one), print all keys and values.', add_help = False, prefix_chars = '-')
group_main = parser.add_argument_group('Main arguments')
group_opt = parser.add_argument_group('Optional arguments')
group_misc = parser.add_argument_group('Miscellaneous arguments')
group_main.add_argument('file', help = 'a registry file (primary) to parse')
group_opt.add_argument('--no-recovery', action = 'store_true', help = 'do not discover and use transaction log files to recover the hive (in memory)')
group_opt.add_argument('--deleted', action = 'store_true', help = 'include deleted keys and values to the output')
group_misc.add_argument('--help', action = 'help', help = 'show this help message and exit')
group_misc.add_argument('--version', action = 'version', help = 'show the version number and exit', version = PROGRAM_VERSION)
parsed_args = parser.parse_args()
primary_file = parsed_args.file
do_recovery = not parsed_args.no_recovery
do_deleted = parsed_args.deleted
return Arguments(primary_file = primary_file, do_recovery = do_recovery, do_deleted = do_deleted)
def print_hive_information(hive):
print('Last written timestamp (UTC): {}'.format(hive.last_written_timestamp()))
try:
print('Last reorganized timestamp (UTC): {}'.format(hive.last_reorganized_timestamp()))
except (ValueError, OverflowError):
pass
try:
print('Serialization timestamp (UTC): {}'.format(hive.offreg_serialization_timestamp()))
except (ValueError, OverflowError):
pass
if hive.are_layered_keys_supported():
print('Layered keys are supported')
print()
def print_value(value):
value_name = value.name()
if value_name == '':
print('Default value')
else:
print('Value name: {}'.format(value_name))
print('Value type: {}'.format(value.type_str()))
print('Data size: {}'.format(value.data_size()))
value_flags = value.flags_str()
if value_flags is not None:
print('Flags: {}'.format(value_flags))
try:
data = value.data()
except UnicodeDecodeError:
data = value.data_raw()
if type(data) is bytes:
print('Data (hexdump):')
print(RegistryHelpers.HexDump(data))
elif type(data) is list:
print('Data (one list element per line):')
for element in data:
print(element)
else:
print('Data (decoded):')
print(data)
print()
def print_key(key):
key_path = key.path()
if key_path == '':
print('Root key')
else:
print('Key path: {}'.format(key_path))
classname = key.classname()
if classname is not None:
print('Class name: {}'.format(classname))
print('Last written timestamp (UTC): {}'.format(key.last_written_timestamp()))
print('Access bits: {}'.format(key.access_bits()))
key_flags = key.flags_str()
if key_flags is not None:
print('Flags: {}'.format(key_flags))
security = key.security()
if security is not None:
security_descriptor = security.descriptor()
try:
owner_sid = RegistryHelpers.ParseSecurityDescriptorRelative(security_descriptor).owner_sid
except Exception:
owner_sid = 'invalid'
print('Owner SID: {}'.format(owner_sid))
print()
for value in key.values():
print_value(value)
if args.do_deleted:
print_note = True
try:
for value in key.remnant_values():
if print_note:
print('Associated deleted values below (may contain reallocated data):')
print()
print_note = False
print_deleted_value(value)
except (Registry.RegistryException, UnicodeDecodeError):
pass
print('---')
print()
def print_key_recursive(key):
print_key(key)
for subkey in key.subkeys():
print_key_recursive(subkey)
def print_deleted_value(value):
value_name = value.name()
if value_name == '':
print('Default value')
else:
print('Value name: {}'.format(value_name))
print('Value type: {}'.format(value.type_str()))
print('Data size: {}'.format(value.data_size()))
value_flags = value.flags_str()
if value_flags is not None:
print('Flags: {}'.format(value_flags))
try:
data = value.data()
except Registry.RegistryException:
data = None
except UnicodeDecodeError:
data = value.data_raw()
if data is None:
print('Data not recovered')
else:
if type(data) is bytes:
print('Data (hexdump):')
print(RegistryHelpers.HexDump(data))
elif type(data) is list:
print('Data (one list element per line):')
for element in data:
print(element)
else:
print('Data (decoded):')
print(data)
print()
def print_deleted_key(key):
try:
key_path = key.path()
except Registry.RegistryException:
key_path = None
if key_path is None:
print('Unknown key path')
print('Partial key path: {}'.format(key.path_partial()))
print('Key name: {}'.format(key.name()))
else:
if key_path == '':
print('Root key')
else:
print('Key path: {}'.format(key_path))
try:
classname = key.classname()
except (Registry.RegistryException, UnicodeDecodeError):
classname = None
if classname is not None:
print('Class name: {}'.format(classname))
try:
print('Last written timestamp (UTC): {}'.format(key.last_written_timestamp()))
except (ValueError, OverflowError):
print('Last written timestamp is not plausible')
print('Access bits: {}'.format(key.access_bits()))
key_flags = key.flags_str()
if key_flags is not None:
print('Flags: {}'.format(key_flags))
try:
security = key.security()
except Registry.RegistryException:
security = None
if security is not None:
security_descriptor = security.descriptor()
try:
owner_sid = RegistryHelpers.ParseSecurityDescriptorRelative(security_descriptor).owner_sid
except Exception:
owner_sid = 'invalid'
print('Owner SID: {}'.format(owner_sid))
print()
try:
for value in key.values():
print_deleted_value(value)
except (Registry.RegistryException, UnicodeDecodeError):
pass
try:
for value in key.remnant_values():
print_deleted_value(value)
except (Registry.RegistryException, UnicodeDecodeError):
pass
print('---')
print()
# Currently, we can use functions for deleted keys and values to print keys and values in a truncated hive.
print_truncated_value = print_deleted_value
print_truncated_key = print_deleted_key
def process_normal_hive(hive):
print('Hive information:')
print()
print_hive_information(hive)
print('Keys and values:')
print()
print_key_recursive(hive.root_key())
if args.do_deleted:
print('Deleted keys and values (may contain reallocated data):')
print()
scanner = RegistryRecover.Scanner(hive, yield_remnant_bytes = True)
for item in scanner.scan():
if type(item) is Registry.RegistryKey:
print_deleted_key(item)
print('Deleted values (all, may contain reallocated data):')
print()
for item in scanner.scan():
if type(item) is Registry.RegistryValue:
print_deleted_value(item)
print('Possible value data or other remnant data (can include deleted registry structures, slack or even garbage):')
print()
for item in scanner.scan():
if type(item) is bytes and len(item) > 4 and len(item.rstrip(b'\x00')) > 4:
print(RegistryHelpers.HexDump(item))
print('---')
def process_truncated_hive(hive):
print('Primary file seems to be truncated, only available keys and values will be printed', file = sys.stderr)
print('Hive information:')
print()
print_hive_information(hive)
print('Keys and values (allocated):')
print()
for item in hive.scan():
if type(item) is Registry.RegistryKey:
print_truncated_key(item)
print('All values (allocated):')
print()
for item in hive.scan():
if type(item) is Registry.RegistryValue:
print_truncated_value(item)
if args.do_deleted:
print('Unallocated keys and values (may contain reallocated data):')
print()
scanner = RegistryRecover.Scanner(hive, False, yield_remnant_bytes = True)
for item in scanner.scan():
if type(item) is Registry.RegistryKey:
print_deleted_key(item)
print('Unallocated values (all, may contain reallocated data):')
print()
for item in scanner.scan():
if type(item) is Registry.RegistryValue:
print_deleted_value(item)
print('Possible value data or other remnant data (can include deleted registry structures, slack or even garbage):')
print()
for item in scanner.scan():
if type(item) is bytes and len(item) > 4 and len(item.rstrip(b'\x00')) > 4:
print(RegistryHelpers.HexDump(item))
print('---')
args = parse_args()
if not os.path.isfile(args.primary_file):
print('Primary file does not exist: {}'.format(args.primary_file), file = sys.stderr)
sys.exit(255)
primary = open(args.primary_file, 'rb')
try:
hive = Registry.RegistryHive(primary)
except RegistryFile.NotSupportedException:
print('File seems to be a transaction log file, converting it to a truncated primary file', file = sys.stderr)
temp_obj = primary
log = RegistryFile.NewLogFile(temp_obj)
primary = log.rebuild_primary_file_using_remnant_log_entries(True)
temp_obj.close()
truncated = True
except RegistryFile.BaseBlockException:
print('File seems to be a fragment, converting it to a truncated primary file', file = sys.stderr)
temp_obj = primary
primary = RegistryFile.FragmentTranslator(temp_obj)
temp_obj.close()
truncated = True
except Registry.RegistryException:
truncated = True
else:
truncated = False
if truncated:
hive = Registry.RegistryHiveTruncated(primary)
process_truncated_hive(hive)
hive = None
primary.close()
sys.exit(0)
if args.do_recovery:
log_files = RegistryHelpers.DiscoverLogFiles(args.primary_file)
log = None
if log_files.log_path is not None:
log = open(log_files.log_path, 'rb')
log1 = None
if log_files.log1_path is not None:
log1 = open(log_files.log1_path, 'rb')
log2 = None
if log_files.log2_path is not None:
log2 = open(log_files.log2_path, 'rb')
try:
recovery_result = hive.recover_auto(log, log1, log2)
except Registry.AutoRecoveryException:
print('An error has occurred when recovering a hive using a transaction log', file = sys.stderr)
recovered = False
else:
recovered = recovery_result.recovered
try:
hive.walk_everywhere()
except (RegistryFile.CellOffsetException, RegistryFile.ReadException):
if args.do_recovery and recovered:
raise
# A truncated dirty hive.
hive = Registry.RegistryHiveTruncated(primary)
process_truncated_hive(hive)
else:
process_normal_hive(hive)
hive = None
primary.close()
if args.do_recovery:
if log is not None:
log.close()
if log1 is not None:
log1.close()
if log2 is not None:
log2.close()