forked from metron-labs/cybereason
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cybereason_session.py
60 lines (51 loc) · 2.82 KB
/
cybereason_session.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
# File: cybereason_session.py
#
# 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 requests
from cybereason_consts import DEFAULT_REQUEST_TIMEOUT
class CybereasonSession:
def __init__(self, connector):
connector.save_progress("Logging in to the Cybereason console...")
self.session = requests.Session()
post_body = {
"username": connector._username,
"password": connector._password
}
try:
url = "{0}/login.html".format(connector._base_url)
res = self.session.post(url, data=post_body, verify=connector._verify_server_cert, timeout=DEFAULT_REQUEST_TIMEOUT)
if self.session.cookies.get_dict().get("JSESSIONID") is None:
connector.save_progress("Error when logging in to the the Cybereason console: No session cookie returned")
connector.save_progress("Status code: {}".format(res.status_code))
connector.debug_print("Status code: {}, message: {}".format(res.status_code, res.text))
elif res.status_code != 200:
connector.save_progress("Error when logging in to the Cybereason console: Unknown error")
connector.save_progress("Status code: {}".format(res.status_code))
connector.debug_print("Status code: {}, message: {}".format(res.status_code, res.text))
else:
connector.save_progress("Successfully logged in to the Cybereason console")
connector.save_progress('CybereasonSession created')
except requests.exceptions.InvalidSchema:
connector.save_progress("Error connecting to server. No connection adapters were found for %s" % (url))
except requests.exceptions.InvalidURL:
connector.save_progress("Error connecting to server. Invalid URL %s" % (url))
except requests.exceptions.ConnectionError:
connector.save_progress("Error Details: Connection Refused from the Server")
except Exception as e:
err = connector._get_error_message_from_exception(e)
connector.save_progress("Error connecting to server. {0}".format(err))
def get_session(self):
return self.session
def get_session_cookies(self):
return self.session.cookies.get_dict()
def post(self, **kwargs):
return self.session.post(kwargs)