Agent: Use random name for monkey temporary bin

This commit is contained in:
Ilija Lazoroski 2022-03-15 14:04:02 +01:00
parent e4d3cc8841
commit 264fa440c6
2 changed files with 17 additions and 7 deletions

View File

@ -20,15 +20,13 @@ from infection_monkey.exploit.powershell_utils.powershell_client import (
IPowerShellClient,
PowerShellClient,
)
from infection_monkey.exploit.tools.helpers import get_monkey_depth
from infection_monkey.exploit.tools.helpers import get_monkey_depth, get_random_file_suffix
from infection_monkey.model import DROPPER_ARG, RUN_MONKEY, VictimHost
from infection_monkey.utils.commands import build_monkey_commandline
from infection_monkey.utils.environment import is_windows_os
logger = logging.getLogger(__name__)
TEMP_MONKEY_BINARY_FILEPATH = "./monkey_temp_bin"
class PowerShellRemotingDisabledError(Exception):
pass
@ -177,16 +175,19 @@ class PowerShellExploiter(HostExploiter):
)
def _copy_monkey_binary_to_victim(self, monkey_path_on_victim):
self._create_local_agent_file(TEMP_MONKEY_BINARY_FILEPATH)
temp_monkey_binary_filepath = f"monkey_temp_bin_{get_random_file_suffix()}"
self._create_local_agent_file(temp_monkey_binary_filepath)
try:
logger.info(f"Attempting to copy the monkey agent binary to {self.host.ip_addr}")
self._client.copy_file(TEMP_MONKEY_BINARY_FILEPATH, monkey_path_on_victim)
self._client.copy_file(temp_monkey_binary_filepath, monkey_path_on_victim)
except Exception as ex:
raise RemoteAgentCopyError(f"Failed to copy the agent binary to the victim: {ex}")
finally:
if os.path.isfile(TEMP_MONKEY_BINARY_FILEPATH):
os.remove(TEMP_MONKEY_BINARY_FILEPATH)
if os.path.isfile(temp_monkey_binary_filepath):
os.remove(temp_monkey_binary_filepath)
def _create_local_agent_file(self, binary_path):
agent_binary_bytes = self.agent_repository.get_agent_binary("windows")

View File

@ -2,6 +2,8 @@ import logging
from typing import Any, Mapping
from infection_monkey.model import VictimHost
import string
from random import SystemRandom
logger = logging.getLogger(__name__)
@ -23,6 +25,13 @@ def get_target_monkey_by_os(is_windows, is_32bit):
)
def get_random_file_suffix() -> str:
character_set = list(string.ascii_letters + string.digits + "_" + "-")
safe_random = SystemRandom()
random_string = "".join(safe_random.choices(character_set, k=8))
return random_string
def get_monkey_depth():
from infection_monkey.config import WormConfiguration