Ejemplo n.º 1
0
    def load_control_config():
        if not WormConfiguration.current_server:
            return
        try:
            reply = requests.get(
                "https://%s/api/monkey/%s" %
                (WormConfiguration.current_server, GUID),  # noqa: DUO123
                verify=False,
                proxies=ControlClient.proxies)

        except Exception as exc:
            LOG.warning("Error connecting to control server %s: %s",
                        WormConfiguration.current_server, exc)
            return

        try:
            unknown_variables = WormConfiguration.from_kv(
                reply.json().get('config'))
            LOG.info("New configuration was loaded from server: %r" %
                     (WormConfiguration.hide_sensitive_info(
                         WormConfiguration.as_dict()), ))
        except Exception as exc:
            # we don't continue with default conf here because it might be dangerous
            LOG.error(
                "Error parsing JSON reply from control server %s (%s): %s",
                WormConfiguration.current_server, reply._content, exc)
            raise Exception(
                "Couldn't load from from server's configuration, aborting. %s"
                % exc)

        if unknown_variables:
            ControlClient.send_config_error()
Ejemplo n.º 2
0
    def load_control_config():
        if not WormConfiguration.current_server:
            return
        try:
            reply = requests.get(  # noqa: DUO123
                "https://%s/api/monkey/%s" %
                (WormConfiguration.current_server, GUID),
                verify=False,
                proxies=ControlClient.proxies,
                timeout=MEDIUM_REQUEST_TIMEOUT,
            )

        except Exception as exc:
            logger.warning("Error connecting to control server %s: %s",
                           WormConfiguration.current_server, exc)
            return

        try:
            unknown_variables = WormConfiguration.from_kv(
                reply.json().get("config"))
            formatted_config = pformat(
                WormConfiguration.hide_sensitive_info(
                    WormConfiguration.as_dict()))
            logger.info(
                f"New configuration was loaded from server:\n{formatted_config}"
            )
        except Exception as exc:
            # we don't continue with default conf here because it might be dangerous
            logger.error(
                "Error parsing JSON reply from control server %s (%s): %s",
                WormConfiguration.current_server,
                reply._content,
                exc,
            )
            raise Exception(
                "Couldn't load from from server's configuration, aborting. %s"
                % exc)

        if unknown_variables:
            ControlClient.send_config_error()
Ejemplo n.º 3
0
def main():
    global LOG

    if 2 > len(sys.argv):
        return True
    freeze_support()  # required for multiprocessing + pyinstaller on windows
    monkey_mode = sys.argv[1]

    if not (monkey_mode in [MONKEY_ARG, DROPPER_ARG]):
        return True

    config_file = EXTERNAL_CONFIG_FILE

    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument('-c', '--config')
    opts, monkey_args = arg_parser.parse_known_args(sys.argv[2:])
    if opts.config:
        config_file = opts.config
    if os.path.isfile(config_file):
        # using print because config can also change log locations
        print("Loading config from %s." % config_file)
        try:
            with open(config_file) as config_fo:
                json_dict = json.load(config_fo)
                WormConfiguration.from_kv(json_dict)
        except ValueError as e:
            print("Error loading config: %s, using default" % (e,))
    else:
        print("Config file wasn't supplied and default path: %s wasn't found, using internal default" % (config_file,))

    print("Loaded Configuration: %r" % WormConfiguration.hide_sensitive_info(WormConfiguration.as_dict()))

    # Make sure we're not in a machine that has the kill file
    kill_path = os.path.expandvars(
        WormConfiguration.kill_file_path_windows) if sys.platform == "win32" else WormConfiguration.kill_file_path_linux
    if os.path.exists(kill_path):
        print("Kill path found, finished run")
        return True

    try:
        if MONKEY_ARG == monkey_mode:
            log_path = get_monkey_log_path()
            monkey_cls = InfectionMonkey
        elif DROPPER_ARG == monkey_mode:
            log_path = get_dropper_log_path()
            monkey_cls = MonkeyDrops
        else:
            return True
    except ValueError:
        return True

    if WormConfiguration.use_file_logging:
        if os.path.exists(log_path):
            # If log exists but can't be removed it means other monkey is running. This usually happens on upgrade
            # from 32bit to 64bit monkey on Windows. In all cases this shouldn't be a problem.
            try:
                os.remove(log_path)
            except OSError:
                pass
        LOG_CONFIG['handlers']['file']['filename'] = log_path
        # noinspection PyUnresolvedReferences
        LOG_CONFIG['root']['handlers'].append('file')
    else:
        del LOG_CONFIG['handlers']['file']

    logging.config.dictConfig(LOG_CONFIG)
    LOG = logging.getLogger()

    def log_uncaught_exceptions(ex_cls, ex, tb):
        LOG.critical(''.join(traceback.format_tb(tb)))
        LOG.critical('{0}: {1}'.format(ex_cls, ex))

    sys.excepthook = log_uncaught_exceptions

    LOG.info(">>>>>>>>>> Initializing monkey (%s): PID %s <<<<<<<<<<",
             monkey_cls.__name__, os.getpid())

    LOG.info(f"version: {get_version()}")

    monkey = monkey_cls(monkey_args)
    monkey.initialize()

    try:
        monkey.start()

        if WormConfiguration.serialize_config:
            with open(config_file, 'w') as config_fo:
                json_dict = WormConfiguration.as_dict()
                json.dump(json_dict, config_fo, skipkeys=True, sort_keys=True, indent=4, separators=(',', ': '))

        return True
    except Exception as e:
        LOG.exception("Exception thrown from monkey's start function. More info: {}".format(e))
    finally:
        monkey.cleanup()