[docs]classTuoniCommand:""" A class that provides data and functionality for a sent command. Attributes: command_id (int): The unique identifier of the command. configuration (dict): The configuration settings for the command. execConf (dict): The execution configuration for the command. created (str): The time, as a string, indicating when the command was created. sent (str): The time, as a string, indicating when the command was sent. result (TuoniResult): The result data returned from the command execution. """def__init__(self,conf,c2):""" Constructor for the command class. Args: conf (dict): Data from the server. c2 (TuoniC2): The related server object that manages communication. """self._load_conf(conf)self.c2=c2def_load_conf(self,conf):self.command_id=conf["id"]self.configuration=conf["configuration"]self.execConf=conf["execConf"]self.created=conf["created"]self.sent=conf["sent"]self.result=conf["result"]
[docs]defreload(self):""" Reload the command data from the C2 server. """ifself.command_idisNone:raiseExceptionTuoniDeleted("")data=self.c2.request_get("/api/v1/commands/%d"%self.command_id)self._load_conf(data)
[docs]defresult_status_done(self,reload=True):""" Check if the command has completed. Args: reload (bool): If True, reload the command data from the server to verify the current status. """ifself.command_idisNone:raiseExceptionTuoniDeleted("")self.reload()ifself.resultisNone:returnFalseifself.result["status"]=="ongoing":returnFalsereturnTrue
[docs]defresult_status_ongoing(self,reload=True):""" Check if the command is still ongoing. Args: reload (bool): If True, reload the command data from the server to verify the current status. """ifself.command_idisNone:raiseExceptionTuoniDeleted("")self.reload()ifself.resultisNone:returnFalseifself.result["status"]=="ongoing":returnTruereturnFalse
[docs]defget_result(self,reload=True):""" Retrieve the result object of the command. Args: reload (bool): If True, reload the command result data from the server. Returns: TuoniResult: The result object containing the data from the command execution. """ifself.command_idisNone:raiseExceptionTuoniDeleted("")self.reload()ifself.resultisNone:returnNonereturnTuoniResult(self.result,self.c2)
[docs]defwait_result(self,interval=1,maxWait=0):""" Wait for the command result object to be available. Args: interval (int): The interval, in seconds, to reload the data from the server. maxWait (int): The maximum time to wait, in seconds, before giving up. A value of 0 means wait indefinitely. Returns: TuoniResult: The result object containing the data from the command execution, if available within the specified time. """self.reload()whileself.resultisNone:time.sleep(interval)self.reload()ifmaxWait>0:maxWait-=intervalifmaxWait<=0:returnNonereturnTuoniResult(self.result,self.c2)