add env to scripts

This commit is contained in:
wh1te909 2022-11-25 00:23:31 -08:00
parent 9565fea27c
commit 6ac14b6d64
6 changed files with 17 additions and 9 deletions

View File

@ -82,7 +82,7 @@ func NewAgentConfig() *rmm.AgentConfig {
}
}
func (a *Agent) RunScript(code string, shell string, args []string, timeout int, runasuser bool) (stdout, stderr string, exitcode int, e error) {
func (a *Agent) RunScript(code string, shell string, args []string, timeout int, runasuser bool, envs []string) (stdout, stderr string, exitcode int, e error) {
content := []byte(code)
@ -158,6 +158,11 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
cmd.Stdout = &outb
cmd.Stderr = &errb
if len(envs) > 0 {
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, envs...)
}
if cmdErr := cmd.Start(); cmdErr != nil {
a.Logger.Debugln(cmdErr)
return "", cmdErr.Error(), 65, cmdErr

View File

@ -169,7 +169,7 @@ type ScriptCheckResult struct {
// ScriptCheck runs either bat, powershell or python script
func (a *Agent) ScriptCheck(data rmm.Check, r *resty.Client) {
start := time.Now()
stdout, stderr, retcode, _ := a.RunScript(data.Script.Code, data.Script.Shell, data.ScriptArgs, data.Timeout, data.Script.RunAsUser)
stdout, stderr, retcode, _ := a.RunScript(data.Script.Code, data.Script.Shell, data.ScriptArgs, data.Timeout, data.Script.RunAsUser, data.Script.Env)
payload := ScriptCheckResult{
ID: data.CheckPK,

View File

@ -42,7 +42,7 @@ func (a *Agent) InstallChoco() {
return
}
_, _, exitcode, err := a.RunScript(string(r.Body()), "powershell", []string{}, 900, false)
_, _, exitcode, err := a.RunScript(string(r.Body()), "powershell", []string{}, 900, false, []string{})
if err != nil {
a.Logger.Debugln(err)
a.rClient.R().SetBody(result).Post(url)

View File

@ -41,6 +41,7 @@ type NatsMsg struct {
ID int `json:"id"`
Code string `json:"code"`
RunAsUser bool `json:"run_as_user"`
Env []string `json:"env"`
}
var (
@ -261,7 +262,7 @@ func (a *Agent) RunRPC() {
var resultData rmm.RunScriptResp
ret := codec.NewEncoderBytes(&resp, new(codec.MsgpackHandle))
start := time.Now()
stdout, stderr, retcode, err := a.RunScript(p.Data["code"], p.Data["shell"], p.ScriptArgs, p.Timeout, p.RunAsUser)
stdout, stderr, retcode, err := a.RunScript(p.Data["code"], p.Data["shell"], p.ScriptArgs, p.Timeout, p.RunAsUser, p.Env)
resultData.ExecTime = time.Since(start).Seconds()
resultData.ID = p.ID
@ -291,7 +292,7 @@ func (a *Agent) RunRPC() {
var retData rmm.RunScriptResp
ret := codec.NewEncoderBytes(&resp, new(codec.MsgpackHandle))
start := time.Now()
stdout, stderr, retcode, err := a.RunScript(p.Data["code"], p.Data["shell"], p.ScriptArgs, p.Timeout, p.RunAsUser)
stdout, stderr, retcode, err := a.RunScript(p.Data["code"], p.Data["shell"], p.ScriptArgs, p.Timeout, p.RunAsUser, p.Env)
retData.ExecTime = time.Since(start).Seconds()
if err != nil {

View File

@ -59,7 +59,7 @@ func (a *Agent) RunTask(id int) error {
action_start := time.Now()
if action.ActionType == "script" {
stdout, stderr, retcode, err := a.RunScript(action.Code, action.Shell, action.Args, action.Timeout, action.RunAsUser)
stdout, stderr, retcode, err := a.RunScript(action.Code, action.Shell, action.Args, action.Timeout, action.RunAsUser, action.Env)
if err != nil {
a.Logger.Debugln(err)

View File

@ -144,6 +144,7 @@ type Script struct {
Shell string `json:"shell"`
Code string `json:"code"`
RunAsUser bool `json:"run_as_user"`
Env []string `json:"env"`
}
type CheckInfo struct {
@ -190,6 +191,7 @@ type TaskAction struct {
Args []string `json:"script_args"`
Timeout int `json:"timeout"`
RunAsUser bool `json:"run_as_user"`
Env []string `json:"env"`
}
type AutomatedTask struct {