forked from GhostPack/PSPKIAudit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoke-PKIAudit.ps1
884 lines (682 loc) · 28.5 KB
/
Invoke-PKIAudit.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
$Version = "0.3.5"
# regex of low-privileged principal names used for testing vulnerable enrollment/access control
# Everyone S-1-1-0
# Authenticated Users S-1-5-11
# Domain Users S-1-5-21domain-513
# Domain Computers S-1-5-21domain-515
# Users S-1-5-32-545
$CommonLowprivPrincipals = "S-1-1-0|S-1-5-11|S-1-5-21.*-513|S-1-5-21.*-515|S-1-5-32-545"`
# cache for username->SID translations
$SIDTranslationCache = @{}
function Invoke-PKIAudit {
<#
.SYNOPSIS
Audits Certificate Authority settings and Certificate Template settings.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER CAComputerName
The name of the Certificate Authority computer to audit.
.PARAMETER CAName
The name of the Certificate Authority to audit.
.PARAMETER ShowAllVulnerableTemplates
Show all vulnerable templates, not just templates published to a CA.
#>
[CmdletBinding()]
Param(
[Parameter()]
[String]
$CAComputerName,
[Parameter()]
[String]
$CAName,
[Switch]
$ShowAllVulnerableTemplates
)
Write-Host -ForegroundColor Green @"
_____ _____ _____ _ _______ _ _ _
| __ \ / ____| __ \| |/ /_ _| /\ | (_) |
| |__) | (___ | |__) | ' / | | / \ _ _ __| |_| |_
| ___/ \___ \| ___/| < | | / /\ \| | | |/ `` | | __|
| | ____) | | | . \ _| |_ / ____ \ |_| | (_| | | |_
|_| |_____/|_| |_|\_\_____/_/ \_\__,_|\__,_|_|\__|
v$($Version)
"@
$Args = @{}
if ($PSBoundParameters['CAComputerName']) { $Args['CAComputerName'] = $CAComputerName }
if ($PSBoundParameters['CAName']) { $Args['CAName'] = $CAName }
Write-Host -ForegroundColor Green "`n[*] Enumerating certificate authorities with Get-AuditCertificateAuthority...`n"
$CAs = Get-AuditCertificateAuthority @Args
ForEach($CA in $CAs) {
$Args['CAComputerName'] = $CA.ComputerName
$Args['CAName'] = $CA.Name
if ($PSBoundParameters['ShowAllVulnerableTemplates']) { $Args['ShowAllVulnerableTemplates'] = $True }
$CAMisconfigurations = @()
$TemplateMisconfigurations = @()
Write-Host -ForegroundColor Green "`n`n=== Certificate Authority ==="
if($CA.VulnerableACL) {
$CAMisconfigurations += "ESC7"
}
if($CA.AllowsUserSuppliedSans) {
$CAMisconfigurations += "ESC6"
}
if($CA.NTLMEnrollmentEndpoints) {
$CAMisconfigurations += "ESC8"
}
$CA | Add-Member -MemberType NoteProperty -Name 'Misconfigurations' -Value $($CAMisconfigurations -join ",")
$CA
if($CA.Misconfigurations) {
Write-Host -ForegroundColor Red "[!] The above CA is misconfigured!"
}
# get the set of templates published to this CA (or all templates if -ShowAllVulnerableTemplates is passed)
$CATemplates = Get-AuditCertificateTemplate @Args
foreach($Template in $CATemplates) {
$TemplateMisconfigurations = @()
if( (-not $Template.CAManagerApproval) -and
($Template.IssuanceRequirements -match "Authorized signature count: 0") -and
($Template.LowPrivCanEnroll) -and
($Template.HasAuthenticationEku) -and
($Template.EnrolleeSuppliesSubject)
) {
$TemplateMisconfigurations += "ESC1"
}
if( (-not $Template.CAManagerApproval) -and
($Template.IssuanceRequirements -match "Authorized signature count: 0") -and
($Template.LowPrivCanEnroll) -and
($Template.HasDangerousEku)
) {
$TemplateMisconfigurations += "ESC2"
}
if( (-not $Template.CAManagerApproval) -and
($Template.IssuanceRequirements -match "Authorized signature count: 0") -and
($Template.LowPrivCanEnroll) -and
($Template.EnrollmentAgentTemplate)
) {
$TemplateMisconfigurations += "ESC3"
}
if ( $Template.VulnerableTemplateACL) {
$TemplateMisconfigurations += "ESC4"
}
$Template | Add-Member -MemberType NoteProperty -Name 'Misconfigurations' -Value $($TemplateMisconfigurations -join ",")
}
if($($CATemplates | Where-Object {$_.Misconfigurations}).Count -eq 0) {
Write-Host -ForegroundColor Green "`n[*] No vulnerable certificate templates found for this CA."
Write-Host -ForegroundColor Green "`n[*] NOTE: this is not a guarantee that this CA environment is secure!`n"
}
else {
Write-Host -ForegroundColor Red "`n[!] Potentially vulnerable Certificate Templates:`n"
$CATemplates | Where-Object {$_.Misconfigurations}
}
if($CA.AllowsUserSuppliedSans) {
$AuthTemplates = $CATemplates | Where-Object {
$_.HasAuthenticationEku -and
(-not $_.CAManagerApproval) -and
($_.IssuanceRequirements -match 'Authorized signature count: 0') -and
$_.LowPrivCanEnroll
}
if($AuthTemplates.Count -gt 0) {
Write-Host -ForegroundColor Red "[!] EDITF_ATTRIBUTESUBJECTALTNAME2 set on this CA, the following templates may be vulnerable:`n"
$AuthTemplates
}
}
}
}
function Get-AuditCertificateAuthority {
<#
.SYNOPSIS
Returns security-related information for all (or the specified) certificate authority.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER CAComputerName
The name of the Certificate Authority computer to return information for.
.PARAMETER CAName
The name of the Certificate Authority to return information for.
#>
[CmdletBinding()]
Param(
[Parameter()]
[String]
$CAComputerName,
[Parameter()]
[String]
$CAName
)
$CAs = @()
if($CAComputerName) {
$CAs = Get-CertificationAuthority -ComputerName $CAComputerName
}
elseif($CAName) {
$CAs = Get-CertificationAuthority -Name $CAName
}
else {
$CAs = Get-CertificationAuthority
}
ForEach($CA in $CAs) {
$CAServer = $CA.ComputerName
$CAName = $CA.Name
Write-Verbose "[Get-AuditCertificateAuthorit] CA: '$CAServer\$CAName'"
try {
$CAACL = $CA | Get-CertificationAuthorityAcl
$DACLString = (($CAACL.Access | ForEach-Object { "$($_.IdentityReference) ($($_.AccessControlType)) - $($_.Rights)"}) -join "`n")
$EnrollmentPrincipals = ForEach($Ace in $CAACL.Access) {
if(
($Ace.AccessControlType -eq [System.Security.AccessControl.AccessControlType]::Allow) -and
($Ace.Rights.HasFlag([SysadminsLV.PKI.Security.AccessControl.CertSrvRights]::Enroll))
) {
$Ace.IdentityReference
}
}
$AllowsUserSANs = Test-UserSpecifiesSAN -ComputerName $CAServer -CAName $CAName
}
catch {
Write-Warning "Error enumerating ACL information for CA '$CAServer\$CAName' : $_"
}
$CAACLVulnerable = $False
if($CAACL) {
$CAACLVulnerable = Test-IsCertificateAuthorityACLVulnerable $CAACL
}
$EnrollmentEndpoints = Get-ADCSEnrollmentEndpoint -ComputerName $CAServer -CAName $CAName -AuthType "Negotiate"
$NTLMEnrollmentEndpoints = Get-ADCSEnrollmentEndpoint -ComputerName $CAServer -CAName $CAName -AuthType "NTLM"
[pscustomobject] @{
ComputerName = $CAServer
CAName = $CAName
ConfigString = "$CAServer\$CAName"
IsRoot = $CA.IsRoot
AllowsUserSuppliedSans = $AllowsUserSANs
VulnerableACL = $CAACLVulnerable
EnrollmentPrincipals = ($EnrollmentPrincipals -join "`n")
EnrollmentEndpoints = ($EnrollmentEndpoints -join "|")
NTLMEnrollmentEndpoints = ($NTLMEnrollmentEndpoints -join "|")
DACL = $DACLString
}
}
}
function Get-ADCSEnrollmentEndpoint {
<#
.SYNOPSIS
Given an AD CS server name, return the enrollment endpoints alive/reachable for the given authentication method.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER ComputerName
The dns hostname of the CA server to check.
.PARAMETER CANAme
The CA name.
.PARAMETER AuthType
The type of authentication to use for the request, default of Negotiate.
#>
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$True)]
[String]
$ComputerName,
[Parameter(Position=1, Mandatory=$True)]
[String]
$CAName,
[Parameter(Position=2)]
[String]
[ValidateSet("NTLM", "Negotiate")]
$AuthType = "Negotiate"
)
foreach($P in @("http://", "https://")) {
foreach($Suffix in @("/certsrv/", "/$($CAName)_CES_Kerberos/service.svc", "/$($CAName)_CES_Kerberos/service.svc/CES", "/ADPolicyProvider_CEP_Kerberos/service.svc", "/certsrv/mscep/")) {
$URL = "$($P)$($ComputerName)$($Suffix)"
Write-Verbose "Testing enrollment URL: $URL"
if(Test-URLEndpoint -URL $URL) {
$URL
}
}
}
}
function Test-URLEndpoint {
<#
.SYNOPSIS
Checks if an HTTP endpoint exists and is reachable given the specified authentication type.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER URL
The URL of the endpoint to check.
.PARAMETER AuthType
The type of authentication to use for the request, default of NTLM.
#>
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$True)]
[String]
$URL,
[Parameter(Position=1)]
[String]
[ValidateSet("NTLM", "Negotiate")]
$AuthType = "NTLM"
)
$Request = [System.Net.WebRequest]::Create($URL)
$Cache = New-Object System.Net.CredentialCache
$Cache.Add([System.Uri]::new($URL), $AuthType, [System.Net.CredentialCache]::DefaultNetworkCredentials)
$Request.Credentials = $Cache
$Request.Timeout = 3000
try {
$Response = $Request.GetResponse()
return $Response.StatusCode -eq [System.Net.HttpStatusCode]::OK
}
catch {
return $False
}
}
function Get-AuditCertificateTemplate {
<#
.SYNOPSIS
Returns security-related information for certificate templates for all (or the specified) certificate authority.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER CAComputerName
The name of the Certificate Authority computer to return information for.
.PARAMETER CAName
The name of the Certificate Authority to return information for.
.PARAMETER ShowAllVulnerableTemplates
Show all vulnerable templates, not just templates published to a CA.
#>
[CmdletBinding()]
Param(
[Parameter()]
[String]
$CAComputerName,
[Parameter()]
[String]
$CAName,
[Switch]
$ShowAllVulnerableTemplates
)
$CAs = @()
if($CAComputerName) {
$CAs = Get-CertificationAuthority -ComputerName $CAComputerName
}
elseif($CAName) {
$CAs = Get-CertificationAuthority -Name $CAName
}
else {
$CAs = Get-CertificationAuthority
}
$Templates = Get-CertificateTemplate
if($ShowAllVulnerableTemplates) {
ForEach($CATemplate in $Templates) {
$CATemplateACL = $CATemplate | Get-CertificateTemplateAcl
$DACLString = (($CATemplateACL.Access | ForEach-Object { "$($_.IdentityReference) ($($_.AccessControlType)) - $($_.Rights)"}) -join "`n")
$IsTemplateACLVulnerable = Test-IsCertificateTemplateACLVulnerable $CATemplateACL
$CanLowPrivEnrollInTemplate = Test-CanLowPrivEnrollInTemplate $CATemplateACL
$EnrolleeSuppliesSubject = $CATemplate.Settings.SubjectName.HasFlag([PKI.CertificateTemplates.CertificateTemplateNameFlags]::EnrolleeSuppliesSubject)
# Client Authentication - 1.3.6.1.5.5.7.3.2
# PKINIT Client Authentication - 1.3.6.1.5.2.3.4
# Smart Card Logon - 1.3.6.1.4.1.311.20.2.2
# Any Purpose - 2.5.29.37.0
# SubCA - (no EKUs present)
$HasAuthenticationEku = ($CATemplate.Settings.EnhancedKeyUsage.Count -eq 0) -or (($CATemplate.Settings.EnhancedKeyUsage | Where-Object {$_.Value -match '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'}).Count -gt 0)
$HasDangerousEku = ($CATemplate.Settings.EnhancedKeyUsage.Count -eq 0) -or (($CATemplate.Settings.EnhancedKeyUsage | Where-Object {$_.Value -match '2\.5\.29\.37\.0'}).Count -gt 0)
# Certificate Request Agent - 1.3.6.1.4.1.311.20.2.1
$EnrollmentAgentTemplate = (($CATemplate.Settings.EnhancedKeyUsage | Where-Object {$_.Value -match '1\.3\.6\.1\.4\.1\.311\.20\.2\.1'}).Count -gt 0)
[pscustomobject] @{
CA = ""
Name = $CATemplate.Name
SchemaVersion = $CATemplate.SchemaVersion
OID = $CATemplate.OID
VulnerableTemplateACL = $IsTemplateACLVulnerable
LowPrivCanEnroll = $CanLowPrivEnrollInTemplate
EnrolleeSuppliesSubject = $EnrolleeSuppliesSubject
EnhancedKeyUsage = ($CATemplate.Settings.EnhancedKeyUsage -join "|")
HasAuthenticationEku = $HasAuthenticationEku
HasDangerousEku = $HasDangerousEku
EnrollmentAgentTemplate = $EnrollmentAgentTemplate
CAManagerApproval = $CATemplate.Settings.CAManagerApproval
IssuanceRequirements = $CATemplate.Settings.RegistrationAuthority.ToString()
ValidityPeriod = $CATemplate.Settings.ValidityPeriod
RenewalPeriod = $CATemplate.Settings.RenewalPeriod
Owner = $CATemplateACL.Owner
DACL = $DACLString
}
}
}
else {
ForEach($CA in $CAs) {
$CAServer = $CA.ComputerName
$CAName = $CA.Name
# get the set of templates published to this CA
$CATemplateNames = Get-ADObject $CA.DistinguishedName -Properties certificatetemplates | Select-Object -ExpandProperty certificatetemplates
if ($null -eq $CATemplateNames) { continue }
$CATemplates = $Templates | Where-Object {$CATemplateNames.Contains($_.Name)}
try {
ForEach($CATemplate in $CATemplates) {
$CATemplateACL = $CATemplate | Get-CertificateTemplateAcl
$DACLString = (($CATemplateACL.Access | ForEach-Object { "$($_.IdentityReference) ($($_.AccessControlType)) - $($_.Rights)"}) -join "`n")
$IsTemplateACLVulnerable = Test-IsCertificateTemplateACLVulnerable $CATemplateACL
$CanLowPrivEnrollInTemplate = Test-CanLowPrivEnrollInTemplate $CATemplateACL
$EnrolleeSuppliesSubject = $CATemplate.Settings.SubjectName.HasFlag([PKI.CertificateTemplates.CertificateTemplateNameFlags]::EnrolleeSuppliesSubject)
# Client Authentication - 1.3.6.1.5.5.7.3.2
# PKINIT Client Authentication - 1.3.6.1.5.2.3.4
# Smart Card Logon - 1.3.6.1.4.1.311.20.2.2
# Any Purpose - 2.5.29.37.0
# SubCA - (no EKUs present)
$HasAuthenticationEku = ($CATemplate.Settings.EnhancedKeyUsage.Count -eq 0) -or (($CATemplate.Settings.EnhancedKeyUsage | Where-Object {$_.Value -match '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'}).Count -gt 0)
$HasDangerousEku = ($CATemplate.Settings.EnhancedKeyUsage.Count -eq 0) -or (($CATemplate.Settings.EnhancedKeyUsage | Where-Object {$_.Value -match '2\.5\.29\.37\.0'}).Count -gt 0)
# Certificate Request Agent - 1.3.6.1.4.1.311.20.2.1
$EnrollmentAgentTemplate = (($CATemplate.Settings.EnhancedKeyUsage | Where-Object {$_.Value -match '1\.3\.6\.1\.4\.1\.311\.20\.2\.1'}).Count -gt 0)
[pscustomobject] @{
CA = "$CAServer\$CAName"
Name = $CATemplate.Name
SchemaVersion = $CATemplate.SchemaVersion
OID = $CATemplate.OID
VulnerableTemplateACL = $IsTemplateACLVulnerable
LowPrivCanEnroll = $CanLowPrivEnrollInTemplate
EnrolleeSuppliesSubject = $EnrolleeSuppliesSubject
EnhancedKeyUsage = ($CATemplate.Settings.EnhancedKeyUsage -join "|")
HasAuthenticationEku = $HasAuthenticationEku
HasDangerousEku = $HasDangerousEku
EnrollmentAgentTemplate = $EnrollmentAgentTemplate
CAManagerApproval = $CATemplate.Settings.CAManagerApproval
IssuanceRequirements = $CATemplate.Settings.RegistrationAuthority.ToString()
ValidityPeriod = $CATemplate.Settings.ValidityPeriod
RenewalPeriod = $CATemplate.Settings.RenewalPeriod
Owner = $CATemplateACL.Owner
DACL = $DACLString
}
}
}
catch {
Write-Error $_
}
}
}
}
function Test-IsCertificateTemplateACLVulnerable {
<#
.SYNOPSIS
Returns true if a certicate template ACL is vulnerable.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER Template
The certificate template ACL to audit.
#>
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$True)]
[SysadminsLV.PKI.Security.AccessControl.CertTemplateSecurityDescriptor]
$TemplateACL
)
if((ConvertName-ToSid $TemplateACL.Owner) -match $CommonLowprivPrincipals) {
return $True
}
ForEach($Ace in $TemplateACL.Access) {
if(
($Ace.AccessControlType -eq [System.Security.AccessControl.AccessControlType]::Allow) -and
( (ConvertName-ToSid $Ace.IdentityReference) -match $CommonLowprivPrincipals) -and
($Ace.Rights.HasFlag([SysadminsLV.PKI.Security.AccessControl.CertTemplateRights]::FullControl) -or $Ace.Rights.HasFlag([SysadminsLV.PKI.Security.AccessControl.CertTemplateRights]::Write))
) {
return $True
}
}
return $False
}
function ConvertName-ToSid {
<#
.SYNOPSIS
Converts a DOMAIN\username to a domain SID.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER Username
The username to convert
#>
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$True)]
[String]
$Username
)
# first check the translation cache
if($SIDTranslationCache.ContainsKey($Username)) {
return $SIDTranslationCache[$Username]
}
else {
try {
$NTAccount = New-Object System.Security.Principal.NTAccount($Username)
$SID = $NTAccount.Translate([System.Security.Principal.SecurityIdentifier])
$SIDTranslationCache[$Username] = $SID.Value
}
catch {
$SIDTranslationCache[$Username] = $Null
Write-Warning "Error converting '$Username' to domain SID."
}
return $SIDTranslationCache[$Username]
}
}
function Test-IsCertificateAuthorityACLVulnerable {
<#
.SYNOPSIS
Returns true if a certicate authority ACL is vulnerable.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER CAACL
The certificate authority ACL to audit.
#>
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$True)]
[SysadminsLV.PKI.Security.AccessControl.CertSrvSecurityDescriptor]
$CAACL
)
if( (ConvertName-ToSid $CAACL.Owner) -match $CommonLowprivPrincipals) {
return $True
}
ForEach($Ace in $CAACL.Access) {
if(
($Ace.AccessControlType -eq [System.Security.AccessControl.AccessControlType]::Allow) -and
( (ConvertName-ToSid $Ace.IdentityReference) -match $CommonLowprivPrincipals) -and
($Ace.Rights.HasFlag([SysadminsLV.PKI.Security.AccessControl.CertSrvRights]::ManageCA) -or $Ace.Rights.HasFlag([SysadminsLV.PKI.Security.AccessControl.CertSrvRights]::ManageCertificates))
) {
return $True
}
}
return $False
}
function Test-CanLowPrivEnrollInTemplate {
<#
.SYNOPSIS
Returns true if default low privileged principals can enroll in a template.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER TemplateACL
The certificate template ACL to audit.
#>
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$True)]
[SysadminsLV.PKI.Security.AccessControl.CertTemplateSecurityDescriptor]
$TemplateACL
)
ForEach($Ace in $TemplateACL.Access) {
if(
($Ace.AccessControlType -eq [System.Security.AccessControl.AccessControlType]::Allow) -and
( (ConvertName-ToSid $Ace.IdentityReference) -match $CommonLowprivPrincipals) -and
($Ace.Rights.HasFlag([SysadminsLV.PKI.Security.AccessControl.CertTemplateRights]::Enroll))
) {
return $True
}
}
return $False
}
function Test-UserSpecifiesSAN {
<#
.SYNOPSIS
Returns true if the specified server\CA has the EDITF_ATTRIBUTESUBJECTALTNAME2 flag set.
License: Ms-PL
Required Dependencies: PSPKI
#>
[CmdletBinding()]
Param
(
[Parameter(Position=0, Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[String]
$ComputerName,
[Parameter(Position=1, Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[String]
$CAName
)
if (Test-Connection $ComputerName -Count 2 -Quiet) {
try {
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $ComputerName)
$Key = $Reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\$($CAName)\PolicyModules\CertificateAuthority_MicrosoftDefault.Policy")
$Data = $Key.GetValue("EditFlags")
($Data -band 0x00040000) -eq 0x00040000
}
catch {
Write-Warning "[Test-UserSpecifiesSAN] Error: $_"
return $false
}
}
else {
Write-Warning "[Test-UserSpecifiesSAN] $ComputerName not reachable!"
return $false
}
}
function Get-AuditPKIADObjectControllers {
<#
.SYNOPSIS
Returns users who have control or edit rights to PKI AD objects.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER Server
The domain controller to connect to.
.PARAMETER Credential
The credential used to authenticate.
.EXAMPLE
$Controllers = Get-AuditPKIADObjectControllers
Format-PKIAdObjectControllers $Controllers
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[string]
$Server,
[Parameter(Mandatory=$False)]
[pscredential]
$Credential
)
$ObjectControllers = New-Object 'System.Collections.Generic.SortedDictionary[[string],[System.Collections.ArrayList]]'
try {
$RootDse = Get-ADRootDSE @PSBoundParameters -ErrorAction Stop
} catch {
throw "Could not query the forest root: $($_)"
}
$PkiServicesContainer = "CN=Public Key Services,CN=Services,$($RootDse.configurationNamingContext)"
foreach($Obj in (Get-ADObject @PSBoundParameters -SearchBase $PkiServicesContainer -Filter * -Properties DistinguishedName,ntsecuritydescriptor)) {
$Dn = $Obj.DistinguishedName
$Sd = $Obj.ntsecuritydescriptor
if($null -eq $Sd) {
Write-Warning "Could not obtain AD security information for the object $($Obj.DistinguishedName)"
continue
}
$OwnerSid = $sd.GetOwner([System.Security.Principal.SecurityIdentifier])
try {
$Owner = $sd.GetOwner([System.Security.Principal.NTAccount])
} catch {
$Owner = $null
}
$Key = "$($Owner)`t$($OwnerSid)"
if(!$ObjectControllers.ContainsKey($Key)) {
$ObjectControllers[$Key] = New-Object System.Collections.ArrayList
}
$null = $ObjectControllers[$Key].Add([PSCustomObject]@{
Right = 'Owner'
ADObject = $Dn
})
$Dacl = $sd.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier])
foreach($Ace in $Dacl) {
$AceOwnerSid = $Ace.IdentityReference
try {
$AceOwner = $AceOwnerSid.Translate([System.Security.Principal.NTAccount])
} catch {
$AceOwner = $null
}
$Key = "$($AceOwner)`t$($AceOwnerSid)"
if(!$ObjectControllers.ContainsKey($Key)) {
$ObjectControllers[$Key] = New-Object System.Collections.ArrayList
}
if($Dn -match 'ADObject') {
$null = $null
}
$Right = $Ace.ActiveDirectoryRights
if($Right.HasFlag([System.DirectoryServices.ActiveDirectoryRights]::GenericAll)) {
$null = $ObjectControllers[$Key].Add([PSCustomObject]@{
Right = 'GenericAll'
ADObject = $Dn
})
} elseif($Right.HasFlag([System.DirectoryServices.ActiveDirectoryRights]::WriteOwner)) {
$null = $ObjectControllers[$Key].Add([PSCustomObject]@{
Right = 'WriteOwner'
ADObject = $Dn
})
} elseif($Right.HasFlag([System.DirectoryServices.ActiveDirectoryRights]::WriteDacl)) {
$null = $ObjectControllers[$Key].Add([PSCustomObject]@{
Right = 'WriteDacl'
ADObject = $Dn
})
} elseif($Right.HasFlag([System.DirectoryServices.ActiveDirectoryRights]::WriteProperty) -and $Ace.ObjectType -eq "00000000-0000-0000-0000-000000000000") {
$null = $ObjectControllers[$Key].Add([PSCustomObject]@{
Right = 'WriteAllProperties'
ADObject = $Dn
})
}
}
}
foreach($Entry in $ObjectControllers.GetEnumerator()) {
$Obj = $Entry.Value
if($Obj.Count -eq 0) {
continue
}
$User,$UserSid = $Entry.Key.Split("`t")
[PSCustomObject]@{
User = $User
UserSid = $UserSid
Access = $Obj
}
}
}
function Format-PKIAdObjectControllers {
<#
.SYNOPSIS
Formats the output of Get-AuditPKIADObjectControllers.
License: Ms-PL
Required Dependencies: PSPKI
.PARAMETER InputObject
Output objects from Get-AuditPKIADObjectControllers to format.
.PARAMETER IncludeDefaultAdministrators
Displays access rights where the principal is a default AD administrator (e.g. Enterprise Admins).
.EXAMPLE
$Controllers = Get-AuditPKIADObjectControllers
Format-PKIAdObjectControllers $Controllers
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
$InputObject,
[switch]
$IncludeDefaultAdministrators
)
Process {
foreach($Obj in $InputObject) {
if(!$IncludeDefaultAdministrators -and (
$Obj.UserSid.EndsWith('-519') -or
$Obj.UserSid.EndsWith('-512') -or
$Obj.UserSid -eq 'S-1-5-32-544' -or
$Obj.UserSid -eq 'S-1-5-18'
)) {
continue
}
if([string]::IsNullOrEmpty($Obj.User)) {
$UserStr = "$($Obj.UserSid)"
} else {
$UserStr = "$($Obj.User) ($($Obj.UserSid))"
}
Write-Host $UserStr -ForegroundColor Red
foreach($i in $Obj.Access) {
Write-Host " $($i.Right.PadRight(18)) $($i.ADObject)"
}
Write-Host
}
}
}