Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove old Agent registration logic #2542

Merged
merged 12 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/).
- "POST /api/log" endpoint. #2485
- "GET /api/local-monkey" endpoint. #2506
- "/api/telemetry" endpoint. #2503
- "/api/agent" endpoint. #2542

### Fixed
- A bug in network map page that caused delay of telemetry log loading. #1545
Expand Down
45 changes: 0 additions & 45 deletions monkey/infection_monkey/control.py

This file was deleted.

6 changes: 1 addition & 5 deletions monkey/infection_monkey/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
notify_relay_on_propagation,
)
from infection_monkey.config import GUID
from infection_monkey.control import ControlClient
from infection_monkey.credential_collectors import (
MimikatzCredentialCollector,
SSHCredentialCollector,
Expand Down Expand Up @@ -93,7 +92,6 @@ def __init__(self, args):
self._cmd_island_ip = self._island_address.ip
self._cmd_island_port = self._island_address.port

self._control_client = ControlClient(server_address=self._island_address)
self._control_channel = ControlChannel(
str(self._island_address), self._agent_id, self._island_api_client
)
Expand Down Expand Up @@ -182,8 +180,6 @@ def start(self):
logger.info("Agent is starting...")
logger.info(f"Agent GUID: {GUID}")

self._control_client.wakeup(parent=self._opts.parent)

should_stop = self._control_channel.should_agent_stop()
if should_stop:
logger.info("The Monkey Island has instructed this agent to stop")
Expand Down Expand Up @@ -338,7 +334,7 @@ def _subscribe_events(self):
)

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

def _is_another_monkey_running(self):
Expand Down
2 changes: 0 additions & 2 deletions monkey/monkey_island/cc/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from monkey_island.cc.resources.exploitations.monkey_exploitation import MonkeyExploitation
from monkey_island.cc.resources.island_mode import IslandMode
from monkey_island.cc.resources.local_run import LocalRun
from monkey_island.cc.resources.monkey import Monkey
from monkey_island.cc.resources.ransomware_report import RansomwareReport
from monkey_island.cc.resources.root import Root
from monkey_island.cc.resources.security_report import SecurityReport
Expand Down Expand Up @@ -150,7 +149,6 @@ def init_restful_endpoints(api: FlaskDIWrapper):
api.add_resource(RegistrationStatus)
api.add_resource(Authenticate)
api.add_resource(Agents)
api.add_resource(Monkey)
api.add_resource(LocalRun)

api.add_resource(IslandMode)
Expand Down
1 change: 0 additions & 1 deletion monkey/monkey_island/cc/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Order of importing matters here, for registering the embedded and referenced documents before
# using them.
from .monkey import Monkey
from .monkey_ttl import MonkeyTtl
from monkey_island.cc.models.report.report import Report
from .simulation import Simulation, IslandMode
from .user_credentials import UserCredentials
Expand Down
64 changes: 0 additions & 64 deletions monkey/monkey_island/cc/models/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@
Define a Document Schema for the Monkey document.

"""
import ring
from mongoengine import (
BooleanField,
DateTimeField,
Document,
DoesNotExist,
DynamicField,
FloatField,
ListField,
ReferenceField,
StringField,
)

from common.network.network_utils import get_my_ip_addresses_legacy
from monkey_island.cc.models.monkey_ttl import MonkeyTtl, create_monkey_ttl_document
from monkey_island.cc.server_utils.consts import DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS


class Monkey(Document):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole file also should go. Instead of get_latest_modifytime we can just fetch the timestamp of the latest event

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am working on to see do we actually need the modifytime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, maybe not. Removing Monkey would require more work, but we can certainly replace the get_latest_modifytime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not touch that code in this PR since Ilija is working on it as part of #2507.

"""
Expand All @@ -31,8 +24,6 @@ class Monkey(Document):

# SCHEMA
guid = StringField(required=True)
should_stop = BooleanField()
dead = BooleanField()
hostname = StringField()
ip_addresses = ListField(StringField())
launch_time = FloatField()
Expand All @@ -43,70 +34,15 @@ class Monkey(Document):
# (even with required=False of null=True).
# See relevant issue: https://github.com/MongoEngine/mongoengine/issues/1904
parent = ListField(ListField(DynamicField()))
ttl_ref = ReferenceField(MonkeyTtl)
tunnel = ReferenceField("self")

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

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

# LOGIC
@staticmethod
def get_single_monkey_by_id(db_id):
try:
return Monkey.objects.get(id=db_id)
except DoesNotExist as ex:
raise MonkeyNotFoundError("info: {0} | id: {1}".format(ex, str(db_id)))

@staticmethod
# See https://www.python.org/dev/peps/pep-0484/#forward-references
def get_single_monkey_by_guid(monkey_guid) -> "Monkey":
try:
return Monkey.objects.get(guid=monkey_guid)
except DoesNotExist as ex:
raise MonkeyNotFoundError("info: {0} | guid: {1}".format(ex, str(monkey_guid)))

@staticmethod
def get_latest_modifytime():
if Monkey.objects.count() > 0:
return Monkey.objects.order_by("-modifytime").first().modifytime
return None

@ring.lru()
@staticmethod
def get_label_by_id(object_id):
current_monkey = Monkey.get_single_monkey_by_id(object_id)
label = Monkey.get_hostname_by_id(object_id) + " : " + current_monkey.ip_addresses[0]
local_ips = map(str, get_my_ip_addresses_legacy())
if len(set(current_monkey.ip_addresses).intersection(local_ips)) > 0:
label = "MonkeyIsland - " + label
return label

@ring.lru()
@staticmethod
def get_hostname_by_id(object_id):
"""
:param object_id: the object ID of a Monkey in the database.
:return: The hostname of that machine.
:note: Use this and not monkey.hostname for performance - this is lru-cached.
"""
return Monkey.get_single_monkey_by_id(object_id).hostname

# data has TTL of 1 second. This is useful for rapid calls for report generation.
@ring.lru(expire=1)
@staticmethod
def is_monkey(object_id):
try:
_ = Monkey.get_single_monkey_by_id(object_id)
return True
except: # noqa: E722
return False

def renew_ttl(self, duration=DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS):
self.ttl_ref = create_monkey_ttl_document(duration)
self.save()


class MonkeyNotFoundError(Exception):
pass
50 changes: 0 additions & 50 deletions monkey/monkey_island/cc/models/monkey_ttl.py

This file was deleted.

82 changes: 0 additions & 82 deletions monkey/monkey_island/cc/resources/monkey.py

This file was deleted.

7 changes: 0 additions & 7 deletions monkey/monkey_island/cc/resources/utils/semaphores.py

This file was deleted.

2 changes: 0 additions & 2 deletions monkey/monkey_island/cc/server_utils/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ def _get_monkey_island_abs_path() -> str:

DEFAULT_DATA_DIR = expand_path(get_default_data_dir())

DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS = 60 * 5

_MONGO_BINARY_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "bin", "mongodb")
_MONGO_EXECUTABLE_PATH_WIN = os.path.join(_MONGO_BINARY_DIR, "mongod.exe")
_MONGO_EXECUTABLE_PATH_LINUX = os.path.join(_MONGO_BINARY_DIR, "bin", "mongod")
Expand Down
Loading