-
Notifications
You must be signed in to change notification settings - Fork 2
/
censys_connector.py
393 lines (292 loc) · 12.2 KB
/
censys_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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File: censys_connector.py
#
# Copyright (c) 2016-2024 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific language governing permissions
# and limitations under the License.
#
#
import json
import phantom.app as phantom
import requests
from phantom.action_result import ActionResult
from phantom.base_connector import BaseConnector
from censys_consts import *
from censys_rest import make_rest_call
from censys_search import CensysSearch
from censys_validation import is_ip
class CensysConnector(BaseConnector):
def __init__(self):
self._headers = {}
super().__init__()
def _test_connectivity(self, param):
"""Test connectivity by retrieving a valid token"""
action_result = self.add_action_result(ActionResult(dict(param)))
self.save_progress("Testing connectivity")
ret_val, _ = make_rest_call(
"/api/v1/account",
action_result,
self.get_config(),
method="get",
)
if phantom.is_fail(ret_val):
self.save_progress(action_result.get_message())
self.save_progress("Connectivity test failed")
return action_result.set_status(
phantom.APP_ERROR, "Connectivity test failed"
)
self.save_progress("Connectivity test passed")
return action_result.set_status(phantom.APP_SUCCESS, "Connectivity test passed")
def _handle_lookup(self, query, dataset, action_result):
req_method, api = CENSYS_API_METHOD_MAP["info"]
api_url = api.format(dataset=dataset, value=query)
ret_val, response = make_rest_call(
api_url,
action_result,
self.get_config(),
method=req_method,
)
if phantom.is_fail(ret_val):
return (action_result.get_status(), None)
self._process_ports(response)
action_result.add_data(response)
return action_result.set_status(phantom.APP_SUCCESS), response
def _process_ports(self, response):
protocols = response.get("protocols")
if not protocols:
return response
ports = [x.split("/")[0] for x in protocols]
if not ports:
return response
response["ports"] = {}
for port in ports:
if port not in response:
continue
response["ports"].update({port: response[port]})
del response[port]
return response
def _lookup_ip(self, param):
self.debug_print("Entering _lookup_ip")
action_result = self.add_action_result(ActionResult(param))
summary_data = action_result.update_summary({})
req_method, endpoint = CENSYS_API_METHOD_MAP["info"]
ip = param[CENSYS_JSON_IP]
if not is_ip(ip):
return action_result.set_status(
phantom.APP_ERROR,
"Please provide a valid value in the 'ip' action parameter",
)
ret_val, response = make_rest_call(
endpoint.format(dataset=CENSYS_QUERY_HOSTS_DATASET, value=ip),
action_result,
self.get_config(),
method=req_method,
)
if phantom.is_fail(ret_val):
return action_result.get_status()
if not response.get("result", {}).get("services"):
return action_result.set_status(phantom.APP_SUCCESS, CENSYS_NO_INFO)
summary_data["port"] = (
response.get("result", {}).get("services", [])[0].get("port")
)
summary_data["service_name"] = (
response.get("result", {}).get("services", [])[0].get("service_name")
)
action_result.add_data(response)
self._update_summary(action_result, response)
self.debug_print("Exiting _lookup_ip")
return action_result.set_status(phantom.APP_SUCCESS)
def _query_ip(self, param):
self.debug_print("Entering _query_ip")
action_result = self.add_action_result(ActionResult(param))
summary_data = action_result.update_summary({})
ret_val, response = CensysSearch(self.get_config()).query_dataset(
action_result,
summary_data,
CENSYS_QUERY_HOSTS_DATASET,
param,
CENSYS_QUERY_IP_DATA_PER_PAGE,
)
if phantom.is_fail(ret_val):
return action_result.get_status()
self._update_summary(action_result, response)
self.debug_print("Exiting _query_ip")
return action_result.set_status(phantom.APP_SUCCESS)
def _lookup_certificate(self, param):
self.debug_print("Entering _lookup_certificate")
action_result = self.add_action_result(ActionResult(param))
ret_val, response = self._handle_lookup(
param[CENSYS_JSON_SHA256], "certificates", action_result
)
if phantom.is_fail(ret_val):
message = action_result.get_message()
if "do not know anything" in message:
return action_result.set_status(phantom.APP_SUCCESS, CENSYS_NO_INFO)
return ret_val
# summary
parsed = response.get("parsed", {})
if parsed:
summary = {}
validity = parsed.get("validity")
if validity:
valid_from = validity.get("start")
valid_to = validity.get("end")
summary.update({"valid_from": valid_from, "valid_to": valid_to})
summary.update(
{
"issuer_dn": parsed.get("issuer_dn"),
"subject_dn": parsed.get("subject_dn"),
}
)
action_result.update_summary(summary)
self.debug_print("Exiting _lookup_certificate")
return ret_val
def _query_certificate(self, param):
"""Use handle_search to query the correct dataset with the query string"""
self.debug_print("Entering _query_certificate")
action_result = self.add_action_result(ActionResult(param))
summary_data = action_result.update_summary({})
ret_val, response = CensysSearch(self.get_config()).query_dataset(
action_result,
summary_data,
CENSYS_QUERY_CERTIFICATE_DATASET,
param,
CENSYS_QUERY_CERTIFICATE_DATA_PER_PAGE,
)
if phantom.is_fail(ret_val):
return action_result.get_status()
self._update_summary(action_result, response)
self.debug_print("Exiting _query_certificate")
return action_result.set_status(phantom.APP_SUCCESS)
def _update_summary(self, action_result, response):
protocols = response.get("protocols")
if protocols:
if not isinstance(protocols, list):
protocols = [protocols]
protocols = ", ".join(protocols)
action_result.update_summary({"protocols": protocols})
return action_result
def _lookup_domain(self, param):
self.debug_print("Entering _lookup_domain")
action_result = self.add_action_result(ActionResult(param))
self.debug_print("Exiting _lookup_domain")
return action_result.set_status(
phantom.APP_ERROR, "This action is not yet supported by Censys in API v2"
)
def _query_domain(self, param):
"""Use handle_search to query the correct dataset with the query string"""
self.debug_print("Entering _query_domain")
action_result = self.add_action_result(ActionResult(param))
self.debug_print("Exiting _query_domain")
return action_result.set_status(
phantom.APP_ERROR, "This action is not yet supported by Censys in API v2"
)
def handle_action(self, param):
action = self.get_action_identifier()
ret_val = phantom.APP_SUCCESS
if action == phantom.ACTION_ID_TEST_ASSET_CONNECTIVITY:
ret_val = self._test_connectivity(param)
elif action == CENSYS_LOOKUP_IP_ACTION:
ret_val = self._lookup_ip(param)
elif action == CENSYS_LOOKUP_DOMAIN_ACTION:
ret_val = self._lookup_domain(param)
elif action == CENSYS_LOOKUP_CERTIFICATE_ACTION:
ret_val = self._lookup_certificate(param)
elif action == CENSYS_QUERY_IP_ACTION:
ret_val = self._query_ip(param)
elif action == CENSYS_QUERY_DOMAIN_ACTION:
ret_val = self._query_domain(param)
elif action == CENSYS_QUERY_CERTIFICATE_ACTION:
ret_val = self._query_certificate(param)
return ret_val
def initialize(self):
# Load the state in initialize, use it to store data
# that needs to be accessed across actions
self._state = self.load_state()
if not isinstance(self._state, dict):
self.debug_print("Resetting the state file with the default format")
self._state = {}
"""
# Access values in asset config by the name
# Required values can be accessed directly
required_config_name = config['required_config_name']
# Optional values should use the .get() function
optional_config_name = config.get('optional_config_name')
"""
self.set_validator("ipv6", is_ip)
return phantom.APP_SUCCESS
def finalize(self):
# Save the state, this data is saved across actions and app upgrades
self.save_state(self._state)
return phantom.APP_SUCCESS
if __name__ == "__main__":
import argparse
import sys
import pudb
pudb.set_trace()
argparser = argparse.ArgumentParser()
argparser.add_argument("input_test_json", help="Input Test JSON file")
argparser.add_argument("-u", "--username", help="username", required=False)
argparser.add_argument("-p", "--password", help="password", required=False)
argparser.add_argument(
"-v",
"--verify",
action="store_true",
help="verify",
required=False,
default=False,
)
args = argparser.parse_args()
session_id = None
username = args.username
password = args.password
verify = args.verify
if username is not None and password is None:
# User specified a username but not a password, so ask
import getpass
password = getpass.getpass("Password: ")
if username and password:
login_url = BaseConnector._get_phantom_base_url() + "login"
try:
print("Accessing the Login page")
r = requests.get(login_url, verify=verify, timeout=CENSYS_DEFAULT_TIMEOUT)
csrftoken = r.cookies["csrftoken"]
data = dict()
data["username"] = username
data["password"] = password
data["csrfmiddlewaretoken"] = csrftoken
headers = dict()
headers["Cookie"] = "csrftoken=" + csrftoken
headers["Referer"] = login_url
print("Logging into Platform to get the session id")
r2 = requests.post(
login_url,
verify=verify,
data=data,
headers=headers,
timeout=CENSYS_DEFAULT_TIMEOUT,
)
session_id = r2.cookies["sessionid"]
except Exception as e:
print("Unable to get session id from the platform. Error: " + str(e))
sys.exit(1)
with open(args.input_test_json) as f:
in_json = f.read()
in_json = json.loads(in_json)
print(json.dumps(in_json, indent=4))
connector = CensysConnector()
connector.print_progress_message = True
if session_id is not None:
in_json["user_session_token"] = session_id
connector._set_csrf_info(csrftoken, headers["Referer"])
ret_val = connector._handle_action(json.dumps(in_json), None)
print(json.dumps(json.loads(ret_val), indent=4))
sys.exit(0)