From 2fbf367ba020871e2de46a815bd10ab8466e2f52 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Thu, 8 Oct 2020 14:53:22 -0700 Subject: [PATCH] more helper funcs, add license --- LICENSE | 21 +++++++++++++++++++++ agent/agent_windows.go | 28 ++++++++++++++++++++++++++++ agent/utils.go | 23 +++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b2bc428 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019-present wh1te909 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/agent/agent_windows.go b/agent/agent_windows.go index 2fb00c1..229f6eb 100644 --- a/agent/agent_windows.go +++ b/agent/agent_windows.go @@ -453,6 +453,33 @@ func LoggedOnUser() string { return "None" } +// ForceKillSalt kills all salt related processes +func (a *WindowsAgent) ForceKillSalt() { + pids := make([]int, 0) + + procs, err := ps.Processes() + if err != nil { + return + } + + for _, process := range procs { + p, err := process.Info() + if err != nil { + continue + } + if strings.ToLower(p.Name) == "python.exe" && strings.Contains(strings.ToLower(p.Exe), "salt") { + pids = append(pids, p.PID) + } + } + + for _, pid := range pids { + a.Logger.Debugln("Killing salt process with pid %d", pid) + if err := KillProc(int32(pid)); err != nil { + a.Logger.Debugln(err) + } + } +} + //RecoverSalt recovers the salt minion func (a *WindowsAgent) RecoverSalt() { saltSVC := "salt-minion" @@ -460,6 +487,7 @@ func (a *WindowsAgent) RecoverSalt() { args := []string{"stop", saltSVC} CMDNoOutput(a.Nssm, args, 45) WaitForService(saltSVC, "stopped", 15) + a.ForceKillSalt() args = []string{"flushdns"} CMDNoOutput("ipconfig", args, 15) args = []string{"start", saltSVC} diff --git a/agent/utils.go b/agent/utils.go index 0d3cc96..743a600 100644 --- a/agent/utils.go +++ b/agent/utils.go @@ -13,6 +13,7 @@ import ( ps "github.com/elastic/go-sysinfo" "github.com/go-resty/resty/v2" + "github.com/shirou/gopsutil/process" ) var client = resty.New() @@ -135,3 +136,25 @@ func StripAll(s string) string { s = strings.Trim(s, "\r") return s } + +// KillProc kills a process and its children +func KillProc(pid int32) error { + p, err := process.NewProcess(pid) + if err != nil { + return err + } + + children, err := p.Children() + if err == nil { + for _, child := range children { + if err := child.Kill(); err != nil { + continue + } + } + } + + if err := p.Kill(); err != nil { + return err + } + return nil +}