forked from phantomcyber/playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
G_Suite_for_GMail_Message_Identifier_Activity_Analysis.py
258 lines (184 loc) · 11.8 KB
/
G_Suite_for_GMail_Message_Identifier_Activity_Analysis.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
"""
Accepts an internet message id, and asks Gmail for a list of mailboxes to search, and then searches each one to look for records that have a matching internet message id. It then produces a normalized output and summary table.\n\nThis may not work in the intended fashion if your organization has more than 500 mailboxes.\n\nRef: D3-IAA: https://d3fend.mitre.org/technique/d3f:IdentifierActivityAnalysis/
"""
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 'artifact_filter' block
artifact_filter(container=container)
return
@phantom.playbook_block()
def artifact_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("artifact_filter() called")
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["playbook_input:message_id", "!=", None]
],
name="artifact_filter:condition_1",
delimiter=",")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
get_mailboxes(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 get_mailboxes(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_mailboxes() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Required step in order to search "All" of a Gsuite organization. This receives
# a list of mailboxes that are passed to the next action.
################################################################################
parameters = []
parameters.append({
"max_items": 500,
"page_token": "",
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("list users", parameters=parameters, name="get_mailboxes", assets=["g_suite_for_gmail"], callback=search_mailboxes)
return
@phantom.playbook_block()
def search_mailboxes(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("search_mailboxes() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
get_mailboxes_result_data = phantom.collect2(container=container, datapath=["get_mailboxes:action_result.data.*.emails.*.address","get_mailboxes:action_result.parameter.context.artifact_id"], action_results=results)
filtered_input_0_message_id = phantom.collect2(container=container, datapath=["filtered-data:artifact_filter:condition_1:playbook_input:message_id"])
parameters = []
# build parameters list for 'search_mailboxes' call
for get_mailboxes_result_item in get_mailboxes_result_data:
for filtered_input_0_message_id_item in filtered_input_0_message_id:
if get_mailboxes_result_item[0] is not None:
parameters.append({
"email": get_mailboxes_result_item[0],
"label": "Inbox",
"query": "",
"max_results": 100,
"internet_message_id": filtered_input_0_message_id_item[0],
"context": {'artifact_id': get_mailboxes_result_item[1]},
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("run query", parameters=parameters, name="search_mailboxes", assets=["g_suite_for_gmail"], callback=results_filter)
return
@phantom.playbook_block()
def format_message_report(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("format_message_report() called")
template = """SOAR searched for occurrences of `{0}` within your environment using GSuite for GMail. The table below shows a summary of the information gathered.\n\n| Recipient | Addressed To | Subject | Sender |\n| --- | --- | --- | --- |\n%%\n| {1} | {2} | {3} | {4} |\n%%\n"""
# parameter list for template variable replacement
parameters = [
"filtered-data:results_filter:condition_1:search_mailboxes:action_result.parameter.internet_message_id",
"filtered-data:results_filter:condition_1:search_mailboxes:action_result.data.*.delivered_to",
"filtered-data:results_filter:condition_1:search_mailboxes:action_result.data.*.to",
"filtered-data:results_filter:condition_1:search_mailboxes:action_result.data.*.subject",
"filtered-data:results_filter:condition_1:search_mailboxes:action_result.data.*.from"
]
################################################################################
## Custom Code Start
################################################################################
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_message_report")
build_message_output(container=container)
return
@phantom.playbook_block()
def build_message_output(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("build_message_output() called")
################################################################################
# Logic regarding observable construction goes here
################################################################################
filtered_result_0_data_results_filter = phantom.collect2(container=container, datapath=["filtered-data:results_filter:condition_1:search_mailboxes:action_result.parameter.internet_message_id","filtered-data:results_filter:condition_1:search_mailboxes:action_result.data.*.delivered_to","filtered-data:results_filter:condition_1:search_mailboxes:action_result.data.*.to","filtered-data:results_filter:condition_1:search_mailboxes:action_result.data.*.subject","filtered-data:results_filter:condition_1:search_mailboxes:action_result.data.*.from"])
filtered_result_0_parameter_internet_message_id = [item[0] for item in filtered_result_0_data_results_filter]
filtered_result_0_data___delivered_to = [item[1] for item in filtered_result_0_data_results_filter]
filtered_result_0_data___to = [item[2] for item in filtered_result_0_data_results_filter]
filtered_result_0_data___subject = [item[3] for item in filtered_result_0_data_results_filter]
filtered_result_0_data___from = [item[4] for item in filtered_result_0_data_results_filter]
build_message_output__observable_array = None
################################################################################
## Custom Code Start
################################################################################
# Variable renaming for convenince
messageID = filtered_result_0_parameter_internet_message_id
recipients = filtered_result_0_data___delivered_to
addressees = filtered_result_0_data___to
subjects = filtered_result_0_data___subject
senders = filtered_result_0_data___from
build_message_output__observable_array = []
recordList = []
for message_id in messageID:
# construct iterables for records
for recipient, addressee, subject, sender in zip(recipients, addressees, subjects, senders):
record = {
"recipient": recipient,
"addressee": addressee,
"subject": subject,
"sender": sender
}
recordList.append(record)
# Create observable body
observable = {
"value": message_id,
"type": "internet message ID",
"count": len(recordList),
"source": "GSuite for GMail",
"message_identifier_activity": recordList
}
build_message_output__observable_array.append(observable)
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="build_message_output:observable_array", value=json.dumps(build_message_output__observable_array))
return
@phantom.playbook_block()
def results_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("results_filter() called")
################################################################################
# Filter results from mailbox search
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
logical_operator="and",
conditions=[
["search_mailboxes:action_result.status", "==", "success"],
["search_mailboxes:action_result.summary.total_messages_returned", ">", 0]
],
name="results_filter:condition_1",
delimiter=",")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
format_message_report(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 on_finish(container, summary):
phantom.debug("on_finish() called")
format_message_report = phantom.get_format_data(name="format_message_report")
build_message_output__observable_array = json.loads(_ if (_ := phantom.get_run_data(key="build_message_output:observable_array")) != "" else "null") # pylint: disable=used-before-assignment
output = {
"observable": build_message_output__observable_array,
"markdown_report": format_message_report,
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.save_playbook_output_data(output=output)
return