Small telemetry refactoring and added ScoutSuite telem

This commit is contained in:
VakarisZ 2020-09-04 15:46:50 +03:00
parent 3adafd31b0
commit 549e621895
5 changed files with 42 additions and 15 deletions

View File

@ -128,12 +128,12 @@ class ControlClient(object):
return {}
@staticmethod
def send_telemetry(telem_category, data):
def send_telemetry(telem_category, json_data: str):
if not WormConfiguration.current_server:
LOG.error("Trying to send %s telemetry before current server is established, aborting." % telem_category)
return
try:
telemetry = {'monkey_guid': GUID, 'telem_category': telem_category, 'data': data}
telemetry = {'monkey_guid': GUID, 'telem_category': telem_category, 'data': json_data}
requests.post("https://%s/api/telemetry" % (WormConfiguration.current_server,), # noqa: DUO123
data=json.dumps(telemetry),
headers={'content-type': 'application/json'},

View File

@ -5,6 +5,7 @@ import logging
from infection_monkey.control import ControlClient
logger = logging.getLogger(__name__)
LOGGED_DATA_LENGTH = 300 # How many characters of telemetry data will be logged
__author__ = 'itay.mizeretz'
@ -22,12 +23,25 @@ class BaseTelem(object, metaclass=abc.ABCMeta):
Sends telemetry to island
"""
data = self.get_data()
serialized_data = json.dumps(data, cls=self.json_encoder)
self.log_telem_sending(serialized_data, log_data)
ControlClient.send_telemetry(self.telem_category, serialized_data)
@abc.abstractmethod
def get_data(self) -> dict:
"""
:return: Data of telemetry (should be dict)
"""
pass
@property
def json_encoder(self):
return json.JSONEncoder
def log_telem_sending(self, serialized_data: str, log_data=True):
logger.debug(f"Sending {self.telem_category} telemetry.")
if log_data:
data_to_log = json.dumps(data)
else:
data_to_log = 'redacted'
logger.debug("Sending {} telemetry. Data: {}".format(self.telem_category, data_to_log))
ControlClient.send_telemetry(self.telem_category, data)
logger.debug(f"Telemetry contents: {BaseTelem.truncate_data(serialized_data)}")
@property
@abc.abstractmethod
@ -37,9 +51,9 @@ class BaseTelem(object, metaclass=abc.ABCMeta):
"""
pass
@abc.abstractmethod
def get_data(self) -> dict:
"""
:return: Data of telemetry (should be dict)
"""
pass
@staticmethod
def truncate_data(data: str):
if len(data) <= LOGGED_DATA_LENGTH:
return data
else:
return f"{data[:LOGGED_DATA_LENGTH]}..."

View File

@ -1,4 +1,6 @@
from common.common_consts.telem_categories import TelemCategoryEnum
from infection_monkey.system_info.collectors.scoutsuite_collector.scoutsuite.ScoutSuite.output.result_encoder import \
ScoutJsonEncoder
from infection_monkey.telemetry.base_telem import BaseTelem
@ -12,6 +14,7 @@ class ScoutSuiteTelem(BaseTelem):
super().__init__()
self.data = data
json_encoder = ScoutJsonEncoder
telem_category = TelemCategoryEnum.SCOUTSUITE
def get_data(self):

View File

@ -31,8 +31,7 @@ class Report(flask_restful.Resource):
"statusesToPillars": ZeroTrustService.get_statuses_to_pillars(),
"pillarsToStatuses": ZeroTrustService.get_pillars_to_statuses(),
"grades": ZeroTrustService.get_pillars_grades()
}
)
})
elif report_data == REPORT_DATA_PRINCIPLES_STATUS:
return jsonify(ZeroTrustService.get_principles_status())
elif report_data == REPORT_DATA_FINDINGS:

View File

@ -0,0 +1,11 @@
from monkey_island.cc.database import mongo
def process_scoutsuite_telemetry(telemetry_json):
update_data(telemetry_json)
def update_data(telemetry_json):
mongo.db.scoutsuite.update(
{'guid': telemetry_json['monkey_guid']},
{'$push': {'results': telemetry_json['data']}})