From c3e33a6def6ee4d12bfa88e814c53d436d674a8f Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Sun, 11 Dec 2022 23:50:00 -0800 Subject: [PATCH] fix path on exchange servers fixes amidaware/tacticalrmm#1359 --- agent/agent_windows.go | 14 ++++++++------ agent/utils.go | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/agent/agent_windows.go b/agent/agent_windows.go index f211987..b19a185 100644 --- a/agent/agent_windows.go +++ b/agent/agent_windows.go @@ -132,7 +132,7 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int, switch shell { case "powershell": - exe = "Powershell" + exe = getPowershellExe() cmdArgs = []string{"-NonInteractive", "-NoProfile", "-ExecutionPolicy", "Bypass", tmpfn.Name()} case "python": exe = a.PyBin @@ -260,23 +260,25 @@ func CMDShell(shell string, cmdArgs []string, command string, timeout int, detac defer cancel() sysProcAttr := &windows.SysProcAttr{} + cmdExe := getCMDExe() + powershell := getPowershellExe() if len(cmdArgs) > 0 && command == "" { switch shell { case "cmd": cmdArgs = append([]string{"/C"}, cmdArgs...) - cmd = exec.Command("cmd.exe", cmdArgs...) + cmd = exec.Command(cmdExe, cmdArgs...) case "powershell": cmdArgs = append([]string{"-NonInteractive", "-NoProfile"}, cmdArgs...) - cmd = exec.Command("powershell.exe", cmdArgs...) + cmd = exec.Command(powershell, cmdArgs...) } } else { switch shell { case "cmd": - cmd = exec.Command("cmd.exe") - sysProcAttr.CmdLine = fmt.Sprintf("cmd.exe /C %s", command) + cmd = exec.Command(cmdExe) + sysProcAttr.CmdLine = fmt.Sprintf("%s /C %s", cmdExe, command) case "powershell": - cmd = exec.Command("Powershell", "-NonInteractive", "-NoProfile", command) + cmd = exec.Command(powershell, "-NonInteractive", "-NoProfile", command) } } diff --git a/agent/utils.go b/agent/utils.go index 3e84288..093fa1b 100644 --- a/agent/utils.go +++ b/agent/utils.go @@ -20,6 +20,7 @@ import ( "math/rand" "net" "os" + "os/exec" "path/filepath" "runtime" goDebug "runtime/debug" @@ -339,3 +340,19 @@ func regRangeToInt(s string) int { max, _ := strconv.Atoi(split[1]) return randRange(min, max) } + +func getPowershellExe() string { + powershell, err := exec.LookPath("powershell.exe") + if err != nil || powershell == "" { + return filepath.Join(os.Getenv("WINDIR"), `System32\WindowsPowerShell\v1.0\powershell.exe`) + } + return powershell +} + +func getCMDExe() string { + cmdExe, err := exec.LookPath("cmd.exe") + if err != nil || cmdExe == "" { + return filepath.Join(os.Getenv("WINDIR"), `System32\cmd.exe`) + } + return cmdExe +}