This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reversinglabs_connector.py
284 lines (205 loc) · 9.46 KB
/
reversinglabs_connector.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
# --
#
# Copyright (c) ReversingLabs Inc 2016-2019
#
# This unpublished material is proprietary to ReversingLabs Inc.
# All rights reserved.
# Reproduction or distribution, in whole
# or in part, is forbidden except by express written permission
# of ReversingLabs Inc.
#
# --
# Phantom imports
import phantom.app as phantom
from phantom.app import BaseConnector
from phantom.app import ActionResult
# THIS Connector imports
from reversinglabs_consts import *
# Other imports used by this connector
import simplejson as json
import hashlib
import requests
from requests.auth import HTTPBasicAuth
from collections import defaultdict
class ReversinglabsConnector(BaseConnector):
# The actions supported by this connector
ACTION_ID_QUERY_FILE = "lookup_file"
def __init__(self):
# Call the BaseConnectors init first
super(ReversinglabsConnector, self).__init__()
self._malicious_status = ["MALICIOUS", "SUSPICIOUS"]
self._headers = {'content-type': 'application/octet-stream'}
self._auth = None
self._mwp_url = MAL_PRESENCE_API_URL
self._xref_url = XREF_API_URL
self._verify_cert = True
def initialize(self):
config = self.get_config()
# setup the auth
self._auth = HTTPBasicAuth(phantom.get_req_value(config, phantom.APP_JSON_USERNAME),
phantom.get_req_value(config, phantom.APP_JSON_PASSWORD))
if "url" in config:
self._mwp_url = "{0}{1}".format(config["url"], MAL_PRESENCE_API_URL_ENDPOINT)
self._xref_url = "{0}{1}".format(config["url"], XREF_API_URL_ENDPOINT)
if "verify_server_cert" in config:
self._verify_cert = config["verify_server_cert"]
self.debug_print('self.status', self.get_status())
return phantom.APP_SUCCESS
def _test_asset_connectivity(self, param):
# Create a hash of a random string
random_string = phantom.get_random_chars(size=10)
md5_hash = hashlib.md5(random_string).hexdigest()
self.save_progress(REVERSINGLABS_GENERATED_RANDOM_HASH)
tree = lambda: defaultdict(tree) # noqa: E731, E261
hash_type = 'md5'
query = tree()
query['rl']['query']['hash_type'] = hash_type
query['rl']['query']['hashes'] = [md5_hash]
try:
r = requests.post(self._mwp_url, auth=self._auth, data=json.dumps(query), headers=self._headers, verify=self._verify_cert)
except Exception as e:
self.set_status(phantom.APP_ERROR, 'Request to server failed', e)
self.save_progress(REVERSINGLABS_SUCC_CONNECTIVITY_TEST)
return self.get_status()
if (r.status_code != 200):
self.set_status(phantom.APP_ERROR)
status_message = '{0}. {1}. HTTP status_code: {2}, reason: {3}'.format(REVERSINGLABS_ERR_CONNECTIVITY_TEST,
REVERSINGLABS_MSG_CHECK_CREDENTIALS, r.status_code, r.reason)
self.append_to_message(status_message)
self.append_to_message(self._mwp_url)
return self.get_status()
return self.set_status_save_progress(phantom.APP_SUCCESS, REVERSINGLABS_SUCC_CONNECTIVITY_TEST)
def _handle_samples(self, action_result, samples):
if (not samples):
return
for sample in samples:
if (not sample):
continue
try:
# Get the data dictionary into the result to store information
hash_data = action_result.get_data()[0]
except:
continue
# Update the data with what we got
hash_data.update(sample)
print "_handle_samples: " + str(sample) + "\n ----------------------------------------------"
try:
positives = sample['xref'][0]['scanner_match']
# Update the summary
action_result.update_summary({REVERSINGLABS_JSON_TOTAL_SCANS: sample['xref'][0]['scanner_count'],
REVERSINGLABS_JSON_POSITIVES: positives})
except:
continue
return
def _get_hash_type(self, hash_to_query):
if (phantom.is_md5(hash_to_query)):
return 'md5'
if (phantom.is_sha1(hash_to_query)):
return 'sha1'
if (phantom.is_sha256(hash_to_query)):
return 'sha256'
return None
def _query_file(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
# get the hash
hash_to_query = param[phantom.APP_JSON_HASH]
# get the hash type
hash_type = self._get_hash_type(hash_to_query)
if (not hash_type):
return action_result.set_status(phantom.APP_ERROR, "Unable to detect Hash Type")
tree = lambda: defaultdict(tree) # noqa: E731, E261
query = tree()
query['rl']['query']['hash_type'] = hash_type
query['rl']['query']['hashes'] = [hash_to_query]
# First the malware presence
self.save_progress(REVERSINGLABS_MSG_CONNECTING_WITH_URL)
try:
r = requests.post(self._mwp_url, auth=self._auth, data=json.dumps(query), headers=self._headers, verify=self._verify_cert)
except Exception as e:
return action_result.set_status(phantom.APP_ERROR, "Request to server failed", e)
if (r.status_code != 200):
return action_result.set_status(phantom.APP_ERROR, REVERSINGLABS_ERR_MALWARE_PRESENCE_QUERY_FAILED, ret_code=r.status_code, ret_reason=r.reason)
try:
rl_result = r.json()
except Exception as e:
return action_result.set_status(phantom.APP_ERROR, "Response does not seem to be a valid JSON", e)
# set the status to success
action_result.set_status(phantom.APP_SUCCESS)
entries = rl_result.get('rl', {}).get('entries')
if (not entries):
return action_result.set_status(phantom.APP_ERROR, "Response does contains empty or None 'entries'")
# Queried for a hash, so it should be present in the return value
entry = entries[0]
# Add a data dictionary into the result to store information
hash_data = action_result.add_data({'mwp_result': entry})
# Add the status into it
hash_data[REVERSINGLABS_JSON_STATUS] = entry.get('status', 'Unknown')
# Set the summary
action_result.update_summary({REVERSINGLABS_JSON_TOTAL_SCANS: 0, REVERSINGLABS_JSON_POSITIVES: 0})
if (hash_data[REVERSINGLABS_JSON_STATUS] not in self._malicious_status):
# No need to do anything more for this hash
return action_result.set_status(phantom.APP_SUCCESS)
self.save_progress(REVERSINGLABS_MSG_CONNECTING_WITH_URL)
try:
r = requests.post(self._xref_url, auth=self._auth, data=json.dumps(query), headers=self._headers, verify=self._verify_cert)
except Exception as e:
return action_result.set_status(phantom.APP_ERROR, "XREF API Request to server failed", e)
if (r.status_code != 200):
self.debug_print("status code", r.status_code)
return action_result.set_status(phantom.APP_ERROR, "XREF API Request to server error: {0}".format(r.status_code))
try:
rl_result = r.json()
except Exception as e:
return action_result.set_status(phantom.APP_ERROR, "XREF Response does not seem to be a valid JSON", e)
action_result.add_debug_data(rl_result)
action_result.add_debug_data({'mwp_result': entry})
samples = rl_result.get('rl', {}).get('samples')
samples.append(entry)
if (not samples):
return action_result.set_status(phantom.APP_ERROR, "Response contains empty or none 'samples'")
self._handle_samples(action_result, samples)
return phantom.APP_SUCCESS
def handle_action(self, param):
"""Function that handles all the actions
Args:
Return:
A status code
"""
result = None
action = self.get_action_identifier()
if (action == self.ACTION_ID_QUERY_FILE):
result = self._query_file(param)
elif (action == phantom.ACTION_ID_TEST_ASSET_CONNECTIVITY):
result = self._test_asset_connectivity(param)
return result
def finalize(self):
# Init the positives
total_positives = 0
# Loop through the action results that we had added before
for action_result in self.get_action_results():
action = self.get_action_identifier()
if (action == self.ACTION_ID_QUERY_FILE):
# get the summary of the current one
summary = action_result.get_summary()
if (REVERSINGLABS_JSON_POSITIVES not in summary):
continue
# If the detection is true
if (summary[REVERSINGLABS_JSON_POSITIVES] > 0):
total_positives += 1
self.update_summary({REVERSINGLABS_JSON_TOTAL_POSITIVES: total_positives})
if __name__ == '__main__':
import sys
# import pudb
# pudb.set_trace()
if (len(sys.argv) < 2):
print "No test json specified as input"
exit(0)
with open(sys.argv[1]) as f:
in_json = f.read()
in_json = json.loads(in_json)
print(json.dumps(in_json, indent=' ' * 4))
connector = ReversinglabsConnector()
connector.print_progress_message = True
ret_val = connector._handle_action(json.dumps(in_json), None)
print ret_val
exit(0)