diff --git a/app/calderacontrol.py b/app/calderacontrol.py index b383dac..a5f89ec 100644 --- a/app/calderacontrol.py +++ b/app/calderacontrol.py @@ -436,7 +436,7 @@ class CalderaControl(): # Link, chain and stuff - def is_operation_finished(self, opid, debug=True): + def is_operation_finished(self, opid, debug=False): """ Checks if an operation finished - finished is not necessary successful ! @param opid: Operation id to check diff --git a/app/machinecontrol.py b/app/machinecontrol.py index 85f35d0..5e84bac 100644 --- a/app/machinecontrol.py +++ b/app/machinecontrol.py @@ -477,9 +477,10 @@ class Machine(): src = os.path.join(self.abs_machinepath_external, "splunkd.go") # sandcat.go local name self.vm_manager.put(src, dst) - cmd = self.__install_caldera_service_cmd().strip() + # cmd = self.__install_caldera_service_cmd().strip() + cmd = self.__wmi_cmd_for_caldera_implant() print(cmd) - self.vm_manager.remote_run(cmd, disown=False) + self.vm_manager.remote_run(cmd, disown=True) if self.get_os() == "linux": dst = self.vm_manager.get_playground() @@ -498,6 +499,20 @@ class Machine(): return self.config.os() + def __wmi_cmd_for_caldera_implant(self): + """ Creates a windows specific command to start the caldera implant in background using wmi """ + + playground = self.vm_manager.get_playground() + if playground: # Workaround for Windows: Can not set target dir for fabric-put in Windows. Only default (none=user) dir available. + playground = playground + "\\" + else: + playground = "%userprofile%\\" + url = "http://" + self.caldera_server + ":8888" + + res = f'wmic process call create "{playground}splunkd.go -server {url} -group {self.config.caldera_group()} -paw {self.config.caldera_paw()}" ' + + return res + def __install_caldera_service_cmd(self): playground = self.vm_manager.get_playground() @@ -547,7 +562,7 @@ START {playground}{filename} -server {url} -group {self.config.caldera_group()} filename = os.path.join(self.abs_machinepath_external, "caldera_agent.bat") with open(filename, "wt") as fh: fh.write(content) - print(f"{CommandlineColors.OKGREEN}Installed Caldera service {CommandlineColors.ENDC}") + print(f"{CommandlineColors.OKGREEN}Installed Caldera server {CommandlineColors.ENDC}") def set_caldera_server(self, server): """ Set the local caldera server config """ diff --git a/plugins/default/vm_controller/vagrant/vagrant_plugin.py b/plugins/default/vm_controller/vagrant/vagrant_plugin.py index 3da058d..bea2bb3 100644 --- a/plugins/default/vm_controller/vagrant/vagrant_plugin.py +++ b/plugins/default/vm_controller/vagrant/vagrant_plugin.py @@ -8,7 +8,9 @@ import vagrant from fabric import Connection import os from app.exceptions import ConfigurationError +from app.exceptions import NetworkError from invoke.exceptions import UnexpectedExit +import paramiko # Experiment with paramiko instead of fabric. Seems fabric has some issues with the "put" command to Windows. There seems no fix (just my workarounds). Maybe paramiko is better. @@ -111,11 +113,21 @@ class VagrantPlugin(MachineryPlugin): print("Vagrant plugin remote run: " + cmd) print("Disown: " + str(disown)) result = None - try: - result = self.c.run(cmd, disown=disown) - print(result) - except UnexpectedExit: - return "Unexpected Exit" + retry = 2 + while retry > 0: + try: + result = self.c.run(cmd, disown=disown) + print(result) + except (paramiko.ssh_exception.NoValidConnectionsError, UnexpectedExit): + if retry <= 0: + raise(NetworkError) + else: + self.disconnect() + self.connect() + retry -= 1 + print("Got some SSH errors. Retrying") + else: + break if result and result.stderr: print("Debug: Stderr: " + str(result.stderr.strip())) @@ -136,12 +148,23 @@ class VagrantPlugin(MachineryPlugin): print(f"{src} -> {dst}") res = "" - try: - res = self.c.put(src, dst) - except UnexpectedExit: - pass - except FileNotFoundError as e: - print(e) + retry = 2 + while retry > 0: + try: + res = self.c.put(src, dst) + except (paramiko.ssh_exception.NoValidConnectionsError, UnexpectedExit): + if retry <= 0: + raise (NetworkError) + else: + self.disconnect() + self.connect() + retry -= 1 + print("Got some SSH errors. Retrying") + except FileNotFoundError as e: + print(e) + break + else: + break return res @@ -153,11 +176,23 @@ class VagrantPlugin(MachineryPlugin): """ self.connect() - res = "" - try: - res = self.c.get(src, dst) - except UnexpectedExit: - pass + retry = 2 + while retry > 0: + try: + res = self.c.get(src, dst) + except (paramiko.ssh_exception.NoValidConnectionsError, UnexpectedExit): + if retry <= 0: + raise (NetworkError) + else: + self.disconnect() + self.connect() + retry -= 1 + print("Got some SSH errors. Retrying") + except FileNotFoundError as e: + print(e) + break + else: + break return res