diff --git a/monkey/infection_monkey/exploit/tools/helpers.py b/monkey/infection_monkey/exploit/tools/helpers.py index a0b53d0e7..47057b63f 100644 --- a/monkey/infection_monkey/exploit/tools/helpers.py +++ b/monkey/infection_monkey/exploit/tools/helpers.py @@ -1,4 +1,5 @@ import logging +from pathlib import Path logger = logging.getLogger(__name__) @@ -16,27 +17,21 @@ def get_target_monkey(host): from infection_monkey.control import ControlClient - if host.monkey_exe: - return host.monkey_exe - if not host.os.get("type"): return None - monkey_path = ControlClient.download_monkey_exe(host) + if host.os.get("type") == platform.system().lower(): + try: + # When running from source, sys.executable will be "python", not an agent. + if "monkey" in Path(sys.executable).name: + return sys.executable + except Exception as ex: + logger.warning( + "Unable to retrieve this executable's path, downloading executable from the island " + f"instead: {ex}" + ) - if host.os.get("machine") and monkey_path: - host.monkey_exe = monkey_path - - if not monkey_path: - if host.os.get("type") == platform.system().lower(): - # if exe not found, and we have the same arch or arch is unknown and we are 32bit, - # use our exe - if (not host.os.get("machine") and sys.maxsize < 2 ** 32) or host.os.get( - "machine", "" - ).lower() == platform.machine().lower(): - monkey_path = sys.executable - - return monkey_path + return ControlClient.download_monkey_exe(host) def get_target_monkey_by_os(is_windows, is_32bit): @@ -54,7 +49,7 @@ def get_monkey_depth(): def get_monkey_dest_path(url_to_monkey): """ Gets destination path from monkey's source url. - :param url_to_monkey: Hosted monkey's url. egz : http://localserver:9999/monkey/windows-32.exe + :param url_to_monkey: Hosted monkey's url. egz : http://localserver:9999/monkey/windows-64.exe :return: Corresponding monkey path from configuration """ from infection_monkey.config import WormConfiguration diff --git a/monkey/infection_monkey/exploit/web_rce.py b/monkey/infection_monkey/exploit/web_rce.py index cdb0ddce9..7bc02a694 100644 --- a/monkey/infection_monkey/exploit/web_rce.py +++ b/monkey/infection_monkey/exploit/web_rce.py @@ -274,7 +274,7 @@ class WebRCE(HostExploiter): "monkey_path": dest_path, "http_path": http_path, } - self.telemetry_messenger.send_telemtry( + self.telemetry_messenger.send_telemetry( T1197Telem(ScanStatus.USED, self.host, BITS_UPLOAD_STRING) ) resp = self.exploit(url, backup_command) @@ -334,10 +334,10 @@ class WebRCE(HostExploiter): command = CHMOD_MONKEY % {"monkey_path": path} try: resp = self.exploit(url, command) - self.telemetry_messenger.send_telemtry(T1222Telem(ScanStatus.USED, command, self.host)) + self.telemetry_messenger.send_telemetry(T1222Telem(ScanStatus.USED, command, self.host)) except Exception as e: logger.error("Something went wrong while trying to change permission: %s" % e) - self.telemetry_messenger.send_telemtry(T1222Telem(ScanStatus.SCANNED, "", self.host)) + self.telemetry_messenger.send_telemetry(T1222Telem(ScanStatus.SCANNED, "", self.host)) return False # If exploiter returns True / False if isinstance(resp, bool): @@ -432,8 +432,7 @@ class WebRCE(HostExploiter): except KeyError: logger.error( 'Unknown key was found. Please use "linux" and "win64" keys to ' - "initialize " - "custom dict of monkey's destination paths" + "initialize custom dict of monkey's destination paths" ) return False diff --git a/monkey/infection_monkey/model/host.py b/monkey/infection_monkey/model/host.py index 3bbd1dfb8..95cc85810 100644 --- a/monkey/infection_monkey/model/host.py +++ b/monkey/infection_monkey/model/host.py @@ -8,7 +8,6 @@ class VictimHost(object): self.os = {} self.services = {} self.icmp = False - self.monkey_exe = None self.default_tunnel = None self.default_server = None @@ -42,7 +41,6 @@ class VictimHost(object): for k, v in list(self.services.items()): victim += "%s-%s " % (k, v) victim += "] ICMP: %s " % (self.icmp) - victim += "target monkey: %s" % self.monkey_exe return victim def set_island_address(self, ip: str, port: Optional[str]): diff --git a/monkey/monkey_island/cc/resources/monkey_download.py b/monkey/monkey_island/cc/resources/monkey_download.py index 24e03280c..ee77091af 100644 --- a/monkey/monkey_island/cc/resources/monkey_download.py +++ b/monkey/monkey_island/cc/resources/monkey_download.py @@ -11,61 +11,23 @@ from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH logger = logging.getLogger(__name__) MONKEY_DOWNLOADS = [ - { - "type": "linux", - "machine": "x86_64", - "filename": "monkey-linux-64", - }, - { - "type": "linux", - "machine": "i686", - "filename": "monkey-linux-32", - }, - { - "type": "linux", - "machine": "i386", - "filename": "monkey-linux-32", - }, { "type": "linux", "filename": "monkey-linux-64", }, { "type": "windows", - "machine": "x86", - "filename": "monkey-windows-32.exe", - }, - { - "type": "windows", - "machine": "amd64", "filename": "monkey-windows-64.exe", }, - { - "type": "windows", - "machine": "64", - "filename": "monkey-windows-64.exe", - }, - { - "type": "windows", - "machine": "32", - "filename": "monkey-windows-32.exe", - }, - { - "type": "windows", - "filename": "monkey-windows-32.exe", - }, ] -def get_monkey_executable(host_os, machine): +def get_monkey_executable(host_os): for download in MONKEY_DOWNLOADS: - if host_os == download.get("type") and machine == download.get("machine"): - logger.info("Monkey exec found for os: {0} and machine: {1}".format(host_os, machine)) + if host_os == download.get("type"): + logger.info(f"Monkey exec found for os: {host_os}") return download - logger.warning( - "No monkey executables could be found for the host os or machine or both: host_os: {" - "0}, machine: {1}".format(host_os, machine) - ) + logger.warning(f"No monkey executables could be found for the host os: {host_os}") return None @@ -80,7 +42,7 @@ class MonkeyDownload(flask_restful.Resource): host_json = json.loads(request.data) host_os = host_json.get("os") if host_os: - result = get_monkey_executable(host_os.get("type"), host_os.get("machine")) + result = get_monkey_executable(host_os.get("type")) if result: # change resulting from new base path diff --git a/monkey/monkey_island/cc/services/run_local_monkey.py b/monkey/monkey_island/cc/services/run_local_monkey.py index ce6c98c61..4cdd89479 100644 --- a/monkey/monkey_island/cc/services/run_local_monkey.py +++ b/monkey/monkey_island/cc/services/run_local_monkey.py @@ -25,7 +25,7 @@ class LocalMonkeyRunService: @staticmethod def run_local_monkey(): # get the monkey executable suitable to run on the server - result = get_monkey_executable(platform.system().lower(), platform.machine().lower()) + result = get_monkey_executable(platform.system().lower()) if not result: return False, "OS Type not found" diff --git a/monkey/tests/unit_tests/infection_monkey/telemetry/test_exploit_telem.py b/monkey/tests/unit_tests/infection_monkey/telemetry/test_exploit_telem.py index 5d6c81f56..600e1db20 100644 --- a/monkey/tests/unit_tests/infection_monkey/telemetry/test_exploit_telem.py +++ b/monkey/tests/unit_tests/infection_monkey/telemetry/test_exploit_telem.py @@ -16,7 +16,6 @@ HOST_AS_DICT = { "os": {}, "services": {}, "icmp": False, - "monkey_exe": None, "default_tunnel": None, "default_server": None, } @@ -37,7 +36,13 @@ ERROR_MSG = "failed because yolo" @pytest.fixture def exploit_telem_test_instance(): - return ExploitTelem(EXPLOITER_NAME, HOST, ExploiterResultData(RESULT, RESULT, OS_LINUX, EXPLOITER_INFO, EXPLOITER_ATTEMPTS, ERROR_MSG)) + return ExploitTelem( + EXPLOITER_NAME, + HOST, + ExploiterResultData( + RESULT, RESULT, OS_LINUX, EXPLOITER_INFO, EXPLOITER_ATTEMPTS, ERROR_MSG + ), + ) def test_exploit_telem_send(exploit_telem_test_instance, spy_send_telemetry): diff --git a/monkey/tests/unit_tests/infection_monkey/telemetry/test_scan_telem.py b/monkey/tests/unit_tests/infection_monkey/telemetry/test_scan_telem.py index 07c6fbf41..a369fe4cf 100644 --- a/monkey/tests/unit_tests/infection_monkey/telemetry/test_scan_telem.py +++ b/monkey/tests/unit_tests/infection_monkey/telemetry/test_scan_telem.py @@ -14,7 +14,6 @@ HOST_AS_DICT = { "os": {}, "services": {}, "icmp": False, - "monkey_exe": None, "default_tunnel": None, "default_server": None, }