forked from cyberark/ACLight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathACLight.ps1
2785 lines (2170 loc) · 94 KB
/
ACLight.ps1
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#----------------------------------------------------------------------------------------------------
##########################################################################################
# #
# Discovering Privileged Accounts and Shadow Admins - using Advanced ACLs Analysis #
# #
##########################################################################################
Release Notes:
The ACLight is a tool for discovering Privileged Accounts through advanced ACLs analysis.
It will discover the Shadow Admins in the network.
It queries the Active Directory for its objects' ACLs and then filters the sensitive permissions from each one of them.
The results are the domain privileged accounts in the network (from the advanced ACLs perspective of the AD).
It automatically scans all the domains of the forest.
You can run the scan with just any regular user in the domain (could be non-privleged user) and it needs PowerShell version 3+.
Version 1.0: 28.8.16
Version 1.1: 15.9.16
version 2.0: 17.5.17
version 2.1: 4.6.17
Authors: Asaf Hecht (@hechtov) - Cyberark's research team.
Using functions from the great PowerView project created by: Will Schroeder (@harmj0y).
The original PowerView have more functionalities:
Powerview: https://github.com/PowerShellEmpire/PowerTools/tree/master/PowerView
------------------------------------------------------------------------------------------------------
HOW TO RUN:
option 1 - Just double click on "Execute-ACLight.bat".
- OR -
option 2 - Open cmd:
Go to "ACLight" main folder -> 1) Type: cd "<ACLight folder path>"
Run the "ACLight" script -> 2) Type: powershell -noprofile -ExecutionPolicy Bypass Import-Module '.\ACLight.psm1' -force ; Start-ACLsAnalysis
- OR -
Option 3 - Open PowerShell (with -ExecutionPolicy Bypass):
1) cd "<ACLight folder path>"
2) Import-Module '.\ACLight.psm1' -force
3) Start-ACLsAnalysis
Execute it and check the result!
You should take care of all the privileged accounts that the tool discovered for you.
Especially - take care of the Shadow Admins!
Those are accounts with direct sensitive ACLs assignments (not through the known privileged groups).
------------------------------------------------------------------------------------------------------
THE RESULTS FILES:
1) First check the - "Accounts with extra permissions.txt" file - It's straight-forward & powerful list of the privileged accounts that were discovered in the network.
2) "All entities with extra permissions.txt" - Will give you more sensitive entities in the network (also the "empty" entities like empty groups).
3) "Privileged Accounts Permissions - Final Report.csv" - This is the final summary report - in this file you can see what is the exact sensitive permission each account has.
4) "Privileged Accounts Permissions - Irregular Accounts.csv" - Similar to the final report just with only the privileged accounts that have direct permissions (not through their group membership).
5) "research.com - Full Output.csv" - Every domain that was scanned will have a csv with more raw results of the ACLs.
----------------------------------------------------------------------------------------------------#>
##Requires -Version 3.0 or above
######################################################################
# #
# Section 1 - main functions for advanced analysis of the ACLs #
# #
######################################################################
# Create the results folder
$resultsPath = $PSScriptRoot + "\Results"
if (Test-Path $resultsPath)
{
write-verbose "The results folder was already exists"
}
else
{
New-Item -ItemType directory -Path $resultsPath
}
# Function for advanced ACLs analysis in a specified domain
function Start-domainACLsAnalysis {
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline=$True)]
[String]
$Full = $False,
[String]
$SamAccountName,
[String]
$Name = "*",
[Alias('DN')]
[String]
$DistinguishedName = "*",
[String]
$Filter,
[String]
$ADSpath,
[String]
$ADSprefix,
[String]
$Domain,
[String]
$DomainController,
[String]
$exportCsvFile = "C:\scanACLsResults.csv",
[ValidateRange(1,10000)]
[Int]
$PageSize = 200
)
#clean the csv output file
if (Test-Path $exportCsvFile) {
Remove-Item $exportCsvFile
}
$Domaintime = New-Object system.Diagnostics.Stopwatch
$DomainTotaltime = New-Object system.Diagnostics.Stopwatch
$Domaintime.Start()
$DomainTotaltime.Start()
$DomainList = @()
$PrivilegedOwners = @()
$PrivilegedEntities = @()
$PrivilegedGroups = @()
$PrivilegedAccounts = @()
$GroupMembersDB = @{}
$domainPrivilegedOwners = @()
$domainPrivilegedEntities = @()
$count++
$DomainDN = "DC=$($Domain.Replace('.', ',DC='))"
Write-Output "Starting the scan for Domain: $domain"
###############################################################################################################################################
# Important - here you can choose each sensitive Active Directory objects you want to scan.
# You can add or remove scan filters and check the new results.
# It will affect the scanning time duration and the results might include less privileged accounts (if you choose less sensitive AD objects).
# It's also recommended to add here the privilged accounts that were discovered in previous scans - to discover who has control over them
###############################################################################################################################################
# the root of the domain
Invoke-ACLScanner -Full $Full -exportCsvFile $exportCsvFile -Domain $Domain -DistinguishedName $DomainDN
# wild char on "admin" - it will be very interesting but also might includes less sensitive objects
Invoke-ACLScanner @PSBoundParameters -Name '*admin*'
# more built-in sensitive groups, every organization can add here more of his unique sensitive groups
Invoke-ACLScanner @PSBoundParameters -Name 'Server Operators'
Invoke-ACLScanner @PSBoundParameters -Name 'Account Operators'
Invoke-ACLScanner @PSBoundParameters -Name 'Backup Operators'
Invoke-ACLScanner @PSBoundParameters -Name 'Group Policy Creator Owners'
# the krbtgt account
Invoke-ACLScanner @PSBoundParameters -Name 'Krbtgt'
# the main containers
$ObjectName = "CN=Users,$DomainDN"
Invoke-ACLScanner @PSBoundParameters -DistinguishedName $ObjectName
$ObjectName = "CN=Computers,$DomainDN"
Invoke-ACLScanner @PSBoundParameters -DistinguishedName $ObjectName
$ObjectName = "CN=System,$DomainDN"
Invoke-ACLScanner @PSBoundParameters -DistinguishedName $ObjectName
$ObjectName = "CN=Policies,CN=System,$DomainDN"
Invoke-ACLScanner @PSBoundParameters -DistinguishedName $ObjectName
$ObjectName = "CN=Managed Service Accounts,$DomainDN"
Invoke-ACLScanner @PSBoundParameters -DistinguishedName $ObjectName
# the AdminSDHolder object
$ObjectName = "CN=AdminSDHolder,CN=System,$DomainDN"
Invoke-ACLScanner @PSBoundParameters -DistinguishedName $ObjectName
#Analyze every OUs, if it's not Full scan it analyzes only the Domain Controller OU
$domainOU = Get-NetOU -Domain $Domain
$counter = 0
$numberOU = $domainOU.count
foreach ($OU in $domainOU){
$counter++
$OUdn = 'None'
$NameArray = $OU -Split(“/”)
[int]$NameCount = 0
ForEach ($NameCell in $NameArray)
{
$NameCount++
if ($NameCount -eq 4){
$OUdn = $NameCell
}
}
if ($OUdn -match "Domain Controller"){
if ($Full -eq $True){
if ($counter -eq 1) {
Write-Output "Finished 13 analysis queries, there are still $numberOU more"
}
}
Invoke-ACLScanner @PSBoundParameters -DistinguishedName $OUdn
}
else {
if ($Full -eq $True){
if ($counter -eq 1) {
Write-Output "Finished 13 analysis queries, there are still $numberOU more"
}
Invoke-ACLScanner @PSBoundParameters -DistinguishedName $OUdn
}
}
}
Write-Output "Finish first analysis on Domain: $Domain"
$Domaintime.Stop()
$runtime = $Domaintime.Elapsed.TotalMilliseconds
$runtime = ($runtime/1000)
$runtimeMin = ($runtime/60)
$runtimeHours = ($runtime/3600)
$runtime = [math]::round($runtime , 2)
$runtimeMin = [math]::round($runtimeMin , 2)
$runtimeHours = [math]::round($runtimeHours , 3)
Write-Host "Time elapsed for this stage: $runtime Second, $runtimeMin Minutes, $runtimeHours Hours"
$Domaintime.reset()
$Domaintime.start()
$NewListACLs = @()
$ListObjectDNs = @()
$domainGroups = Get-NetGroup -Domain $Domain
if ($domainGroups.count -eq 0){
write-warning "There was a critical problem of getting the domain groups"
}
$ObjectMembersList = @{}
$counterLines = 0
$NameCount = 0
Import-Csv $exportCsvFile | Where-Object {$_} | ForEach-Object {
If ($ListObjectDNs -notcontains $_.ObjectDN)
{
$ListObjectDNs += $_.ObjectDN
}
#adding group members
$GroupMembers = $Null
$EntityType = "Other"
$domainGroupName = "None"
$NameArray = $_.UpdatedIdentityReference -Split(“\\”)
$NameCount = 0
ForEach ($NameCell in $NameArray)
{
$NameCount++
if ($NameCount -eq 1)
{continue}
else
{$domainGroupName = $NameCell}
}
if ($domainGroups -contains $domainGroupName) {
$EntityType = "Group"
if ($GroupMembersDB.ContainsKey($domainGroupName)){
$GroupMembers = $GroupMembersDB.$domainGroupName
}
else {
try {
$GroupMembersRecursive = Get-NetGroupMember -Domain $Domain -Recurse -UseMatchingRule -GroupName $domainGroupName
}
catch {
$GroupMembersRecursive = Get-NetGroupMember -Domain $Domain -GroupName $domainGroupName
Write-Warning $_
}
$GroupMembers = @()
foreach ($Entity in $GroupMembersRecursive){
if (!($GroupMembers -match $Entity.MemberName)){
$GroupMembers += $Entity.MemberName
}
}
$GroupMembersDB.add($domainGroupName, $GroupMembers)
$GroupMembersRecursive = $Null
}
}
$GroupMembersCount = $GroupMembers.count
#create the $ObjectMembersList
$isMemberOfOtherGroups = $Null
if ($domainGroups -contains $domainGroupName) {
if ($ObjectMembersList.ContainsKey($_.ObjectDN)){
$ObjectDN = $_.ObjectDN
foreach ($user in $GroupMembers){
if ($ObjectMembersList.$ObjectDN -notcontains $domainGroupName){
$ObjectMembersList.$ObjectDN += $domainGroupName
if ($ObjectMembersList.$ObjectDN -notcontains $user){
$ObjectMembersList.$ObjectDN += $user
}
}
}
}
else {
$ObjectMembersList.add($_.ObjectDN, $GroupMembers)
}
}
#in the future step it checks the class of the object
$ObjectClassCategory = $Null
#creates the structure to output the csv
$ObjectACE = [PSCustomObject][ordered] @{
#$ObjectACE = [PSCustomObject] @{
ObjectDN = [string]$_.ObjectDN
ObjectOwner = [string]$_.ObjectOwner
EntityName = [string]$_.UpdatedIdentityReference
ActiveDirectoryRights = [string]$_.ActiveDirectoryRights
ObjectRights = [string]$_.ObjectType
ObjectClass = [string]$_.ObjectClass
ObjectClassCategory = [string]$ObjectClassCategory
EntityType = [string]$EntityType
EntityGroupMembers = [string]$GroupMembers
EntityGroupMembersCount = [string]$GroupMembersCount
isMemberOfOtherGroups = [string]$isMemberOfOtherGroups
IsInherited = [string]$_.IsInherited
PropagationFlags = [string]$_.PropagationFlags
InheritanceFlags = [string]$_.InheritanceFlags
InheritedObjectType = [string]$_.InheritedObjectType
InheritanceType = [string]$_.InheritanceType
ObjectFlags = [string]$_.ObjectFlags
AccessControlType = [string]$_.AccessControlType
ObjectSID = [string]$_.ObjectSID
IdentitySID = [string]$_.IdentitySID
}
$NewListACLs += $ObjectACE
#counter
$counterLines++
$counter = $counterLines
if (($counter %= 50000) -eq 0){
write-host "$counterLines Permission lines were finished"
}
$_ = $Null
}
Write-Output "`nGood, the second stage was over"
$Domaintime.Stop()
$runtime = $Domaintime.Elapsed.TotalMilliseconds
$runtime = ($runtime/1000)
$runtimeMin = ($runtime/60)
$runtimeHours = ($runtime/3600)
$runtime = [math]::round($runtime , 2)
$runtimeMin = [math]::round($runtimeMin , 2)
$runtimeHours = [math]::round($runtimeHours , 3)
Write-Host "Time of the second stage: $runtime Second, $runtimeMin Minutes, $runtimeHours Hours"
$Domaintime.reset()
$Domaintime.start()
foreach ($ACE in $NewListACLs){
#check object class
$ObjectClassCategory = $ACE.ObjectClass
if ($ACE.ObjectClass -match "domain") {$ObjectClassCategory = "Domain"}
elseif ($ACE.ObjectClass -match "container") {$ObjectClassCategory = "Container"}
elseif ($ACE.ObjectClass -match "group") {$ObjectClassCategory = "Group"}
elseif ($ACE.ObjectClass -match "computer") {$ObjectClassCategory = "Computer"}
elseif ($ACE.ObjectClass -match "user") {$ObjectClassCategory = "User"}
elseif ($ACE.ObjectClass -match "dns") {$ObjectClassCategory = "DNS"}
elseif ($ACE.ObjectClass -match "organizationalUnit") {$ObjectClassCategory = "OU"}
$ACE.ObjectClassCategory = $ObjectClassCategory
try {
$isMemberOfOtherGroups = 'False'
$NameArray = $ACE.EntityName -Split(“\\”)
[int]$NameCount = 0
ForEach ($NameCell in $NameArray)
{
$NameCount++
if ($NameCount -eq 1)
{continue}
else
{$userName = $NameCell}
}
$ObjectDN = $ACE.ObjectDN
if ($userName.count -gt 0) {
if ($ObjectMembersList.$ObjectDN -contains $userName) {
$isMemberOfOtherGroups = 'True'
}
}
$ACE.isMemberOfOtherGroups = $isMemberOfOtherGroups
}
catch{}
}
$numObjectAnalyzed = $ListObjectDNs.Count
$NewListACLs | Export-Csv -NoTypeInformation $exportCsvFile
Write-Output "`nExcellent, The scan for $Domain was finished - check the results file:"
Write-Output $exportCsvFile
Write-Output "`nNumber of Objects that were Analyzed: $numObjectAnalyzed"
$DomainTotaltime.Stop()
$runtime = $DomainTotaltime.Elapsed.TotalMilliseconds
$runtime = ($runtime/1000)
$runtimeMin = ($runtime/60)
$runtimeHours = ($runtime/3600)
$runtime = [math]::round($runtime , 2)
$runtimeMin = [math]::round($runtimeMin , 2)
$runtimeHours = [math]::round($runtimeHours , 3)
Write-Host "The scan for this Domain took: $runtime Second, $runtimeMin Minutes, $runtimeHours Hours"
}
# Function to reorder the results for more straight-forward output
function OrderPermissionsByAccounts {
[CmdletBinding()]
Param (
[String]
$inputCSV,
[String]
$Domain,
[array]
$privilegedAccountList,
[hashtable]
$domainsPrivilegedAccountDB,
[String]
$exportCsvFolder
)
$newAccountPermissionList = @()
$owner = "ObjectOwner"
$privDomainAcc = $domainsPrivilegedAccountDB.$Domain
Import-Csv $inputCSV | Where-Object {$_} | ForEach-Object {
foreach($account in $privilegedAccountList){
if (($_.EntityName -eq $account) -or ($_.EntityGroupMembers -eq $account)){
$accountPermissionLine = [PSCustomObject][ordered] @{
Domain = [string]$Domain
AccountName = [string]$account
AccountGroup = [string]$_.EntityName
ActiveDirectoryRights = [string]$_.ActiveDirectoryRights
ObjectRights = [string]$_.ObjectRights
ObjectDN = [string]$_.ObjectDN
ObjectOwner = [string]$_.ObjectOwner
ObjectClassCategory = [string]$_.ObjectClassCategory
}
$newAccountPermissionList += $accountPermissionLine
}
else{
if ($_.ObjectOwner -eq $account){
$accountPermissionLine = [PSCustomObject][ordered] @{
Domain = [string]$Domain
AccountName = [string]$account
AccountGroup = [string]$_.ObjectOwner
ActiveDirectoryRights = [string]$owner
ObjectRights = [string]$owner
ObjectDN = [string]$_.ObjectDN
ObjectOwner = [string]$_.ObjectOwner
ObjectClassCategory = [string]$_.ObjectClassCategory
}
$newAccountPermissionList += $accountPermissionLine
}
}
}
foreach($account in $privDomainAcc){
if ($_.EntityGroupMembers -match $account){
$accountPermissionLine = [PSCustomObject][ordered] @{
Domain = [string]$Domain
AccountName = [string]$account
AccountGroup = [string]$_.EntityName
ActiveDirectoryRights = [string]$_.ActiveDirectoryRights
ObjectRights = [string]$_.ObjectRights
ObjectDN = [string]$_.ObjectDN
ObjectOwner = [string]$_.ObjectOwner
ObjectClassCategory = [string]$_.ObjectClassCategory
}
$newAccountPermissionList += $accountPermissionLine
}
}
}
$exportCsvFolder += $Domain
$exportCsvFolder += " - Sensitive Accounts.csv"
$exportAccCsvFile = $exportCsvFolder
$newAccountPermissionList | sort AccountName, AccountGroup, Domain, ObjectDN | Export-Csv -NoTypeInformation $exportAccCsvFile
}
# The main function - here it's the starting point of the Privileged ACLs scan
function Start-ACLsAnalysis {
<#
.SYNOPSIS
Thi is the function to start the ACLs advanced scan.
It will do analysis of the Permissions and ACLs on all the domains in the forest - automatically.
In the end of the scanning - there will be good reports in the output folder.
The scan will discover who are the privileged accounts in the forest and what permissions exactly they have.
.EXAMPLE
1. Open PowerShell
2. Import-Module '.\ACLight.psm1' -force
3. Start-ACLsAnalysis
#>
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline=$True)]
[String]
$Full = $False,
[String]
$SamAccountName,
[String]
$Name = "*",
[Alias('DN')]
[String]
$DistinguishedName = "*",
[String]
$Filter,
[String]
$ADSpath,
[String]
$ADSprefix,
[String]
$Domain,
[String]
$DomainController,
[String]
$ScriptRoot = $PSScriptRoot,
[String]
$exportCsvFolder = "$resultsPath",
[ValidateRange(1,10000)]
[Int]
$PageSize = 200
)
if ($PSVersionTable.PSVersion.Major -ge 3){
$time = New-Object system.Diagnostics.Stopwatch
$stagetime = New-Object system.Diagnostics.Stopwatch
$time.Start()
Write-Output "`nGreat, the scan was started.`nIt could take a while (5-60+ mins) depends on the size of the network`n"
$PathFolder = $exportCsvFolder
$PathFolder = $PathFolder.substring($PathFolder.length - 1, 1)
if ($PathFolder -ne "\"){
$exportCsvFolder += "\"
}
$DomainList = Get-NetForestDomain
$count = 0
$privilegedAccountList = @()
$privilegedAllList = @()
$processPointersList = @()
$domainsPrivilegedAccountDB = @{}
$domainNumber = $DomainList.count
Write-Output "Discovered $domainNumber Domain"
# run ACLs analysis on every domain
foreach ($Domain in $DomainList){
Write-Output "`n******************************`nOpened process for analyzing Domain: $Domain`n"
$exportCsvFile = $exportCsvFolder
$exportCsvFile += $Domain
$exportCsvFile += " - Full Output.csv"
$exportCsvFile = '\"' + $exportCsvFile + '\"'
# The scan will automatically scan all the domain in a parallel time - it's much more time efficient
$processPointer = start-process powershell.exe -PassThru -WorkingDirectory $ScriptRoot -argument "-noprofile -ExecutionPolicy Bypass Import-Module '.\ACLight.psm1' -force ; Start-domainACLsAnalysis -Full $Full -exportCsvFile $exportCsvFile -Domain $Domain"
$processPointersList += $processPointer
# if you don't won't to scan all the domains in parallel (but one after the other):
#$processPointer | Wait-Process
}
Write-Output "Waiting for all the scans to be completed.."
foreach ($processPt in $processPointersList){
try{
$processPt | Wait-Process
}
catch{
}
}
Write-Output "All the processes completed. Now, starting Accounts analysis.."
foreach ($Domain in $DomainList){
$exportCsvFile = $exportCsvFolder
$exportCsvFile += $Domain
$exportCsvFile += " - Full Output.csv"
#create the final list of privileged accounts
$privilegedDomainAccountList = @()
$domainGroups = Get-NetGroup -Domain $Domain
$domainUsers = Get-NetUser -Domain $Domain
$domainUserList = @()
$privDomain = @()
foreach ($userAccount in $domainUsers){
$domainUserList += $domainUsers.name
}
Import-Csv $exportCsvFile | Where-Object {$_} | ForEach-Object {
If ($privilegedDomainAccountList -notcontains $_.ObjectOwner){
$privilegedDomainAccountList += $_.ObjectOwner
}
If ($privilegedDomainAccountList -notcontains $_.EntityName){
$privilegedDomainAccountList += $_.EntityName
}
}
$EntityStartName = ""
foreach ($fullNameEntity in $privilegedDomainAccountList){
$domainEntityName = $fullNameEntity
if ($fullNameEntity -match "\\"){
$NameArray = $fullNameEntity -Split(“\\”)
$NameCount = 0
ForEach ($NameCell in $NameArray)
{
$NameCount++
if ($NameCount -eq 1){
$EntityStartName = $NameCell
}
else
{$domainEntityName = $NameCell}
}
}
if ($privilegedAllList -notcontains $fullNameEntity){
$privilegedAllList += $fullNameEntity
}
if ($EntityStartName -notmatch "BUILTIN"){
if ($domainGroups -contains $domainEntityName){
try {
$GroupMembersRecursive = Get-NetGroupMember -domain $Domain -Recurse -UseMatchingRule -GroupName $domainEntityName
}
catch {
$GroupMembersRecursive = Get-NetGroupMember -domain $Domain -GroupName $domainEntityName
Write-Warning $_
}
foreach ($accountName in $GroupMembersRecursive){
$accountDomainName = $EntityStartName + "\" + $accountName.MemberName
if ($privilegedAccountList -notcontains $accountDomainName){
$privilegedAccountList += $accountDomainName
#create hash table for accounts by their domain values
if ($privilegedAllList -notcontains $accountDomainName){
$privilegedAllList += $accountDomainName
}
}
$accountN = $accountName.MemberName
if ($privDomain -notcontains $accountN){
$privDomain += $accountN
}
}
}
else {
if ($domainUserList -contains $domainEntityName){
if ($privilegedAccountList -notcontains $fullNameEntity ){
$privilegedAccountList += $fullNameEntity
}
if ($privDomain -notcontains $domainEntityName){
$privDomain += $domainEntityName
}
}
}
}
# adding a special test for the dangerous case of "Authenticated Users"
if ($fullNameEntity -like "NT AUTHORITY\Authenticated Users"){
if ($privilegedAccountList -notcontains $fullNameEntity ){
$privilegedAccountList += $fullNameEntity
}
}
}
$domainsPrivilegedAccountDB.add($Domain, $privDomain)
$exportCsvFile = $exportCsvFolder
$exportCsvFile += $Domain
$exportCsvFile += " - Full Output.csv"
OrderPermissionsByAccounts -inputCSV $exportCsvFile -Domain $Domain -domainsPrivilegedAccountDB $domainsPrivilegedAccountDB -privilegedAccountList $privilegedAccountList -exportCsvFolder $exportCsvFolder
}
$exportAllAccCsvFile = $exportCsvFolder
$exportAllAccCsvFile += "Privileged Accounts Permissions - Final Report.csv"
$exportAllIrregularAccCsvFile = $exportCsvFolder + "Privileged Accounts Permissions - Irregular Accounts.csv"
if (Test-Path $exportAllAccCsvFile) {
Remove-Item $exportAllAccCsvFile
}
if (Test-Path $exportAllIrregularAccCsvFile) {
Remove-Item $exportAllIrregularAccCsvFile
}
foreach ($Domain in $DomainList){
$exportAccCsvFile = $exportCsvFolder
$exportAccCsvFile += $Domain
$exportAccCsvFile += " - Sensitive Accounts.csv"
$importedCsvData = Import-Csv $exportAccCsvFile
$importedCsvData | sort Domain,AccountName,AccountGroup,ActiveDirectoryRights,ObjectRights,ObjectDN,ObjectOwner,ObjectClassCategory -Unique | Export-Csv -NoTypeInformation –Append $exportAllAccCsvFile
$importedCsvData | Where { ($_.AccountGroup -eq $_.AccountName)} | sort Domain,AccountName,AccountGroup,ActiveDirectoryRights,ObjectRights,ObjectDN,ObjectOwner,ObjectClassCategory -Unique | Export-Csv -NoTypeInformation –Append $exportAllIrregularAccCsvFile
if (Test-Path $exportAccCsvFile) {
Remove-Item $exportAccCsvFile
}
}
Write-Host "Finished Account analysis"
#create the final list of the privileged Accounts
$exportListFile = $exportCsvFolder + "Accounts with extra permissions.txt"
$privilegedAccountList | sort | Out-File $exportListFile
$numberAccounts = $privilegedAccountList.count
Write-host "`nDiscovered $numberAccounts privileged accounts" -ForegroundColor Yellow
Write-host "Check the list of the accounts with extra permissions:`n$exportListFile"
write-host "`nPrivileged ACLs scan completed - the results are in the folder:`n$exportCsvFolder`nCheck the `"Final Report`""-ForegroundColor Yellow
$exportListFile = $exportCsvFolder + "All entities with extra permissions.txt"
$privilegedAllList | sort | Out-File $exportListFile
$time.Stop()
$runtime = $time.Elapsed.TotalMilliseconds
$runtime = ($runtime/1000)
$runtimeMin = ($runtime/60)
$runtimeHours = ($runtime/3600)
$runtime = [math]::round($runtime , 2)
$runtimeMin = [math]::round($runtimeMin , 2)
$runtimeHours = [math]::round($runtimeHours , 3)
#Write-Output "`n----------FINISHED----------`n`nTotal time of the scaning: $runtime Second, $runtimeMin Minutes, $runtimeHours Hours"
#Write-Output "Check the results files in the folder: `n$exportCsvFolder `n"
}
else {
Write-Output "`nSorry,`nThe tool need powershell version 3 or higher to perform the efficient Permissions scan`nYou can upgrade the PowerShell version from Microsoft official website:`nhttps://www.microsoft.com/en-us/download/details.aspx?id=34595`n`nFinished without running.`n"
}
}
###############################################################
# #
# Section 2 - functions from PowerView #
# The filter in Invoke-ACLScanner function was modified #
# #
###############################################################
function Get-NetUser {
<#
.SYNOPSIS
Query information for a given user or users in the domain
using ADSI and LDAP. Another -Domain can be specified to
query for users across a trust.
Replacement for "net users /domain"
.PARAMETER UserName
Username filter string, wildcards accepted.
.PARAMETER Domain
The domain to query for users, defaults to the current domain.
.PARAMETER DomainController
Domain controller to reflect LDAP queries through.
.PARAMETER ADSpath
The LDAP source to search through, e.g. "LDAP://OU=secret,DC=testlab,DC=local"
Useful for OU queries.
.PARAMETER Filter
A customized ldap filter string to use, e.g. "(description=*admin*)"
.PARAMETER AdminCount
Switch. Return users with adminCount=1.
.PARAMETER SPN
Switch. Only return user objects with non-null service principal names.
.PARAMETER Unconstrained
Switch. Return users that have unconstrained delegation.
.PARAMETER AllowDelegation
Switch. Return user accounts that are not marked as 'sensitive and not allowed for delegation'
.PARAMETER PageSize
The PageSize to set for the LDAP searcher object.
.EXAMPLE
PS C:\> Get-NetUser -Domain testing
.EXAMPLE
PS C:\> Get-NetUser -ADSpath "LDAP://OU=secret,DC=testlab,DC=local"
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$True)]
[String]
$UserName,
[String]
$Domain,
[String]
$DomainController,
[String]
$ADSpath,
[String]
$Filter,
[Switch]
$SPN,
[Switch]
$AdminCount,
[Switch]
$Unconstrained,
[Switch]
$AllowDelegation,
[ValidateRange(1,10000)]
[Int]
$PageSize = 200
)
begin {
# so this isn't repeated if users are passed on the pipeline
$UserSearcher = Get-DomainSearcher -Domain $Domain -ADSpath $ADSpath -DomainController $DomainController -PageSize $PageSize
}
process {
if($UserSearcher) {
# if we're checking for unconstrained delegation
if($Unconstrained) {
Write-Verbose "Checking for unconstrained delegation"
$Filter += "(userAccountControl:1.2.840.113556.1.4.803:=524288)"
}
if($AllowDelegation) {
Write-Verbose "Checking for users who can be delegated"
# negation of "Accounts that are sensitive and not trusted for delegation"
$Filter += "(!(userAccountControl:1.2.840.113556.1.4.803:=1048574))"
}
if($AdminCount) {
Write-Verbose "Checking for adminCount=1"
$Filter += "(admincount=1)"
}
# check if we're using a username filter or not
if($UserName) {
# samAccountType=805306368 indicates user objects
$UserSearcher.filter="(&(samAccountType=805306368)(samAccountName=$UserName)$Filter)"
}
elseif($SPN) {
$UserSearcher.filter="(&(samAccountType=805306368)(servicePrincipalName=*)$Filter)"
}
else {
# filter is something like "(samAccountName=*blah*)" if specified
$UserSearcher.filter="(&(samAccountType=805306368)$Filter)"
}
$UserSearcher.FindAll() | Where-Object {$_} | ForEach-Object {
# convert/process the LDAP fields for each result
Convert-LDAPProperty -Properties $_.Properties
}
}
}
}
function Get-NetForest {
<#
.SYNOPSIS
Returns a given forest object.
.PARAMETER Forest
The forest name to query for, defaults to the current domain.
.EXAMPLE
PS C:\> Get-NetForest -Forest external.domain
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$True)]
[String]
$Forest
)
process {
if($Forest) {
$ForestContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext('Forest', $Forest)
try {
$ForestObject = [System.DirectoryServices.ActiveDirectory.Forest]::GetForest($ForestContext)
}
catch {
Write-Debug "The specified forest $Forest does not exist, could not be contacted, or there isn't an existing trust."
$Null
}
}
else {
# otherwise use the current forest
$ForestObject = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
}
if($ForestObject) {
# get the SID of the forest root
$ForestSid = (New-Object System.Security.Principal.NTAccount($ForestObject.RootDomain,"krbtgt")).Translate([System.Security.Principal.SecurityIdentifier]).Value
$Parts = $ForestSid -Split "-"
$ForestSid = $Parts[0..$($Parts.length-2)] -join "-"
$ForestObject | Add-Member NoteProperty 'RootDomainSid' $ForestSid
$ForestObject
}
}
}
function Get-NetForestDomain {
<#
.SYNOPSIS
Return all domains for a given forest.
.PARAMETER Forest
The forest name to query domain for.
.PARAMETER Domain
Return domains that match this term/wildcard.
.EXAMPLE
PS C:\> Get-NetForestDomain
.EXAMPLE
PS C:\> Get-NetForestDomain -Forest external.local
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$True)]
[String]
$Forest,
[String]
$Domain
)
process {
if($Domain) {
# try to detect a wild card so we use -like
if($Domain.Contains('*')) {
(Get-NetForest -Forest $Forest).Domains | Where-Object {$_.Name -like $Domain}
}
else {
# match the exact domain name if there's not a wildcard
(Get-NetForest -Forest $Forest).Domains | Where-Object {$_.Name.ToLower() -eq $Domain.ToLower()}
}
}
else {
# return all domains
$ForestObject = Get-NetForest -Forest $Forest
if($ForestObject) {
$ForestObject.Domains
}
}
}
}
function Get-NetDomain {
<#
.SYNOPSIS
Returns a given domain object.
.PARAMETER Domain
The domain name to query for, defaults to the current domain.
.EXAMPLE
PS C:\> Get-NetDomain -Domain testlab.local
.LINK