Agent: Remove WormConfiguration and cleanup related infrastructure

This commit is contained in:
vakarisz 2022-06-17 16:38:52 +03:00
parent e8001d8cf7
commit f8855d290d
4 changed files with 10 additions and 45 deletions

View File

@ -64,6 +64,3 @@ class Configuration(object):
max_depth = None
keep_tunnel_open_time = 30
WormConfiguration = Configuration()

View File

@ -1,7 +1,6 @@
import json
import logging
import platform
from pprint import pformat
from socket import gethostname
from typing import Mapping, Optional
@ -10,7 +9,7 @@ from requests.exceptions import ConnectionError
import infection_monkey.tunnel as tunnel
from common.common_consts.timeouts import LONG_REQUEST_TIMEOUT, MEDIUM_REQUEST_TIMEOUT
from infection_monkey.config import GUID, WormConfiguration
from infection_monkey.config import GUID
from infection_monkey.network.info import get_host_subnets, local_ips
from infection_monkey.transport.http import HTTPConnectProxy
from infection_monkey.transport.tcp import TcpProxy
@ -151,38 +150,7 @@ class ControlClient:
except Exception as exc:
logger.warning(f"Error connecting to control server {self.server_address}: {exc}")
def load_control_config(self):
if not self.server_address:
return
try:
reply = requests.get( # noqa: DUO123
f"https://{self.server_address}/api/agent/",
verify=False,
proxies=self.proxies,
timeout=MEDIUM_REQUEST_TIMEOUT,
)
except Exception as exc:
logger.warning(f"Error connecting to control server {self.server_address}: {exc}")
return
try:
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",
self.server_address,
reply._content,
exc,
)
raise Exception("Couldn't load from from server's configuration, aborting. %s" % exc)
def create_control_tunnel(self):
def create_control_tunnel(self, keep_tunnel_open_time: int):
if not self.server_address:
return None
@ -200,7 +168,7 @@ class ControlClient:
return tunnel.MonkeyTunnel(
proxy_class,
keep_tunnel_open_time=WormConfiguration.keep_tunnel_open_time,
keep_tunnel_open_time=keep_tunnel_open_time,
target_addr=target_addr,
target_port=target_port,
)

View File

@ -5,13 +5,11 @@ import os
import sys
import traceback
from multiprocessing import freeze_support
from pprint import pformat
# dummy import for pyinstaller
# noinspection PyUnresolvedReferences
import infection_monkey.post_breach # noqa: F401
from common.version import get_version
from infection_monkey.config import WormConfiguration
from infection_monkey.dropper import MonkeyDrops
from infection_monkey.model import DROPPER_ARG, MONKEY_ARG
from infection_monkey.monkey import InfectionMonkey
@ -57,9 +55,6 @@ def main():
mode_args, mode_specific_args = arg_parser.parse_known_args()
mode = mode_args.mode
formatted_config = pformat(WormConfiguration.hide_sensitive_info(WormConfiguration.as_dict()))
print(f"Loaded Configuration:\n{formatted_config}")
try:
if MONKEY_ARG == mode:
log_path = get_agent_log_path()

View File

@ -149,7 +149,6 @@ class InfectionMonkey:
raise Exception(f"Monkey couldn't find server with {self._opts.tunnel} default tunnel.")
self._control_client.wakeup(parent=self._opts.parent)
self._control_client.load_control_config()
def _current_server_is_set(self) -> bool:
if self._control_client.find_server(default_tunnel=self._opts.tunnel):
@ -165,7 +164,13 @@ class InfectionMonkey:
if firewall.is_enabled():
firewall.add_firewall_rule()
self._monkey_inbound_tunnel = self._control_client.create_control_tunnel()
control_channel = ControlChannel(
self._control_client.server_address, GUID, self._control_client.proxies
)
keep_tunnel_open_time = control_channel.get_config()["config"]["keep_tunnel_open_time"]
self._monkey_inbound_tunnel = self._control_client.create_control_tunnel(
keep_tunnel_open_time
)
if self._monkey_inbound_tunnel and self._propagation_enabled():
self._monkey_inbound_tunnel.start()