Skip to content

Commit 1ae6fd7

Browse files
Merge pull request #2542 from guardicore/2272-remove-old-agent-registration-logic
Remove old Agent registration logic
2 parents 52d3ea4 + 46fa783 commit 1ae6fd7

File tree

12 files changed

+3
-346
lines changed

12 files changed

+3
-346
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/).
142142
- "POST /api/log" endpoint. #2485
143143
- "GET /api/local-monkey" endpoint. #2506
144144
- "/api/telemetry" endpoint. #2503
145+
- "/api/agent" endpoint. #2542
145146

146147
### Fixed
147148
- A bug in network map page that caused delay of telemetry log loading. #1545

monkey/infection_monkey/control.py

-45
This file was deleted.

monkey/infection_monkey/monkey.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
notify_relay_on_propagation,
2929
)
3030
from infection_monkey.config import GUID
31-
from infection_monkey.control import ControlClient
3231
from infection_monkey.credential_collectors import (
3332
MimikatzCredentialCollector,
3433
SSHCredentialCollector,
@@ -93,7 +92,6 @@ def __init__(self, args):
9392
self._cmd_island_ip = self._island_address.ip
9493
self._cmd_island_port = self._island_address.port
9594

96-
self._control_client = ControlClient(server_address=self._island_address)
9795
self._control_channel = ControlChannel(
9896
str(self._island_address), self._agent_id, self._island_api_client
9997
)
@@ -182,8 +180,6 @@ def start(self):
182180
logger.info("Agent is starting...")
183181
logger.info(f"Agent GUID: {GUID}")
184182

185-
self._control_client.wakeup(parent=self._opts.parent)
186-
187183
should_stop = self._control_channel.should_agent_stop()
188184
if should_stop:
189185
logger.info("The Monkey Island has instructed this agent to stop")
@@ -338,7 +334,7 @@ def _subscribe_events(self):
338334
)
339335

340336
def _running_on_island(self, local_network_interfaces: List[IPv4Interface]) -> bool:
341-
server_ip = self._control_client.server_address.ip
337+
server_ip = self._island_address.ip
342338
return server_ip in {interface.ip for interface in local_network_interfaces}
343339

344340
def _is_another_monkey_running(self):

monkey/monkey_island/cc/app.py

-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from monkey_island.cc.resources.exploitations.monkey_exploitation import MonkeyExploitation
3535
from monkey_island.cc.resources.island_mode import IslandMode
3636
from monkey_island.cc.resources.local_run import LocalRun
37-
from monkey_island.cc.resources.monkey import Monkey
3837
from monkey_island.cc.resources.ransomware_report import RansomwareReport
3938
from monkey_island.cc.resources.root import Root
4039
from monkey_island.cc.resources.security_report import SecurityReport
@@ -150,7 +149,6 @@ def init_restful_endpoints(api: FlaskDIWrapper):
150149
api.add_resource(RegistrationStatus)
151150
api.add_resource(Authenticate)
152151
api.add_resource(Agents)
153-
api.add_resource(Monkey)
154152
api.add_resource(LocalRun)
155153

156154
api.add_resource(IslandMode)

monkey/monkey_island/cc/models/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Order of importing matters here, for registering the embedded and referenced documents before
22
# using them.
33
from .monkey import Monkey
4-
from .monkey_ttl import MonkeyTtl
54
from monkey_island.cc.models.report.report import Report
65
from .simulation import Simulation, IslandMode
76
from .user_credentials import UserCredentials

monkey/monkey_island/cc/models/monkey.py

-64
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@
22
Define a Document Schema for the Monkey document.
33
44
"""
5-
import ring
65
from mongoengine import (
7-
BooleanField,
86
DateTimeField,
97
Document,
10-
DoesNotExist,
118
DynamicField,
129
FloatField,
1310
ListField,
1411
ReferenceField,
1512
StringField,
1613
)
1714

18-
from common.network.network_utils import get_my_ip_addresses_legacy
19-
from monkey_island.cc.models.monkey_ttl import MonkeyTtl, create_monkey_ttl_document
20-
from monkey_island.cc.server_utils.consts import DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS
21-
2215

2316
class Monkey(Document):
2417
"""
@@ -31,8 +24,6 @@ class Monkey(Document):
3124

