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 {
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)
}
}

View File

@ -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
}