-
Notifications
You must be signed in to change notification settings - Fork 203
/
AD_LDAP_Entity_Attribute_Lookup.py
610 lines (446 loc) · 26.5 KB
/
AD_LDAP_Entity_Attribute_Lookup.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
"""
Accepts a user or device and looks up the most recent attributes and groups for that user or device. This playbook produces a normalized output for each user and device.
"""
import phantom.rules as phantom
import json
from datetime import datetime, timedelta
@phantom.playbook_block()
def on_start(container):
phantom.debug('on_start() called')
# call 'playbook_inputs_filter' block
playbook_inputs_filter(container=container)
return
@phantom.playbook_block()
def get_user_attributes(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_user_attributes() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Query for the user attributes for filtered playbook inputs.
################################################################################
filtered_input_0_user = phantom.collect2(container=container, datapath=["filtered-data:playbook_inputs_filter:condition_1:playbook_input:user"])
parameters = []
# build parameters list for 'get_user_attributes' call
for filtered_input_0_user_item in filtered_input_0_user:
if filtered_input_0_user_item[0] is not None:
parameters.append({
"attributes": "userPrincipalName;mail;accountExpires;name;memberOf;title;department;manager;sAMAccountName;distinguishedName;mobile;ipPhone;homePhone;telephoneNumber;otherMobile;otherIpPhone;otherHomePhone;otherTelephone;whenCreated;objectSid;objectGUID",
"principals": filtered_input_0_user_item[0],
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("get attributes", parameters=parameters, name="get_user_attributes", assets=["ad_ldap"], callback=filter_user_results)
return
@phantom.playbook_block()
def format_user_outputs(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("format_user_outputs() called")
################################################################################
# Format a normalized output for each user.
################################################################################
filtered_result_0_data_filter_user_results = phantom.collect2(container=container, datapath=["filtered-data:filter_user_results:condition_1:get_user_attributes:action_result.data.*.entries.*.attributes","filtered-data:filter_user_results:condition_1:get_user_attributes:action_result.parameter.principals"])
get_user_group_attributes_result_data = phantom.collect2(container=container, datapath=["get_user_group_attributes:action_result.data.*.entries.*.attributes"], action_results=results)
filtered_result_0_data___entries___attributes = [item[0] for item in filtered_result_0_data_filter_user_results]
filtered_result_0_parameter_principals = [item[1] for item in filtered_result_0_data_filter_user_results]
get_user_group_attributes_result_item_0 = [item[0] for item in get_user_group_attributes_result_data]
format_user_outputs__observable_array = None
################################################################################
## Custom Code Start
################################################################################
format_user_outputs__observable_array = []
# create dictionary of group attributes
group_dict = {}
for group in get_user_group_attributes_result_item_0:
group_dict[group['distinguishedname']] = group
# create normalized output dictionary
for pb_input, user in zip(filtered_result_0_parameter_principals, filtered_result_0_data___entries___attributes):
user_dict = {
"account_type": "LDAP Account",
"account_type_id": 1,
"account_uid": user['userPrincipalName'],
"domain": user['userPrincipalName'].split('@')[1],
"email_addr": user['mail'],
"end_date": user['accountExpires'],
"full_name": user['name'],
"labels": [
user['title'],
user['department']
],
"managed_by": user['manager'],
"name": user['sAMAccountName'],
"start_date": user['whenCreated'],
"type": "User",
"type_id": 1,
"uid": user['distinguishedName'],
"uuid": user['objectGUID']
}
phones = [
user['mobile'],
user['ipPhone'],
user['homePhone'],
user['telephoneNumber'],
user['otherMobile'],
user['otherIpPhone'],
user['otherHomePhone'],
user['otherTelephone']
]
# lookup groups
group_list = []
for group in user['memberOf']:
if group_dict.get(group):
group_details = group_dict[group]
if not group_details['description']:
group_details['description'].append("None")
temp_group_dict = {
"type": "Domain",
"desc": group_details['description'][0],
"name": group_details['name'],
"privileges": group_details['memberof'],
"uid": group_details['distinguishedname']
}
# drop values with []
for k,v in temp_group_dict.copy().items():
if v == []:
temp_group_dict.pop(k)
group_list.append(temp_group_dict)
if group_list:
user_dict['groups'] = group_list
# clean up phone numbers
phones = [phone for phone in phones if phone != []]
if phones:
user_dict['phones'] = phones
final_output = {
"type": "user",
"value": pb_input,
"attributes": user_dict,
"soure": "AD LDAP"
}
format_user_outputs__observable_array.append(final_output)
# phantom.debug(format_user_outputs__observable_array)
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="format_user_outputs:observable_array", value=json.dumps(format_user_outputs__observable_array))
return
@phantom.playbook_block()
def merge_user_groups(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("merge_user_groups() called")
################################################################################
# Merge user groups, from preceding action block results, into a single deduplicated
# list.
################################################################################
filtered_result_0_data_filter_user_results = phantom.collect2(container=container, datapath=["filtered-data:filter_user_results:condition_1:get_user_attributes:action_result.data.*.entries.*.attributes.memberOf"])
filtered_result_0_data___entries___attributes_memberof = [item[0] for item in filtered_result_0_data_filter_user_results]
merge_user_groups__list = None
################################################################################
## Custom Code Start
################################################################################
group_list = []
for group in filtered_result_0_data___entries___attributes_memberof:
group_list.extend(group)
merge_user_groups__list = list(set(group_list))
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="merge_user_groups:list", value=json.dumps(merge_user_groups__list))
get_user_group_attributes(container=container)
return
@phantom.playbook_block()
def get_user_group_attributes(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_user_group_attributes() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
filter_formatted_string = phantom.format(
container=container,
template="""(|\n%%\n(distinguishedName={0})\n%%\n)\n""",
parameters=[
"merge_user_groups:custom_function:list"
])
################################################################################
# Get details for the groups to which the user belongs.
################################################################################
merge_user_groups__list = json.loads(_ if (_ := phantom.get_run_data(key="merge_user_groups:list")) != "" else "null") # pylint: disable=used-before-assignment
parameters = []
if filter_formatted_string is not None:
parameters.append({
"filter": filter_formatted_string,
"attributes": "description;name;memberOf;distinguishedName",
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("run query", parameters=parameters, name="get_user_group_attributes", assets=["ad_ldap"], callback=format_user_outputs)
return
@phantom.playbook_block()
def get_device_attributes(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_device_attributes() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Query for the device attributes for filtered playbook inputs.
################################################################################
format_device_query__as_list = phantom.get_format_data(name="format_device_query__as_list")
parameters = []
# build parameters list for 'get_device_attributes' call
for format_device_query__item in format_device_query__as_list:
if format_device_query__item is not None:
parameters.append({
"filter": format_device_query__item,
"attributes": "description;distinguishedName;objectSid;memberOf;name;sAMAccountName",
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("run query", parameters=parameters, name="get_device_attributes", assets=["ad_ldap"], callback=filter_device_results)
return
@phantom.playbook_block()
def playbook_inputs_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("playbook_inputs_filter() called")
################################################################################
# Filter inputs to route inputs to appropriate actions.
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["playbook_input:user", "!=", None]
],
name="playbook_inputs_filter:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
get_user_attributes(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
# collect filtered artifact ids and results for 'if' condition 2
matched_artifacts_2, matched_results_2 = phantom.condition(
container=container,
conditions=[
["playbook_input:device", "!=", None]
],
name="playbook_inputs_filter:condition_2")
# call connected blocks if filtered artifacts or results
if matched_artifacts_2 or matched_results_2:
format_device_query(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_2, filtered_results=matched_results_2)
return
@phantom.playbook_block()
def merge_device_groups(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("merge_device_groups() called")
################################################################################
# Merge device groups, from preceding action block results, into a single deduplicated
# list.
################################################################################
filtered_result_0_data_filter_device_results = phantom.collect2(container=container, datapath=["filtered-data:filter_device_results:condition_1:get_device_attributes:action_result.data.*.entries.*.attributes.memberof"])
filtered_result_0_data___entries___attributes_memberof = [item[0] for item in filtered_result_0_data_filter_device_results]
merge_device_groups__list = None
################################################################################
## Custom Code Start
################################################################################
group_list = []
for group in filtered_result_0_data___entries___attributes_memberof:
group_list.extend(group)
merge_device_groups__list = list(set(group_list))
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="merge_device_groups:list", value=json.dumps(merge_device_groups__list))
get_device_group_attributes(container=container)
return
@phantom.playbook_block()
def get_device_group_attributes(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_device_group_attributes() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
filter_formatted_string = phantom.format(
container=container,
template="""(|\n%%\n(distinguishedName={0})\n%%\n)""",
parameters=[
"merge_device_groups:custom_function:list"
])
################################################################################
# Get details for the groups to which the device belongs.
################################################################################
merge_device_groups__list = json.loads(_ if (_ := phantom.get_run_data(key="merge_device_groups:list")) != "" else "null") # pylint: disable=used-before-assignment
parameters = []
if filter_formatted_string is not None:
parameters.append({
"filter": filter_formatted_string,
"attributes": "description;name;memberOf;distinguishedName",
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("run query", parameters=parameters, name="get_device_group_attributes", assets=["ad_ldap"], callback=format_device_outputs)
return
@phantom.playbook_block()
def format_device_outputs(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("format_device_outputs() called")
################################################################################
# Format a normalized output for each device.
################################################################################
filtered_result_0_data_filter_device_results = phantom.collect2(container=container, datapath=["filtered-data:filter_device_results:condition_1:get_device_attributes:action_result.data.*.entries.*.attributes","filtered-data:filter_device_results:condition_1:get_device_attributes:action_result.parameter.filter"])
get_device_group_attributes_result_data = phantom.collect2(container=container, datapath=["get_device_group_attributes:action_result.data.*.entries.*.attributes"], action_results=results)
filtered_result_0_data___entries___attributes = [item[0] for item in filtered_result_0_data_filter_device_results]
filtered_result_0_parameter_filter = [item[1] for item in filtered_result_0_data_filter_device_results]
get_device_group_attributes_result_item_0 = [item[0] for item in get_device_group_attributes_result_data]
format_device_outputs__observable_array = None
################################################################################
## Custom Code Start
################################################################################
import re
format_device_outputs__observable_array = []
def extract_domain_name(dn):
dn_rex = re.search(r'(DC=.+)', dn)
if dn_rex:
dc_list = dn_rex.group(1).replace('DC=', '').split(',')
return '.'.join(dc_list)
def extract_ou(dn):
dn_rex = re.search(r'(OU=.+?),DC', dn)
if dn_rex:
ou_list = dn_rex.group(1).replace('OU=', '').split(',')
return ou_list
def extract_pb_input_from_filter(filter_query_list):
pb_input_list = []
for query in filter_query_list:
query_rex = re.search(r'sAMAccountName=([^\)]+)', query)
hostname = query_rex.group(1).replace('*', '')
pb_input_list.append(hostname)
return pb_input_list
# create dictionary of group attributes
group_dict = {}
for group in get_device_group_attributes_result_item_0:
group_dict[group['distinguishedname']] = group
pb_input_hostname_list = extract_pb_input_from_filter(filtered_result_0_parameter_filter)
# create normalized output dictionary
for pb_input, device in zip(pb_input_hostname_list, filtered_result_0_data___entries___attributes):
device_dict = {
"desc": device['description'],
"domain": extract_domain_name(device['distinguishedname']),
"hostname": device['name'],
"name": device['samaccountname'],
"type": "Unknown",
"type_id": 0,
"uid": device['objectsid']
}
# use OUs as labels
ou_list = extract_ou(device['distinguishedname'])
if ou_list:
device_dict['labels'] = ou_list
# lookup groups
group_list = []
for group in device['memberof']:
if group_dict.get(group):
group_details = group_dict[group]
if not group_details['description']:
group_details['description'].append("None")
temp_group_dict = {
"type": "Domain",
"desc": group_details['description'][0],
"name": group_details['name'],
"privileges": group_details['memberof'],
"uid": group_details['distinguishedname']
}
# drop values with []
for k,v in temp_group_dict.copy().items():
if v == []:
temp_group_dict.pop(k)
group_list.append(temp_group_dict)
if group_list:
device_dict['groups'] = group_list
final_output = {
"type": "host name",
"value": pb_input,
"attributes": device_dict,
"source": "AD LDAP"
}
format_device_outputs__observable_array.append(final_output)
# phantom.debug(format_device_outputs__observable_array)
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="format_device_outputs:observable_array", value=json.dumps(format_device_outputs__observable_array))
return
@phantom.playbook_block()
def filter_user_results(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("filter_user_results() called")
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["get_user_attributes:action_result.summary.total_objects", ">", 0]
],
name="filter_user_results:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
merge_user_groups(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
@phantom.playbook_block()
def filter_device_results(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("filter_device_results() called")
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["get_device_attributes:action_result.summary.total_objects", ">", 0]
],
name="filter_device_results:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
merge_device_groups(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
@phantom.playbook_block()
def format_device_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("format_device_query() called")
################################################################################
# Iterate through the playbook inputs and format a list of queries.
################################################################################
template = """%%\n(&(objectCategory=computer)(|(sAMAccountName={0}*)(distinguishedName={0}*)(userPrincipalName={0}*)))\n%%"""
# parameter list for template variable replacement
parameters = [
"filtered-data:playbook_inputs_filter:condition_2:playbook_input:device"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_device_query")
get_device_attributes(container=container)
return
@phantom.playbook_block()
def on_finish(container, summary):
phantom.debug("on_finish() called")
format_user_outputs__observable_array = json.loads(_ if (_ := phantom.get_run_data(key="format_user_outputs:observable_array")) != "" else "null") # pylint: disable=used-before-assignment
format_device_outputs__observable_array = json.loads(_ if (_ := phantom.get_run_data(key="format_device_outputs:observable_array")) != "" else "null") # pylint: disable=used-before-assignment
observable_combined_value = phantom.concatenate(format_user_outputs__observable_array, format_device_outputs__observable_array)
output = {
"observable": observable_combined_value,
}
################################################################################
## Custom Code Start
################################################################################
# This function is called after all actions are completed.
# summary of all the action and/or all details of actions
# can be collected here.
# summary_json = phantom.get_summary()
# if 'result' in summary_json:
# for action_result in summary_json['result']:
# if 'action_run_id' in action_result:
# action_results = phantom.get_action_results(action_run_id=action_result['action_run_id'], result_data=False, flatten=False)
# phantom.debug(action_results)
################################################################################
## Custom Code End
################################################################################
phantom.save_playbook_output_data(output=output)
return