Debian Codegraph @

modules/c2/c2.go

raw symbols SCIP
1 package c2
2
3 import (
4 "bytes"
5 "crypto/tls"
6 "fmt"
7 "github.com/acarl005/stripansi"
8 "github.com/bettercap/bettercap/modules/events_stream"
9 "github.com/bettercap/bettercap/session"
10 "github.com/evilsocket/islazy/log"
11 "github.com/evilsocket/islazy/str"
12 irc "github.com/thoj/go-ircevent"
13 "strings"
14 "text/template"
15 )
16
17 type settings struct {
18 server string
19 tls bool
20 tlsVerify bool
21 nick string
22 user string
23 password string
24 saslUser string
25 saslPassword string
26 operator string
27 controlChannel string
28 eventsChannel string
29 outputChannel string
30 }
31
32 type C2 struct {
33 session.SessionModule
34
35 settings settings
36 stream *events_stream.EventsStream
37 templates map[string]*template.Template
38 channels map[string]string
39 client *irc.Connection
40 eventBus session.EventBus
41 quit chan bool
42 }
43
44 type eventContext struct {
45 Session *session.Session
46 Event session.Event
47 }
48
49 func NewC2(s *session.Session) *C2 {
50 mod := &C2{
51 SessionModule: session.NewSessionModule("c2", s),
52 stream: events_stream.NewEventsStream(s),
53 templates: make(map[string]*template.Template),
54 channels: make(map[string]string),
55 quit: make(chan bool),
56 settings: settings{
57 server: "localhost:6697",
58 tls: true,
59 tlsVerify: false,
60 nick: "bettercap",
61 user: "bettercap",
62 password: "password",
63 operator: "admin",
64 eventsChannel: "#events",
65 outputChannel: "#events",
66 controlChannel: "#events",
67 },
68 }
69
70 mod.AddParam(session.NewStringParameter("c2.server",
71 mod.settings.server,
72 "",
73 "IRC server address and port."))
74
75 mod.AddParam(session.NewBoolParameter("c2.server.tls",
76 "true",
77 "Enable TLS."))
78
79 mod.AddParam(session.NewBoolParameter("c2.server.tls.verify",
80 "false",
81 "Enable TLS certificate validation."))
82
83 mod.AddParam(session.NewStringParameter("c2.operator",
84 mod.settings.operator,
85 "",
86 "IRC nickname of the user allowed to run commands."))
87
88 mod.AddParam(session.NewStringParameter("c2.nick",
89 mod.settings.nick,
90 "",
91 "IRC nickname."))
92
93 mod.AddParam(session.NewStringParameter("c2.username",
94 mod.settings.user,
95 "",
96 "IRC username."))
97
98 mod.AddParam(session.NewStringParameter("c2.password",
99 mod.settings.password,
100 "",
101 "IRC server password."))
102
103 mod.AddParam(session.NewStringParameter("c2.sasl.username",
104 mod.settings.saslUser,
105 "",
106 "IRC SASL username."))
107
108 mod.AddParam(session.NewStringParameter("c2.sasl.password",
109 mod.settings.saslPassword,
110 "",
111 "IRC server SASL password."))
112
113 mod.AddParam(session.NewStringParameter("c2.channel.output",
114 mod.settings.outputChannel,
115 "",
116 "IRC channel to send commands output to."))
117
118 mod.AddParam(session.NewStringParameter("c2.channel.events",
119 mod.settings.eventsChannel,
120 "",
121 "IRC channel to send events to."))
122
123 mod.AddParam(session.NewStringParameter("c2.channel.control",
124 mod.settings.controlChannel,
125 "",
126 "IRC channel to receive commands from."))
127
128 mod.AddHandler(session.NewModuleHandler("c2 on", "",
129 "Start the C2 module.",
130 func(args []string) error {
131 return mod.Start()
132 }))
133
134 mod.AddHandler(session.NewModuleHandler("c2 off", "",
135 "Stop the C2 module.",
136 func(args []string) error {
137 return mod.Stop()
138 }))
139
140 mod.AddHandler(session.NewModuleHandler("c2.channel.set EVENT_TYPE CHANNEL",
141 "c2.channel.set ([^\\s]+) (.+)",
142 "Set a specific channel to report events of this type.",
143 func(args []string) error {
144 eventType := args[0]
145 channel := args[1]
146
147 mod.Debug("setting channel for event %s: %v", eventType, channel)
148 mod.channels[eventType] = channel
149 return nil
150 }))
151
152 mod.AddHandler(session.NewModuleHandler("c2.channel.clear EVENT_TYPE",
153 "c2.channel.clear ([^\\s]+)",
154 "Clear the channel to use for a specific event type.",
155 func(args []string) error {
156 eventType := args[0]
157 if _, found := mod.channels[args[0]]; found {
158 delete(mod.channels, eventType)
159 mod.Debug("cleared channel for %s", eventType)
160 } else {
161 return fmt.Errorf("channel for event %s not set", args[0])
162 }
163 return nil
164 }))
165
166 mod.AddHandler(session.NewModuleHandler("c2.template.set EVENT_TYPE TEMPLATE",
167 "c2.template.set ([^\\s]+) (.+)",
168 "Set the reporting template to use for a specific event type.",
169 func(args []string) error {
170 eventType := args[0]
171 eventTemplate := args[1]
172
173 parsed, err := template.New(eventType).Parse(eventTemplate)
174 if err != nil {
175 return err
176 }
177
178 mod.Debug("setting template for event %s: %v", eventType, parsed)
179 mod.templates[eventType] = parsed
180 return nil
181 }))
182
183 mod.AddHandler(session.NewModuleHandler("c2.template.clear EVENT_TYPE",
184 "c2.template.clear ([^\\s]+)",
185 "Clear the reporting template to use for a specific event type.",
186 func(args []string) error {
187 eventType := args[0]
188 if _, found := mod.templates[args[0]]; found {
189 delete(mod.templates, eventType)
190 mod.Debug("cleared template for %s", eventType)
191 } else {
192 return fmt.Errorf("template for event %s not set", args[0])
193 }
194 return nil
195 }))
196
197 mod.Session.Events.OnPrint(mod.onPrint)
198
199 return mod
200 }
201
202 func (mod *C2) Name() string {
203 return "c2"
204 }
205
206 func (mod *C2) Description() string {
207 return "A CnC module that connects to an IRC server for reporting and commands."
208 }
209
210 func (mod *C2) Author() string {
211 return "Simone Margaritelli <[email protected]>"
212 }
213
214 func (mod *C2) Configure() (err error) {
215 if mod.Running() {
216 return session.ErrAlreadyStarted(mod.Name())
217 }
218
219 if err, mod.settings.server = mod.StringParam("c2.server"); err != nil {
220 return err
221 } else if err, mod.settings.tls = mod.BoolParam("c2.server.tls"); err != nil {
222 return err
223 } else if err, mod.settings.tlsVerify = mod.BoolParam("c2.server.tls.verify"); err != nil {
224 return err
225 } else if err, mod.settings.nick = mod.StringParam("c2.nick"); err != nil {
226 return err
227 } else if err, mod.settings.user = mod.StringParam("c2.username"); err != nil {
228 return err
229 } else if err, mod.settings.password = mod.StringParam("c2.password"); err != nil {
230 return err
231 } else if err, mod.settings.saslUser = mod.StringParam("c2.sasl.username"); err != nil {
232 return err
233 } else if err, mod.settings.saslPassword = mod.StringParam("c2.sasl.password"); err != nil {
234 return err
235 } else if err, mod.settings.operator = mod.StringParam("c2.operator"); err != nil {
236 return err
237 } else if err, mod.settings.eventsChannel = mod.StringParam("c2.channel.events"); err != nil {
238 return err
239 } else if err, mod.settings.controlChannel = mod.StringParam("c2.channel.control"); err != nil {
240 return err
241 } else if err, mod.settings.outputChannel = mod.StringParam("c2.channel.output"); err != nil {
242 return err
243 }
244
245 mod.eventBus = mod.Session.Events.Listen()
246
247 mod.client = irc.IRC(mod.settings.nick, mod.settings.user)
248
249 if log.Level == log.DEBUG {
250 mod.client.VerboseCallbackHandler = true
251 mod.client.Debug = true
252 }
253
254 mod.client.Password = mod.settings.password
255 mod.client.UseTLS = mod.settings.tls
256 mod.client.TLSConfig = &tls.Config{
257 InsecureSkipVerify: !mod.settings.tlsVerify,
258 }
259
260 if mod.settings.saslUser != "" || mod.settings.saslPassword != "" {
261 mod.client.SASLLogin = mod.settings.saslUser
262 mod.client.SASLPassword = mod.settings.saslPassword
263 mod.client.UseSASL = true
264 }
265
266 mod.client.AddCallback("PRIVMSG", func(event *irc.Event) {
267 channel := event.Arguments[0]
268 message := event.Message()
269 from := event.Nick
270
271 if from != mod.settings.operator {
272 mod.client.Privmsg(event.Nick, "nope")
273 return
274 }
275
276 if channel != mod.settings.controlChannel && channel != mod.settings.nick {
277 mod.Debug("from:%s on:%s - '%s'", from, channel, message)
278 return
279 }
280
281 mod.Debug("from:%s on:%s - '%s'", from, channel, message)
282
283 parts := strings.SplitN(message, " ", 2)
284 cmd := parts[0]
285 args := ""
286 if len(parts) > 1 {
287 args = parts[1]
288 }
289
290 if cmd == "join" {
291 mod.client.Join(args)
292 } else if cmd == "part" {
293 mod.client.Part(args)
294 } else if cmd == "nick" {
295 mod.client.Nick(args)
296 } else if err = mod.Session.Run(message); err == nil {
297
298 } else {
299 mod.client.Privmsgf(event.Nick, "error: %v", stripansi.Strip(err.Error()))
300 }
301 })
302
303 mod.client.AddCallback("001", func(e *irc.Event) {
304 mod.Debug("got 101")
305 mod.client.Join(mod.settings.controlChannel)
306 mod.client.Join(mod.settings.outputChannel)
307 mod.client.Join(mod.settings.eventsChannel)
308 })
309
310 return mod.client.Connect(mod.settings.server)
311 }
312
313 func (mod *C2) onPrint(format string, args ...interface{}) {
314 if !mod.Running() {
315 return
316 }
317
318 msg := stripansi.Strip(str.Trim(fmt.Sprintf(format, args...)))
319
320 for _, line := range strings.Split(msg, "\n") {
321 mod.client.Privmsg(mod.settings.outputChannel, line)
322 }
323 }
324
325 func (mod *C2) onEvent(e session.Event) {
326 if mod.Session.EventsIgnoreList.Ignored(e) {
327 return
328 }
329
330 // default channel or event specific channel?
331 channel := mod.settings.eventsChannel
332 if custom, found := mod.channels[e.Tag]; found {
333 channel = custom
334 }
335
336 var out bytes.Buffer
337 if tpl, found := mod.templates[e.Tag]; found {
338 // use a custom template to render this event
339 if err := tpl.Execute(&out, eventContext{
340 Session: mod.Session,
341 Event: e,
342 }); err != nil {
343 fmt.Fprintf(&out, "%v", err)
344 }
345 } else {
346 // use the default view to render this event
347 mod.stream.Render(&out, e)
348 }
349
350 // make sure colors and in general bash escape sequences are removed
351 msg := stripansi.Strip(str.Trim(string(out.Bytes())))
352
353 mod.client.Privmsg(channel, msg)
354 }
355
356 func (mod *C2) Start() error {
357 if err := mod.Configure(); err != nil {
358 return err
359 }
360
361 return mod.SetRunning(true, func() {
362 mod.Info("started")
363
364 for mod.Running() {
365 var e session.Event
366 select {
367 case e = <-mod.eventBus:
368 mod.onEvent(e)
369
370 case <-mod.quit:
371 mod.Debug("got quit")
372 return
373 }
374 }
375 })
376 }
377
378 func (mod *C2) Stop() error {
379 return mod.SetRunning(false, func() {
380 mod.quit <- true
381 mod.Session.Events.Unlisten(mod.eventBus)
382 mod.client.Quit()
383 mod.client.Disconnect()
384 })
385 }