def _check_new_version(): """ Checks if newer monkey version is available :return: False if not, version in string format ('1.6.2') otherwise """ url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % ( env_singleton.env.get_deployment(), get_version(), ) 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 VersionServerConnectionError res = reply.json().get("newer_version", None) if res is False: return res [int(x) for x in res.split(".") ] # raises value error if version is invalid format return res
def _data_dir_version_mismatch_exists(data_dir_path: Path) -> bool: try: data_dir_version = get_version_from_dir(data_dir_path) except FileNotFoundError: logger.debug("Version file not found.") return True island_version = get_version() return island_version != data_dir_version
def cleanup(self): LOG.info("Monkey cleanup started") self._keep_running = False if self._upgrading_to_64: InfectionMonkey.close_tunnel() firewall.close() else: StateTelem(is_done=True, version=get_version()).send() # Signal the server (before closing the tunnel) InfectionMonkey.close_tunnel() firewall.close() if WormConfiguration.send_log_to_server: self.send_log() self._singleton.unlock() InfectionMonkey.self_delete() LOG.info("Monkey is shutting down")
def _check_new_version(): """ Checks if newer monkey version is available :return: False if not, version in string format ('1.6.2') otherwise """ url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % ( env_singleton.env.get_deployment(), get_version()) reply = requests.get(url, timeout=15) res = reply.json().get('newer_version', None) if res is False: return res [int(x) for x in res.split('.') ] # raises value error if version is invalid format return res
def get(self): return { 'current_version': get_version(), 'newer_version': VersionUpdateService.get_newer_version(), 'download_link': VersionUpdateService.get_download_link() }
from pathlib import Path from importlib import import_module ROOT_PATH = Path(__file__ + "/..").resolve() sys.path.append(str(ROOT_PATH)) from common.docopt import docopt from common.version import get_version from common.output import colorize import re from schema import Schema, And, Or, Use, SchemaError, Optional, Regex arguments = docopt(__doc__, version=get_version()) schema = Schema({ 'PROCESSOR': And(len, lambda n: (ROOT_PATH / ('preprocessors/' + str(n) + '.py')).is_file(), error='That preprocessor does not exist'), 'INPUT': And(len, lambda n: os.path.exists(n), error='That folder does not exist'), '--output-dir': len, '--output-name': len, '--img-dir': Or(None, len), '--mip-dir': Or(None, len), '--data-dir': Or(None, len), '--filter-window': And(Use(int), lambda n: n > 0, error='--filter-window must be > 0'), '--gamma': And(Use(float), lambda n: n > 0, error='--gamma must be > 0'), '--channel': And(Use(int), lambda n: n > 0), '--data-set': Or(None, len), '--pixel-size': And(Use(float), lambda n: n >= 0, error='--pixel-size must be > 0'), '--rolling-ball-size': And(Use(int), lambda n: n > 0),
def start(self): try: LOG.info("Monkey is starting...") LOG.debug("Starting the setup phase.") # Sets island's IP and port for monkey to communicate to self.set_default_server() self.set_default_port() # Create a dir for monkey files if there isn't one create_monkey_dir() self.upgrade_to_64_if_needed() ControlClient.wakeup(parent=self._parent) ControlClient.load_control_config() if is_windows_os(): T1106Telem(ScanStatus.USED, UsageEnum.SINGLETON_WINAPI).send() self.shutdown_by_not_alive_config() if self.is_started_on_island(): ControlClient.report_start_on_island() ControlClient.should_monkey_run(self._opts.vulnerable_port) if firewall.is_enabled(): firewall.add_firewall_rule() monkey_tunnel = ControlClient.create_control_tunnel() if monkey_tunnel: monkey_tunnel.start() StateTelem(is_done=False, version=get_version()).send() TunnelTelem().send() LOG.debug("Starting the post-breach phase.") self.collect_system_info_if_configured() PostBreach().execute_all_configured() LOG.debug("Starting the propagation phase.") self.shutdown_by_max_depth_reached() for iteration_index in range(WormConfiguration.max_iterations): ControlClient.keepalive() ControlClient.load_control_config() self._network.initialize() self._fingerprint = HostFinger.get_instances() self._exploiters = HostExploiter.get_classes() if not self._keep_running or not WormConfiguration.alive: break machines = self._network.get_victim_machines( max_find=WormConfiguration.victims_max_find, stop_callback=ControlClient.check_for_stop) is_empty = True for machine in machines: if ControlClient.check_for_stop(): break is_empty = False for finger in self._fingerprint: LOG.info( "Trying to get OS fingerprint from %r with module %s", machine, finger.__class__.__name__) finger.get_host_fingerprint(machine) ScanTelem(machine).send() # skip machines that we've already exploited if machine in self._exploited_machines: LOG.debug("Skipping %r - already exploited", machine) continue elif machine in self._fail_exploitation_machines: if WormConfiguration.retry_failed_explotation: LOG.debug( "%r - exploitation failed before, trying again", machine) else: LOG.debug( "Skipping %r - exploitation failed before", machine) continue if monkey_tunnel: monkey_tunnel.set_tunnel_for_host(machine) if self._default_server: if self._network.on_island(self._default_server): machine.set_default_server( get_interface_to_target(machine.ip_addr) + (':' + self._default_server_port if self. _default_server_port else '')) else: machine.set_default_server(self._default_server) LOG.debug("Default server for machine: %r set to %s" % (machine, machine.default_server)) # Order exploits according to their type self._exploiters = sorted( self._exploiters, key=lambda exploiter_: exploiter_.EXPLOIT_TYPE.value) host_exploited = False for exploiter in [ exploiter(machine) for exploiter in self._exploiters ]: if self.try_exploiting(machine, exploiter): host_exploited = True VictimHostTelem('T1210', ScanStatus.USED, machine=machine).send() break if not host_exploited: self._fail_exploitation_machines.add(machine) VictimHostTelem('T1210', ScanStatus.SCANNED, machine=machine).send() if not self._keep_running: break if (not is_empty) and (WormConfiguration.max_iterations > iteration_index + 1): time_to_sleep = WormConfiguration.timeout_between_iterations LOG.info( "Sleeping %d seconds before next life cycle iteration", time_to_sleep) time.sleep(time_to_sleep) if self._keep_running and WormConfiguration.alive: LOG.info("Reached max iterations (%d)", WormConfiguration.max_iterations) elif not WormConfiguration.alive: LOG.info("Marked not alive from configuration") # if host was exploited, before continue to closing the tunnel ensure the exploited host had its chance to # connect to the tunnel if len(self._exploited_machines) > 0: time_to_sleep = WormConfiguration.keep_tunnel_open_time LOG.info( "Sleeping %d seconds for exploited machines to connect to tunnel", time_to_sleep) time.sleep(time_to_sleep) if monkey_tunnel: monkey_tunnel.stop() monkey_tunnel.join() except PlannedShutdownException: LOG.info( "A planned shutdown of the Monkey occurred. Logging the reason and finishing execution." ) LOG.exception("Planned shutdown, reason:")
def get_download_link(): return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % ( env_singleton.env.get_deployment(), get_version())
def write_version(dir_path: Path): version_file_path = dir_path / _version_filename version_file_path.write_text(get_version())
def get(self): return { "current_version": get_version(), "newer_version": VersionUpdateService.get_newer_version(), "download_link": VersionUpdateService.get_download_link(), }
def start(self): try: logger.info("Monkey is starting...") logger.debug("Starting the setup phase.") # Sets island's IP and port for monkey to communicate to self.set_default_server() self.set_default_port() # Create a dir for monkey files if there isn't one create_monkey_dir() self.upgrade_to_64_if_needed() ControlClient.wakeup(parent=self._parent) ControlClient.load_control_config() if is_windows_os(): T1106Telem(ScanStatus.USED, UsageEnum.SINGLETON_WINAPI).send() self.shutdown_by_not_alive_config() if is_running_on_island(): WormConfiguration.started_on_island = True ControlClient.report_start_on_island() if not ControlClient.should_monkey_run(self._opts.vulnerable_port): raise PlannedShutdownException( "Monkey shouldn't run on current machine " "(it will be exploited later with more depth)." ) if firewall.is_enabled(): firewall.add_firewall_rule() self._monkey_tunnel = ControlClient.create_control_tunnel() if self._monkey_tunnel: self._monkey_tunnel.start() StateTelem(is_done=False, version=get_version()).send() TunnelTelem().send() logger.debug("Starting the post-breach phase asynchronously.") self._post_breach_phase = Thread(target=self.start_post_breach_phase) self._post_breach_phase.start() if not InfectionMonkey.max_propagation_depth_reached(): logger.info("Starting the propagation phase.") logger.debug("Running with depth: %d" % WormConfiguration.depth) self.propagate() else: logger.info( "Maximum propagation depth has been reached; monkey will not propagate." ) TraceTelem(MAX_DEPTH_REACHED_MESSAGE).send() if self._keep_running and WormConfiguration.alive: InfectionMonkey.run_ransomware() # if host was exploited, before continue to closing the tunnel ensure the exploited # host had its chance to # connect to the tunnel if len(self._exploited_machines) > 0: time_to_sleep = WormConfiguration.keep_tunnel_open_time logger.info( "Sleeping %d seconds for exploited machines to connect to tunnel", time_to_sleep ) time.sleep(time_to_sleep) except PlannedShutdownException: logger.info( "A planned shutdown of the Monkey occurred. Logging the reason and finishing " "execution." ) logger.exception("Planned shutdown, reason:") finally: if self._monkey_tunnel: self._monkey_tunnel.stop() self._monkey_tunnel.join() if self._post_breach_phase: self._post_breach_phase.join()
def _storage_service_agent(): return 'Archivematica Storage Service-%s' % version.get_version()