forked from phantomcyber/playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhishTank_URL_Reputation_Analysis.py
320 lines (232 loc) · 13.7 KB
/
PhishTank_URL_Reputation_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
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
"""
Accepts a URL and does reputation analysis on the objects. Generates a global report and a per observable sub-report and normalized score. The score can be customized as desired.\n\nRef: https://d3fend.mitre.org/technique/d3f:IdentifierReputationAnalysis/
"""
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 'url_reputation' block
url_reputation(container=container)
return
@phantom.playbook_block()
def url_reputation(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("url_reputation() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Queries PhishTank for information about the provided URL(s)
################################################################################
playbook_input_url = phantom.collect2(container=container, datapath=["playbook_input:url"])
parameters = []
# build parameters list for 'url_reputation' call
for playbook_input_url_item in playbook_input_url:
if playbook_input_url_item[0] is not None:
parameters.append({
"url": playbook_input_url_item[0],
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("url reputation", parameters=parameters, name="url_reputation", assets=["phishtank"], callback=url_result_filter)
return
@phantom.playbook_block()
def normalize_score_url(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("normalize_score_url() called")
################################################################################
# Contains custom code for normalizing score. Adjust the logic as desired in the
# documented sections.
################################################################################
filtered_result_0_data_url_result_filter = phantom.collect2(container=container, datapath=["filtered-data:url_result_filter:condition_1:url_reputation:action_result.summary.In_Database","filtered-data:url_result_filter:condition_1:url_reputation:action_result.summary.Valid","filtered-data:url_result_filter:condition_1:url_reputation:action_result.summary.Verified"])
filtered_result_0_summary_in_database = [item[0] for item in filtered_result_0_data_url_result_filter]
filtered_result_0_summary_valid = [item[1] for item in filtered_result_0_data_url_result_filter]
filtered_result_0_summary_verified = [item[2] for item in filtered_result_0_data_url_result_filter]
normalize_score_url__url_score_object = None
normalize_score_url__scores = None
################################################################################
## Custom Code Start
################################################################################
# Assign variables
normalize_score_url__url_score_object = []
normalize_score_url__scores = []
in_database_list = filtered_result_0_summary_in_database
valid_list = filtered_result_0_summary_valid
verified_list = filtered_result_0_summary_verified
# Reference for scores: https://schema.ocsf.io/objects/reputation
score_table = {
"-1": "Other",
"0":"Unknown",
"1":"Very_Safe",
"2":"Safe",
"3":"Probably_Safe",
"4":"Leans_Safe",
"5":"May_not_be_Safe",
"6":"Exercise_Caution",
"7":"Suspicious_or_Risky",
"8":"Possibly_Malicious",
"9":"Probably_Malicious",
"10":"Malicious"
}
# PhishTank URL Data
# Adjust logic as desired
for in_database, valid, verified in zip(in_database_list, valid_list, verified_list):
# Condition 1 - In database but verified not valid phish
if in_database and verified and not valid:
score_id = "2"
# Condition 2 - In database and valid phish
elif in_database and valid:
score_id = "10"
# Condition 3 - In database but not verified
elif in_database and not verified:
score_id = "5"
# Condition 4 - Not in database
elif not in_database:
score_id = "0"
# Condition 5 - Catch all
else:
score_id = "-1"
score = score_table[score_id]
normalize_score_url__url_score_object.append(
{
'score': score,
'score_id': score_id
}
)
normalize_score_url__scores.append(score)
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="normalize_score_url:url_score_object", value=json.dumps(normalize_score_url__url_score_object))
phantom.save_run_data(key="normalize_score_url:scores", value=json.dumps(normalize_score_url__scores))
format_report_url(container=container)
return
@phantom.playbook_block()
def url_result_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("url_result_filter() called")
################################################################################
# Filters successful url reputation results.
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["url_reputation:action_result.status", "==", "success"]
],
name="url_result_filter:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
normalize_score_url(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_report_url(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("format_report_url() called")
################################################################################
# Format a summary table with the information gathered from the playbook.
################################################################################
template = """SOAR analyzed URL(s) using PhishTank. The table below shows a summary of the information gathered.\n\n| URL | Normalized Score | Report Link | Source |\n| --- | --- | --- | --- |\n%%\n| `{0}` | {1} | {2} | PhishTank |\n%%"""
# parameter list for template variable replacement
parameters = [
"filtered-data:url_result_filter:condition_1:url_reputation:action_result.parameter.url",
"normalize_score_url:custom_function:scores",
"filtered-data:url_result_filter:condition_1:url_reputation:action_result.data.*.phish_detail_page"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_report_url")
parse_url(container=container)
return
@phantom.playbook_block()
def build_url_output(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("build_url_output() called")
################################################################################
# Generate an observable dictionary to output into the observables data path.
################################################################################
filtered_result_0_data_url_result_filter = phantom.collect2(container=container, datapath=["filtered-data:url_result_filter:condition_1:url_reputation:action_result.parameter.url","filtered-data:url_result_filter:condition_1:url_reputation:action_result.data.*.phish_detail_page"])
parse_url__result = phantom.collect2(container=container, datapath=["parse_url:custom_function_result.data"])
normalize_score_url__url_score_object = json.loads(_ if (_ := phantom.get_run_data(key="normalize_score_url:url_score_object")) != "" else "null") # pylint: disable=used-before-assignment
filtered_result_0_parameter_url = [item[0] for item in filtered_result_0_data_url_result_filter]
filtered_result_0_data___phish_detail_page = [item[1] for item in filtered_result_0_data_url_result_filter]
parse_url_data = [item[0] for item in parse_url__result]
build_url_output__observable_array = None
################################################################################
## Custom Code Start
################################################################################
# Assign variables
build_url_output__observable_array = []
url_list = filtered_result_0_parameter_url
parsed_url_list = parse_url_data
detail_page_list = filtered_result_0_data___phish_detail_page
url_reputation_list = normalize_score_url__url_score_object
for url, detail_page, parsed_url, url_rep in zip(url_list, detail_page_list, parsed_url_list, url_reputation_list):
observable_object = {
"value": url,
"type": "url",
"reputation": {
"score_id": url_rep['score_id'],
"score": url_rep['score'],
},
"attributes": {
"hostname": parsed_url['hostname'],
"scheme": parsed_url['scheme']
},
"source": "PhishTank",
"source_link": detail_page
}
if parsed_url['path']:
observable_object['attributes']['path'] = parsed_url['path']
if parsed_url['query']:
observable_object['attributes']['query'] = parsed_url['query']
if parsed_url['port']:
observable_object['attributes']['query'] = parsed_url['query']
build_url_output__observable_array.append(observable_object)
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="build_url_output:observable_array", value=json.dumps(build_url_output__observable_array))
return
@phantom.playbook_block()
def parse_url(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("parse_url() called")
filtered_result_0_data_url_result_filter = phantom.collect2(container=container, datapath=["filtered-data:url_result_filter:condition_1:url_reputation:action_result.parameter.url"])
parameters = []
# build parameters list for 'parse_url' call
for filtered_result_0_item_url_result_filter in filtered_result_0_data_url_result_filter:
parameters.append({
"input_url": filtered_result_0_item_url_result_filter[0],
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/url_parse", parameters=parameters, name="parse_url", callback=build_url_output)
return
@phantom.playbook_block()
def on_finish(container, summary):
phantom.debug("on_finish() called")
format_report_url = phantom.get_format_data(name="format_report_url")
build_url_output__observable_array = json.loads(_ if (_ := phantom.get_run_data(key="build_url_output:observable_array")) != "" else "null") # pylint: disable=used-before-assignment
output = {
"observable": build_url_output__observable_array,
"markdown_report": format_report_url,
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.save_playbook_output_data(output=output)
return