def Execute(self): if self.CheckHelpRequested(): return if self.CheckMinimunRequiredParameters(2): return requestType = self.request[0] filePath = self.request[1] if requestType != "-r" and requestType != "-p": PrintRedAndLog(f"Unsupported requestType {requestType}") return if requestType == "-p": if filePath not in GetAllPreMadeBatchNames(): PrintRedAndLog(f"No such PreMade batch command: '{filePath}'") return filePath = f"{PreMadeBatchCommandsPath}{filePath}" try: with open(filePath, "r") as file: commandLines = list( filter(lambda a: not a.startswith('#'), list(file.read().splitlines()))) PrintAndLog( f"Adding {len(commandLines)} Commands from {ntpath.basename(filePath)}" ) self.context.CommandQueue.EnqueueCommandsNext(commandLines) file.close() except Exception as ex: print( f"Failed to load command batch file, exception encountered:\n") PrintRedAndLog(ex)
def GetCommandClass(self, request): if not request: PrintRedAndLog("Request cannot be null or empty") return None request = shlex.split(request) if len(request) < 1: PrintRedAndLog("") return None requestedCommand = str.lower(request[0]) if requestedCommand not in CommandMapping: PrintRedAndLog(f"No such supported command '{requestedCommand}'") return None return CommandMapping[requestedCommand](request[1:], self.context)
def GetNewCommands(readSource, command, swaps): swapDictionary = {} for pair in swaps: kvp = str(pair).split("=") if len(kvp) != 2: PrintRedAndLog("Invalid syntax") if readSource == "-p": swapDictionary[kvp[0]] = GetParamArrayFromString(kvp[1]) elif readSource == "-f": swapDictionary[kvp[0]] = GetParamArrayFromFile(kvp[1]) else: PrintRedAndLog(f"No supported read source parameter {readSource}") return return GetCommandsFromSwapDictionary(command, swapDictionary)
def ConnectAndPerformCommands(hostname, port, username, password, commands): try: client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname=hostname, port=port, username=username, password=password) PrintBlueAndLog( f"Successfully established ssh connection to {hostname}") if commands and len(commands) > 0: for command in commands: PrintAndLog(f"Running command remotely: {command}") stdin, stdout, stderr = client.exec_command(command) output = stdout.read().strip().decode().splitlines() responseText = "Command Executed, remote machine response:\n" for line in output: responseText = f"{responseText}\n" \ f" {line}" PrintAndLog(f"{responseText}\n") except Exception as ex: PrintRedAndLog( f"Failed attempting to connect to remote host, exception encountered:\n" f" {ex}\n") finally: PrintBlueAndLog(f"Finished running commands, close connection") client.close()
def RunSubProcess(commandLineInput): try: PrintBlueAndLog(f"Running shell command: {commandLineInput}") result = subprocess.run(commandLineInput, shell=True, check=True, stdout=subprocess.PIPE) PrintAndLog(result.stdout.decode('utf-8')) except Exception as ex: PrintAndLog(f"Failed execute bash command '{commandLineInput}', exception encountered:\n") PrintRedAndLog(ex)
def Execute(self): if self.CheckHelpRequested(): return if self.CheckMinimunRequiredParameters(2): return userType = self.request[0] if userType == "-r": return RunSubProcess(f"sudo useradd {self.request[0]}") if userType == "-a": return RunSubProcess(f"sudo useradd {self.request[0]} sudo") if userType == "-g": if len(self.request) < 2: PrintRedAndLog("Missing required parameters") return RunSubProcess( f"sudo useradd {self.request[0]} -g {self.request[1]}") PrintRedAndLog("Invalid parameter passed") return
def Execute(self): if self.CheckHelpRequested(): return try: if str.lower(GetOperatingSystemName()) != "linux": PrintRedAndLog( "Failed to upgrade AttackAgent. Upgrade currently supported only in Linux" ) return RunSubProcess( "cd /home/AriotsAttackAgent;sudo git checkout -f HEAD;sudo git pull;sudo chmod +x AttackAgent.py;" ) except Exception as ex: PrintAndLog( f"Failed to upgrade AttackAgent. Upgrade currently supported only in Linux. Ensure you have" f"required permissions and the Attack Agent is not running in another process." ) PrintRedAndLog(ex)
def WaitForInput(): if not RunningOnPermittedMachine(): PrintRedAndLog("Not running on non Ariots permitted machine. Exiting") return PrintAndLog( "Hello, Welcome to the Linux based ArIoTS Attack Agent. Type --help at any stage for more information" ) while True: EnqueueAndRun( input("\nPlease enter your next command, type help for options\n"))
def RunCommands(self): while self.CommandQueueNotEmpty(): command = self.DeQueueCommand() try: command.Execute() except Exception as ex: PrintRedAndLog( f"Failed running command, exception encountered:\n\n {ex}\n" ) self.EmptyCommandQueue() return
def Execute(self): if self.CheckHelpRequested(): return if self.CheckMinimunRequiredParameters(2): return requestedTimeType = self.request[0] waitTimeRequested = self.request[1] if requestedTimeType not in TimeConversionsFromSeconds.keys(): PrintRedAndLog( f"Requested wait type '{requestedTimeType}' not valid type") return try: WaitTime = float(waitTimeRequested ) * TimeConversionsFromSeconds[requestedTimeType] except ValueError: PrintRedAndLog("The Wait time entered is not a valid number") return time.sleep(WaitTime)
def Execute(self): if self.CheckHelpRequested(): return if self.CheckMinimunRequiredParameters(2): return deleteType = self.request[0] path = self.request[1] if deleteType == "-f": return RunSubProcess(f"sudo rm {path}") if deleteType == "-d": return RunSubProcess(f"sudo rm -rf {path}") PrintRedAndLog("Invalid parameter passed")
def Execute(self): if self.CheckHelpRequested(): return if self.CheckMinimunRequiredParameters(0): return requestPing = self.request[0] if requestPing == "google": return RunSubProcess("ping -c 4 google.com") if requestPing == "self": return RunSubProcess("ping -c 4 127.0.0.1") if requestPing == "8": return RunSubProcess("ping -c 4 8.8.8.8") if requestPing == "-c" and len(self.request) > 1: return RunSubProcess(f"ping -c 4 {self.request[1]}") PrintRedAndLog("Invalid or missing parameters")
def Execute(self): if self.CheckHelpRequested(): return if self.CheckMinimunRequiredParameters(3): return createType = self.request[0] originPath = self.request[1] destinationPath = self.request[2] if createType == "-f": return RunSubProcess(f"sudo cp {originPath} {destinationPath}") if createType == "-d": return RunSubProcess( f"sudo cp -avr {originPath} {destinationPath}") PrintRedAndLog("Invalid parameter passed")
def Execute(self): if self.CheckHelpRequested(): return if self.CheckMinimunRequiredParameters(2): return createType = self.request[0] path = self.request[1] if createType == "-f": return RunSubProcess(f"sudo touch {path}") if createType == "-f-bus": return RunSubProcess(f"sudo touch d-bus {path}") if createType == "-d": return RunSubProcess(f"sudo mkdir -p {path}") PrintRedAndLog("Invalid parameter passed")
def Execute(self): if self.CheckHelpRequested(): return if self.CheckMinimunRequiredParameters(2): return killType = self.request[0] killName = self.request[1] if killType == "-p": return RunSubProcess(f"sudo kill -9 $(lsof -t -i:{killName})") if killType == "-n": return RunSubProcess(f"sudo killall -9 {killName}") if killType == "-s": return RunSubProcess(f"sudo pkill -9 {killName}") if killType == "-i": return RunSubProcess(f"sudo kill -9 {killName}") return PrintRedAndLog(f"Invalid parameter killType: {killType} passed")
def GetParamArrayFromFile(path): try: with open(path, "r") as file: parameters = list(file.read().splitlines()) PrintAndLog( f"Adding {len(parameters)} commands using commands from {path}" ) file.close() splitParameters = [] for param in parameters: if "," in param: splitParameters.extend(param.split(",")) continue splitParameters.append(param) return splitParameters except Exception as ex: print(f"Failed to load command batch file, exception encountered:\n") PrintRedAndLog(ex)
def Execute(self): if self.CheckHelpRequested(): return if self.CheckMinimunRequiredParameters(2): return iterations = self.request[0] command = ' '.join(self.request[1:]) try: commandsList = [] intIterations = int(iterations) for i in range(intIterations): commandsList.append(command) self.context.CommandQueue.EnqueueCommandsNext(commandsList) except Exception as ex: PrintAndLog( f"Failed to execute Loop on repetitions: {iterations}, command: {command}:\n" ) PrintRedAndLog(ex) return
CreatePublicEndpointMap() def EnqueueAndRun(request): context = GetContext() context.CommandQueue.EnqueueCommand(request) context.CommandQueue.RunCommands() def WaitForInput(): if not RunningOnPermittedMachine(): PrintRedAndLog("Not running on non Ariots permitted machine. Exiting") return PrintAndLog( "Hello, Welcome to the Linux based ArIoTS Attack Agent. Type --help at any stage for more information" ) while True: EnqueueAndRun( input("\nPlease enter your next command, type help for options\n")) if len(sys.argv) > 1: if RunningOnPermittedMachine(): WriteToLog(f"User input: {sys.argv}") EnqueueAndRun(" ".join(sys.argv[1:])) else: PrintRedAndLog("Not running on non Ariots permitted machine. Exiting") elif __name__ == '__main__': WaitForInput()
def CheckMinimunRequiredParameters(self, min): if not self.request or len(self.request) < min: PrintRedAndLog("Missing required parameters") return True return False