forked from jakehildreth/Locksmith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoke-Locksmith.ps1
1609 lines (1528 loc) · 78.7 KB
/
Invoke-Locksmith.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
param (
[int]$Mode,
[Parameter()]
[ValidateSet('Auditing', 'ESC1', 'ESC2', 'ESC3', 'ESC4', 'ESC5', 'ESC6', 'ESC8', 'All', 'PromptMe')]
[array]$Scans = 'All'
)
function ConvertFrom-IdentityReference {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[array]$Object
)
$Principal = New-Object System.Security.Principal.NTAccount($Object)
if ($Principal -match '^(S-1|O:)') {
$SID = $Principal
}
else {
$SID = ($Principal.Translate([System.Security.Principal.SecurityIdentifier])).Value
}
return $SID
}
function Export-RevertScript {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[array]$AuditingIssues,
[Parameter(Mandatory = $false)]
[array]$ESC1,
[Parameter(Mandatory = $false)]
[array]$ESC2,
[Parameter(Mandatory = $false)]
[array]$ESC6
)
begin {
$Output = 'Invoke-RevertLocksmith.ps1'
Set-Content -Path $Output -Value "<#`nScript to revert changes performed by Locksmith`nCreated $(Get-Date)`n#>" -Force
$Objects = $AuditingIssues + $ESC1 + $ESC2 + $ESC6
}
process {
if ($Objects) {
$Objects | ForEach-Object {
Add-Content -Path $Output -Value $_.Revert
Start-Sleep -Seconds 5
}
}
}
}
function Find-AuditingIssue {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[array]$ADCSObjects
)
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKIEnrollmentService') -and
($_.AuditFilter -ne '127')
} | ForEach-Object {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name 'Forest' -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name 'Name' -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name 'DistinguishedName' -Value $_.DistinguishedName -Force
if ($_.AuditFilter -match 'CA Unavailable') {
$Issue | Add-Member -MemberType NoteProperty -Name 'Issue' -Value $_.AuditFilter -Force
$Issue | Add-Member -MemberType NoteProperty -Name 'Fix' -Value 'N/A' -Force
$Issue | Add-Member -MemberType NoteProperty -Name 'Revert' -Value 'N/A' -Force
$Issue | Add-Member -MemberType NoteProperty -Name 'Technique' -Value 'DETECT' -Force
}
else {
$Issue | Add-Member -MemberType NoteProperty -Name 'Issue' -Value "Auditing is not fully enabled on $($_.CAFullName). Current value is $($_.AuditFilter)" -Force
$Issue | Add-Member -MemberType NoteProperty -Name 'Fix' `
-Value "certutil.exe -config `'$($_.CAFullname)`' -setreg `'CA\AuditFilter`' 127; Invoke-Command -ComputerName `'$($_.dNSHostName)`' -ScriptBlock { Get-Service -Name `'certsvc`' | Restart-Service -Force }" -Force
$Issue | Add-Member -MemberType NoteProperty -Name 'Revert' `
-Value "certutil.exe -config $($_.CAFullname) -setreg CA\AuditFilter $($_.AuditFilter); Invoke-Command -ComputerName `'$($_.dNSHostName)`' -ScriptBlock { Get-Service -Name `'certsvc`' | Restart-Service -Force }" -Force
$Issue | Add-Member -MemberType NoteProperty -Name 'Technique' -Value 'DETECT' -Force
}
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name 'Severity' -Value $Severity
$Issue
}
}
function Find-ESC1 {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[array]$ADCSObjects,
[Parameter(Mandatory = $true)]
[array]$SafeUsers
)
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKICertificateTemplate') -and
($_.pkiExtendedKeyUsage -match $ClientAuthEKUs) -and
($_.'msPKI-Certificate-Name-Flag' -eq 1) -and
!($_.'msPKI-Enrollment-Flag' -band 2) -and
( ($_.'msPKI-RA-Signature' -eq 0) -or ($null -eq $_.'msPKI-RA-Signature') )
} | ForEach-Object {
foreach ($entry in $_.nTSecurityDescriptor.Access) {
$Principal = New-Object System.Security.Principal.NTAccount($entry.IdentityReference)
if ($Principal -match '^(S-1|O:)') {
$SID = $Principal
}
else {
$SID = ($Principal.Translate([System.Security.Principal.SecurityIdentifier])).Value
}
if ( ($SID -notmatch $SafeUsers) -and ($entry.ActiveDirectoryRights -match 'ExtendedRight') ) {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
$Issue | Add-Member -MemberType NoteProperty -Name IdentityReference -Value $entry.IdentityReference -Force
$Issue | Add-Member -MemberType NoteProperty -Name ActiveDirectoryRights -Value $entry.ActiveDirectoryRights -Force
$Issue | Add-Member -MemberType NoteProperty -Name Issue `
-Value "$($entry.IdentityReference) can enroll in this Client Authentication template using a SAN without Manager Approval" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix `
-Value "Get-ADObject `'$($_.DistinguishedName)`' | Set-ADObject -Replace @{'msPKI-Certificate-Name-Flag' = 0}" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert `
-Value "Get-ADObject `'$($_.DistinguishedName)`' | Set-ADObject -Replace @{'msPKI-Certificate-Name-Flag' = 1}" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC1'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
}
}
}
function Find-ESC2 {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[array]$ADCSObjects,
[Parameter(Mandatory = $true)]
[string]$SafeUsers
)
$ADCSObjects | Where-Object {
($_.ObjectClass -eq 'pKICertificateTemplate') -and
( (!$_.pkiExtendedKeyUsage) -or ($_.pkiExtendedKeyUsage -match '2.5.29.37.0') ) -and
($_.'msPKI-Certificate-Name-Flag' -eq 1) -and
!($_.'msPKI-Enrollment-Flag' -band 2) -and
( ($_.'msPKI-RA-Signature' -eq 0) -or ($null -eq $_.'msPKI-RA-Signature') )
} | ForEach-Object {
foreach ($entry in $_.nTSecurityDescriptor.Access) {
$Principal = New-Object System.Security.Principal.NTAccount($entry.IdentityReference)
if ($Principal -match '^(S-1|O:)') {
$SID = $Principal
}
else {
$SID = ($Principal.Translate([System.Security.Principal.SecurityIdentifier])).Value
}
if ( ($SID -notmatch $SafeUsers) -and ($entry.ActiveDirectoryRights -match 'ExtendedRight') ) {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
$Issue | Add-Member -MemberType NoteProperty -Name IdentityReference -Value $entry.IdentityReference -Force
$Issue | Add-Member -MemberType NoteProperty -Name ActiveDirectoryRights -Value $entry.ActiveDirectoryRights -Force
$Issue | Add-Member -MemberType NoteProperty -Name Issue `
-Value "$($entry.IdentityReference) can request a SubCA certificate without Manager Approval" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix `
-Value "Get-ADObject `'$($_.DistinguishedName)`' | Set-ADObject -Replace @{'msPKI-Certificate-Name-Flag' = 0}" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert `
-Value "Get-ADObject `'$($_.DistinguishedName)`' | Set-ADObject -Replace @{'msPKI-Certificate-Name-Flag' = 1}" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC2'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
}
}
}
function Find-ESC3Condition1 {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[array]$ADCSObjects,
[Parameter(Mandatory = $true)]
[array]$SafeUsers
)
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKICertificateTemplate') -and
($_.pkiExtendedKeyUsage -match $EnrollmentAgentEKU) -and
!($_.'msPKI-Enrollment-Flag' -band 2) -and
( ($_.'msPKI-RA-Signature' -eq 0) -or ($null -eq $_.'msPKI-RA-Signature') )
} | ForEach-Object {
foreach ($entry in $_.nTSecurityDescriptor.Access) {
$Principal = New-Object System.Security.Principal.NTAccount($entry.IdentityReference)
if ($Principal -match '^(S-1|O:)') {
$SID = $Principal
}
else {
$SID = ($Principal.Translate([System.Security.Principal.SecurityIdentifier])).Value
}
if ( ($SID -notmatch $SafeUsers) -and ($entry.ActiveDirectoryRights -match 'ExtendedRight') ) {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
$Issue | Add-Member -MemberType NoteProperty -Name IdentityReference -Value $entry.IdentityReference -Force
$Issue | Add-Member -MemberType NoteProperty -Name ActiveDirectoryRights -Value $entry.ActiveDirectoryRights -Force
$Issue | Add-Member -MemberType NoteProperty -Name Issue `
-Value "$($entry.IdentityReference) can enroll in this Enrollment Agent template without Manager Approval" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix `
-Value "Get-ADObject `'$($_.DistinguishedName)`' | Set-ADObject -Replace @{'msPKI-Certificate-Name-Flag' = 0}" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert `
-Value "Get-ADObject `'$($_.DistinguishedName)`' | Set-ADObject -Replace @{'msPKI-Certificate-Name-Flag' = 1}" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC3'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
}
}
}
function Find-ESC3Condition2 {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[array]$ADCSObjects,
[Parameter(Mandatory = $true)]
[array]$SafeUsers
)
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKICertificateTemplate') -and
($_.pkiExtendedKeyUsage -match $ClientAuthEKU) -and
($_.'msPKI-Certificate-Name-Flag' -eq 1) -and
!($_.'msPKI-Enrollment-Flag' -band 2) -and
($_.'msPKI-RA-Application-Policies' -eq '1.3.6.1.4.1.311.20.2.1') -and
( ($_.'msPKI-RA-Signature' -eq 1) )
} | ForEach-Object {
foreach ($entry in $_.nTSecurityDescriptor.Access) {
$Principal = New-Object System.Security.Principal.NTAccount($entry.IdentityReference)
if ($Principal -match '^(S-1|O:)') {
$SID = $Principal
}
else {
$SID = ($Principal.Translate([System.Security.Principal.SecurityIdentifier])).Value
}
if ( ($SID -notmatch $SafeUsers) -and ($entry.ActiveDirectoryRights -match 'ExtendedRight') ) {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
$Issue | Add-Member -MemberType NoteProperty -Name IdentityReference -Value $entry.IdentityReference -Force
$Issue | Add-Member -MemberType NoteProperty -Name ActiveDirectoryRights -Value $entry.ActiveDirectoryRights -Force
$Issue | Add-Member -MemberType NoteProperty -Name Issue `
-Value "$($entry.IdentityReference) can enroll in this Client Authentication template using a SAN without Manager Approval" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix `
-Value "Get-ADObject `'$($_.DistinguishedName)`' | Set-ADObject -Replace @{'msPKI-Certificate-Name-Flag' = 0}" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert `
-Value "Get-ADObject `'$($_.DistinguishedName)`' | Set-ADObject -Replace @{'msPKI-Certificate-Name-Flag' = 1}" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC3'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
}
}
}
function Find-ESC4 {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
$ADCSObjects,
[Parameter(Mandatory = $true)]
$DangerousRights,
[Parameter(Mandatory = $true)]
$SafeOwners,
[Parameter(Mandatory = $true)]
$SafeUsers
)
$ADCSObjects | ForEach-Object {
$Principal = New-Object System.Security.Principal.NTAccount($_.nTSecurityDescriptor.Owner)
if ($Principal -match '^(S-1|O:)') {
$SID = $Principal
}
else {
$SID = ($Principal.Translate([System.Security.Principal.SecurityIdentifier])).Value
}
if ( ($_.objectClass -eq 'pKICertificateTemplate') -and ($SID -match $UnsafeOwners) ) {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
$Issue | Add-Member -MemberType NoteProperty -Name IdentityReference -Value $entry.IdentityReference -Force
$Issue | Add-Member -MemberType NoteProperty -Name ActiveDirectoryRights -Value $entry.ActiveDirectoryRights -Force
$Issue | Add-Member -MemberType NoteProperty -Name Issue `
-Value "$($_.nTSecurityDescriptor.Owner) has Owner rights on this template" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix -Value "`$Owner = New-Object System.Security.Principal.SecurityIdentifier(`'$PreferredOwner`'); `$ACL = Get-Acl -Path `'AD:$($_.DistinguishedName)`'; `$ACL.SetOwner(`$Owner); Set-ACL -Path `'AD:$($_.DistinguishedName)`' -AclObject `$ACL" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert -Value "`$Owner = New-Object System.Security.Principal.SecurityIdentifier(`'$($_.nTSecurityDescriptor.Owner)`'); `$ACL = Get-Acl -Path `'AD:$($_.DistinguishedName)`'; `$ACL.SetOwner(`$Owner); Set-ACL -Path `'AD:$($_.DistinguishedName)`' -AclObject `$ACL" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC4'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
elseif ( ($_.objectClass -eq 'pKICertificateTemplate') -and ($SID -notmatch $SafeOwners) ) {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
$Issue | Add-Member -MemberType NoteProperty -Name IdentityReference -Value $entry.IdentityReference -Force
$Issue | Add-Member -MemberType NoteProperty -Name ActiveDirectoryRights -Value $entry.ActiveDirectoryRights -Force
$Issue | Add-Member -MemberType NoteProperty -Name Issue `
-Value "$($_.nTSecurityDescriptor.Owner) has Owner rights on this template" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix -Value '[TODO]' -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert -Value '[TODO]' -Force
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC4'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
foreach ($entry in $_.nTSecurityDescriptor.Access) {
$Principal = New-Object System.Security.Principal.NTAccount($entry.IdentityReference)
if ($Principal -match '^(S-1|O:)') {
$SID = $Principal
}
else {
$SID = ($Principal.Translate([System.Security.Principal.SecurityIdentifier])).Value
}
if ( ($_.objectClass -eq 'pKICertificateTemplate') -and
($SID -notmatch $SafeUsers) -and
($entry.ActiveDirectoryRights -match $DangerousRights) -and
($entry.ActiveDirectoryRights.ObjectType -notmatch $SafeObjectTypes)
) {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
$Issue | Add-Member -MemberType NoteProperty -Name IdentityReference -Value $entry.IdentityReference -Force
$Issue | Add-Member -MemberType NoteProperty -Name ActiveDirectoryRights -Value $entry.ActiveDirectoryRights -Force
$Issue | Add-Member -MemberType NoteProperty -Name Issue `
-Value "$($entry.IdentityReference) has $($entry.ActiveDirectoryRights) rights on this template" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix -Value "`$ACL = Get-Acl -Path `'AD:$($_.DistinguishedName)`'; foreach ( `$ace in `$ACL.access ) { if ( (`$ace.IdentityReference.Value -like '$($Principal.Value)' ) -and ( `$ace.ActiveDirectoryRights -notmatch '^ExtendedRight$') ) { `$ACL.RemoveAccessRule(`$ace) | Out-Null ; Set-Acl -Path `'AD:$($_.DistinguishedName)`' -AclObject `$ACL } }" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert -Value '[TODO]' -Force
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC4'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
}
}
}
function Find-ESC5 {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
$ADCSObjects,
[Parameter(Mandatory = $true)]
$DangerousRights,
[Parameter(Mandatory = $true)]
$SafeOwners,
[Parameter(Mandatory = $true)]
$SafeUsers
)
$ADCSObjects | ForEach-Object {
$Principal = New-Object System.Security.Principal.NTAccount($_.nTSecurityDescriptor.Owner)
if ($Principal -match '^(S-1|O:)') {
$SID = $Principal
}
else {
$SID = ($Principal.Translate([System.Security.Principal.SecurityIdentifier])).Value
}
if ( ($_.objectClass -ne 'pKICertificateTemplate') -and ($SID -match $UnsafeOwners) ) {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
$Issue | Add-Member -MemberType NoteProperty -Name IdentityReference -Value $entry.IdentityReference -Force
$Issue | Add-Member -MemberType NoteProperty -Name ActiveDirectoryRights -Value $entry.ActiveDirectoryRights -Force
$Issue | Add-Member -MemberType NoteProperty -Name Issue `
-Value "$($_.nTSecurityDescriptor.Owner) has Owner rights on this template" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix -Value "`$Owner = New-Object System.Security.Principal.SecurityIdentifier(`'$PreferredOwner`'); `$ACL = Get-Acl -Path `'AD:$($_.DistinguishedName)`'; `$ACL.SetOwner(`$Owner); Set-ACL -Path `'AD:$($_.DistinguishedName)`' -AclObject `$ACL" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert -Value "`$Owner = New-Object System.Security.Principal.SecurityIdentifier(`'$($_.nTSecurityDescriptor.Owner)`'); `$ACL = Get-Acl -Path `'AD:$($_.DistinguishedName)`'; `$ACL.SetOwner(`$Owner); Set-ACL -Path `'AD:$($_.DistinguishedName)`' -AclObject `$ACL" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC5'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
elseif ( ($_.objectClass -ne 'pKICertificateTemplate') -and
($SID -notmatch $SafeOwners) -and
($entry.ActiveDirectoryRights.ObjectType -notmatch $SafeObjectTypes)
) {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
$Issue | Add-Member -MemberType NoteProperty -Name IdentityReference -Value $entry.IdentityReference -Force
$Issue | Add-Member -MemberType NoteProperty -Name ActiveDirectoryRights -Value $entry.ActiveDirectoryRights -Force
$Issue | Add-Member -MemberType NoteProperty -Name Issue `
-Value "$($_.nTSecurityDescriptor.Owner) has Owner rights on this object" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix -Value '[TODO]' -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert -Value '[TODO]' -Force
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC5'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
foreach ($entry in $_.nTSecurityDescriptor.Access) {
$Principal = New-Object System.Security.Principal.NTAccount($entry.IdentityReference)
if ($Principal -match '^(S-1|O:)') {
$SID = $Principal
}
else {
$SID = ($Principal.Translate([System.Security.Principal.SecurityIdentifier])).Value
}
if ( ($_.objectClass -ne 'pKICertificateTemplate') -and
($SID -notmatch $SafeUsers) -and
($entry.ActiveDirectoryRights -match $DangerousRights) ) {
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
$Issue | Add-Member -MemberType NoteProperty -Name IdentityReference -Value $entry.IdentityReference -Force
$Issue | Add-Member -MemberType NoteProperty -Name ActiveDirectoryRights -Value $entry.ActiveDirectoryRights -Force
$Issue | Add-Member -MemberType NoteProperty -Name Issue `
-Value "$($entry.IdentityReference) has $($entry.ActiveDirectoryRights) rights on this object" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix -Value "`$ACL = Get-Acl -Path `'AD:$($_.DistinguishedName)`'; foreach ( `$ace in `$ACL.access ) { if ( (`$ace.IdentityReference.Value -like '$($Principal.Value)' ) -and ( `$ace.ActiveDirectoryRights -notmatch '^ExtendedRight$') ) { `$ACL.RemoveAccessRule(`$ace) | Out-Null ; Set-Acl -Path `'AD:$($_.DistinguishedName)`' -AclObject `$ACL } }" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert -Value '[TODO]' -Force
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC5'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
}
}
}
function Find-ESC6 {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
$ADCSObjects
)
process {
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKIEnrollmentService') -and
($_.SANFlag -ne 'No')
} | ForEach-Object {
[string]$CAFullName = "$($_.dNSHostName)\$($_.Name)"
$Issue = New-Object -TypeName pscustomobject
$Issue | Add-Member -MemberType NoteProperty -Name Forest -Value $_.CanonicalName.split('/')[0] -Force
$Issue | Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -Force
$Issue | Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $_.DistinguishedName -Force
if ($_.SANFlag -eq 'Yes') {
$Issue | Add-Member -MemberType NoteProperty -Name Issue -Value 'EDITF_ATTRIBUTESUBJECTALTNAME2 is enabled.' -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix `
-Value "certutil -config $CAFullname -setreg policy\EditFlags -EDITF_ATTRIBUTESUBJECTALTNAME2; Invoke-Command -ComputerName `"$($_.dNSHostName)`" -ScriptBlock { Get-Service -Name `'certsvc`' | Restart-Service -Force }" -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert `
-Value "certutil -config $CAFullname -setreg policy\EditFlags +EDITF_ATTRIBUTESUBJECTALTNAME2; Invoke-Command -ComputerName `"$($_.dNSHostName)`" -ScriptBlock { Get-Service -Name `'certsvc`' | Restart-Service -Force }" -Force
}
else {
$Issue | Add-Member -MemberType NoteProperty -Name Issue -Value $_.AuditFilter -Force
$Issue | Add-Member -MemberType NoteProperty -Name Fix -Value 'N/A' -Force
$Issue | Add-Member -MemberType NoteProperty -Name Revert -Value 'N/A' -Force
}
$Issue | Add-Member -MemberType NoteProperty -Name Technique -Value 'ESC6'
$Severity = Set-Severity -Issue $Issue
$Issue | Add-Member -MemberType NoteProperty -Name Severity -Value $Severity
$Issue
}
}
}
function Find-ESC8 {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
$ADCSObjects
)
process {
$ADCSObjects | Where-Object {
$_.CAEnrollmentEndpoint
} | ForEach-Object {
$Issue = [ordered] @{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
}
if ($_.CAEnrollmentEndpoint -like '^http*') {
$Issue['Issue'] = 'HTTP enrollment is enabled.'
$Issue['CAEnrollmentEndpoint'] = $_.CAEnrollmentEndpoint
$Issue['Fix'] = 'TBD - Remediate by doing 1, 2, and 3'
$Issue['Revert'] = 'TBD'
}
else {
$Issue['Issue'] = 'HTTPS enrollment is enabled.'
$Issue['CAEnrollmentEndpoint'] = $_.CAEnrollmentEndpoint
$Issue['Fix'] = 'TBD - Remediate by doing 1, 2, and 3'
$Issue['Revert'] = 'TBD'
}
$Issue['Technique'] = 'ESC8'
$Severity = Set-Severity -Issue $Issue
$Issue['Severity'] = $Severity
[PSCustomObject] $Issue
}
}
}
function Format-Result {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
$Issue,
[Parameter(Mandatory = $true)]
[int]$Mode
)
$IssueTable = @{
DETECT = 'Auditing Not Fully Enabled'
ESC1 = 'ESC1 - Vulnerable Certificate Template - Authentication'
ESC2 = 'ESC2 - Vulnerable Certificate Template - Subordinate CA'
ESC3 = 'ESC3 - Vulnerable Certificate Template - Enrollment Agent'
ESC4 = 'ESC4 - Vulnerable Access Control - Certifcate Template'
ESC5 = 'ESC5 - Vulnerable Access Control - PKI Object'
ESC6 = 'ESC6 - EDITF_ATTRIBUTESUBJECTALTNAME2 Flag Enabled'
ESC8 = 'ESC8 - HTTP/S Enrollment Enabled'
}
if ($null -ne $Issue) {
$UniqueIssue = $Issue.Technique | Sort-Object -Unique
Write-Host "`n########## $($IssueTable[$UniqueIssue]) ##########`n"
switch ($Mode) {
0 {
$Issue | Format-Table Technique, Name, Issue -Wrap
}
1 {
if ($Issue.Technique -eq 'ESC8') {
$Issue | Format-List Technique, Name, DistinguishedName, CAEnrollmentEndpoint, Issue, Fix
}
else {
$Issue | Format-List Technique, Name, DistinguishedName, Issue, Fix
if (($Issue.Technique -eq "DETECT" -or $Issue.Technique -eq "ESC6") -and (Get-RestrictedAdminModeSetting)) {
Write-Warning "Restricted Admin Mode appears to be configured. Certutil.exe may not work from this host, therefore you may need to execute the 'Fix' commands on the CA server itself"
}
}
}
}
}
}
function Get-ADCSObject {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[array]$Targets,
[System.Management.Automation.PSCredential]$Credential
)
foreach ( $forest in $Targets ) {
if ($Credential) {
$ADRoot = (Get-ADRootDSE -Credential $Credential -Server $forest).defaultNamingContext
Get-ADObject -Filter * -SearchBase "CN=Public Key Services,CN=Services,CN=Configuration,$ADRoot" -SearchScope 2 -Properties * -Credential $Credential
}
else {
$ADRoot = (Get-ADRootDSE -Server $forest).defaultNamingContext
Get-ADObject -Filter * -SearchBase "CN=Public Key Services,CN=Services,CN=Configuration,$ADRoot" -SearchScope 2 -Properties *
}
}
}
function Get-CAHostObject {
[CmdletBinding()]
param (
[parameter(
Mandatory = $true,
ValueFromPipeline = $true)]
[array]$ADCSObjects,
[System.Management.Automation.PSCredential]$Credential
)
process {
if ($Credential) {
$ADCSObjects | Where-Object objectClass -Match 'pKIEnrollmentService' | ForEach-Object {
Get-ADObject $_.CAHostDistinguishedName -Properties * -Server $ForestGC -Credential $Credential
}
}
else {
$ADCSObjects | Where-Object objectClass -Match 'pKIEnrollmentService' | ForEach-Object {
Get-ADObject $_.CAHostDistinguishedName -Properties * -Server $ForestGC
}
}
}
}
function Get-RestrictedAdminModeSetting {
$Path = 'HKLM:SYSTEM\CurrentControlSet\Control\Lsa'
try {
$RAM = (Get-ItemProperty -Path $Path).DisableRestrictedAdmin
$Creds = (Get-ItemProperty -Path $Path).DisableRestrictedAdminOutboundCreds
if ($RAM -eq '0' -and $Creds -eq '1') {
return $true
}
else {
return $false
}
}
catch {
return $false
}
}
function Get-Target {
param (
[string]$Forest,
[string]$InputPath,
[System.Management.Automation.PSCredential]$Credential
)
if ($Forest) {
$Targets = $Forest
}
elseif ($InputPath) {
$Targets = Get-Content $InputPath
}
else {
if ($Credential) {
$Targets = (Get-ADForest -Credential $Credential).Name
}
else {
$Targets = (Get-ADForest).Name
}
}
return $Targets
}
function Install-RSATADPowerShell {
<#
.SYNOPSIS
Installs the RSAT AD PowerShell module.
.DESCRIPTION
This function checks if the current process is elevated and if it is it will prompt to install the RSAT AD PowerShell module.
.EXAMPLE
Install-RSATADPowerShell
#>
if (Test-IsElevated) {
$OS = (Get-CimInstance -ClassName Win32_OperatingSystem).ProductType
# 1 - workstation, 2 - domain controller, 3 - non-dc server
if ($OS -gt 1) {
Write-Warning "The Active Directory PowerShell module is not installed."
Write-Host "If you continue, Locksmith will attempt to install the Active Directory PowerShell module for you.`n" -ForegroundColor Yellow
Write-Host "`nCOMMAND: Install-WindowsFeature -Name RSAT-AD-PowerShell`n" -ForegroundColor Cyan
Write-Host "Continue with this operation? [Y] Yes " -NoNewline
Write-Host "[N] " -ForegroundColor Yellow -NoNewline
Write-Host "No: " -NoNewline
$WarningError = ''
$WarningError = Read-Host
if ($WarningError -like 'y') {
try {
Write-Host "Beginning the ActiveDirectory PowerShell module installation, please wait.."
# Attempt to install ActiveDirectory PowerShell module for Windows Server OSes, works with Windows Server 2012 R2 through Windows Server 2022
Install-WindowsFeature -Name RSAT-AD-PowerShell
}
catch {
Write-Error 'Could not install ActiveDirectory PowerShell module. This module needs to be installed to run Locksmith successfully.'
}
}
else {
Write-Host "ActiveDirectory PowerShell module NOT installed. Please install to run Locksmith successfully.`n" -ForegroundColor Yellow
break;
}
}
else {
Write-Warning "The Active Directory PowerShell module is not installed."
Write-Host "If you continue, Locksmith will attempt to install the Active Directory PowerShell module for you.`n" -ForegroundColor Yellow
Write-Host "`nCOMMAND: Add-WindowsCapability -Name Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0 -Online`n" -ForegroundColor Cyan
Write-Host "Continue with this operation? [Y] Yes " -NoNewline
Write-Host "[N] " -ForegroundColor Yellow -NoNewline
Write-Host "No: " -NoNewline
$WarningError = ''
$WarningError = Read-Host
if ($WarningError -like 'y') {
try {
Write-Host "Beginning the ActiveDirectory PowerShell module installation, please wait.."
# Attempt to install ActiveDirectory PowerShell module for Windows Desktop OSes
Add-WindowsCapability -Name Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0 -Online
}
catch {
Write-Error 'Could not install ActiveDirectory PowerShell module. This module needs to be installed to run Locksmith successfully.'
}
}
else {
Write-Host "ActiveDirectory PowerShell module NOT installed. Please install to run Locksmith successfully.`n" -ForegroundColor Yellow
break;
}
}
}
else {
Write-Warning -Message "The ActiveDirectory PowerShell module is required for Locksmith, but is not installed. Please launch an elevated PowerShell session to have this module installed for you automatically."
# The goal here is to exit the script without closing the PowerShell window. Need to test.
Return
}
}
function Invoke-Scans {
[CmdletBinding()]
param (
# Could split Scans and PromptMe into separate parameter sets.
[Parameter()]
[ValidateSet('Auditing', 'ESC1', 'ESC2', 'ESC3', 'ESC4', 'ESC5', 'ESC6', 'ESC8', 'All', 'PromptMe')]
[array]$Scans = 'All'
)
# Is this needed?
if ($Scans -eq $IsNullOrEmpty) {
$Scans = 'All'
}
if ( $Scans -eq 'PromptMe' ) {
$GridViewTitle = 'Select the tests to run and press Enter or click OK to continue...'
# Check for Out-GridView or Out-ConsoleGridView
if ((Get-Command Out-ConsoleGridView -ErrorAction SilentlyContinue) -and ($PSVersionTable.PSVersion.Major -ge 7)) {
[array]$Scans = ($Dictionary | Select-Object Name, Category, Subcategory | Out-ConsoleGridView -OutputMode Multiple -Title $GridViewTitle).Name | Sort-Object -Property Name
}
elseif (Get-Command -Name Out-GridView -ErrorAction SilentlyContinue) {
[array]$Scans = ($Dictionary | Select-Object Name, Category, Subcategory | Out-GridView -PassThru -Title $GridViewTitle).Name | Sort-Object -Property Name
}
else {
# To Do: Check for admin and prompt to install features/modules or revert to 'All'.
Write-Information "Out-GridView and Out-ConsoleGridView were not found on your system. Defaulting to `'All`'."
$Scans = 'All'
}
}
switch ( $Scans ) {
Auditing {
Write-Host 'Identifying auditing issues...'
[array]$AuditingIssues = Find-AuditingIssue -ADCSObjects $ADCSObjects
}
ESC1 {
Write-Host 'Identifying AD CS templates with dangerous ESC1 configurations...'
[array]$ESC1 = Find-ESC1 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
}
ESC2 {
Write-Host 'Identifying AD CS templates with dangerous ESC2 configurations...'
[array]$ESC2 = Find-ESC2 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
}
ESC3 {
Write-Host 'Identifying AD CS templates with dangerous ESC3 configurations...'
[array]$ESC3 = Find-ESC3Condition1 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
[array]$ESC3 += Find-ESC3Condition2 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
}
ESC4 {
Write-Host 'Identifying AD CS template and other objects with poor access control (ESC4)...'
[array]$ESC4 = Find-ESC4 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -DangerousRights $DangerousRights -SafeOwners $SafeOwners
}
ESC5 {
Write-Host 'Identifying AD CS template and other objects with poor access control (ESC5)...'
[array]$ESC5 = Find-ESC5 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -DangerousRights $DangerousRights -SafeOwners $SafeOwners
}
ESC6 {
Write-Host 'Identifying AD CS template and other objects with poor access control (ESC6)...'
[array]$ESC6 = Find-ESC6 -ADCSObjects $ADCSObjects
}
ESC8 {
Write-Host 'Identifying HTTP-based certificate enrollment interfaces (ESC8)...'
[array]$ESC8 = Find-ESC8 -ADCSObjects $ADCSObjects
}
All {
Write-Host 'Identifying auditing issues...'
[array]$AuditingIssues = Find-AuditingIssue -ADCSObjects $ADCSObjects
Write-Host 'Identifying AD CS templates with dangerous ESC1 configurations...'
[array]$ESC1 = Find-ESC1 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
Write-Host 'Identifying AD CS templates with dangerous ESC2 configurations...'
[array]$ESC2 = Find-ESC2 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
Write-Host 'Identifying AD CS templates with dangerous ESC3 configurations...'
[array]$ESC3 = Find-ESC3Condition1 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
[array]$ESC3 += Find-ESC3Condition2 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
Write-Host 'Identifying AD CS template and other objects with poor access control (ESC4)...'
[array]$ESC4 = Find-ESC4 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -DangerousRights $DangerousRights -SafeOwners $SafeOwners
Write-Host 'Identifying AD CS template and other objects with poor access control (ESC5)...'
[array]$ESC5 = Find-ESC5 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -DangerousRights $DangerousRights -SafeOwners $SafeOwners
Write-Host 'Identifying AD CS template and other objects with poor access control (ESC6)...'
[array]$ESC6 = Find-ESC6 -ADCSObjects $ADCSObjects
Write-Host 'Identifying HTTP-based certificate enrollment interfaces (ESC8)...'
[array]$ESC8 = Find-ESC8 -ADCSObjects $ADCSObjects
}
}
[array]$AllIssues = $AuditingIssues + $ESC1 + $ESC2 + $ESC3 + $ESC4 + $ESC5 + $ESC6 + $ESC8
# If these are all empty = no issues found, exit
if ((!$AuditingIssues) -and (!$ESC1) -and (!$ESC2) -and (!$ESC3) -and (!$ESC4) -and (!$ESC5) -and (!$ESC6) -and (!$ESC8) ) {
Write-Host "`n$(Get-Date) : No ADCS issues were found." -ForegroundColor Green
break
}
# Return a hash table of array names (keys) and arrays (values) so they can be directly referenced with other functions
Return @{
AllIssues = $AllIssues
AuditingIssues = $AuditingIssues
ESC1 = $ESC1
ESC2 = $ESC2
ESC3 = $ESC3
ESC4 = $ESC4
ESC5 = $ESC5
ESC6 = $ESC6
ESC8 = $ESC8
}
}
<#
.SYNOPSIS
Create a dictionary of the escalation paths and insecure configurations that Locksmith scans for.
.DESCRIPTION
The New-Dictionary function is used to instantiate an array of objects that contain the names, definitions,
descriptions, code used to find, code used to fix, and reference URLs. This is invoked by the module's main function.
.NOTES
VulnerableConfigurationItem Class Definition:
Version Update each time the class definition or the dictionary below is changed.
Name The short name of the vulnerable configuration item (VCI).
Category The high level category of VCI types, including escalation path, server configuration, GPO setting, etc.
Subcategory The subcategory of vulnerable configuration item types.
Summary A summary of the vulnerability and how it can be abused.
FindIt The name of the function that is used to look for the VCI, stored as an invokable scriptblock.
FixIt The name of the function that is used to fix the VCI, stored as an invokable scriptblock.
ReferenceUrls An array of URLs that are used as references to learn more about the VCI.
#>
function New-Dictionary {
class VulnerableConfigurationItem {
static [string] $Version = '2023.10.01.000'
[string]$Name
[ValidateSet('Escalation Path', 'Server Configuration', 'GPO Setting')][string]$Category
[string]$Subcategory
[string]$Summary
[scriptblock]$FindIt
[scriptblock]$FixIt
[uri[]]$ReferenceUrls
}
[VulnerableConfigurationItem[]]$Dictionary = @(
[VulnerableConfigurationItem]@{
Name = 'ESC1'
Category = 'Escalation Path'
Subcategory = 'Misconfigured Certificate Templates'
Summary = ''
FindIt = { Find-ESC1 }
FixIt = { Write-Output "Add code to fix the vulnerable configuration." }
ReferenceUrls = 'https://posts.specterops.io/certified-pre-owned-d95910965cd2#:~:text=Misconfigured%20Certificate%20Templates%20%E2%80%94%20ESC1'
},
[VulnerableConfigurationItem]@{
Name = 'ESC2'
Category = 'Escalation Path'
Subcategory = 'Misconfigured Certificate Templates'
Summary = ''
FindIt = { Find-ESC2 }
FixIt = { Write-Output 'Add code to fix the vulnerable configuration.' }
ReferenceUrls = 'https://posts.specterops.io/certified-pre-owned-d95910965cd2#:~:text=Misconfigured%20Certificate%20Templates%20%E2%80%94%20ESC2'
},
[VulnerableConfigurationItem]@{
Name = 'ESC3'
Category = 'Escalation Path'
Subcategory = 'Enrollment Agent Templates'
Summary = ''
FindIt = {
Find-ESC3Condition1
Find-ESC3Condition2
}
FixIt = { Write-Output 'Add code to fix the vulnerable configuration.' }
ReferenceUrls = 'https://posts.specterops.io/certified-pre-owned-d95910965cd2#:~:text=Enrollment%20Agent%20Templates%20%E2%80%94%20ESC3'
},
[VulnerableConfigurationItem]@{
Name = 'ESC4';
Category = 'Escalation Path'
Subcategory = 'Vulnerable Certificate Template Access Control'
Summary = ''
FindIt = { Find-ESC4 }
FixIt = { Write-Output 'Add code to fix the vulnerable configuration.' }
ReferenceUrls = 'https://posts.specterops.io/certified-pre-owned-d95910965cd2#:~:text=Vulnerable%20Certificate%20Template%20Access%20Control%20%E2%80%94%20ESC4'
},
[VulnerableConfigurationItem]@{
Name = 'ESC5';
Category = 'Escalation Path'
Subcategory = 'Vulnerable PKI Object Access Control'
Summary = ''
FindIt = { Find-ESC5 }
FixIt = { Write-Output 'Add code to fix the vulnerable configuration.' }
ReferenceUrls = 'https://posts.specterops.io/certified-pre-owned-d95910965cd2#:~:text=Vulnerable%20PKI%20Object%20Access%20Control%20%E2%80%94%20ESC5'
},
[VulnerableConfigurationItem]@{
Name = 'ESC6'
Category = 'Escalation Path'
Subcategory = 'EDITF_ATTRIBUTESUBJECTALTNAME2'
Summary = ''
FindIt = { Find-ESC6 }
FixIt = { Write-Output 'Add code to fix the vulnerable configuration.' }
ReferenceUrls = 'https://posts.specterops.io/certified-pre-owned-d95910965cd2#:~:text=EDITF_ATTRIBUTESUBJECTALTNAME2%20%E2%80%94%20ESC6'
},
[VulnerableConfigurationItem]@{
Name = 'ESC7'
Category = 'Escalation Path'
Subcategory = 'Vulnerable Certificate Authority Access Control'
Summary = ''
FindIt = { Write-Output 'We have not created Find-ESC7 yet.' }
FixIt = { Write-Output 'Add code to fix the vulnerable configuration.' }
ReferenceUrls = 'https://posts.specterops.io/certified-pre-owned-d95910965cd2#:~:text=Vulnerable%20Certificate%20Authority%20Access%20Control%20%E2%80%94%20ESC7'
},
[VulnerableConfigurationItem]@{
Name = 'ESC8'
Category = 'Escalation Path'
Subcategory = 'NTLM Relay to AD CS HTTP Endpoints'
Summary = ''
FindIt = { Find-ESC8 }
FixIt = { Write-Output 'Add code to fix the vulnerable configuration.' }
ReferenceUrls = 'https://posts.specterops.io/certified-pre-owned-d95910965cd2#:~:text=NTLM%20Relay%20to%20AD%20CS%20HTTP%20Endpoints'
},
[VulnerableConfigurationItem]@{
Name = 'Auditing'
Category = 'Server Configuration'
Subcategory = 'Gaps in auditing on certificate authorities and AD CS objects.'
Summary = ''
FindIt = { Find-AuditingIssue }
FixIt = { Write-Output 'Add code to fix the vulnerable configuration.' }
ReferenceUrls = @('https://github.com/TrimarcJake/Locksmith', 'https://techcommunity.microsoft.com/t5/ask-the-directory-services-team/designing-and-implementing-a-pki-part-i-design-and-planning/ba-p/396953')
}
)
Return $Dictionary
}
function New-OutputPath {
[CmdletBinding(SupportsShouldProcess)]
param ()
# Create one output directory per forest
foreach ( $forest in $Targets ) {
$ForestPath = $OutputPath + "`\" + $forest
New-Item -Path $ForestPath -ItemType Directory -Force | Out-Null
}
}
function Set-AdditionalCAProperty {
[CmdletBinding(SupportsShouldProcess)]
param (
[parameter(
Mandatory = $true,
ValueFromPipeline = $true)]
[array]$ADCSObjects,
[System.Management.Automation.PSCredential]$Credential
)
process {
$ADCSObjects | Where-Object objectClass -Match 'pKIEnrollmentService' | ForEach-Object {
[string]$CAEnrollmentEndpoint = $_.'msPKI-Enrollment-Servers' | Select-String 'http.*' | ForEach-Object { $_.Matches[0].Value }
[string]$CAFullName = "$($_.dNSHostName)\$($_.Name)"
$CAHostname = $_.dNSHostName.split('.')[0]
# $CAName = $_.Name
if ($Credential) {
$CAHostDistinguishedName = (Get-ADObject -Filter { (Name -eq $CAHostName) -and (objectclass -eq 'computer') } -Server $ForestGC -Credential $Credential).DistinguishedName
$CAHostFQDN = (Get-ADObject -Filter { (Name -eq $CAHostName) -and (objectclass -eq 'computer') } -Properties DnsHostname -Server $ForestGC -Credential $Credential).DnsHostname
}
else {
$CAHostDistinguishedName = (Get-ADObject -Filter { (Name -eq $CAHostName) -and (objectclass -eq 'computer') } -Server $ForestGC ).DistinguishedName
$CAHostFQDN = (Get-ADObject -Filter { (Name -eq $CAHostName) -and (objectclass -eq 'computer') } -Properties DnsHostname -Server $ForestGC).DnsHostname
}
$ping = Test-Connection -ComputerName $CAHostFQDN -Quiet -Count 1
if ($ping) {
try {
if ($Credential) {
$CertutilAudit = Invoke-Command -ComputerName $CAHostname -Credential $Credential -ScriptBlock { param($CAFullName); certutil -config $CAFullName -getreg CA\AuditFilter } -ArgumentList $CAFullName
}
else {
$CertutilAudit = certutil -config $CAFullName -getreg CA\AuditFilter
}
}
catch {
$AuditFilter = 'Failure'
}
try {
if ($Credential) {
$CertutilFlag = Invoke-Command -ComputerName $CAHostname -Credential $Credential -ScriptBlock { param($CAFullName); certutil -config $CAFullName -getreg policy\EditFlags } -ArgumentList $CAFullName
}
else {
$CertutilFlag = certutil -config $CAFullName -getreg policy\EditFlags
}
}
catch {
$AuditFilter = 'Failure'
}
}
else {
$AuditFilter = 'CA Unavailable'
$SANFlag = 'CA Unavailable'
}
if ($CertutilAudit) {
try {
[string]$AuditFilter = $CertutilAudit | Select-String 'AuditFilter REG_DWORD = ' | Select-String '\('
$AuditFilter = $AuditFilter.split('(')[1].split(')')[0]
}
catch {
try {
[string]$AuditFilter = $CertutilAudit | Select-String 'AuditFilter REG_DWORD = '
$AuditFilter = $AuditFilter.split('=')[1].trim()
}
catch {
$AuditFilter = 'Never Configured'
}
}
}
if ($CertutilFlag) {
[string]$SANFlag = $CertutilFlag | Select-String ' EDITF_ATTRIBUTESUBJECTALTNAME2 -- 40000 \('
if ($SANFlag) {
$SANFlag = 'Yes'