Agent: Change typehints of agent destination path to PurePath

This commit is contained in:
vakaris_zilius 2022-03-24 14:47:07 +00:00
parent 49d3433ade
commit 25c7696300
5 changed files with 17 additions and 17 deletions

View File

@ -1,6 +1,6 @@
import logging import logging
import os import os
from pathlib import Path from pathlib import PurePath
from time import sleep from time import sleep
import pymssql import pymssql
@ -132,7 +132,7 @@ class MSSQLExploiter(HostExploiter):
raise Exception("Couldn't execute MSSQL exploiter because payload was too long") raise Exception("Couldn't execute MSSQL exploiter because payload was too long")
self.run_mssql_commands(array_of_commands) self.run_mssql_commands(array_of_commands)
def run_monkey(self, monkey_path_on_victim: Path): def run_monkey(self, monkey_path_on_victim: PurePath):
monkey_launch_command = self.get_monkey_launch_command(monkey_path_on_victim) monkey_launch_command = self.get_monkey_launch_command(monkey_path_on_victim)
self.run_mssql_command(monkey_launch_command) self.run_mssql_command(monkey_launch_command)
self.run_payload_file() self.run_payload_file()
@ -142,7 +142,7 @@ class MSSQLExploiter(HostExploiter):
self.cursor.execute(cmd) self.cursor.execute(cmd)
sleep(MSSQLExploiter.QUERY_BUFFER) sleep(MSSQLExploiter.QUERY_BUFFER)
def upload_monkey(self, monkey_path_on_victim: Path): def upload_monkey(self, monkey_path_on_victim: PurePath):
monkey_download_command = self.write_download_command_to_payload(monkey_path_on_victim) monkey_download_command = self.write_download_command_to_payload(monkey_path_on_victim)
self.run_payload_file() self.run_payload_file()
self.add_executed_cmd(monkey_download_command.command) self.add_executed_cmd(monkey_download_command.command)
@ -158,7 +158,7 @@ class MSSQLExploiter(HostExploiter):
) )
self.run_mssql_command(tmp_dir_removal_command) self.run_mssql_command(tmp_dir_removal_command)
def start_monkey_server(self, monkey_path_on_victim: Path) -> LockedHTTPServer: def start_monkey_server(self, monkey_path_on_victim: PurePath) -> LockedHTTPServer:
self.agent_http_path, http_thread = HTTPTools.create_locked_transfer( self.agent_http_path, http_thread = HTTPTools.create_locked_transfer(
self.host, str(monkey_path_on_victim), self.agent_repository self.host, str(monkey_path_on_victim), self.agent_repository
) )
@ -169,12 +169,12 @@ class MSSQLExploiter(HostExploiter):
http_thread.stop() http_thread.stop()
http_thread.join(LONG_REQUEST_TIMEOUT) http_thread.join(LONG_REQUEST_TIMEOUT)
def write_download_command_to_payload(self, monkey_path_on_victim: Path): def write_download_command_to_payload(self, monkey_path_on_victim: PurePath):
monkey_download_command = self.get_monkey_download_command(monkey_path_on_victim) monkey_download_command = self.get_monkey_download_command(monkey_path_on_victim)
self.run_mssql_command(monkey_download_command) self.run_mssql_command(monkey_download_command)
return monkey_download_command return monkey_download_command
def get_monkey_launch_command(self, monkey_path_on_victim: Path): def get_monkey_launch_command(self, monkey_path_on_victim: PurePath):
# Form monkey's launch command # Form monkey's launch command
monkey_args = build_monkey_commandline( monkey_args = build_monkey_commandline(
self.host, self.current_depth - 1, monkey_path_on_victim self.host, self.current_depth - 1, monkey_path_on_victim
@ -187,7 +187,7 @@ class MSSQLExploiter(HostExploiter):
suffix=suffix, suffix=suffix,
) )
def get_monkey_download_command(self, monkey_path_on_victim: Path): def get_monkey_download_command(self, monkey_path_on_victim: PurePath):
monkey_download_command = MSSQLExploiter.MONKEY_DOWNLOAD_COMMAND.format( monkey_download_command = MSSQLExploiter.MONKEY_DOWNLOAD_COMMAND.format(
http_path=self.agent_http_path, dst_path=str(monkey_path_on_victim) http_path=self.agent_http_path, dst_path=str(monkey_path_on_victim)
) )

