-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
651 lines (599 loc) · 14.9 KB
/
config.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/DSU-DefSec/DWAYNE-INATOR-5000/checks"
)
var (
configErrors = []string{}
)
type config struct {
Event string
Verbose bool
NoPasswords bool
EasyPCR bool
Port int
Https bool
Cert string
Key string
Timezone string
StartPaused bool
DisableInfoPage bool
DisableHeadToHead bool
DisableExternalPorts bool
Uptime bool // Score agent callback uptime (like CCS uptime)
UptimeSLA int // Number in minutes
Persists bool // Score persistence or not (for purple team comps)
Delay int
Jitter int
Timeout int
SlaThreshold int
// Points per service check.
ServicePoints int
SlaPoints int
Admin []TeamData
Red []TeamData
Team []TeamData
Box []Box
Creds []checks.CredData
Running bool
// Inject API key
InjectAPIKey string
DBPath string
}
type Box struct {
Name string
IP string
// Only used for delayed check
Time time.Time `gorm:"-"`
CheckList []checks.Check
Cmd []checks.Cmd
Dns []checks.Dns
Ftp []checks.Ftp
Imap []checks.Imap
Ldap []checks.Ldap
Ping []checks.Ping
Rdp []checks.Rdp
Smb []checks.Smb
Smtp []checks.Smtp
Sql []checks.Sql
Ssh []checks.Ssh
Tcp []checks.Tcp
Vnc []checks.Vnc
Web []checks.Web
WinRM []checks.WinRM
}
func (b Box) InjectTime() time.Time {
return startTime.Add(b.Time.Sub(ZeroTime)).In(loc)
}
func getBoxChecks(b Box) []checks.Check {
// Please forgive me
checkList := []checks.Check{}
for _, c := range b.Cmd {
checkList = append(checkList, c)
}
for _, c := range b.Dns {
checkList = append(checkList, c)
}
for _, c := range b.Ftp {
checkList = append(checkList, c)
}
for _, c := range b.Imap {
checkList = append(checkList, c)
}
for _, c := range b.Ping {
checkList = append(checkList, c)
}
for _, c := range b.Ldap {
checkList = append(checkList, c)
}
for _, c := range b.Rdp {
checkList = append(checkList, c)
}
for _, c := range b.Smb {
checkList = append(checkList, c)
}
for _, c := range b.Smtp {
checkList = append(checkList, c)
}
for _, c := range b.Sql {
checkList = append(checkList, c)
}
for _, c := range b.Ssh {
checkList = append(checkList, c)
}
for _, c := range b.Tcp {
checkList = append(checkList, c)
}
for _, c := range b.Vnc {
checkList = append(checkList, c)
}
for _, c := range b.Web {
checkList = append(checkList, c)
}
for _, c := range b.WinRM {
checkList = append(checkList, c)
}
return checkList
}
func readConfig(conf *config) {
fileContent, err := ioutil.ReadFile(*configPath)
if err != nil {
log.Fatalln("Configuration file ("+*configPath+") not found:", err)
}
if md, err := toml.Decode(string(fileContent), &conf); err != nil {
log.Fatalln(err)
} else {
for _, undecoded := range md.Undecoded() {
errMsg := "[WARN] Undecoded scoring configuration key \"" + undecoded.String() + "\" will not be used."
configErrors = append(configErrors, errMsg)
log.Println(errMsg)
}
}
}
func checkConfig(conf *config) error {
// general error checking and set defaults
if conf.Event == "" {
return errors.New("event title blank or not specified")
}
if conf.Delay == 0 {
conf.Delay = defaultDelay
}
if conf.Jitter == 0 {
conf.Jitter = 30
}
if conf.Https == true {
if conf.Cert == "" || conf.Key == "" {
return errors.New("illegal config: https requires a cert and key pair")
}
}
if conf.Port == 0 {
if conf.Https == false {
conf.Port = 80
} else {
conf.Port = 443
}
}
if conf.DBPath == "" {
conf.DBPath = "dwayne.db"
}
if conf.InjectAPIKey == "" {
log.Println("[WARN] No Inject API Key specified, setting to random UUID")
conf.InjectAPIKey = getUUID()
}
if conf.Jitter >= conf.Delay {
return errors.New("illegal config: jitter not smaller than delay")
}
if conf.Timeout >= conf.Delay-conf.Jitter {
return errors.New("illegal config: timeout not smaller than delay minus jitter")
}
if conf.Timeout != 0 {
dur, err := time.ParseDuration(strconv.Itoa(conf.Timeout) + "s")
if err != nil {
return errors.New("illegal config: invalid value for timeout: " + err.Error())
}
checks.GlobalTimeout = dur
} else {
checks.GlobalTimeout = time.Second * 30
}
if conf.UptimeSLA != 0 {
dur, err := time.ParseDuration(strconv.Itoa(conf.UptimeSLA) + "m")
if err != nil {
return errors.New("illegal config: invalid value for uptime SLA: " + err.Error())
}
uptimeSLA = dur
} else {
uptimeSLA = time.Minute * 10
}
for _, admin := range conf.Admin {
if admin.Name == "" || admin.Pw == "" {
return errors.New("admin " + admin.Name + " missing required property")
}
}
// setting defaults
if conf.Timezone == "" {
conf.Timezone = "America/Rainy_River"
}
// apply default cred lists
if len(dwConf.Creds) == 0 {
return errors.New("illegal config: no valid credentials")
}
if conf.ServicePoints == 0 {
conf.ServicePoints = 3
}
if conf.SlaThreshold == 0 {
conf.SlaThreshold = 6
}
if conf.SlaPoints == 0 {
conf.SlaPoints = conf.SlaThreshold * 2
}
// sort boxes
sort.SliceStable(conf.Box, func(i, j int) bool {
return conf.Box[i].IP < conf.Box[j].IP
})
// check for duplicate boxes
dupeBoxMap := make(map[string]bool)
for _, b := range conf.Box {
if _, ok := dupeBoxMap[b.Name]; ok {
return errors.New("duplicate box name found: " + b.Name)
}
}
// credential list checking
usernameList := []string{}
for _, c := range conf.Creds {
usernameList = append(usernameList, c.Usernames...)
}
// sort creds and look for duplicate usernames
sort.SliceStable(usernameList, func(i, j int) bool {
return usernameList[i] < usernameList[j]
})
for i := 0; i < len(usernameList)-1; i++ {
if usernameList[i] == usernameList[i+1] {
return errors.New("illegal config: duplicate username found in cred lists: " + usernameList[i])
}
}
// look for duplicate team prefix
sort.SliceStable(conf.Team, func(i, j int) bool {
return conf.Team[i].IP < conf.Team[i].IP
})
for i := 0; i < len(conf.Team)-1; i++ {
if conf.Team[i].IP == "" {
return errors.New("illegal config: non-set prefix for team")
}
if conf.Team[i].IP == conf.Team[i+1].IP {
return errors.New("illegal config: duplicate team prefix found")
}
}
// assign team identifiers
for i := range conf.Team {
if strings.TrimSpace(conf.Team[i].Name) == "" {
conf.Team[i].Name = "team" + strconv.Itoa(i+1)
}
}
// look for missing team properties
for _, team := range conf.Team {
if team.Name == "" || team.Pw == "" || team.IP == "" {
return errors.New("illegal config: team missing one or more required property: name, password, or prefix")
}
}
// if persists, make sure they have tokens
if conf.Persists {
for i, team := range conf.Team {
if team.Token == "" {
conf.Team[i].Token = team.Name
log.Println("[INFO] Persist token for " + team.Name + " not found, setting to team name (" + team.Name + ")")
}
}
// look for duplicate token
sort.SliceStable(conf.Team, func(i, j int) bool {
return conf.Team[i].Token < conf.Team[i].Token
})
for i := 0; i < len(conf.Team)-1; i++ {
if conf.Team[i].Token == conf.Team[i+1].Token {
return errors.New("illegal config: duplicate team persist tokens found: " + conf.Team[i].Token)
}
}
// sort by ip again lol
sort.SliceStable(conf.Team, func(i, j int) bool {
return conf.Team[i].IP < conf.Team[i].IP
})
}
err := validateChecks(conf.Box)
if err != nil {
return err
}
// look for duplicate checks
for _, b := range conf.Box {
for j := 0; j < len(b.CheckList)-1; j++ {
if b.CheckList[j].FetchName() == b.CheckList[j+1].FetchName() {
return errors.New("duplicate check name '" + b.CheckList[j].FetchName() + "' and '" + b.CheckList[j+1].FetchName() + "' for box " + b.Name)
}
}
}
checks.CredLists = dwConf.Creds
return nil
}
func validateChecks(boxList []Box) error {
// check validators
// please overlook this transgression
for i, b := range boxList {
boxList[i].CheckList = getBoxChecks(b)
if b.IP == "" {
return errors.New("illegal config: no ip found for box " + b.Name)
}
// Ensure IP replacement chars are lowercase
b.IP = strings.ToLower(b.IP)
boxList[i].IP = b.IP
for j, c := range boxList[i].CheckList {
switch c.(type) {
case checks.Cmd:
ck := c.(checks.Cmd)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "cmd"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if len(ck.CredLists) < 1 && !strings.Contains(ck.Command, "USERNAME") && !strings.Contains(ck.Command, "PASSWORD") {
ck.Anonymous = true
}
boxList[i].CheckList[j] = ck
case checks.Dns:
ck := c.(checks.Dns)
ck.IP = b.IP
ck.Anonymous = true // call me when you need authed DNS
if ck.Display == "" {
ck.Display = "dns"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if len(ck.Record) < 1 {
return errors.New("dns check " + ck.Name + " has no records")
}
if ck.Port == 0 {
ck.Port = 53
}
boxList[i].CheckList[j] = ck
case checks.Ftp:
ck := c.(checks.Ftp)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "ftp"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
ck.Port = 21
}
for _, f := range ck.File {
if f.Regex != "" && f.Hash != "" {
return errors.New("can't have both regex and hash for ftp file check")
}
}
boxList[i].CheckList[j] = ck
case checks.Imap:
ck := c.(checks.Imap)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "imap"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
ck.Port = 143
}
boxList[i].CheckList[j] = ck
case checks.Ldap:
ck := c.(checks.Ldap)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "ldap"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
ck.Port = 636
}
if ck.Anonymous {
return errors.New("anonymous ldap not supported")
}
boxList[i].CheckList[j] = ck
case checks.Ping:
ck := c.(checks.Ping)
ck.IP = b.IP
ck.Anonymous = true
if ck.Count == 0 {
ck.Count = 1
}
if ck.Display == "" {
ck.Display = "ping"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
boxList[i].CheckList[j] = ck
case checks.Rdp:
ck := c.(checks.Rdp)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "rdp"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
ck.Port = 3389
}
boxList[i].CheckList[j] = ck
case checks.Smb:
ck := c.(checks.Smb)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "smb"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
ck.Port = 445
}
boxList[i].CheckList[j] = ck
case checks.Smtp:
ck := c.(checks.Smtp)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "smtp"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
ck.Port = 25
}
boxList[i].CheckList[j] = ck
case checks.Sql:
ck := c.(checks.Sql)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "sql"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Kind == "" {
ck.Kind = "mysql"
}
if ck.Port == 0 {
ck.Port = 3306
}
for _, q := range ck.Query {
if q.DatabaseExists && (q.Column != "" || q.Table != "" || q.Output != "") {
return errors.New("cannot use both database exists check and row check")
}
if q.DatabaseExists && q.Database == "" {
return errors.New("must specify database for database exists check")
}
if q.UseRegex {
regexp.MustCompile(q.Output)
}
if q.UseRegex && q.Contains {
return errors.New("cannot use both regex and contains")
}
}
boxList[i].CheckList[j] = ck
case checks.Ssh:
ck := c.(checks.Ssh)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "ssh"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
ck.Port = 22
}
if ck.PrivKey != "" && ck.BadAttempts != 0 {
return errors.New("can not have bad attempts with pubkey for ssh")
}
for _, r := range ck.Command {
if r.UseRegex {
regexp.MustCompile(r.Output)
}
if r.UseRegex && r.Contains {
return errors.New("cannot use both regex and contains")
}
}
if ck.Anonymous {
return errors.New("anonymous ssh not supported")
}
boxList[i].CheckList[j] = ck
case checks.Tcp:
ck := c.(checks.Tcp)
ck.IP = b.IP
ck.Anonymous = true
if ck.Display == "" {
ck.Display = "tcp"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
return errors.New("tcp port required")
}
boxList[i].CheckList[j] = ck
case checks.Vnc:
ck := c.(checks.Vnc)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "vnc"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
ck.Port = 5900
}
boxList[i].CheckList[j] = ck
case checks.Web:
ck := c.(checks.Web)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "web"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
ck.Port = 80
}
if len(ck.Url) == 0 {
return errors.New("no urls specified for web check " + ck.Name)
}
if len(ck.CredLists) < 1 {
ck.Anonymous = true
}
if ck.Scheme == "" {
ck.Scheme = "http"
}
for _, u := range ck.Url {
if u.Diff != 0 && u.CompareFile == "" {
return errors.New("need compare file for diff in web")
}
}
boxList[i].CheckList[j] = ck
case checks.WinRM:
ck := c.(checks.WinRM)
ck.IP = b.IP
if ck.Display == "" {
ck.Display = "winrm"
}
if ck.Name == "" {
ck.Name = b.Name + "-" + ck.Display
}
if ck.Port == 0 {
if ck.Encrypted {
ck.Port = 443
} else {
ck.Port = 80
}
}
if ck.Anonymous {
return errors.New("anonymous winrm not supported")
}
for _, r := range ck.Command {
if r.UseRegex {
regexp.MustCompile(r.Output)
}
if r.UseRegex && r.Contains {
return errors.New("cannot use both regex and contains")
}
}
boxList[i].CheckList[j] = ck
}
}
}
return nil
}
func getCheckName(check checks.Check) string {
name := strings.Split(reflect.TypeOf(check).String(), ".")[1]
fmt.Println("name is ", name)
return name
}
func (m *config) GetFullIP(boxIP, teamIP string) string {
return strings.Replace(boxIP, "x", teamIP, 1)
}