/
CheckPhish.py
264 lines (197 loc) · 6.89 KB
/
CheckPhish.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
import demistomock as demisto
from CommonServerPython import *
from CommonServerUserPython import *
''' IMPORTS '''
import json
import requests
import urllib3
# Disable insecure warnings
urllib3.disable_warnings()
''' GLOBALS/PARAMS '''
HEADERS = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
STATUS_SUFFIX = '/status'
CLEAN_STATUS = 'clean'
PENDING_STATUS = 'PENDING'
DONE_STATUS = 'DONE'
DEFAULT_GOOD_DISP = {
'clean'
}
DEFAULT_SUSP_DISP = {
'drug_spam',
'gambling',
'hacked_website',
'streaming',
'suspicious'
}
DEFAULT_BAD_DISP = {
'cryptojacking',
'phish',
'likely_phish',
'scam'
}
''' HELPER FUNCTIONS '''
def http_request(method, url, use_ssl, params=None, data=None):
res = requests.request(
method,
url,
verify=use_ssl,
params=params,
data=data,
headers=HEADERS
)
if res.status_code not in {200}:
return_error('Error in API call to CheckPhish [%d] - %s' % (res.status_code, res.reason))
try:
return res.json()
except ValueError as err:
return_error('Failed to parse response from service, received the following error:\n{}'.format(str(err)))
def unite_dispositions(good_disp, susp_disp, bad_disp):
for disp in good_disp:
DEFAULT_GOOD_DISP.add(disp)
for disp in susp_disp:
DEFAULT_SUSP_DISP.add(disp)
for disp in bad_disp:
DEFAULT_BAD_DISP.add(disp)
def get_dbot_score(disposition):
if disposition in DEFAULT_BAD_DISP:
return 3
if disposition in DEFAULT_SUSP_DISP:
return 2
if disposition in DEFAULT_GOOD_DISP:
return 1
return 0
''' COMMANDS + REQUESTS FUNCTIONS '''
def test_module(**kwargs):
query = {
'apiKey': kwargs.get('api_key'),
'urlInfo': {'url': 'https://www.google.com'}
}
res = http_request('POST', kwargs.get('base_url'), kwargs.get('use_ssl'), data=json.dumps(query))
if res and 'errorMessage' not in res:
return 'ok'
return res['errorMessage'] # the errorMessage field contains the error message
def submit_to_checkphish(url, api_key, base_url, use_ssl):
""" Submit a URL for analysis in CheckPhish
Args:
url(str): URL to be sent to CheckPhish for analysis
api_key: api key for the authorization
base_url: url for the API request
use_ssl: for the http request
Returns:
(str). jobID retrieved from CheckPhish for the URL
"""
if 'http' not in url:
url = 'http://' + url
if re.match(urlRegex, url):
query = {
'apiKey': api_key,
'urlInfo': {'url': url},
'scanType': 'full'
}
res = http_request('POST', base_url, use_ssl, data=json.dumps(query))
if res.get('errorMessage'):
raise ValueError(res.get('errorMessage'))
return res['jobID']
else:
return_error(url + ' is not a valid url')
def is_job_ready_checkphish(jobID, api_key, base_url, use_ssl):
query = {
'apiKey': api_key,
'jobID': jobID
}
res = http_request('POST', base_url + STATUS_SUFFIX, use_ssl, data=json.dumps(query))
if res and res['status'] == DONE_STATUS:
return True
return False
def get_result_checkphish(jobID, api_key, base_url, use_ssl, reliability):
query = {
'apiKey': api_key,
'jobID': jobID
}
res = http_request('POST', base_url + STATUS_SUFFIX, use_ssl, data=json.dumps(query))
if res and 'errorMessage' not in res:
result = {
'url': res['url'],
'jobID': jobID,
'status': res['status'],
'disposition': res['disposition'],
'brand': res['brand']
}
url_dict = {
'Data': result['url']
}
if result['disposition'] != CLEAN_STATUS:
url_dict['Malicious'] = {
'Vendor': 'CheckPhish',
'Description': 'Targets ' + result['brand']
}
dbot_score = {
'Type': 'url',
'Vendor': 'CheckPhish',
'Indicator': result['url'],
'Score': get_dbot_score(result['disposition']),
'Reliability': reliability
}
context = {
'CheckPhish.' + outputPaths['url']: result,
outputPaths['url']: url_dict,
'DBotScore': dbot_score
}
human_readable = tableToMarkdown('CheckPhish reputation for ' + result['url'],
result,
['url', 'disposition', 'brand', 'status', 'jobID'])
return_outputs(human_readable, context, res)
else:
return_error('Error getting job status')
def checkphish_check_urls(**kwargs):
urls = argToList(demisto.args().get('url'))
job_ids = []
for url in urls:
submit = submit_to_checkphish(url, kwargs.get('api_key'), kwargs.get('base_url'), kwargs.get('use_ssl'))
if submit:
job_ids.append(submit)
while len(job_ids):
for job_id in job_ids[:]:
if is_job_ready_checkphish(job_id, kwargs.get('api_key'), kwargs.get('base_url'), kwargs.get('use_ssl')):
get_result_checkphish(job_id, kwargs.get('api_key'), kwargs.get('base_url'), kwargs.get('use_ssl'),
kwargs.get('reliability'))
job_ids.remove(job_id)
''' COMMANDS MANAGER / SWITCH PANEL '''
LOG('Command being called is %s' % (demisto.command()))
handle_proxy()
def main():
demisto_params = demisto.params()
good_disp = argToList(demisto_params.get('good_disp'))
susp_disp = argToList(demisto_params.get('susp_disp'))
bad_disp = argToList(demisto_params.get('bad_disp'))
api_key = demisto_params.get('credentials_api_token', {}).get('password') or demisto_params.get('token')
if not api_key:
raise DemistoException('API token must be provided.')
unite_dispositions(good_disp, susp_disp, bad_disp)
reliability = demisto_params.get('integrationReliability')
reliability = reliability if reliability else DBotScoreReliability.B
if DBotScoreReliability.is_valid_type(reliability):
reliability = DBotScoreReliability.get_dbot_score_reliability_from_str(reliability)
else:
raise Exception("Please provide a valid value for the Source Reliability parameter.")
params = {
'base_url': demisto_params['url'],
'api_key': api_key,
'use_ssl': not demisto_params.get('insecure', False),
'reliability': reliability
}
try:
if demisto.command() == 'test-module':
demisto.results(test_module(**params))
elif demisto.command() in ['CheckPhish-check-urls', 'url']:
checkphish_check_urls(**params)
# Log exceptions
except Exception as e:
LOG(str(e))
LOG.print_log()
raise
if __name__ in ['__main__', '__builtin__', 'builtins']:
main()