add chocolatey

This commit is contained in:
wh1te909 2021-01-19 14:56:55 -08:00
parent 1b85ccfc67
commit cebde22fa0
3 changed files with 89 additions and 9 deletions

62
agent/choco_windows.go Normal file
View File

@ -0,0 +1,62 @@
package agent
import (
"time"
"github.com/go-resty/resty/v2"
nats "github.com/nats-io/nats.go"
"github.com/ugorji/go/codec"
rmm "github.com/wh1te909/rmmagent/shared"
)
func (a *WindowsAgent) InstallChoco(nc *nats.Conn) {
var resp []byte
var result rmm.ChocoInstalled
result.AgentID = a.AgentID
result.Installed = false
ret := codec.NewEncoderBytes(&resp, new(codec.MsgpackHandle))
rClient := resty.New()
rClient.SetTimeout(30 * time.Second)
r, err := rClient.R().Get("https://chocolatey.org/install.ps1")
if err != nil {
ret.Encode(result)
nc.PublishRequest(a.AgentID, "chocoinstall", resp)
return
}
if r.IsError() {
ret.Encode(result)
nc.PublishRequest(a.AgentID, "chocoinstall", resp)
return
}
_, _, exitcode, err := a.RunScript(string(r.Body()), "powershell", []string{}, 900)
if err != nil {
ret.Encode(result)
nc.PublishRequest(a.AgentID, "chocoinstall", resp)
return
}
if exitcode != 0 {
ret.Encode(result)
nc.PublishRequest(a.AgentID, "chocoinstall", resp)
return
}
result.Installed = true
ret.Encode(result)
nc.PublishRequest(a.AgentID, "chocoinstall", resp)
}
func (a *WindowsAgent) InstallWithChoco(name, version string) (string, error) {
out, err := CMD("choco.exe", []string{"install", name, "--version", version, "--yes"}, 900, false)
if err != nil {
a.Logger.Errorln(err)
return err.Error(), err
}
if out[1] != "" {
return out[1], nil
}
return out[0], nil
}

View File

@ -13,15 +13,17 @@ import (
)
type NatsMsg struct {
Func string `json:"func"`
Timeout int `json:"timeout"`
Data map[string]string `json:"payload"`
ScriptArgs []string `json:"script_args"`
ProcPID int32 `json:"procpid"`
TaskPK int `json:"taskpk"`
ScheduledTask SchedTask `json:"schedtaskpayload"`
RecoveryCommand string `json:"recoverycommand"`
UpdateGUIDs []string `json:"guids"`
Func string `json:"func"`
Timeout int `json:"timeout"`
Data map[string]string `json:"payload"`
ScriptArgs []string `json:"script_args"`
ProcPID int32 `json:"procpid"`
TaskPK int `json:"taskpk"`
ScheduledTask SchedTask `json:"schedtaskpayload"`
RecoveryCommand string `json:"recoverycommand"`
UpdateGUIDs []string `json:"guids"`
ChocoProgName string `json:"choco_prog_name"`
ChocoProgVersion string `json:"choco_prog_ver"`
}
var (
@ -340,6 +342,17 @@ func (a *WindowsAgent) RunRPC() {
go func() {
CMD(a.EXE, []string{"-m", "installsalt"}, 3600, true)
}()
case "installchoco":
go a.InstallChoco(nc)
case "installwithchoco":
go func(p *NatsMsg) {
var resp []byte
ret := codec.NewEncoderBytes(&resp, new(codec.MsgpackHandle))
msg.Respond(resp)
out, _ := a.InstallWithChoco(p.ChocoProgName, p.ChocoProgVersion)
ret.Encode(out)
msg.Respond(resp)
}(payload)
case "getwinupdates":
go func() {
if !atomic.CompareAndSwapUint32(&getWinUpdateLocker, 0, 1) {

View File

@ -33,6 +33,11 @@ type AgentNeedsReboot struct {
NeedsReboot bool `json:"needs_reboot"`
}
type ChocoInstalled struct {
AgentID string `json:"agent_id"`
Installed bool `json:"installed"`
}
// WindowsService holds windows service info
type WindowsService struct {
Name string `json:"name"`