fix path on exchange servers fixes amidaware/tacticalrmm#1359

This commit is contained in:
wh1te909 2022-12-11 23:50:00 -08:00
parent 293151ea0a
commit c3e33a6def
2 changed files with 25 additions and 6 deletions

View File

@ -132,7 +132,7 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
switch shell { switch shell {
case "powershell": case "powershell":
exe = "Powershell" exe = getPowershellExe()
cmdArgs = []string{"-NonInteractive", "-NoProfile", "-ExecutionPolicy", "Bypass", tmpfn.Name()} cmdArgs = []string{"-NonInteractive", "-NoProfile", "-ExecutionPolicy", "Bypass", tmpfn.Name()}
case "python": case "python":
exe = a.PyBin exe = a.PyBin
@ -260,23 +260,25 @@ func CMDShell(shell string, cmdArgs []string, command string, timeout int, detac
defer cancel() defer cancel()
sysProcAttr := &windows.SysProcAttr{} sysProcAttr := &windows.SysProcAttr{}
cmdExe := getCMDExe()
powershell := getPowershellExe()
if len(cmdArgs) > 0 && command == "" { if len(cmdArgs) > 0 && command == "" {
switch shell { switch shell {
case "cmd": case "cmd":
cmdArgs = append([]string{"/C"}, cmdArgs...) cmdArgs = append([]string{"/C"}, cmdArgs...)
cmd = exec.Command("cmd.exe", cmdArgs...) cmd = exec.Command(cmdExe, cmdArgs...)
case "powershell": case "powershell":
cmdArgs = append([]string{"-NonInteractive", "-NoProfile"}, cmdArgs...) cmdArgs = append([]string{"-NonInteractive", "-NoProfile"}, cmdArgs...)
cmd = exec.Command("powershell.exe", cmdArgs...) cmd = exec.Command(powershell, cmdArgs...)
} }
} else { } else {
switch shell { switch shell {
case "cmd": case "cmd":
cmd = exec.Command("cmd.exe") cmd = exec.Command(cmdExe)
sysProcAttr.CmdLine = fmt.Sprintf("cmd.exe /C %s", command) sysProcAttr.CmdLine = fmt.Sprintf("%s /C %s", cmdExe, command)
case "powershell": case "powershell":
cmd = exec.Command("Powershell", "-NonInteractive", "-NoProfile", command) cmd = exec.Command(powershell, "-NonInteractive", "-NoProfile", command)
} }
} }

View File

@ -20,6 +20,7 @@ import (
"math/rand" "math/rand"
"net" "net"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
goDebug "runtime/debug" goDebug "runtime/debug"
@ -339,3 +340,19 @@ func regRangeToInt(s string) int {
max, _ := strconv.Atoi(split[1]) max, _ := strconv.Atoi(split[1])
return randRange(min, max) 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
}