3225
# SCHEMA
3326
guid = StringField(required=True)
34-
should_stop = BooleanField()
35-
dead = BooleanField()
3627
hostname = StringField()
3728
ip_addresses = ListField(StringField())
3829
launch_time = FloatField()
@@ -43,70 +34,15 @@ class Monkey(Document):
4334
# (even with required=False of null=True).
4435
# See relevant issue: https://github.com/MongoEngine/mongoengine/issues/1904
4536
parent = ListField(ListField(DynamicField()))
46-
ttl_ref = ReferenceField(MonkeyTtl)
4737
tunnel = ReferenceField("self")
4838

4939
# This field only exists when the monkey is running on an AWS
5040
aws_instance_id = StringField(required=False)
5141

5242
# instance. See https://github.com/guardicore/monkey/issues/426.
5343

54-
# LOGIC
55-
@staticmethod
56-
def get_single_monkey_by_id(db_id):
57-
try:
58-
return Monkey.objects.get(id=db_id)
59-
except DoesNotExist as ex:
60-
raise MonkeyNotFoundError("info: {0} | id: {1}".format(ex, str(db_id)))
61-
62-
@staticmethod
63-
# See https://www.python.org/dev/peps/pep-0484/#forward-references
64-
def get_single_monkey_by_guid(monkey_guid) -> "Monkey":
65-
try:
66-
return Monkey.objects.get(guid=monkey_guid)
67-
except DoesNotExist as ex:
68-
raise MonkeyNotFoundError("info: {0} | guid: {1}".format(ex, str(monkey_guid)))
69-
7044
@staticmethod
7145
def get_latest_modifytime():
7246
if Monkey.objects.count() > 0:
7347
return Monkey.objects.order_by("-modifytime").first().modifytime
7448
return None
75-
76-
@ring.lru()
77-
@staticmethod
78-
def get_label_by_id(object_id):
79-
current_monkey = Monkey.get_single_monkey_by_id(object_id)
80-
label = Monkey.get_hostname_by_id(object_id) + " : " + current_monkey.ip_addresses[0]
81-
local_ips = map(str, get_my_ip_addresses_legacy())
82-
if len(set(current_monkey.ip_addresses).intersection(local_ips)) > 0:
83-
label = "MonkeyIsland - " + label
84-
return label
85-
86-
@ring.lru()
87-
@staticmethod
88-
def get_hostname_by_id(object_id):
89-
"""
90-
:param object_id: the object ID of a Monkey in the database.
91-
:return: The hostname of that machine.
92-
:note: Use this and not monkey.hostname for performance - this is lru-cached.
93-
"""
94-
return Monkey.get_single_monkey_by_id(object_id).hostname
95-
96-
# data has TTL of 1 second. This is useful for rapid calls for report generation.
97-
@ring.lru(expire=1)
98-
@staticmethod
99-
def is_monkey(object_id):
100-
try:
101-
_ = Monkey.get_single_monkey_by_id(object_id)
102-
return True
103-
except: # noqa: E722
104-
return False
105-
106-
def renew_ttl(self, duration=DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS):
107-
self.ttl_ref = create_monkey_ttl_document(duration)
108-
self.save()
109-
110-
111-
class MonkeyNotFoundError(Exception):
112-
pass

monkey/monkey_island/cc/models/monkey_ttl.py

-50
This file was deleted.

monkey/monkey_island/cc/resources/monkey.py

-82
This file was deleted.

monkey/monkey_island/cc/resources/utils/semaphores.py

-7
This file was deleted.

monkey/monkey_island/cc/server_utils/consts.py

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ def _get_monkey_island_abs_path() -> str:
2626

2727
DEFAULT_DATA_DIR = expand_path(get_default_data_dir())
2828

29-
DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS = 60 * 5
30-
3129
_MONGO_BINARY_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "bin", "mongodb")
3230
_MONGO_EXECUTABLE_PATH_WIN = os.path.join(_MONGO_BINARY_DIR, "mongod.exe")
3331
_MONGO_EXECUTABLE_PATH_LINUX = os.path.join(_MONGO_BINARY_DIR, "bin", "mongod")

0 commit comments

Comments
 (0)