From 22105eabe3d76096c5b6f65f23dd53e7a4976683 Mon Sep 17 00:00:00 2001 From: Itay Mizeretz Date: Sun, 15 Oct 2017 20:06:26 +0300 Subject: [PATCH] Add basic report logic --- monkey_island/cc/app.py | 3 +- monkey_island/cc/resources/report.py | 10 +++++++ monkey_island/cc/services/report.py | 41 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 monkey_island/cc/resources/report.py create mode 100644 monkey_island/cc/services/report.py diff --git a/monkey_island/cc/app.py b/monkey_island/cc/app.py index 6cfea1502..70001521a 100644 --- a/monkey_island/cc/app.py +++ b/monkey_island/cc/app.py @@ -15,7 +15,7 @@ from cc.resources.monkey_download import MonkeyDownload from cc.resources.netmap import NetMap from cc.resources.edge import Edge from cc.resources.node import Node - +from cc.resources.report import Report from cc.resources.root import Root from cc.services.config import ConfigService @@ -88,5 +88,6 @@ def init_app(mongo_url): api.add_resource(NetMap, '/api/netmap', '/api/netmap/') api.add_resource(Edge, '/api/netmap/edge', '/api/netmap/edge/') api.add_resource(Node, '/api/netmap/node', '/api/netmap/node/') + api.add_resource(Report, '/api/report', '/api/report/') return app diff --git a/monkey_island/cc/resources/report.py b/monkey_island/cc/resources/report.py new file mode 100644 index 000000000..e967b207f --- /dev/null +++ b/monkey_island/cc/resources/report.py @@ -0,0 +1,10 @@ +import flask_restful + +from cc.services.report import ReportService + +__author__ = "itay.mizeretz" + + +class Report(flask_restful.Resource): + def get(self): + return ReportService.get_report() diff --git a/monkey_island/cc/services/report.py b/monkey_island/cc/services/report.py new file mode 100644 index 000000000..4356c84fd --- /dev/null +++ b/monkey_island/cc/services/report.py @@ -0,0 +1,41 @@ +from cc.database import mongo + +__author__ = "itay.mizeretz" + + +class ReportService: + def __init__(self): + pass + + @staticmethod + def get_first_monkey_time(): + return mongo.db.telemetry.find({}, {'timestamp': 1}).sort([('$natural', 1)]).limit(1)[0]['timestamp'] + + @staticmethod + def get_last_monkey_dead_time(): + return mongo.db.telemetry.find({}, {'timestamp': 1}).sort([('$natural', -1)]).limit(1)[0]['timestamp'] + + @staticmethod + def get_breach_count(): + return mongo.db.edge.count({'exploits.result': True}) + + @staticmethod + def get_successful_exploit_types(): + exploit_types = mongo.db.command({'distinct': 'edge', 'key': 'exploits.exploiter'})['values'] + return [exploit for exploit in exploit_types if ReportService.did_exploit_type_succeed(exploit)] + + @staticmethod + def get_report(): + return \ + { + 'first_monkey_time': ReportService.get_first_monkey_time(), + 'last_monkey_dead_time': ReportService.get_last_monkey_dead_time(), + 'breach_count': ReportService.get_breach_count(), + 'successful_exploit_types': ReportService.get_successful_exploit_types(), + } + + @staticmethod + def did_exploit_type_succeed(exploit_type): + return mongo.db.edge.count( + {'exploits': {'$elemMatch': {'exploiter': exploit_type, 'result': True}}}, + limit=1) > 0