2020-10-05 21:35:16 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
//HelloPost post
|
|
|
|
type HelloPost struct {
|
|
|
|
Agentid string `json:"agent_id"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
OS string `json:"operating_system"`
|
|
|
|
TotalRAM float64 `json:"total_ram"`
|
|
|
|
Platform string `json:"plat"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
BootTime int64 `json:"boot_time"`
|
|
|
|
SaltVersion string `json:"salt_ver"`
|
|
|
|
}
|
|
|
|
|
|
|
|
//HelloPatch patch
|
|
|
|
type HelloPatch struct {
|
|
|
|
Agentid string `json:"agent_id"`
|
|
|
|
Services []WindowsService `json:"services"`
|
|
|
|
PublicIP string `json:"public_ip"`
|
2020-10-07 05:27:35 +00:00
|
|
|
Disks []Disk `json:"disks"`
|
|
|
|
Username string `json:"logged_in_username"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
BootTime int64 `json:"boot_time"`
|
2020-10-05 21:35:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 03:34:41 +00:00
|
|
|
// WinAgentSvc tacticalagent windows nssm service
|
|
|
|
func (a *WindowsAgent) WinAgentSvc() {
|
2020-10-05 21:35:16 +00:00
|
|
|
a.Logger.Infoln("Agent service started")
|
2020-10-17 22:33:32 +00:00
|
|
|
a.CleanupPythonAgent()
|
2020-10-05 21:35:16 +00:00
|
|
|
var data map[string]interface{}
|
|
|
|
var sleep int
|
|
|
|
|
2020-10-18 05:28:20 +00:00
|
|
|
url := a.Server + "/api/v3/hello/"
|
2020-10-10 06:36:40 +00:00
|
|
|
req := &APIRequest{
|
|
|
|
URL: url,
|
|
|
|
Headers: a.Headers,
|
|
|
|
Timeout: 15,
|
|
|
|
LocalCert: a.DB.Cert,
|
|
|
|
Debug: a.Debug,
|
|
|
|
}
|
|
|
|
|
2020-10-05 21:35:16 +00:00
|
|
|
plat, osinfo := OSInfo()
|
|
|
|
|
|
|
|
postPayload := HelloPost{
|
|
|
|
Agentid: a.AgentID,
|
|
|
|
Hostname: a.Hostname,
|
|
|
|
OS: osinfo,
|
|
|
|
TotalRAM: TotalRAM(),
|
|
|
|
Platform: plat,
|
|
|
|
Version: a.Version,
|
|
|
|
BootTime: BootTime(),
|
|
|
|
SaltVersion: a.GetProgramVersion("salt minion"),
|
|
|
|
}
|
|
|
|
|
2020-10-10 06:36:40 +00:00
|
|
|
req.Method = "POST"
|
|
|
|
req.Payload = postPayload
|
|
|
|
a.Logger.Debugln(req)
|
2020-10-05 21:35:16 +00:00
|
|
|
|
2020-10-10 06:36:40 +00:00
|
|
|
_, err := req.MakeRequest()
|
2020-10-05 21:35:16 +00:00
|
|
|
if err != nil {
|
|
|
|
a.Logger.Debugln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
for {
|
|
|
|
patchPayload := HelloPatch{
|
|
|
|
Agentid: a.AgentID,
|
|
|
|
Services: a.GetServices(),
|
2020-10-07 06:39:42 +00:00
|
|
|
PublicIP: PublicIP(),
|
2020-10-07 05:27:35 +00:00
|
|
|
Disks: a.GetDisks(),
|
2020-10-07 09:15:53 +00:00
|
|
|
Username: LoggedOnUser(),
|
2020-10-05 21:35:16 +00:00
|
|
|
Version: a.Version,
|
|
|
|
BootTime: BootTime(),
|
|
|
|
}
|
|
|
|
|
2020-10-10 06:36:40 +00:00
|
|
|
req.Method = "PATCH"
|
|
|
|
req.Payload = patchPayload
|
|
|
|
a.Logger.Debugln(req)
|
2020-10-05 21:35:16 +00:00
|
|
|
|
2020-10-10 06:36:40 +00:00
|
|
|
r, err := req.MakeRequest()
|
2020-10-05 21:35:16 +00:00
|
|
|
if err != nil {
|
|
|
|
a.Logger.Debugln(err)
|
|
|
|
} else {
|
2020-10-08 23:11:42 +00:00
|
|
|
ret := DjangoStringResp(r.String())
|
2020-10-05 21:35:16 +00:00
|
|
|
if len(ret) > 0 && ret != "ok" {
|
|
|
|
if err := json.Unmarshal(r.Body(), &data); err != nil {
|
|
|
|
a.Logger.Debugln(err)
|
|
|
|
} else {
|
|
|
|
if action, ok := data["recovery"].(string); ok {
|
|
|
|
switch action {
|
|
|
|
case "salt":
|
|
|
|
go a.RecoverSalt()
|
|
|
|
case "mesh":
|
|
|
|
go a.RecoverMesh()
|
|
|
|
case "command":
|
|
|
|
if cmd, ok := data["cmd"].(string); ok {
|
|
|
|
go a.RecoverCMD(cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sleep = randRange(30, 120)
|
|
|
|
time.Sleep(time.Duration(sleep) * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randRange(min, max int) int {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
return rand.Intn(max-min) + min
|
|
|
|
}
|