From 1cbcb69697a8a472f942c5692bf3b2ff370f34e9 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 12 Oct 2020 16:25:30 +0300 Subject: [PATCH 1/3] Fixed schedule_jobs bug, where scheduled job is never deleted and monkey freezes indefinitelly. --- monkey/infection_monkey/control.py | 2 +- .../infection_monkey/post_breach/actions/schedule_jobs.py | 4 +++- .../post_breach/job_scheduling/windows_job_scheduling.py | 2 +- monkey/infection_monkey/post_breach/post_breach_handler.py | 7 ++++--- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/monkey/infection_monkey/control.py b/monkey/infection_monkey/control.py index 35922286f..32b639492 100644 --- a/monkey/infection_monkey/control.py +++ b/monkey/infection_monkey/control.py @@ -81,7 +81,7 @@ class ControlClient(object): if ControlClient.proxies: debug_message += " through proxies: %s" % ControlClient.proxies LOG.debug(debug_message) - requests.get("https://%s/api?action=is-up" % (server,), # noqa: DUO123 + requests.get(f"https://{server}/api?action=is-up", # noqa: DUO123 verify=False, proxies=ControlClient.proxies, timeout=TIMEOUT_IN_SECONDS) diff --git a/monkey/infection_monkey/post_breach/actions/schedule_jobs.py b/monkey/infection_monkey/post_breach/actions/schedule_jobs.py index d6cdd2765..f7d8d8056 100644 --- a/monkey/infection_monkey/post_breach/actions/schedule_jobs.py +++ b/monkey/infection_monkey/post_breach/actions/schedule_jobs.py @@ -15,5 +15,7 @@ class ScheduleJobs(PBA): super(ScheduleJobs, self).__init__(name=POST_BREACH_JOB_SCHEDULING, linux_cmd=' '.join(linux_cmds), windows_cmd=windows_cmds) - + + def run(self): + super(ScheduleJobs, self).run() remove_scheduled_jobs() diff --git a/monkey/infection_monkey/post_breach/job_scheduling/windows_job_scheduling.py b/monkey/infection_monkey/post_breach/job_scheduling/windows_job_scheduling.py index 017203821..6fd888d67 100644 --- a/monkey/infection_monkey/post_breach/job_scheduling/windows_job_scheduling.py +++ b/monkey/infection_monkey/post_breach/job_scheduling/windows_job_scheduling.py @@ -5,7 +5,7 @@ SCHEDULED_TASK_COMMAND = r'C:\windows\system32\cmd.exe' def get_windows_commands_to_schedule_jobs(): - return f'schtasks /Create /SC monthly /TN {SCHEDULED_TASK_NAME} /TR {SCHEDULED_TASK_COMMAND}' + return f'schtasks /Create /SC monthly /F /TN {SCHEDULED_TASK_NAME} /TR {SCHEDULED_TASK_COMMAND}' def get_windows_commands_to_remove_scheduled_jobs(): diff --git a/monkey/infection_monkey/post_breach/post_breach_handler.py b/monkey/infection_monkey/post_breach/post_breach_handler.py index e20727ccf..888984551 100644 --- a/monkey/infection_monkey/post_breach/post_breach_handler.py +++ b/monkey/infection_monkey/post_breach/post_breach_handler.py @@ -25,9 +25,9 @@ class PostBreach(object): """ Executes all post breach actions. """ - pool = Pool(5) - pool.map(self.run_pba, self.pba_list) - LOG.info("All PBAs executed. Total {} executed.".format(len(self.pba_list))) + with Pool(5) as pool: + pool.map(self.run_pba, self.pba_list) + LOG.info("All PBAs executed. Total {} executed.".format(len(self.pba_list))) @staticmethod def config_to_pba_list() -> Sequence[PBA]: @@ -40,5 +40,6 @@ class PostBreach(object): try: LOG.debug("Executing PBA: '{}'".format(pba.name)) pba.run() + LOG.debug(f"Execution of {pba.name} finished") except Exception as e: LOG.error("PBA {} failed. Error info: {}".format(pba.name, e)) From 7abafb70e1f885794b6181d516c7c84afcf29a6c Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Fri, 9 Oct 2020 10:19:32 +0300 Subject: [PATCH 2/3] Fixed bad exception handling in version_update.py --- monkey/common/utils/exceptions.py | 4 ++++ monkey/monkey_island/cc/services/version_update.py | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/monkey/common/utils/exceptions.py b/monkey/common/utils/exceptions.py index fa026933c..5134ade0a 100644 --- a/monkey/common/utils/exceptions.py +++ b/monkey/common/utils/exceptions.py @@ -20,3 +20,7 @@ class CredentialsNotRequiredError(RegistrationNotNeededError): class AlreadyRegisteredError(RegistrationNotNeededError): """ Raise to indicate the reason why registration is not required """ + + +class NoInternetError(Exception): + """ Raise to indicate problems caused when no internet connection is present""" diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index ad1f81513..e4dea2b95 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -3,6 +3,7 @@ import logging import requests import monkey_island.cc.environment.environment_singleton as env_singleton +from common.utils.exceptions import NoInternetError from common.version import get_version __author__ = "itay.mizeretz" @@ -29,8 +30,8 @@ class VersionUpdateService: if VersionUpdateService.newer_version is None: try: VersionUpdateService.newer_version = VersionUpdateService._check_new_version() - except Exception: - logger.exception('Failed updating version number') + except NoInternetError: + logger.info('Failed updating version number') return VersionUpdateService.newer_version @@ -42,7 +43,11 @@ class VersionUpdateService: """ url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % (env_singleton.env.get_deployment(), get_version()) - reply = requests.get(url, timeout=15) + try: + reply = requests.get(url, timeout=7) + except requests.exceptions.RequestException: + logger.info("Can't get latest monkey version, probably no connection to the internet.") + raise NoInternetError res = reply.json().get('newer_version', None) From a77743137f5e03a00f5c047d5b34323a1ecbf5d4 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Thu, 15 Oct 2020 15:19:19 +0300 Subject: [PATCH 3/3] Refactored exception name to a more specific one --- monkey/common/utils/exceptions.py | 4 ++-- monkey/monkey_island/cc/services/version_update.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/monkey/common/utils/exceptions.py b/monkey/common/utils/exceptions.py index 5134ade0a..6b36402a0 100644 --- a/monkey/common/utils/exceptions.py +++ b/monkey/common/utils/exceptions.py @@ -22,5 +22,5 @@ class AlreadyRegisteredError(RegistrationNotNeededError): """ Raise to indicate the reason why registration is not required """ -class NoInternetError(Exception): - """ Raise to indicate problems caused when no internet connection is present""" +class VersionServerConnectionError(Exception): + """ Raise to indicate that connection to version update server failed """ diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index e4dea2b95..af47bf93a 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -3,7 +3,7 @@ import logging import requests import monkey_island.cc.environment.environment_singleton as env_singleton -from common.utils.exceptions import NoInternetError +from common.utils.exceptions import VersionServerConnectionError from common.version import get_version __author__ = "itay.mizeretz" @@ -30,7 +30,7 @@ class VersionUpdateService: if VersionUpdateService.newer_version is None: try: VersionUpdateService.newer_version = VersionUpdateService._check_new_version() - except NoInternetError: + except VersionServerConnectionError: logger.info('Failed updating version number') return VersionUpdateService.newer_version @@ -47,7 +47,7 @@ class VersionUpdateService: reply = requests.get(url, timeout=7) except requests.exceptions.RequestException: logger.info("Can't get latest monkey version, probably no connection to the internet.") - raise NoInternetError + raise VersionServerConnectionError res = reply.json().get('newer_version', None)