forked from lkarlslund/Adalanche
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
818 lines (722 loc) · 25.5 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
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
package main
import (
"embed"
"flag"
"fmt"
"io"
"io/fs"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"strings"
"syscall"
"time"
"github.com/Showmax/go-fqdn"
"github.com/gofrs/uuid"
jsoniter "github.com/json-iterator/go"
"github.com/pierrec/lz4"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/schollz/progressbar/v3"
"github.com/tinylib/msgp/msgp"
"golang.org/x/crypto/ssh/terminal"
)
//go:embed html/* readme.MD
var embeddedassets embed.FS
var (
qjson = jsoniter.ConfigCompatibleWithStandardLibrary
)
var (
programname = "adalanche"
builddate = "unknown_date"
commit = "unknown_commit"
)
func showUsage() {
log.Info().Msg("Usage: adalanche [-options ...] command")
log.Info().Msg(`Commands are:`)
log.Info().Msg(` dump - to dump an AD into a compressed file`)
log.Info().Msg(` analyze - launches embedded webservice`)
log.Info().Msg(` dump-analyze - dumps an AD and launches embedded webservice`)
log.Info().Msg(` export - save analysis to graph files`)
log.Info().Msg(`Options:`)
flag.CommandLine.VisitAll(func(f *flag.Flag) {
name, usage := flag.UnquoteUsage(f)
s := fmt.Sprintf(" -%s", f.Name) // Two spaces before -; see next two comments.
totallength := len(f.Name)
if len(name) > 0 {
s += " " + name
totallength += 1 + len(name)
}
if totallength < 20 {
s += strings.Repeat(" ", 20-totallength)
}
s += strings.ReplaceAll(usage, "\n", "\n ")
if f.DefValue != "" {
s += fmt.Sprintf(" (default %v)", f.DefValue)
}
log.Info().Msg(s)
})
os.Exit(0)
}
func main() {
server := flag.String("server", "", "DC to connect to, use IP or full hostname ex. -dc=\"dc.contoso.local\", random DC is auto-detected if not supplied")
port := flag.Int("port", 636, "LDAP port to connect to (389 or 636 typical)")
domain := flag.String("domain", "", "domain suffix to analyze (auto-detected if not supplied)")
user := flag.String("username", "", "username to connect with ex. -username=\"someuser\"")
pass := flag.String("password", "", "password to connect with ex. -password=\"testpass!\"")
tlsmode := flag.String("tlsmode", "TLS", "Transport mode (TLS, StartTLS, NoTLS)")
ignoreCert := flag.Bool("ignorecert", true, "Disable certificate checks")
var authmodeString string
if runtime.GOOS == "windows" {
flag.StringVar(&authmodeString, "authmode", "ntlmsspi", "Bind mode: unauth, simple, md5, ntlm, ntlmpth (password is hash), ntlmsspi (current user, default)")
} else {
// change default for non windows platofrms
flag.StringVar(&authmodeString, "authmode", "ntlm", "Bind mode: unauth, simple, md5, ntlm, ntlmpth (password is hash), ntlmsspi (current user, default)")
}
authdomain := flag.String("authdomain", "", "domain for authentication, if using ntlm auth")
datapath := flag.String("datapath", "data", "folder to store cached ldap data")
dumpquery := flag.String("dumpquery", "(objectClass=*)", "LDAP query for dump, defaults to everything")
analyzequery := flag.String("analyzequery", "(&(objectClass=group)(|(name=Domain Admins)(name=Enterprise Admins)))", "LDAP query to locate targets for analysis")
importall := flag.Bool("importall", false, "Load all attributes from dump (expands search options, but at the cost of memory")
exportinverted := flag.Bool("exportinverted", false, "Invert analysis, discover how much damage targets can do")
exporttype := flag.String("exporttype", "cytoscapejs", "Graph type to export (cytoscapejs, graphviz)")
attributesparam := flag.String("attributes", "", "Comma seperated list of attributes to get, blank means everything")
debuglogging := flag.Bool("debug", false, "Enable debug logging")
nosacl := flag.Bool("nosacl", true, "Request data with NO SACL flag, allows normal users to dump ntSecurityDescriptor field")
pagesize := flag.Int("pagesize", 1000, "Chunk requests into pages of this count of objects")
bind := flag.String("bind", "127.0.0.1:8080", "Address and port of webservice to bind to")
nobrowser := flag.Bool("nobrowser", false, "Don't launch browser after starting webservice")
dumpgpos := flag.Bool("dumpgpos", false, "When dumping, do you want to include GPO file contents?")
gpopath := flag.String("gpopath", "", "Override path to GPOs, useful for non Windows OS'es with mounted drive (/mnt/policies/ or similar)")
flag.Parse()
if !*debuglogging {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
} else {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Msg("Debug logging enabled")
}
// We do lots of allocations when importing stuff, so lets set this aggressively
debug.SetGCPercent(10)
log.Info().Msg("adalanche (c) 2020-2021 Lars Karlslund, released under GPLv3, This program comes with ABSOLUTELY NO WARRANTY")
// Ensure the cache folder is available
if _, err := os.Stat(*datapath); os.IsNotExist(err) {
err = os.Mkdir(*datapath, 0600)
if err != nil {
log.Fatal().Msgf("Could not create cache folder %v: %v", datapath, err)
}
}
command := "dump-analyze"
if flag.NArg() < 1 {
log.Info().Msg("No command issued, assuming 'dump-analyze'. Try command 'help' to get help.")
} else {
command = flag.Arg(0)
}
// Auto detect domain if not supplied
if *domain == "" {
log.Info().Msg("No domain supplied, auto-detecting")
*domain = strings.ToLower(os.Getenv("USERDNSDOMAIN"))
if *domain == "" {
// That didn't work, lets try something else
f, err := fqdn.FqdnHostname()
if err == nil {
*domain = strings.ToLower(f[strings.Index(f, ".")+1:])
}
}
if *domain == "" {
log.Fatal().Msg("Domain auto-detection failed")
} else {
log.Info().Msgf("Auto-detected domain as %v", *domain)
}
}
// Dump data?
if command == "dump" || command == "dump-analyze" {
if *domain != "" && *server == "" {
// Auto-detect server
cname, servers, err := net.LookupSRV("", "", "_ldap._tcp.dc._msdcs."+*domain)
if err == nil && cname != "" && len(servers) != 0 {
*server = servers[0].Target
log.Info().Msgf("AD controller detected as: %v", *server)
} else {
log.Warn().Msg("AD controller auto-detection failed, use -server xxxx parameter")
}
}
var authmode byte
switch authmodeString {
case "unauth":
authmode = 0
case "simple":
authmode = 1
case "md5":
authmode = 2
case "ntlm":
authmode = 3
case "ntlmpth":
authmode = 4
case "ntlmsspi":
authmode = 5
default:
log.Error().Msgf("Unknown LDAP authentication mode %v", authmodeString)
showUsage()
}
if len(*domain) == 0 {
log.Warn().Msg("Missing domain name - please provider this on commandline")
showUsage()
}
if len(*server) == 0 {
log.Warn().Msg("Missing AD controller server name - please provider this on commandline")
showUsage()
}
var username string
if authmode != 5 {
if *user == "" {
// Auto-detect user
*user = os.Getenv("USERNAME")
if *user != "" {
log.Info().Msgf("Auto-detected username as %v", *user)
}
}
if *user == "" {
log.Warn().Msg("Missing username - please provider this on commandline")
showUsage()
}
if *pass == "" {
fmt.Printf("Please enter password for %v: ", *user)
passwd, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println()
if err == nil {
*pass = string(passwd)
}
}
username = *user + "@" + *domain
} else {
log.Info().Msg("Using integrated NTLM authentication")
}
tlsm, err := TLSmodeString(*tlsmode)
if err != nil {
log.Warn().Msgf("Unknown TLS mode %v", *tlsmode)
showUsage()
}
ad := AD{
Domain: *domain,
Server: *server,
Port: uint16(*port),
User: username,
Password: *pass,
AuthDomain: *authdomain,
TLSMode: tlsm,
IgnoreCert: *ignoreCert,
}
err = ad.Connect(authmode)
if err != nil {
log.Fatal().Msgf("Problem connecting to AD: %v", err)
}
var attributes []string
if *attributesparam != "" {
attributes = strings.Split(*attributesparam, ",")
}
outfile, err := os.Create(filepath.Join(*datapath, *domain+".objects.lz4.msgp"))
if err != nil {
log.Fatal().Msgf("Problem opening domain cache file: %v", err)
}
boutfile := lz4.NewWriter(outfile)
boutfile.Header.CompressionLevel = 10
e := msgp.NewWriter(boutfile)
dumpbar := progressbar.NewOptions(0,
progressbar.OptionSetDescription("Dumping..."),
progressbar.OptionShowCount(),
progressbar.OptionShowIts(),
progressbar.OptionSetItsString("objects"),
progressbar.OptionOnCompletion(func() { fmt.Println() }),
progressbar.OptionThrottle(time.Second*1),
)
log.Info().Msg("Dumping schema objects ...")
rawobjects, err := ad.Dump("CN=Schema,CN=Configuration,"+ad.RootDn(), *dumpquery, attributes, *nosacl, *pagesize)
if err != nil {
log.Fatal().Msgf("Problem dumping AD: %v", err)
}
log.Debug().Msgf("Saving %v schema objects ...", len(rawobjects))
for _, object := range rawobjects {
err = object.EncodeMsg(e)
if err != nil {
log.Fatal().Msgf("Problem encoding LDAP object %v: %v", object.DistinguishedName, err)
}
dumpbar.Add(1)
}
log.Info().Msg("Dumping configuration objects ...")
rawobjects, err = ad.Dump("CN=Configuration,"+ad.RootDn(), *dumpquery, attributes, *nosacl, *pagesize)
if err != nil {
log.Fatal().Msgf("Problem dumping AD: %v", err)
}
log.Debug().Msgf("Saving %v configuration objects ...", len(rawobjects))
for _, object := range rawobjects {
err = object.EncodeMsg(e)
if err != nil {
log.Fatal().Msgf("Problem encoding LDAP object %v: %v", object.DistinguishedName, err)
}
dumpbar.Add(1)
}
log.Info().Msg("Dumping forest DNS objects ...")
rawobjects, err = ad.Dump("DC=ForestDnsZones,"+ad.RootDn(), *dumpquery, attributes, *nosacl, *pagesize)
if err != nil {
log.Warn().Msgf("Problem dumping forest DNS zones (maybe it doesn't exist): %v", err)
} else {
log.Debug().Msgf("Saving %v forest DNS objects ...", len(rawobjects))
for _, object := range rawobjects {
err = object.EncodeMsg(e)
if err != nil {
log.Fatal().Msgf("Problem encoding LDAP object %v: %v", object.DistinguishedName, err)
}
dumpbar.Add(1)
}
}
log.Info().Msg("Dumping domain DNS objects ...")
rawobjects, err = ad.Dump("DC=DomainDnsZones,"+ad.RootDn(), *dumpquery, attributes, *nosacl, *pagesize)
if err != nil {
log.Warn().Msgf("Problem dumping domain DNS zones (maybe it doesn't exist): %v", err)
} else {
log.Debug().Msgf("Saving %v domain DNS objects ...", len(rawobjects))
for _, object := range rawobjects {
err = object.EncodeMsg(e)
if err != nil {
log.Fatal().Msgf("Problem encoding LDAP object %v: %v", object.DistinguishedName, err)
}
dumpbar.Add(1)
}
}
log.Info().Msg("Dumping main AD objects ...")
rawobjects, err = ad.Dump(ad.RootDn(), *dumpquery, attributes, *nosacl, *pagesize)
if err != nil {
log.Fatal().Msgf("Problem dumping AD: %v", err)
}
if *dumpgpos {
log.Debug().Msg("Collecting GPO files ...")
for _, object := range rawobjects {
// Let's check if it this is a GPO and then add some fake attributes to represent it
if gpfsp, found := object.Attributes["gPCFileSysPath"]; found {
gpodisplayname, _ := object.Attributes["displayName"]
gpoguid, _ := object.Attributes["name"]
gppath := gpfsp[0]
if *gpopath != "" {
if len(gpoguid) != 1 {
log.Warn().Msgf("GPO %v GUID not readable, skipping", gpodisplayname)
continue
}
// Override path, possibly on other OS'es or if you dont have DNS running
gppath = *gpopath
// gppath = strings.ReplaceAll(gppath, "%SERVER%", *server)
// gppath = strings.ReplaceAll(gppath, "%DOMAIN%", *domain)
// gppath = strings.ReplaceAll(gppath, "%GUID%", gpoguid[0])
gppath = filepath.Join(*gpopath, gpoguid[0])
}
log.Info().Msgf("Dumping group policy files from %v ...", gppath)
_, err := os.Stat(gppath)
if err != nil {
log.Warn().Msg("Can't access path, aborting this GPO ...")
} else {
offset := len(gppath)
filepath.WalkDir(gppath, func(curpath string, d fs.DirEntry, err error) error {
if !d.IsDir() {
rawfile, err := ioutil.ReadFile(curpath)
if err == nil {
subpath := curpath[offset:]
object.Attributes[path.Join("_gpofile", strings.TrimPrefix(strings.ReplaceAll(subpath, "\\", "/"), "/"))] = []string{string(rawfile)}
}
}
return nil
})
}
}
}
}
log.Debug().Msgf("Saving %v AD objects ...", len(rawobjects))
for _, object := range rawobjects {
err = object.EncodeMsg(e)
if err != nil {
log.Fatal().Msgf("Problem encoding LDAP object %v: %v", object.DistinguishedName, err)
}
dumpbar.Add(1)
}
dumpbar.Finish()
err = ad.Disconnect()
if err != nil {
log.Fatal().Msgf("Problem disconnecting from AD: %v", err)
}
e.Flush()
boutfile.Close()
outfile.Close()
}
if command == "dump" {
os.Exit(0)
}
// Everything else requires us to load data
if len(*domain) == 0 {
log.Error().Msg("Please provide domain name")
showUsage()
}
for _, domain := range strings.Split(*domain, ",") {
if AllObjects.Base == "" { // Shoot me, this is horrible
AllObjects.Base = "dc=" + strings.Replace(domain, ".", ",dc=", -1)
AllObjects.Domain = domain
}
cachefile, err := os.Open(filepath.Join(*datapath, domain+".objects.lz4.msgp"))
if err != nil {
log.Fatal().Msgf("Problem opening domain cache file: %v", err)
}
bcachefile := lz4.NewReader(cachefile)
cachestat, _ := cachefile.Stat()
loadbar := progressbar.NewOptions(int(cachestat.Size()),
progressbar.OptionSetDescription("Loading objects from "+domain+" ..."),
progressbar.OptionShowBytes(true),
progressbar.OptionThrottle(time.Second*1),
progressbar.OptionOnCompletion(func() { fmt.Println() }),
)
d := msgp.NewReader(bcachefile)
// d := msgp.NewReader(&progressbar.Reader{bcachefile, &loadbar})
// Load all the stuff
var lastpos int64
for {
var rawObject RawObject
err = rawObject.DecodeMsg(d)
pos, _ := cachefile.Seek(0, io.SeekCurrent)
loadbar.Add(int(pos - lastpos))
lastpos = pos
if err == nil {
newObject := rawObject.ToObject(*importall)
AllObjects.Add(&newObject)
} else if msgp.Cause(err) == io.EOF {
break
} else {
log.Fatal().Msgf("Problem decoding object: %v", err)
}
}
cachefile.Close()
loadbar.Finish()
}
log.Debug().Msgf("Loaded %v ojects", len(AllObjects.AsArray()))
// Add our known SIDs if they're missing
for sid, name := range knownsids {
binsid, err := SIDFromString(sid)
if err != nil {
log.Fatal().Msgf("Problem parsing SID %v", sid)
}
if _, found := AllObjects.FindSID(binsid); !found {
dn := "CN=" + name + ",CN=microsoft-builtin"
log.Info().Msgf("Adding missing well known SID %v (%v) as %v", name, sid, dn)
AllObjects.Add(&Object{
DistinguishedName: dn,
Attributes: map[Attribute][]string{
Name: {name},
ObjectSid: {string(binsid)},
ObjectClass: {"person", "user", "top"},
ObjectCategory: {"Group"},
},
})
}
}
// ShowAttributePopularity()
// Generate member of chains
processbar := progressbar.NewOptions(int(len(AllObjects.dnmap)),
progressbar.OptionSetDescription("Processing objects..."),
progressbar.OptionShowCount(),
progressbar.OptionShowIts(),
progressbar.OptionSetItsString("objects"),
progressbar.OptionOnCompletion(func() { fmt.Println() }),
progressbar.OptionThrottle(time.Second*1),
)
// everyonesid, _ := SIDFromString("S-1-1-0")
// everyone, ok := AllObjects.FindSID(everyonesid)
// if !ok {
// log.Fatal().Msgf("Could not locate Everyone, aborting")
// }
// authenticateduserssid, _ := SIDFromString("S-1-5-11")
// authenticatedusers, ok := AllObjects.FindSID(authenticateduserssid)
// if !ok {
// log.Fatal().Msgf("Could not locate Authenticated Users, aborting")
// }
log.Info().Msg("Pre-processing directory data ...")
for _, object := range AllObjects.AsArray() {
processbar.Add(1)
object.MemberOf()
// Crude special handling for Everyone and Authenticated Users
// if object.Type() == ObjectTypeUser || object.Type() == ObjectTypeComputer || object.Type() == ObjectTypeManagedServiceAccount {
// everyone.imamemberofyou(object)
// authenticatedusers.imamemberofyou(object)
// object.memberof = append(object.memberof, everyone, authenticatedusers)
// }
object.SetAttr(MetaType, object.Type().String())
if lastlogon, ok := object.AttrTimestamp(LastLogonTimestamp); ok {
object.SetAttr(MetaLastLoginAge, strconv.Itoa(int(time.Since(lastlogon)/time.Hour)))
}
if passwordlastset, ok := object.AttrTimestamp(PwdLastSet); ok {
object.SetAttr(MetaPasswordAge, strconv.Itoa(int(time.Since(passwordlastset)/time.Hour)))
}
if strings.Contains(strings.ToLower(object.OneAttr(OperatingSystem)), "linux") {
object.SetAttr(MetaLinux, "1")
}
if strings.Contains(strings.ToLower(object.OneAttr(OperatingSystem)), "windows") {
object.SetAttr(MetaWindows, "1")
}
if len(object.Attr(MSmcsAdmPwdExpirationTime)) > 0 {
object.SetAttr(MetaLAPSInstalled, "1")
}
if uac, ok := object.AttrInt(UserAccountControl); ok {
if uac&UAC_TRUSTED_FOR_DELEGATION != 0 {
object.SetAttr(MetaUnconstrainedDelegation, "1")
}
if uac&UAC_TRUSTED_TO_AUTH_FOR_DELEGATION != 0 {
object.SetAttr(MetaConstrainedDelegation, "1")
}
if uac&UAC_NOT_DELEGATED != 0 {
log.Debug().Msgf("%v has can't be used as delegation", object.DN())
}
if uac&UAC_WORKSTATION_TRUST_ACCOUNT != 0 {
object.SetAttr(MetaWorkstation, "1")
}
if uac&UAC_SERVER_TRUST_ACCOUNT != 0 {
object.SetAttr(MetaServer, "1")
}
if uac&UAC_ACCOUNTDISABLE != 0 {
object.SetAttr(MetaAccountDisabled, "1")
}
if uac&UAC_PASSWD_CANT_CHANGE != 0 {
object.SetAttr(MetaPasswordCantChange, "1")
}
if uac&UAC_DONT_EXPIRE_PASSWORD != 0 {
object.SetAttr(MetaPasswordNoExpire, "1")
}
if uac&UAC_PASSWD_NOTREQD != 0 {
object.SetAttr(MetaPasswordNotRequired, "1")
}
}
if object.Type() == ObjectTypeTrust {
// http://www.frickelsoft.net/blog/?p=211
var direction string
dir, _ := object.AttrInt(TrustDirection)
switch dir {
case 0:
direction = "disabled"
case 1:
direction = "incoming"
case 2:
direction = "outgoing"
case 3:
direction = "bidirectional"
}
attr, _ := object.AttrInt(TrustAttributes)
log.Debug().Msgf("Domain has a %v trust with %v", direction, object.OneAttr(TrustPartner))
if dir&2 != 0 && attr&4 != 0 {
log.Debug().Msgf("SID filtering is not enabled, so pwn %v and pwn this AD too", object.OneAttr(TrustPartner))
}
}
/*
// Add DA EA AD to Hackers Won
switch object.Type() {
case ObjectTypeGroup:
switch object.OneAttr(Name) {
case "Domain Admins", "Enterprise Admins", "Administrators":
HackersWonObject.CanPwn.Set(object, PwnMemberOfGroup)
object.PwnableBy.Set(HackersWonObject, PwnMemberOfGroup)
}
case ObjectTypeCertificateTemplate:
if nf, ok := object.AttrInt(MSPKICertificateNameFlag); nf&1 == 1 && ok {
// Template that users can supply SAN to - can it be abused to do Client Auth?
var clientauth bool
// No usage == client auth, so says SpectreOps
if len(object.Attr(PKIExtendedUsage)) == 0 {
clientauth = true
}
for _, eu := range object.Attr(PKIExtendedUsage) {
switch eu {
case "1.3.6.1.5.5.7.3.2", "1.3.5.1.5.2.3.4", "1.3.6.1.4.1.311.20.2.2", "2.5.29.37.0":
clientauth = true
}
}
if clientauth {
HackersWonObject.CanPwn.Set(object, PwnMemberOfGroup)
object.PwnableBy.Set(HackersWonObject, PwnMemberOfGroup)
}
}
}
*/
// Special types of Objects
if object.HasAttrValue(ObjectClass, "controlAccessRight") {
u, err := uuid.FromString(object.OneAttr(RightsGUID))
// log.Debug().Msgf("Adding right %v %v", u, object.OneAttr(DisplayName))
if err == nil {
AllRights[u] = object
}
} else if object.HasAttrValue(ObjectClass, "attributeSchema") {
objectGUID, err := uuid.FromBytes([]byte(object.OneAttr(SchemaIDGUID)))
objectGUID = SwapUUIDEndianess(objectGUID)
// log.Debug().Msgf("Adding schema attribute %v %v", u, object.OneAttr(Name))
if err == nil {
AllSchemaAttributes[objectGUID] = object
switch object.OneAttr(Name) {
case "ms-Mcs-AdmPwd":
log.Info().Msg("Detected LAPS schema extension, adding extra analyzer")
PwnAnalyzers = append(PwnAnalyzers, PwnAnalyzer{
Method: PwnReadLAPSPassword,
ObjectAnalyzer: func(o *Object) []*Object {
var results []*Object
// Only for computers
if o.Type() != ObjectTypeComputer {
return results
}
// ... that has LAPS installed
if len(o.Attr(MSmcsAdmPwdExpirationTime)) == 0 {
return results
}
// Analyze ACL
sd, err := o.SecurityDescriptor()
if err != nil {
return results
}
for index, acl := range sd.DACL.Entries {
if sd.DACL.AllowObjectClass(index, o, RIGHT_DS_CONTROL_ACCESS, objectGUID) {
results = append(results, AllObjects.FindOrAddSID(acl.SID))
}
}
return results
},
})
}
}
} else if object.HasAttrValue(ObjectClass, "classSchema") {
u, err := uuid.FromBytes([]byte(object.OneAttr(SchemaIDGUID)))
u = SwapUUIDEndianess(u)
// log.Debug().Msgf("Adding schema class %v %v", u, object.OneAttr(Name))
if err == nil {
AllSchemaClasses[u] = object
}
}
}
processbar.Finish()
// This sucks in a very bad way, Objects really needs to be an AD object :-\
ad := AD{
Domain: *domain,
}
// Find dsHeuristics, this defines groups EXCLUDED From AdminSDHolder application
// https://social.technet.microsoft.com/wiki/contents/articles/22331.adminsdholder-protected-groups-and-security-descriptor-propagator.aspx#What_is_a_protected_group
var excluded string
if ds, found := AllObjects.Find("CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration," + ad.RootDn()); found {
excluded = ds.OneAttr(DsHeuristics)
}
// Let's see if we can find the AdminSDHolder container
if adminsdholder, found := AllObjects.Find("cn=AdminSDHolder,cn=System," + ad.RootDn()); found {
// We found it - so we know it can theoretically "pwn" any object with AdminCount > 0
PwnAnalyzers = append(PwnAnalyzers, MakeAdminSDHolderPwnanalyzerFunc(adminsdholder, excluded))
}
// Generate member of chains
pwnbar := progressbar.NewOptions(int(len(AllObjects.dnmap)),
progressbar.OptionSetDescription("Analyzing who can pwn who ..."),
progressbar.OptionShowCount(),
progressbar.OptionShowIts(),
progressbar.OptionSetItsString("objects"),
// progressbar.OptionShowBytes(true),
progressbar.OptionOnCompletion(func() { fmt.Println() }),
progressbar.OptionThrottle(time.Second*1),
)
var pwnlinks int
for _, object := range AllObjects.AsArray() {
pwnbar.Add(1)
// log.Info().Msg(object.String())
for _, analyzer := range PwnAnalyzers {
for _, pwnobject := range analyzer.ObjectAnalyzer(object) {
if pwnobject == object || pwnobject.SID() == object.SID() { // SID check solves (some) dual-AD analysis problems
// We don't care about self owns
continue
}
// Ignore these, SELF = self own, Creator/Owner always has full rights
if pwnobject.SID() == SelfSID || pwnobject.SID() == CreatorOwnerSID || pwnobject.SID() == SystemSID {
continue
}
// log.Debug().Msgf("Detected that %v can pwn %v by %v", pwnobject.DN(), object.DN(), analyzer.Method)
pwnobject.CanPwn[object] = pwnobject.CanPwn[object].Set(analyzer.Method)
object.PwnableBy[pwnobject] = object.PwnableBy[pwnobject].Set(analyzer.Method)
pwnlinks++
}
}
}
pwnbar.Finish()
log.Debug().Msgf("Detected %v ways to pwn objects", pwnlinks)
switch command {
case "exportacls":
log.Info().Msg("Finding most valuable assets ...")
output, err := os.Create("debug.txt")
if err != nil {
log.Fatal().Msgf("Error opening output file: %v", err)
}
for _, object := range AllObjects.AsArray() {
fmt.Fprintf(output, "Object:\n%v\n\n-----------------------------\n", object)
}
output.Close()
log.Info().Msg("Done")
case "export":
log.Info().Msg("Finding most valuable assets ...")
q, err := ParseQueryStrict(*analyzequery)
if err != nil {
log.Fatal().Msgf("Error parsing LDAP query: %v", err)
}
includeobjects := AllObjects.Filter(func(o *Object) bool {
return q.Evaluate(o)
})
mode := "normal"
if *exportinverted {
mode = "inverted"
}
resultgraph := AnalyzeObjects(includeobjects, nil, PwnMethod(PwnAllMethods), mode, 99)
switch *exporttype {
case "graphviz":
err = ExportGraphViz(resultgraph, "adalanche-"+*domain+".dot")
case "cytoscapejs":
err = ExportCytoscapeJS(resultgraph, "adalanche-cytoscape-js-"+*domain+".json")
default:
log.Error().Msg("Unknown export format")
showUsage()
}
if err != nil {
log.Fatal().Msgf("Problem exporting graph: %v", err)
}
log.Info().Msg("Done")
case "analyze", "dump-analyze":
quit := make(chan bool)
srv := webservice(*bind)
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Msgf("Problem launching webservice listener: %s", err)
} else {
quit <- true
}
}()
// Launch browser
if !*nobrowser {
var err error
url := "http://" + *bind
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Debug().Msgf("Problem launching browser: %v", err)
}
}
// Wait for webservice to end
<-quit
default:
log.Error().Msgf("Unknown command %v", flag.Arg(0))
showUsage()
}
}