-
Notifications
You must be signed in to change notification settings - Fork 91
/
Invoke-Locksmith.ps1
3898 lines (3484 loc) · 165 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 {
<#
.SYNOPSIS
Converts an identity reference to a security identifier (SID).
.DESCRIPTION
The ConvertFrom-IdentityReference function takes an identity reference as input and
converts it to a security identifier (SID). It supports both SID strings and NTAccount objects.
.PARAMETER Object
Specifies the identity reference to be converted. This parameter is mandatory.
.EXAMPLE
$object = "S-1-5-21-3623811015-3361044348-30300820-1013"
ConvertFrom-IdentityReference -Object $object
# Returns "S-1-5-21-3623811015-3361044348-30300820-1013"
.EXAMPLE
$object = New-Object System.Security.Principal.NTAccount("DOMAIN\User")
ConvertFrom-IdentityReference -Object $object
# Returns "S-1-5-21-3623811015-3361044348-30300820-1013"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[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 {
<#
.SYNOPSIS
Creates a script that reverts the changes performed by Locksmith.
.DESCRIPTION
This script is used to revert changes performed by Locksmith.
It takes in various arrays of objects representing auditing issues and ESC misconfirugrations.
It creates a new script called 'Invoke-RevertLocksmith.ps1' and adds the necessary commands
to revert the changes made by Locksmith.
.PARAMETER AuditingIssues
An array of auditing issues to be reverted.
.PARAMETER ESC1
An array of ESC1 changes to be reverted.
.PARAMETER ESC2
An array of ESC2 changes to be reverted.
.PARAMETER ESC3
An array of ESC3 changes to be reverted.
.PARAMETER ESC4
An array of ESC4 changes to be reverted.
.PARAMETER ESC5
An array of ESC5 changes to be reverted.
.PARAMETER ESC6
An array of ESC6 changes to be reverted.
.PARAMETER ESC11
An array of ESC11 changes to be reverted.
.PARAMETER ESC13
An array of ESC13 changes to be reverted.
.EXAMPLE
$params = @{
AuditingIssues = $AuditingIssues
ESC1 = $ESC1
ESC2 = $ESC2
ESC3 = $ESC3
ESC4 = $ESC4
ESC5 = $ESC5
ESC6 = $ESC6
ESC11 = $ESC11
ESC13 = $ESC13
}
Export-RevertScript @params
Reverts the changes performed by Locksmith using the specified arrays of objects.
#>
[CmdletBinding()]
param(
[array]$AuditingIssues,
[array]$ESC1,
[array]$ESC2,
[array]$ESC3,
[array]$ESC4,
[array]$ESC5,
[array]$ESC6,
[array]$ESC11,
[array]$ESC13
)
begin {
$Output = 'Invoke-RevertLocksmith.ps1'
$RevertScript = [System.Text.StringBuilder]::New()
[void]$RevertScript.Append("<#`nScript to revert changes performed by Locksmith`nCreated $(Get-Date)`n#>`n")
$Objects = $AuditingIssues + $ESC1 + $ESC2 + $ESC3 + $ESC4 + $ESC5 + $ESC6 + $ESC11 + $ESC13
}
process {
if ($Objects) {
$Objects | ForEach-Object {
[void]$RevertScript.Append("$($_.Revert)`n")
}
$RevertScript.ToString() | Out-File -FilePath $Output
}
}
}
function Find-AuditingIssue {
<#
.SYNOPSIS
A function to find auditing issues on AD CS CAs.
.DESCRIPTION
This script takes an array of AD CS objects and filters them based on specific criteria to identify auditing issues.
It checks if the object's objectClass is 'pKIEnrollmentService' and if the AuditFilter is not equal to '127'.
For each matching object, it creates a custom object with information about the issue, fix, and revert actions.
.PARAMETER ADCSObjects
Specifies an array of ADCS objects to be checked for auditing issues.
.OUTPUTS
System.Management.Automation.PSCustomObject
A custom object is created for each ADCS object that matches the criteria, containing the following properties:
- Forest: The forest name of the object.
- Name: The name of the object.
- DistinguishedName: The distinguished name of the object.
- Technique: The technique used to detect the issue (always 'DETECT').
- Issue: The description of the auditing issue.
- Fix: The command to fix the auditing issue.
- Revert: The command to revert the auditing issue.
.EXAMPLE
$ADCSObjects = Get-ADObject -Filter * -SearchBase 'CN=Enrollment Services,CN=Public Key Services,CN=Services,CN=Configuration,DC=contoso,DC=com'
$AuditingIssues = Find-AuditingIssue -ADCSObjects $ADCSObjects
$AuditingIssues
This example retrieves ADCS objects from the specified search base and passes them to the Find-AuditingIssue function.
It then returns the auditing issues for later use.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[array]$ADCSObjects
)
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKIEnrollmentService') -and
($_.AuditFilter -ne '127')
} | ForEach-Object {
$Issue = [pscustomobject]@{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
Technique = 'DETECT'
Issue = "Auditing is not fully enabled on $($_.CAFullName). Important security events may go unnoticed."
Fix = @"
certutil.exe -config `'$($_.CAFullname)`' -setreg `'CA\AuditFilter`' 127
Invoke-Command -ComputerName `'$($_.dNSHostName)`' -ScriptBlock {
Get-Service -Name `'certsvc`' | Restart-Service -Force
}
"@
Revert = @"
certutil.exe -config $($_.CAFullname) -setreg CA\AuditFilter $($_.AuditFilter)
Invoke-Command -ComputerName `'$($_.dNSHostName)`' -ScriptBlock {
Get-Service -Name `'certsvc`' | Restart-Service -Force
}
"@
}
if ($_.AuditFilter -match 'CA Unavailable') {
$Issue.Issue = $_.AuditFilter
$Issue.Fix = 'N/A'
$Issue.Revert = 'N/A'
}
$Issue
}
}
function Find-ESC1 {
<#
.SYNOPSIS
This script finds AD CS (Active Directory Certificate Services) objects that have the ESC1 vulnerability.
.DESCRIPTION
The script takes an array of ADCS objects as input and filters them based on the specified conditions.
For each matching object, it creates a custom object with properties representing various information about
the object, such as Forest, Name, DistinguishedName, IdentityReference, ActiveDirectoryRights, Issue, Fix, Revert, and Technique.
.PARAMETER ADCSObjects
Specifies the array of ADCS objects to be processed. This parameter is mandatory.
.PARAMETER SafeUsers
Specifies the list of SIDs of safe users who are allowed to have specific rights on the objects. This parameter is mandatory.
.PARAMETER ClientAuthEKUs
A list of EKUs that can be used for client authentication.
.OUTPUTS
The script outputs an array of custom objects representing the matching ADCS objects and their associated information.
.EXAMPLE
$Targets = Get-Target
$ADCSObjects = Get-ADCSObject -Targets $Targets
$SafeUsers = '-512$|-519$|-544$|-18$|-517$|-500$|-516$|-9$|-526$|-527$|S-1-5-10'
$ClientAuthEKUs = '1\.3\.6\.1\.5\.5\.7\.3\.2|1\.3\.6\.1\.5\.2\.3\.4|1\.3\.6\.1\.4\.1\.311\.20\.2\.2|2\.5\.29\.37\.0'
$Results = Find-ESC1 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -ClientAuthEKUs $ClientAuthEKUs
$Results
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[array]$ADCSObjects,
[Parameter(Mandatory)]
[array]$SafeUsers,
[Parameter(Mandatory)]
$ClientAuthEKUs,
[Parameter(Mandatory)]
[int]$Mode
)
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKICertificateTemplate') -and
($_.pkiExtendedKeyUsage -match $ClientAuthEKUs) -and
($_.'msPKI-Certificate-Name-Flag' -band 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') -or ($entry.ActiveDirectoryRights -match 'GenericAll') ) ) {
$Issue = [pscustomobject]@{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
IdentityReference = $entry.IdentityReference
ActiveDirectoryRights = $entry.ActiveDirectoryRights
Enabled = $_.Enabled
EnabledOn = $_.EnabledOn
Issue = @"
$($entry.IdentityReference) can provide a Subject Alternative Name (SAN) while
enrolling in this Client Authentication template, and enrollment does not require
Manager Approval.
The resultant certificate can be used by an attacker to authenticate as any
principal listed in the SAN up to and including Domain Admins, Enterprise Admins,
or Domain Controllers.
More info:
- https://posts.specterops.io/certified-pre-owned-d95910965cd2
"@
Fix = @"
# Enable Manager Approval
`$Object = `'$($_.DistinguishedName)`'
Get-ADObject `$Object | Set-ADObject -Replace @{'msPKI-Enrollment-Flag' = 2}
"@
Revert = @"
# Disable Manager Approval
`$Object = `'$($_.DistinguishedName)`'
Get-ADObject `$Object | Set-ADObject -Replace @{'msPKI-Enrollment-Flag' = 0}
"@
Technique = 'ESC1'
}
if ( $Mode -in @(1, 3, 4) ) {
Update-ESC1Remediation -Issue $Issue
}
$Issue
}
}
}
}
function Find-ESC11 {
<#
.SYNOPSIS
This script finds AD CS (Active Directory Certificate Services) objects that have the ESC11 vulnerability.
.DESCRIPTION
The script takes an array of ADCS objects as input and filters them based on objects that have the objectClass
'pKIEnrollmentService' and the InterfaceFlag set to 'No'. For each matching object, it creates a custom object with
properties representing various information about the object, such as Forest, Name, DistinguishedName, Technique,
Issue, Fix, and Revert.
.PARAMETER ADCSObjects
Specifies the array of ADCS objects to be processed. This parameter is mandatory.
.OUTPUTS
The script outputs an array of custom objects representing the matching ADCS objects and their associated information.
.EXAMPLE
$ADCSObjects = Get-ADCSObject -Target (Get-Target)
Find-ESC11 -ADCSObjects $ADCSObjects
$Results
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
$ADCSObjects
)
process {
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKIEnrollmentService') -and
($_.InterfaceFlag -ne 'Yes')
} | ForEach-Object {
[string]$CAFullName = "$($_.dNSHostName)\$($_.Name)"
$Issue = [pscustomobject]@{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
Technique = 'ESC11'
Issue = $_.InterfaceFlag
Fix = 'N/A'
Revert = 'N/A'
}
if ($_.InterfaceFlag -eq 'No') {
$Issue.Issue = @"
The IF_ENFORCEENCRYPTICERTREQUEST flag is disabled on this Certification
Authority (CA). It is possible to relay NTLM authentication to the RPC interface
of this CA.
If the LAN Manager authentication level of any domain in this forest is 2 or
less, an attacker can coerce authentication from a Domain Controller (DC) to
receive a certificate which can be used to authenticate as that DC.
More info:
- https://blog.compass-security.com/2022/11/relaying-to-ad-certificate-services-over-rpc/
"@
$Issue.Fix = @"
# Enable the flag
certutil -config $CAFullname -setreg CA\InterfaceFlags +IF_ENFORCEENCRYPTICERTREQUEST
# Restart the Ceritification Authority service
Invoke-Command -ComputerName `'$($_.dNSHostName)`' -ScriptBlock {
Get-Service -Name `'certsvc`' | Restart-Service -Force
}
"@
$Issue.Revert = @"
# Disable the flag
certutil -config $CAFullname -setreg CA\InterfaceFlags -IF_ENFORCEENCRYPTICERTREQUEST
# Restart the Ceritification Authority service
Invoke-Command -ComputerName `'$($_.dNSHostName)`' -ScriptBlock {
Get-Service -Name `'certsvc`' | Restart-Service -Force
}
"@
}
$Issue
}
}
}
function Find-ESC13 {
<#
.SYNOPSIS
This script finds AD CS (Active Directory Certificate Services) objects that have the ESC13 vulnerability.
.DESCRIPTION
The script takes an array of ADCS objects as input and filters them based on the specified conditions.
For each matching object, it creates a custom object with properties representing various information about
the object, such as Forest, Name, DistinguishedName, IdentityReference, ActiveDirectoryRights, Issue, Fix, Revert, and Technique.
.PARAMETER ADCSObjects
Specifies the array of ADCS objects to be processed. This parameter is mandatory.
.PARAMETER SafeUsers
Specifies the list of SIDs of safe users who are allowed to have specific rights on the objects. This parameter is mandatory.
.PARAMETER ClientAuthEKUs
A list of EKUs that can be used for client authentication.
.OUTPUTS
The script outputs an array of custom objects representing the matching ADCS objects and their associated information.
.EXAMPLE
$ADCSObjects = Get-ADCSObjects
$SafeUsers = '-512$|-519$|-544$|-18$|-517$|-500$|-516$|-9$|-526$|-527$|S-1-5-10'
$ClientAuthEKUs = '1\.3\.6\.1\.5\.5\.7\.3\.2|1\.3\.6\.1\.5\.2\.3\.4|1\.3\.6\.1\.4\.1\.311\.20\.2\.2|2\.5\.29\.37\.0'
$Results = $ADCSObjects | Find-ESC13 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -ClientAuthEKUs $ClientAuthEKUs
$Results
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[Microsoft.ActiveDirectory.Management.ADEntity[]]$ADCSObjects,
[Parameter(Mandatory)]
[array]$SafeUsers,
[Parameter(Mandatory)]
$ClientAuthEKUs
)
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKICertificateTemplate') -and
($_.pkiExtendedKeyUsage -match $ClientAuthEKUs) -and
($_.'msPKI-Certificate-Policy')
} | ForEach-Object {
foreach ($policy in $_.'msPKI-Certificate-Policy') {
if ($ADCSObjects.'msPKI-Cert-Template-OID' -contains $policy) {
$OidToCheck = $ADCSObjects | Where-Object 'msPKI-Cert-Template-OID' -EQ $policy
if ($OidToCheck.'msDS-OIDToGroupLink') {
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 = [pscustomobject]@{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
IdentityReference = $entry.IdentityReference
ActiveDirectoryRights = $entry.ActiveDirectoryRights
Enabled = $_.Enabled
EnabledOn = $_.EnabledOn
LinkedGroup = $OidToCheck.'msDS-OIDToGroupLink'
Issue = @"
$($entry.IdentityReference) can enroll in this Client Authentication template
which is linked to the group $($OidToCheck.'msDS-OIDToGroupLink').
If $($entry.IdentityReference) uses this certificate for authentication, they
will gain the rights of the linked group while the group membership appears empty.
More info:
- https://posts.specterops.io/adcs-esc13-abuse-technique-fda4272fbd53
"@
Fix = @"
# Enable Manager Approval
`$Object = `'$($_.DistinguishedName)`'
Get-ADObject `$Object | Set-ADObject -Replace @{'msPKI-Enrollment-Flag' = 2}
"@
Revert = @"
# Disable Manager Approval
`$Object = `'$($_.DistinguishedName)`'
Get-ADObject `$Object | Set-ADObject -Replace @{'msPKI-Enrollment-Flag' = 0}
"@
Technique = 'ESC13'
}
$Issue
}
}
}
}
}
}
}
function Find-ESC15 {
<#
.SYNOPSIS
This script finds AD CS (Active Directory Certificate Services) objects that have the ESC15/EUKwu vulnerability.
.DESCRIPTION
The script takes an array of ADCS objects as input and filters them based on the specified conditions.
For each matching object, it creates a custom object with properties representing various information about
the object, such as Forest, Name, DistinguishedName, IdentityReference, ActiveDirectoryRights, Issue, Fix, Revert, and Technique.
.PARAMETER ADCSObjects
Specifies the array of ADCS objects to be processed. This parameter is mandatory.
.OUTPUTS
The script outputs an array of custom objects representing the matching ADCS objects and their associated information.
.EXAMPLE
$Targets = Get-Target
$ADCSObjects = Get-ADCSObjects -Targets $Targets
$SafeUsers = '-512$|-519$|-544$|-18$|-517$|-500$|-516$|-9$|-526$|-527$|S-1-5-10'
$Results = Find-ESC15 -ADCSObjects $ADCSObjects -SafeUser $SafeUsers
$Results
#>
[alias('Find-EKUwu')]
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[array]$ADCSObjects,
[Parameter(Mandatory)]
$SafeUsers
)
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKICertificateTemplate') -and
($_.'msPKI-Template-Schema-Version' -eq 1) -and
($Enabled)
} | 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') -or ($entry.ActiveDirectoryRights -match 'GenericAll') ) ) {
$Issue = [pscustomobject]@{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
IdentityReference = $entry.IdentityReference
ActiveDirectoryRights = $entry.ActiveDirectoryRights
Enabled = $_.Enabled
EnabledOn = $_.EnabledOn
Issue = @"
$($_.Name) uses AD CS Template Schema Version 1, and $($entry.IdentityReference)
is allowed to enroll in this template.
If patches for CVE-2024-49019 have not been applied it may be possible to include
arbitrary Application Policies while enrolling in this template, including
Application Policies that permit Client Authentication or allow the creation
of Subordinate CAs.
More info:
- https://trustedsec.com/blog/ekuwu-not-just-another-ad-cs-esc
- https://msrc.microsoft.com/update-guide/vulnerability/CVE-2024-49019
"@
Fix = @"
# Option 1: Manual Remediation
# Step 1: Identify if this template is Enabled on any CA.
# Step 2: If Enabled, identify if this template has recently been used to generate a certificate.
# Step 3a: If recently used, either restrict enrollment scope or convert to the template to Schema V2.
# Step 3b: If not recently used, unpublish the template from all CAs.
# Option 2: Scripted Remediation
# Step 1: Open an elevated Powershell session as an AD or PKI Admin
# Step 2: Run Unpublish-SchemaV1Templates.ps1
Invoke-WebRequest -Uri https://bit.ly/Fix-ESC15 | Invoke-Expression
"@
Revert = '[TODO]'
Technique = 'ESC15/EKUwu'
}
$Issue
}
}
}
}
function Find-ESC2 {
<#
.SYNOPSIS
This script finds AD CS (Active Directory Certificate Services) objects that have the ESC2 vulnerability.
.DESCRIPTION
The script takes an array of ADCS objects as input and filters them based on the specified conditions.
For each matching object, it creates a custom object with properties representing various information about
the object, such as Forest, Name, DistinguishedName, IdentityReference, ActiveDirectoryRights, Issue, Fix, Revert, and Technique.
.PARAMETER ADCSObjects
Specifies the array of ADCS objects to be processed. This parameter is mandatory.
.PARAMETER SafeUsers
Specifies the list of SIDs of safe users who are allowed to have specific rights on the objects. This parameter is mandatory.
.OUTPUTS
The script outputs an array of custom objects representing the matching ADCS objects and their associated information.
.EXAMPLE
$ADCSObjects = Get-ADCSObjects
$SafeUsers = '-512$|-519$|-544$|-18$|-517$|-500$|-516$|-9$|-526$|-527$|S-1-5-10'
$Results = $ADCSObjects | Find-ESC2 -SafeUsers $SafeUsers
$Results
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[array]$ADCSObjects,
[Parameter(Mandatory)]
[string]$SafeUsers
)
$ADCSObjects | Where-Object {
($_.ObjectClass -eq 'pKICertificateTemplate') -and
( (!$_.pkiExtendedKeyUsage) -or ($_.pkiExtendedKeyUsage -match '2.5.29.37.0') ) -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') -or ($entry.ActiveDirectoryRights -match 'GenericAll') ) ) {
$Issue = [pscustomobject]@{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
IdentityReference = $entry.IdentityReference
ActiveDirectoryRights = $entry.ActiveDirectoryRights
Enabled = $_.Enabled
EnabledOn = $_.EnabledOn
Issue = @"
$($entry.IdentityReference) can use this template to request a Subordinate
Certification Authority (SubCA) certificate without Manager Approval.
The resultant certificate can be used by an attacker to instantiate their own
SubCA which is trusted by AD.
By default, certificates created from this attacker-controlled SubCA cannot be
used for authentication, but they can be used for other purposes such as TLS
certs and code signing.
However, if an attacker can modify the NtAuthCertificates object (see ESC5),
they can convert their rogue CA into one trusted for authentication.
More info:
- https://posts.specterops.io/certified-pre-owned-d95910965cd2
"@
Fix = @"
# Enable Manager Approval
`$Object = `'$($_.DistinguishedName)`'
Get-ADObject `$Object | Set-ADObject -Replace @{'msPKI-Enrollment-Flag' = 2}
"@
Revert = @"
# Disable Manager Approval
`$Object = `'$($_.DistinguishedName)`'
Get-ADObject `$Object | Set-ADObject -Replace @{'msPKI-Enrollment-Flag' = 0}
"@
Technique = 'ESC2'
}
$Issue
}
}
}
}
function Find-ESC3Condition1 {
<#
.SYNOPSIS
This script finds AD CS (Active Directory Certificate Services) objects that match the first condition required for ESC3 vulnerability.
.DESCRIPTION
The script takes an array of ADCS objects as input and filters them based on the specified conditions.
For each matching object, it creates a custom object with properties representing various information about
the object, such as Forest, Name, DistinguishedName, IdentityReference, ActiveDirectoryRights, Issue, Fix, Revert, and Technique.
.PARAMETER ADCSObjects
Specifies the array of ADCS objects to be processed. This parameter is mandatory.
.PARAMETER SafeUsers
Specifies the list of SIDs of safe users who are allowed to have specific rights on the objects. This parameter is mandatory.
.OUTPUTS
The script outputs an array of custom objects representing the matching ADCS objects and their associated information.
.EXAMPLE
$ADCSObjects = Get-ADCSObjects
$SafeUsers = '-512$|-519$|-544$|-18$|-517$|-500$|-516$|-9$|-526$|-527$|S-1-5-10'
$Results = $ADCSObjects | Find-ESC3Condition1 -SafeUsers $SafeUsers
$Results
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[array]$ADCSObjects,
[Parameter(Mandatory)]
[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') -or ($entry.ActiveDirectoryRights -match 'GenericAll') ) ) {
$Issue = [pscustomobject]@{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
IdentityReference = $entry.IdentityReference
ActiveDirectoryRights = $entry.ActiveDirectoryRights
Enabled = $_.Enabled
EnabledOn = $_.EnabledOn
Issue = @"
$($entry.IdentityReference) can use this template to request an Enrollment Agent
certificate without Manager Approval.
The resulting certificate can be used to enroll in any template that requires
an Enrollment Agent to submit the request.
More info:
- https://posts.specterops.io/certified-pre-owned-d95910965cd2
"@
Fix = @"
# Enable Manager Approval
`$Object = `'$($_.DistinguishedName)`'
Get-ADObject `$Object | Set-ADObject -Replace @{'msPKI-Enrollment-Flag' = 2}
"@
Revert = @"
# Disable Manager Approval
`$Object = `'$($_.DistinguishedName)`'
Get-ADObject `$Object | Set-ADObject -Replace @{'msPKI-Enrollment-Flag' = 0}
"@
Technique = 'ESC3'
}
$Issue
}
}
}
}
function Find-ESC3Condition2 {
<#
.SYNOPSIS
This script finds AD CS (Active Directory Certificate Services) objects that match the second condition required for ESC3 vulnerability.
.DESCRIPTION
The script takes an array of ADCS objects as input and filters them based on the specified conditions.
For each matching object, it creates a custom object with properties representing various information about
the object, such as Forest, Name, DistinguishedName, IdentityReference, ActiveDirectoryRights, Issue, Fix, Revert, and Technique.
.PARAMETER ADCSObjects
Specifies the array of ADCS objects to be processed. This parameter is mandatory.
.PARAMETER SafeUsers
Specifies the list of SIDs of safe users who are allowed to have specific rights on the objects. This parameter is mandatory.
.OUTPUTS
The script outputs an array of custom objects representing the matching ADCS objects and their associated information.
.EXAMPLE
$ADCSObjects = Get-ADCSObjects
$SafeUsers = '-512$|-519$|-544$|-18$|-517$|-500$|-516$|-9$|-526$|-527$|S-1-5-10'
$Results = $ADCSObjects | Find-ESC3Condition2 -SafeUsers $SafeUsers
$Results
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[array]$ADCSObjects,
[Parameter(Mandatory)]
[array]$SafeUsers
)
$ADCSObjects | Where-Object {
($_.objectClass -eq 'pKICertificateTemplate') -and
($_.pkiExtendedKeyUsage -match $ClientAuthEKU) -and
!($_.'msPKI-Enrollment-Flag' -band 2) -and
($_.'msPKI-RA-Application-Policies' -match '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') -or ($entry.ActiveDirectoryRights -match 'GenericAll') ) ) {
$Issue = [pscustomobject]@{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
IdentityReference = $entry.IdentityReference
ActiveDirectoryRights = $entry.ActiveDirectoryRights
Enabled = $_.Enabled
EnabledOn = $_.EnabledOn
Issue = @"
If the holder of an Enrollment Agent certificate requests a certificate using
this template, they will receive a certificate which allows them to authenticate
as $($entry.IdentityReference).
More info:
- https://posts.specterops.io/certified-pre-owned-d95910965cd2
"@
Fix = @"
First, eliminate unused Enrollment Agent templates.
Then, tightly scope any Enrollment Agent templates that remain and:
# Enable Manager Approval
`$Object = `'$($_.DistinguishedName)`'
Get-ADObject `$Object | Set-ADObject -Replace @{'msPKI-Enrollment-Flag' = 2}
"@
Revert = @"
# Disable Manager Approval
`$Object = `'$($_.DistinguishedName)`'
Get-ADObject `$Object | Set-ADObject -Replace @{'msPKI-Enrollment-Flag' = 0}
"@
Technique = 'ESC3'
}
$Issue
}
}
}
}
function Find-ESC4 {
<#
.SYNOPSIS
This script finds AD CS (Active Directory Certificate Services) objects that have the ESC4 vulnerability.
.DESCRIPTION
The script takes an array of ADCS objects as input and filters them based on the specified conditions.
For each matching object, it creates a custom object with properties representing various information about
the object, such as Forest, Name, DistinguishedName, IdentityReference, ActiveDirectoryRights, Issue, Fix, Revert, and Technique.
.PARAMETER ADCSObjects
Specifies the array of ADCS objects to be processed. This parameter is mandatory.
.PARAMETER DangerousRights
Specifies the list of dangerous rights that should not be assigned to users. This parameter is mandatory.
.PARAMETER SafeOwners
Specifies the list of SIDs of safe owners who are allowed to have owner rights on the objects. This parameter is mandatory.
.PARAMETER SafeUsers
Specifies the list of SIDs of safe users who are allowed to have specific rights on the objects. This parameter is mandatory.
.PARAMETER SafeObjectTypes
Specifices a list of ObjectTypes which are not a security concern. This parameter is mandatory.
.OUTPUTS
The script outputs an array of custom objects representing the matching ADCS objects and their associated information.
.EXAMPLE
$ADCSObjects = Get-ADCSObject -Targets (Get-Target)
# GenericAll, WriteDacl, and WriteOwner all permit full control of an AD object.
# WriteProperty may or may not permit full control depending the specific property and AD object type.
$DangerousRights = @('GenericAll', 'WriteProperty', 'WriteOwner', 'WriteDacl')
# -512$ = Domain Admins group
# -519$ = Enterprise Admins group
# -544$ = Administrators group
# -18$ = SYSTEM
# -517$ = Cert Publishers
# -500$ = Built-in Administrator
$SafeOwners = '-512$|-519$|-544$|-18$|-517$|-500$'
# -512$ = Domain Admins group
# -519$ = Enterprise Admins group
# -544$ = Administrators group
# -18$ = SYSTEM
# -517$ = Cert Publishers
# -500$ = Built-in Administrator
# -516$ = Domain Controllers
# -9$ = Enterprise Domain Controllers
# -526$ = Key Admins
# -527$ = Enterprise Key Admins
# S-1-5-10 = SELF
$SafeUsers = '-512$|-519$|-544$|-18$|-517$|-500$|-516$|-9$|-526$|-527$|S-1-5-10'
# The well-known GUIDs for Enroll and AutoEnroll rights on AD CS templates.
$SafeObjectTypes = '0e10c968-78fb-11d2-90d4-00c04f79dc55|a05b8cc2-17bc-4802-a710-e7c15ab866a2'
# Set output mode
$Mode = 1
$Results = Find-ESC4 -ADCSObjects $ADCSObjects -DangerousRights $DangerousRights -SafeOwners $SafeOwners -SafeUsers $SafeUsers -SafeObjectTypes $SafeObjectTypes -Mode $Mode
$Results
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
$ADCSObjects,
[Parameter(Mandatory)]
$DangerousRights,
[Parameter(Mandatory)]
$SafeOwners,
[Parameter(Mandatory)]
$SafeUsers,
[Parameter(Mandatory)]
$SafeObjectTypes,
[Parameter(Mandatory)]
[int]$Mode
)
$ADCSObjects | Where-Object objectClass -EQ 'pKICertificateTemplate' | ForEach-Object {
if ($_.Name -ne '' -and $null -ne $_.Name) {
$Principal = [System.Security.Principal.NTAccount]::New($_.nTSecurityDescriptor.Owner)
if ($Principal -match '^(S-1|O:)') {
$SID = $Principal
}
else {
$SID = ($Principal.Translate([System.Security.Principal.SecurityIdentifier])).Value
}
}
if ($SID -notmatch $SafeOwners) {
$Issue = [pscustomobject]@{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
Enabled = $_.Enabled
EnabledOn = $_.EnabledOn
Issue = @"
$($_.nTSecurityDescriptor.Owner) has Owner rights on this template and can
modify it into a template that can create ESC1, ESC2, and ESC3 templates.
More info:
- https://posts.specterops.io/certified-pre-owned-d95910965cd2
"@
Fix = @"
`$Owner = New-Object System.Security.Principal.SecurityIdentifier(`'$PreferredOwner`')
`$ACL = Get-Acl -Path `'AD:$($_.DistinguishedName)`'
`$ACL.SetOwner(`$Owner)
Set-ACL -Path `'AD:$($_.DistinguishedName)`' -AclObject `$ACL
"@
Revert = @"
`$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
"@
Technique = 'ESC4'
}
$Issue
}
foreach ($entry in $_.nTSecurityDescriptor.Access) {
if ($_.Name -ne '' -and $null -ne $_.Name) {
$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.AccessControlType -eq 'Allow') -and
($entry.ActiveDirectoryRights -match $DangerousRights) -and
($entry.ObjectType -notmatch $SafeObjectTypes)
) {
$Issue = [pscustomobject]@{
Forest = $_.CanonicalName.split('/')[0]
Name = $_.Name
DistinguishedName = $_.DistinguishedName
IdentityReference = $entry.IdentityReference
ActiveDirectoryRights = $entry.ActiveDirectoryRights
Enabled = $_.Enabled
EnabledOn = $_.EnabledOn
Issue = @"
$($entry.IdentityReference) has been granted $($entry.ActiveDirectoryRights) rights on this template.
$($entry.IdentityReference) can likely modify this template into an ESC1 template.
More info:
- https://posts.specterops.io/certified-pre-owned-d95910965cd2
"@
Fix = @"
`$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
"@
Revert = '[TODO]'
Technique = 'ESC4'
}
if ( $Mode -in @(1, 3, 4) ) {
Update-ESC4Remediation -Issue $Issue
}
$Issue
}
}
}