-
Notifications
You must be signed in to change notification settings - Fork 20
/
sam.py
437 lines (347 loc) · 14.4 KB
/
sam.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
# -*- coding: utf-8 -*-
"""Security Accounts Manager (SAM) collector."""
import pyfwnt
from dfdatetime import filetime as dfdatetime_filetime
from dfdatetime import semantic_time as dfdatetime_semantic_time
from winregrc import data_format
from winregrc import errors
from winregrc import interface
class UserAccount(object):
"""User account.
Attributes:
account_expiration_time (dfdatetime.DateTimeValues): account expiration
date and time.
codepage (str): code page.
comment (str): comment.
full_name (str): full name.
last_login_time (dfdatetime.DateTimeValues): last log-in date and time.
last_password_failure_time (dfdatetime.DateTimeValues): last password
failure date and time.
last_password_set_time (dfdatetime.DateTimeValues): last password set
date and time.
name (str): name
number_of_logons (int): number of log-ons.
number_of_password_failures (int): number of password failures.
primary_gid (int): primary group identifier (GID).
rid (str): relative identifier (RID).
user_account_control_flags (int): user account control flags.
user_comment (str): user comment.
username (str): username.
"""
def __init__(self):
"""Initializes an user account."""
super(UserAccount, self).__init__()
self.account_expiration_time = None
self.codepage = None
self.comment = None
self.full_name = None
self.last_login_time = None
self.last_password_failure_time = None
self.last_password_set_time = None
self.name = None
self.number_of_logons = None
self.number_of_password_failures = None
self.primary_gid = None
self.rid = None
self.user_account_control_flags = None
self.user_comment = None
self.username = None
class SecurityAccountManagerDataParser(data_format.BinaryDataFormat):
"""Security Accounts Manager (SAM) data parser."""
_DEFINITION_FILE = 'sam.yaml'
_USER_INFORMATION_DESCRIPTORS = [
'security descriptor',
'username',
'full name',
'comment',
'user comment',
'unknown1',
'home directory',
'home directory connect',
'script path',
'profile path',
'workstations',
'hours allowed',
'unknown2',
'LM hash',
'NTLM hash',
'unknown3',
'unknown4']
_USER_ACCOUNT_CONTROL_FLAGS = {
0x00000001: 'USER_ACCOUNT_DISABLED',
0x00000002: 'USER_HOME_DIRECTORY_REQUIRED',
0x00000004: 'USER_PASSWORD_NOT_REQUIRED',
0x00000008: 'USER_TEMP_DUPLICATE_ACCOUNT',
0x00000010: 'USER_NORMAL_ACCOUNT',
0x00000020: 'USER_MNS_LOGON_ACCOUNT',
0x00000040: 'USER_INTERDOMAIN_TRUST_ACCOUNT',
0x00000080: 'USER_WORKSTATION_TRUST_ACCOUNT',
0x00000100: 'USER_SERVER_TRUST_ACCOUNT',
0x00000200: 'USER_DONT_EXPIRE_PASSWORD',
0x00000400: 'USER_ACCOUNT_AUTO_LOCKED',
0x00000800: 'USER_ENCRYPTED_TEXT_PASSWORD_ALLOWED',
0x00001000: 'USER_SMARTCARD_REQUIRED',
0x00002000: 'USER_TRUSTED_FOR_DELEGATION',
0x00004000: 'USER_NOT_DELEGATED',
0x00008000: 'USER_USE_DES_KEY_ONLY',
0x00010000: 'USER_DONT_REQUIRE_PREAUTH',
0x00020000: 'USER_PASSWORD_EXPIRED',
0x00040000: 'USER_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION',
0x00080000: 'USER_NO_AUTH_DATA_REQUIRED',
0x00100000: 'USER_PARTIAL_SECRETS_ACCOUNT',
0x00200000: 'USER_USE_AES_KEYS'}
_DEBUG_INFO_C_VALUE = [
('format_version', 'Format version', '_FormatIntegerAsDecimal'),
('unknown1', 'Unknown1', '_FormatIntegerAsHexadecimal2'),
('unknown2', 'Unknown1', '_FormatIntegerAsHexadecimal4'),
('security_descriptor_size', 'Security descriptor size',
'_FormatIntegerAsDecimal'),
('unknown3', 'Unknown1', '_FormatIntegerAsHexadecimal2'),
('unknown4', 'Unknown1', '_FormatIntegerAsHexadecimal2'),
('security_descriptor', 'Security descriptor',
'_FormatSecurityDescriptor')]
_DEBUG_INFO_F_VALUE = [
('major_version', 'Major version', '_FormatIntegerAsDecimal'),
('minor_version', 'Minor version', '_FormatIntegerAsDecimal'),
('unknown1', 'Unknown1', '_FormatIntegerAsHexadecimal8'),
('last_login_time', 'Last login time', '_FormatIntegerAsFiletime'),
('unknown2', 'Unknown2', '_FormatIntegerAsHexadecimal8'),
('last_password_set_time', 'Last password set time',
'_FormatIntegerAsFiletime'),
('account_expiration_time', 'Account expiration time',
'_FormatIntegerAsFiletime'),
('last_password_failure_time', 'Last password failure time',
'_FormatIntegerAsFiletime'),
('rid', 'Relative identifier (RID)', '_FormatIntegerAsDecimal'),
('primary_gid', 'Primary group identifier (GID)',
'_FormatIntegerAsDecimal'),
('user_account_control_flags', 'User account control flags',
'_FormatIntegerAsHexadecimal8'),
('user_account_control_flags', None, '_FormatUserAccountControlFlags'),
('country_code', 'Country code', '_FormatIntegerAsHexadecimal4'),
('codepage', 'Codepage', '_FormatIntegerAsDecimal'),
('number_of_password_failures', 'Number of password failures',
'_FormatIntegerAsDecimal'),
('number_of_logons', 'Number of logons', '_FormatIntegerAsDecimal'),
('unknown6', 'Unknown6', '_FormatIntegerAsHexadecimal8'),
('unknown7', 'Unknown7', '_FormatIntegerAsHexadecimal8'),
('unknown8', 'Unknown8', '_FormatIntegerAsHexadecimal8')]
def _DebugPrintUserInformationDescriptor(
self, index, descriptor, descriptor_data_offset, descriptor_data):
"""Prints an user information descriptor.
Args:
index (int): index of the user information descriptor.
descriptor (user_information_descriptor): user information descriptor.
descriptor_data_offset (int): offset of the descriptor data relative from
the start of the V value data.
descriptor_data (bytes): descriptor data.
"""
descriptor_index = index + 1
self._DebugPrintText(
f'User information descriptor: {descriptor_index:d}:\n')
value_string = self._USER_INFORMATION_DESCRIPTORS[index]
self._DebugPrintValue('Description', value_string)
self._DebugPrintValue('Offset', (
f'0x{descriptor.offset:08x} (0x{descriptor_data_offset:08x})'))
self._DebugPrintDecimalValue('Size', descriptor.size)
self._DebugPrintValue('Unknown1', f'0x{descriptor.unknown1:08x}')
self._DebugPrintData('Data', descriptor_data)
# pylint: disable=no-member,using-constant-test
def _FormatSecurityDescriptor(self, security_descriptor_data):
"""Formats security descriptor.
Args:
security_descriptor_data (bytes): security descriptor data.
Returns:
str: formatted security descriptor.
"""
fwnt_descriptor = pyfwnt.security_descriptor()
fwnt_descriptor.copy_from_byte_stream(security_descriptor_data)
lines = []
if fwnt_descriptor.owner:
identifier_string = fwnt_descriptor.owner.get_string()
lines.append(f'\tOwner: {identifier_string:s}')
if fwnt_descriptor.group:
identifier_string = fwnt_descriptor.group.get_string()
lines.append(f'\tGroup: {identifier_string:s}')
# TODO: format SACL
# TODO: format DACL
lines.append('')
return '\n'.join(lines)
# pylint: enable=no-member,using-constant-test
def _FormatUserAccountControlFlags(self, user_account_control_flags):
"""Formats user account control flags.
Args:
user_account_control_flags (int): user account control flags.
Returns:
str: formatted user account control flags.
"""
lines = []
if user_account_control_flags:
for flag, identifier in sorted(
self._USER_ACCOUNT_CONTROL_FLAGS.items()):
if flag & user_account_control_flags:
lines.append(f'\t{identifier:s} (0x{flag:08x})')
lines.append('')
lines.append('')
return '\n'.join(lines)
def ParseCValue(self, value_data):
"""Parses the C value data.
Args:
value_data (bytes): F value data.
Raises:
ParseError: if the value data could not be parsed.
"""
data_type_map = self._GetDataTypeMap('c_value')
try:
c_value = self._ReadStructureFromByteStream(
value_data, 0, data_type_map, 'C value')
except (ValueError, errors.ParseError) as exception:
raise errors.ParseError(
f'Unable to parse C value with error: {exception!s}')
if self._debug:
self._DebugPrintStructureObject(c_value, self._DEBUG_INFO_C_VALUE)
def _ParseFiletime(self, filetime):
"""Parses a FILETIME timestamp value.
Args:
filetime (int): a FILETIME timestamp value.
Returns:
dfdatetime.DateTimeValues: date and time values.
"""
if filetime == 0:
return dfdatetime_semantic_time.SemanticTime(string='Not set')
if filetime == 0x7fffffffffffffff:
return dfdatetime_semantic_time.SemanticTime(string='Never')
return dfdatetime_filetime.Filetime(timestamp=filetime)
def ParseFValue(self, value_data, user_account):
"""Parses the F value data.
Args:
value_data (bytes): F value data.
user_account (UserAccount): user account.
Raises:
ParseError: if the value data could not be parsed.
"""
data_type_map = self._GetDataTypeMap('f_value')
try:
f_value = self._ReadStructureFromByteStream(
value_data, 0, data_type_map, 'F value')
except (ValueError, errors.ParseError) as exception:
raise errors.ParseError(
f'Unable to parse F value with error: {exception!s}')
# TODO: change FILETIME timestamps into date time values.
# date_time = self._ParseFiletime(f_value.last_login_time)
user_account.last_login_time = f_value.last_login_time
user_account.last_password_set_time = f_value.last_password_set_time
user_account.account_expiration_time = f_value.account_expiration_time
user_account.last_password_failure_time = f_value.last_password_failure_time
user_account.rid = f_value.rid
user_account.primary_gid = f_value.primary_gid
user_account.user_account_control_flags = f_value.user_account_control_flags
user_account.codepage = f_value.codepage
user_account.number_of_password_failures = (
f_value.number_of_password_failures)
user_account.number_of_logons = f_value.number_of_logons
if self._debug:
self._DebugPrintStructureObject(f_value, self._DEBUG_INFO_F_VALUE)
def ParseVValue(self, value_data, user_account):
"""Parses the V value data.
Args:
value_data (bytes): V value data.
user_account (UserAccount): user account.
Raises:
ParseError: if the value data could not be parsed.
"""
data_type_map = self._GetDataTypeMap('v_value')
try:
v_value = self._ReadStructureFromByteStream(
value_data, 0, data_type_map, 'V value')
except (ValueError, errors.ParseError) as exception:
raise errors.ParseError(
f'Unable to parse V value with error: {exception!s}')
for index in range(0, 17):
user_information_descriptor = v_value[index]
data_start_offset = user_information_descriptor.offset + 0xcc
data_end_offset = data_start_offset + user_information_descriptor.size
descriptor_data = value_data[data_start_offset:data_end_offset]
if self._debug:
self._DebugPrintUserInformationDescriptor(
index, user_information_descriptor, data_start_offset,
descriptor_data)
if index == 0:
if self._debug:
value_string = self._FormatSecurityDescriptor(descriptor_data)
self._DebugPrintText('Security descriptor:\n')
self._DebugPrintText(value_string)
self._DebugPrintText('\n')
elif index == 1:
user_account.username = descriptor_data.decode(
'utf-16-le').rstrip('\x00')
if self._debug:
self._DebugPrintValue('Username', user_account.username)
self._DebugPrintText('\n')
elif index == 2:
user_account.full_name = descriptor_data.decode(
'utf-16-le').rstrip('\x00')
if self._debug:
self._DebugPrintValue('Full name', user_account.full_name)
self._DebugPrintText('\n')
elif index == 3:
user_account.comment = descriptor_data.decode(
'utf-16-le').rstrip('\x00')
if self._debug:
self._DebugPrintValue('Comment', user_account.comment)
self._DebugPrintText('\n')
elif index == 4:
user_account.user_comment = descriptor_data.decode(
'utf-16-le').rstrip('\x00')
if self._debug:
self._DebugPrintValue(
'User comment', user_account.user_comment)
self._DebugPrintText('\n')
if self._debug:
self._DebugPrintText('\n')
class SecurityAccountManagerCollector(interface.WindowsRegistryKeyCollector):
"""Security Accounts Manager (SAM) collector.
Attributes:
user_accounts (list[UserAccount]): user accounts.
"""
_USERS_KEY_PATH = (
'HKEY_LOCAL_MACHINE\\SAM\\SAM\\Domains\\Account\\Users')
def __init__(self, debug=False, output_writer=None):
"""Initializes a Security Accounts Manager (SAM) collector.
Args:
debug (Optional[bool]): True if debug information should be printed.
output_writer (Optional[OutputWriter]): output writer.
"""
super(SecurityAccountManagerCollector, self).__init__(debug=debug)
self._parser = SecurityAccountManagerDataParser(
debug=debug, output_writer=output_writer)
self.user_accounts = []
def Collect(self, registry): # pylint: disable=arguments-differ
"""Collects the Security Accounts Manager (SAM) information.
Args:
registry (dfwinreg.WinRegistry): Windows Registry.
Returns:
bool: True if the Security Accounts Manager (SAM) information key was
found, False if not.
"""
main_key = registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\SAM\\SAM')
if not main_key:
return False
c_value = main_key.GetValueByName('C')
if c_value:
self._parser.ParseCValue(c_value.data)
users_key = registry.GetKeyByPath(self._USERS_KEY_PATH)
if not users_key:
return False
for subkey in users_key.GetSubkeys():
if subkey.name == 'Names':
continue
user_account = UserAccount()
f_value = subkey.GetValueByName('F')
if f_value:
self._parser.ParseFValue(f_value.data, user_account)
v_value = subkey.GetValueByName('V')
if v_value:
self._parser.ParseVValue(v_value.data, user_account)
self.user_accounts.append(user_account)
return True