feat: make environment variables useful.

fix: made config file an environment variable.

fix: reordered and renamed part of the Agent structure.
This commit is contained in:
James Wells 2023-03-30 17:39:04 -07:00
parent 2b0975b30a
commit 8077c66fc9
Signed by: jwells
GPG key ID: 73196D10B8E65666
5 changed files with 50 additions and 27 deletions

View file

@ -34,15 +34,15 @@ func Main(wg *sync.WaitGroup, config gstructs.GagentConfig) {
var err error
if config.CMode {
agent.ScriptCode, err = ioutil.ReadFile(config.Agent)
agent.Script, err = ioutil.ReadFile(config.Agent)
if err != nil {
log.Printf("[ERROR] No such file or directory: %s", config.Agent)
os.Exit(4)
}
log.Printf("[DEBUG] Agent file contents: \n----- -----\n%s\n----- -----\n", agent.ScriptCode)
log.Printf("[DEBUG] Agent file contents: \n----- -----\n%s\n----- -----\n", agent.Script)
}
agent.Client = config.UUID
tmpsum := sha.Sum256([]byte(agent.ScriptCode))
tmpsum := sha.Sum256([]byte(agent.Script))
agent.Shasum = fmt.Sprintf("%v", hex.EncodeToString(tmpsum[:]))
log.Printf("[INFO] SHA256 of Agent file: %s", agent.Shasum)
agent.Status = 1
@ -72,7 +72,7 @@ func getTagsFromHints(agent gstructs.AgentDetails) []string {
// Use named capture groups to extract the hints
re := regexp.MustCompile(`^*set\s+GHINT\s*\[\s*split\s*"(?P<Hints>[^"]+)"\s*,\s*\]`)
res := re.FindStringSubmatch(string(agent.ScriptCode))
res := re.FindStringSubmatch(string(agent.Script))
// If we don't have at least 2 matches, we have no hints
if len(res) < 2 {
@ -108,6 +108,7 @@ func sendAgent(wg *sync.WaitGroup, uuid string, connectString string, agent gstr
}
log.Printf("[DEBUG] Start sending agent...\n")
agent.Status = 2
status, err := sock.SendMessage(agent)
if err != nil {
log.Printf("[ERROR] Failed to send agent to router\n")

View file

@ -111,10 +111,10 @@ type WorkerDetails struct {
}
type AgentDetails struct {
Client string `hcl:"client"`
Shasum string `hcl:"shasum"`
Status int `hcl:"status"`
Hints []string
ScriptCode []byte
Answer []byte
Status byte `hcl:"status"`
Client string `hcl:"client"`
Shasum string `hcl:"shasum"`
Hints []string
Script []byte
Answer []byte
}

View file

@ -105,15 +105,23 @@ LOOP:
/*
* Create listener for client requests
*/
func createClientListener(wg *sync.WaitGroup, config gstructs.GagentConfig) {
func createClientListener(wg *sync.WaitGroup, config gstructs.GagentConfig) error {
defer wg.Done()
clientSock, _ := zmq.NewSocket(zmq.ROUTER)
clientSock, err := zmq.NewSocket(zmq.ROUTER)
if err != nil {
log.Printf("[ERROR] Error creating client socket: %s", err)
return err
}
defer clientSock.Close()
clientListener := "tcp://" + config.ListenAddr + ":" + strconv.Itoa(config.ClientPort)
log.Printf("[DEBUG] Binding to: %s", clientListener)
_ = clientSock.Bind(clientListener)
err = clientSock.Bind(clientListener)
if err != nil {
log.Printf("[ERROR] Error binding client socket: %s", err)
return err
}
for {
msg, err := clientSock.RecvMessage(0)
@ -122,6 +130,7 @@ func createClientListener(wg *sync.WaitGroup, config gstructs.GagentConfig) {
}
log.Printf("[DEBUG] Client message received: %s", msg)
}
return nil
}
func unwrap(msg []string) (head string, tail []string) {