View File

@ -1,5 +1,5 @@
import logging import logging
from pathlib import Path from pathlib import Path, PurePath
from typing import List, Optional from typing import List, Optional
from infection_monkey.exploit.HostExploiter import HostExploiter from infection_monkey.exploit.HostExploiter import HostExploiter
@ -182,7 +182,7 @@ class PowerShellExploiter(HostExploiter):
f"Failed to execute the agent binary on the victim: {ex}" f"Failed to execute the agent binary on the victim: {ex}"
) )
def _copy_monkey_binary_to_victim(self, monkey_path_on_victim: Path): def _copy_monkey_binary_to_victim(self, monkey_path_on_victim: PurePath):
temp_monkey_binary_filepath = Path(f"./monkey_temp_bin_{get_random_file_suffix()}") temp_monkey_binary_filepath = Path(f"./monkey_temp_bin_{get_random_file_suffix()}")

View File

@ -1,6 +1,6 @@
import abc import abc
import logging import logging
from pathlib import Path from pathlib import Path, PurePath
from typing import Optional from typing import Optional
import pypsrp import pypsrp
@ -64,7 +64,7 @@ class IPowerShellClient(Protocol, metaclass=abc.ABCMeta):
pass pass
@abc.abstractmethod @abc.abstractmethod
def copy_file(self, src: Path, dest: Path) -> bool: def copy_file(self, src: Path, dest: PurePath) -> bool:
pass pass
@abc.abstractmethod @abc.abstractmethod
@ -102,7 +102,7 @@ class PowerShellClient(IPowerShellClient):
output, _, _ = self._client.execute_cmd(cmd) output, _, _ = self._client.execute_cmd(cmd)
return output return output
def copy_file(self, src: Path, dest: Path): def copy_file(self, src: Path, dest: PurePath):
try: try:
self._client.copy(str(src), str(dest)) self._client.copy(str(src), str(dest))
logger.debug(f"Successfully copied {src} to {dest} on {self._ip_addr}") logger.debug(f"Successfully copied {src} to {dest} on {self._ip_addr}")

View File

@ -1,6 +1,6 @@
import io import io
import logging import logging
from pathlib import Path from pathlib import PurePath
import paramiko import paramiko
@ -265,7 +265,7 @@ class SSHExploiter(HostExploiter):
return self.exploit_result return self.exploit_result
def _set_executable_bit_on_agent_binary( def _set_executable_bit_on_agent_binary(
self, ftp: paramiko.sftp_client.SFTPClient, monkey_path_on_victim: Path self, ftp: paramiko.sftp_client.SFTPClient, monkey_path_on_victim: PurePath
): ):
ftp.chmod(str(monkey_path_on_victim), 0o700) ftp.chmod(str(monkey_path_on_victim), 0o700)
self.telemetry_messenger.send_telemetry( self.telemetry_messenger.send_telemetry(

View File

@ -2,7 +2,7 @@ import logging
import ntpath import ntpath
import pprint import pprint
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import PurePath
from typing import Optional from typing import Optional
from impacket.dcerpc.v5 import srvs, transport from impacket.dcerpc.v5 import srvs, transport
@ -22,7 +22,7 @@ class SmbTools(object):
def copy_file( def copy_file(
host, host,
agent_file: BytesIO, agent_file: BytesIO,
dst_path: Path, dst_path: PurePath,
username, username,
password, password,
lm_hash="", lm_hash="",