forked from phantomcyber/playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_find_inactive_users.py
235 lines (162 loc) · 10.5 KB
/
aws_find_inactive_users.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
"""
Find AWS accounts that have not been used for a long time (90 days by default). For each unused account, gather additional group and policy information and create an artifact to enable further automation or manual action.
"""
import phantom.rules as phantom
import json
from datetime import datetime, timedelta
def on_start(container):
phantom.debug('on_start() called')
# call 'list_all_accounts' block
list_all_accounts(container=container)
# call 'calculate_start_time' block
calculate_start_time(container=container)
return
def list_all_accounts(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("list_all_accounts() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# List all AWS IAM accounts, which will include the PasswordLastUsed field for
# us to filter on.
################################################################################
parameters = []
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("list users", parameters=parameters, name="list_all_accounts", assets=["aws_iam"], callback=join_filter_inactive_accounts)
return
def calculate_start_time(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("calculate_start_time() called")
parameters = []
parameters.append({
"input_datetime": None,
"input_format_string": None,
"modification_unit": "days",
"amount_to_modify": -90,
"output_format_string": "%Y-%m-%dT%H:%M:%SZ",
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/datetime_modify", parameters=parameters, name="calculate_start_time", callback=join_filter_inactive_accounts)
return
def join_filter_inactive_accounts(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("join_filter_inactive_accounts() called")
if phantom.completed(action_names=["list_all_accounts"], custom_function_names=["calculate_start_time"]):
# call connected block "filter_inactive_accounts"
filter_inactive_accounts(container=container, handle=handle)
return
def filter_inactive_accounts(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("filter_inactive_accounts() called")
################################################################################
# Compare the PasswordLastUsed field to the calculated start time to find unused
# accounts. Ignore accounts with no value for PasswordLastUsed. This will ignore
# all accounts with no passwords, such as accounts that only use API access keys.
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
logical_operator="and",
conditions=[
["list_all_accounts:action_result.data.*.PasswordLastUsed", "<", "calculate_start_time:custom_function_result.data.datetime_string"],
["list_all_accounts:action_result.data.*.PasswordLastUsed", "!=", ""]
],
name="filter_inactive_accounts:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
get_unused_account_information(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
def get_unused_account_information(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_unused_account_information() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Use the "get user" action to gather more information about the unused accounts,
# including group membership and policy assignments.
################################################################################
filtered_result_0_data_filter_inactive_accounts = phantom.collect2(container=container, datapath=["filtered-data:filter_inactive_accounts:condition_1:list_all_accounts:action_result.data.*.UserName"])
parameters = []
# build parameters list for 'get_unused_account_information' call
for filtered_result_0_item_filter_inactive_accounts in filtered_result_0_data_filter_inactive_accounts:
if filtered_result_0_item_filter_inactive_accounts[0] is not None:
parameters.append({
"username": filtered_result_0_item_filter_inactive_accounts[0],
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("get user", parameters=parameters, name="get_unused_account_information", assets=["aws_iam"], callback=save_to_artifacts)
return
def save_to_artifacts(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("save_to_artifacts() called")
id_value = container.get("id", None)
get_unused_account_information_result_data = phantom.collect2(container=container, datapath=["get_unused_account_information:action_result.parameter.username","get_unused_account_information:action_result.parameter.context.artifact_id"], action_results=results)
parameters = []
# build parameters list for 'save_to_artifacts' call
for get_unused_account_information_result_item in get_unused_account_information_result_data:
parameters.append({
"container": id_value,
"name": "Unused AWS Account",
"label": "user",
"severity": None,
"cef_field": "awsUserName",
"cef_value": get_unused_account_information_result_item[0],
"cef_data_type": "aws iam user name",
"tags": None,
"run_automation": "false",
"input_json": None,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/artifact_create", parameters=parameters, name="save_to_artifacts", callback=playbook_aws_disable_user_accounts_1)
return
def playbook_aws_disable_user_accounts_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("playbook_aws_disable_user_accounts_1() called")
get_unused_account_information_result_data = phantom.collect2(container=container, datapath=["get_unused_account_information:action_result.parameter.username"], action_results=results)
get_unused_account_information_parameter_username = [item[0] for item in get_unused_account_information_result_data]
inputs = {
"aws_username": get_unused_account_information_parameter_username,
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
# call playbook "community/aws_disable_user_accounts", returns the playbook_run_id
playbook_run_id = phantom.playbook("community/aws_disable_user_accounts", container=container, inputs=inputs)
return
def on_finish(container, summary):
phantom.debug("on_finish() called")
################################################################################
## 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
################################################################################
return