-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab demo.py
218 lines (148 loc) · 9.36 KB
/
lab demo.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
"""
"""
import phantom.rules as phantom
import json
from datetime import datetime, timedelta
def on_start(container):
phantom.debug('on_start() called')
# call 'geolocate_ip_1' block
geolocate_ip_1(container=container)
return
def geolocate_ip_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("geolocate_ip_1() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
container_artifact_data = phantom.collect2(container=container, datapath=["artifact:*.cef.sourceAddress","artifact:*.id"])
parameters = []
# build parameters list for 'geolocate_ip_1' call
for container_artifact_item in container_artifact_data:
if container_artifact_item[0] is not None:
parameters.append({
"ip": container_artifact_item[0],
"context": {'artifact_id': container_artifact_item[1]},
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
phantom.debug(parameters)
################################################################################
## Custom Code End
################################################################################
phantom.act("geolocate ip", parameters=parameters, name="geolocate_ip_1", assets=["maxmind"], callback=geolocate_ip_1_callback)
return
def geolocate_ip_1_callback(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("geolocate_ip_1_callback() called")
debug_1(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=filtered_artifacts, filtered_results=filtered_results)
filter_3(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=filtered_artifacts, filtered_results=filtered_results)
return
def debug_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("debug_1() called")
name_value = container.get("name", None)
description_value = container.get("description", None)
container_artifact_data = phantom.collect2(container=container, datapath=["artifact:*.cef.sourceAddress","artifact:*.id"])
geolocate_ip_1_result_data = phantom.collect2(container=container, datapath=["geolocate_ip_1:action_result.data.*.country_name","geolocate_ip_1:action_result.parameter.ip","geolocate_ip_1:action_result.parameter.context.artifact_id"], action_results=results)
container_artifact_cef_item_0 = [item[0] for item in container_artifact_data]
geolocate_ip_1_result_item_0 = [item[0] for item in geolocate_ip_1_result_data]
geolocate_ip_1_parameter_ip = [item[1] for item in geolocate_ip_1_result_data]
parameters = []
parameters.append({
"input_1": container_artifact_cef_item_0,
"input_2": name_value,
"input_3": description_value,
"input_4": geolocate_ip_1_result_item_0,
"input_5": geolocate_ip_1_parameter_ip,
"input_6": None,
"input_7": None,
"input_8": None,
"input_9": None,
"input_10": None,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/debug", parameters=parameters, name="debug_1")
return
def filter_3(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("filter_3() called")
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["geolocate_ip_1:action_result.data.*.country_name", "!=", ""]
],
name="filter_3:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
countries(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
def countries(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("countries() called")
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["filtered-data:filter_3:condition_1:geolocate_ip_1:action_result.data.*.country_name", "in", "custom_list:countries"]
],
name="countries:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
pin_3(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=[
["filtered-data:filter_3:condition_1:geolocate_ip_1:action_result.data.*.country_name", "not in", "custom_list:countries"]
],
name="countries:condition_2")
# call connected blocks if filtered artifacts or results
if matched_artifacts_2 or matched_results_2:
pin_4(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_2, filtered_results=matched_results_2)
return
def pin_3(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("pin_3() called")
filtered_result_0_data_countries = phantom.collect2(container=container, datapath=["filtered-data:countries:condition_1:geolocate_ip_1:action_result.data.*.country_name"])
filtered_result_0_data___country_name = [item[0] for item in filtered_result_0_data_countries]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.pin(container=container, data=filtered_result_0_data___country_name, message="IPs in list ", pin_style="blue", pin_type="card")
return
def pin_4(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("pin_4() called")
filtered_result_0_data_countries = phantom.collect2(container=container, datapath=["filtered-data:countries:condition_2:geolocate_ip_1:action_result.data.*.country_name"])
filtered_result_0_data___country_name = [item[0] for item in filtered_result_0_data_countries]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.pin(container=container, data=filtered_result_0_data___country_name, message="Not in List", pin_style="red", pin_type="card")
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