From 27d9e8bcee432e9f3151b848b433c44beef06f62 Mon Sep 17 00:00:00 2001 From: Itay Mizeretz Date: Sun, 1 Oct 2017 16:34:11 +0300 Subject: [PATCH] Fix bug in processing tunnel edges --- monkey_island/cc/resources/monkey.py | 14 ++++++-------- monkey_island/cc/resources/telemetry.py | 5 ++--- monkey_island/cc/services/edge.py | 3 ++- monkey_island/cc/services/node.py | 5 +++-- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/monkey_island/cc/resources/monkey.py b/monkey_island/cc/resources/monkey.py index d6d89e2c8..2e2da8a5d 100644 --- a/monkey_island/cc/resources/monkey.py +++ b/monkey_island/cc/resources/monkey.py @@ -46,9 +46,8 @@ class Monkey(flask_restful.Resource): update['$set']['config_error'] = monkey_json['config_error'] if 'tunnel' in monkey_json: - host = monkey_json['tunnel'].split(":")[-2].replace("//", "") - tunnel_host_id = NodeService.get_monkey_by_ip(host)["_id"] - NodeService.set_monkey_tunnel(monkey["_id"], tunnel_host_id) + tunnel_host_ip = monkey_json['tunnel'].split(":")[-2].replace("//", "") + NodeService.set_monkey_tunnel(monkey["_id"], tunnel_host_ip) return mongo.db.monkey.update({"_id": monkey["_id"]}, update, upsert=False) @@ -98,10 +97,9 @@ class Monkey(flask_restful.Resource): else: monkey_json['parent'] = db_monkey.get('parent') + [parent_to_add] - tunnel_host_id = None + tunnel_host_ip = None if 'tunnel' in monkey_json: - host = monkey_json['tunnel'].split(":")[-2].replace("//", "") - tunnel_host_id = NodeService.get_monkey_by_ip(host)["_id"] + tunnel_host_ip = monkey_json['tunnel'].split(":")[-2].replace("//", "") monkey_json.pop('tunnel') mongo.db.monkey.update({"guid": monkey_json["guid"]}, @@ -112,8 +110,8 @@ class Monkey(flask_restful.Resource): new_monkey_id = mongo.db.monkey.find_one({"guid": monkey_json["guid"]})["_id"] - if tunnel_host_id is not None: - NodeService.set_monkey_tunnel(new_monkey_id, tunnel_host_id) + if tunnel_host_ip is not None: + NodeService.set_monkey_tunnel(new_monkey_id, tunnel_host_ip) existing_node = mongo.db.node.find_one({"ip_addresses": {"$in": monkey_json["ip_addresses"]}}) diff --git a/monkey_island/cc/resources/telemetry.py b/monkey_island/cc/resources/telemetry.py index 0fea9b4d5..25f45212d 100644 --- a/monkey_island/cc/resources/telemetry.py +++ b/monkey_island/cc/resources/telemetry.py @@ -89,9 +89,8 @@ class Telemetry(flask_restful.Resource): def process_tunnel_telemetry(self, telemetry_json): monkey_id = NodeService.get_monkey_by_guid(telemetry_json['monkey_guid'])["_id"] if telemetry_json['data']['proxy'] is not None: - host = telemetry_json['data']['proxy'].split(":")[-2].replace("//", "") - tunnel_host_id = NodeService.get_monkey_by_ip(host)["_id"] - NodeService.set_monkey_tunnel(monkey_id, tunnel_host_id) + tunnel_host_ip = telemetry_json['data']['proxy'].split(":")[-2].replace("//", "") + NodeService.set_monkey_tunnel(monkey_id, tunnel_host_ip) else: NodeService.unset_all_monkey_tunnels(monkey_id) diff --git a/monkey_island/cc/services/edge.py b/monkey_island/cc/services/edge.py index 276fe488f..96d676c0e 100644 --- a/monkey_island/cc/services/edge.py +++ b/monkey_island/cc/services/edge.py @@ -22,7 +22,7 @@ class EdgeService: @staticmethod def edge_to_displayed_edge(edge): - services = {} + services = [] os = {} exploits = [] if len(edge["scans"]) > 0: @@ -52,6 +52,7 @@ class EdgeService: exploit_container["end_timestamp"] = new_exploit["timestamp"] displayed_edge = EdgeService.edge_to_net_edge(edge) + displayed_edge["ip_address"] = edge["ip_address"] displayed_edge["services"] = services displayed_edge["os"] = os diff --git a/monkey_island/cc/services/node.py b/monkey_island/cc/services/node.py index 85b05bdcb..f5dbcf37c 100644 --- a/monkey_island/cc/services/node.py +++ b/monkey_island/cc/services/node.py @@ -148,7 +148,8 @@ class NodeService: upsert=False) @staticmethod - def set_monkey_tunnel(monkey_id, tunnel_host_id): + def set_monkey_tunnel(monkey_id, tunnel_host_ip): + tunnel_host_id = NodeService.get_monkey_by_ip(tunnel_host_ip)["_id"] NodeService.unset_all_monkey_tunnels(monkey_id) mongo.db.monkey.update( {"_id": monkey_id}, @@ -156,7 +157,7 @@ class NodeService: upsert=False) tunnel_edge = EdgeService.get_or_create_edge(monkey_id, tunnel_host_id) mongo.db.edge.update({"_id": tunnel_edge["_id"]}, - {'$set': {'tunnel': True}}, + {'$set': {'tunnel': True, 'ip_address': tunnel_host_ip}}, upsert=False) @staticmethod