[docs]classTuoniAgent:""" A class that encapsulates the data and functionality of a connected agent. Attributes: guid (GUID): A unique identifier (GUID) assigned to the agent. first_registration_time (str): The time, in string format, when the agent first connected. last_callback_time (str): The time, in string format, of the agent's most recent connection. metadata (dict): A dictionary containing metadata about the agent. active (bool): A boolean flag indicating whether the agent is currently active. recentListeners (list): A list of listeners that the agent uses to maintain its connection. availableCommands (dict): A dictionary of commands that the agent is capable of executing. """def__init__(self,conf,c2):""" Constructor. Attributes: conf (dict): Data from server. c2 (TuoniC2): Related server object. """self.c2=c2self._load_conf(conf)def_load_conf(self,conf):self.guid=conf["guid"]self.first_registration_time=conf["firstRegistrationTime"]self.last_callback_time=conf["lastCallbackTime"]self.metadata=conf["metadata"]self.active=conf["active"]self.recentListeners=conf["recentListeners"]self._fill_available_commands(conf["availableCommandTemplates"])
[docs]defsend_command(self,command_type,command_conf=None,execution_conf=None,files=None):""" Send command to agent. Args: command_type (str | TuoniAlias | TuoniDefaultCommand): What command to send. command_conf (dict): Command configuration. execution_conf (dict): Execution configuration. files (dict): Files to send with command. Returns: TuoniCommand: An object representing the created command. Examples: >>> command1 = agent.send_command(TuoniCommandLs(".\subdir", 2)) >>> command2 = agent.send_command(TuoniCommandProcinfo(), execution_conf ={"execType": "NEW"}) >>> command3 = agent.send_command(TuoniCommandProcinfo(), execution_conf = {"execType": "EXISTING", "pid": 1234}) >>> command4 = agent.send_command("ls", {"dir": ".\subdir", "depth": 2}) >>> command5 = agent.send_command(alias_object, {"conf_value_name": "conf_value"}) """ifself.guidisNone:raiseExceptionTuoniDeleted("")ifisinstance(command_type,TuoniDefaultCommand):command_conf=command_type.command_confexecution_conf=command_type.execution_conffiles=command_type.filescommand_type=command_type.command_typeifisinstance(command_type,TuoniAlias):command_type=command_type.alias_idifcommand_confisNone:command_conf={}data={"template":command_type,"configuration":command_conf}ifexecution_confisnotNone:data["execConf"]=execution_confdata=self.c2.request_post("/api/v1/agents/%s/commands"%self.guid,data,files)returnTuoniCommand(data,self.c2)
[docs]defget_commands(self):""" Retrieves a list of all commands associated with the agent. Returns: list[TuoniCommand]: List of commands sent to the agent. """ifself.guidisNone:raiseExceptionTuoniDeleted("")commands_data=self.c2.request_get("/api/v1/agents/%s/commands"%self.guid)commands=[]forcommand_nrincommands_data:command_data=commands_data[command_nr]command_obj=TuoniCommand(command_data,self.c2)commands.append(command_obj)returncommands