-
Notifications
You must be signed in to change notification settings - Fork 23
/
ADCSTemplate.psm1
executable file
·470 lines (430 loc) · 21.5 KB
/
ADCSTemplate.psm1
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
#requires -Version 5.0 -Modules ActiveDirectory
Function Get-RandomHex {
param ([int]$Length)
$Hex = '0123456789ABCDEF'
[string]$Return = $null
For ($i=1;$i -le $length;$i++) {
$Return += $Hex.Substring((Get-Random -Minimum 0 -Maximum 16),1)
}
Return $Return
}
Function IsUniqueOID {
param ($cn,$TemplateOID,$Server,$ConfigNC)
$Search = Get-ADObject -Server $Server `
-SearchBase "CN=OID,CN=Public Key Services,CN=Services,$ConfigNC" `
-Filter {cn -eq $cn -and msPKI-Cert-Template-OID -eq $TemplateOID}
If ($Search) {$False} Else {$True}
}
Function New-TemplateOID {
Param($Server,$ConfigNC)
<#
OID CN/Name [10000000-99999999].[32 hex characters (MD5hash)]
OID msPKI-Cert-Template-OID [Forest base OID].[1000000-99999999].[10000000-99999999] <--- second number same as first number in OID name
#>
do {
$OID_Part_1 = Get-Random -Minimum 10000000 -Maximum 99999999
$OID_Part_2 = Get-Random -Minimum 10000000 -Maximum 99999999
$OID_Part_3 = Get-RandomHex -Length 32
$OID_Forest = Get-ADObject -Server $Server `
-Identity "CN=OID,CN=Public Key Services,CN=Services,$ConfigNC" `
-Properties msPKI-Cert-Template-OID |
Select-Object -ExpandProperty msPKI-Cert-Template-OID
$msPKICertTemplateOID = "$OID_Forest.$OID_Part_1.$OID_Part_2"
$Name = "$OID_Part_2.$OID_Part_3"
} until (IsUniqueOID -cn $Name -TemplateOID $msPKICertTemplateOID -Server $Server -ConfigNC $ConfigNC)
Return @{
TemplateOID = $msPKICertTemplateOID
TemplateName = $Name
}
}
<#
.SYNOPSIS
Returns the properties of either a single or all Active Directory Certificate Template(s).
.DESCRIPTION
Returns the properties of either a single or list of Active Directory Certificate Template(s)
depending on whether a DisplayName parameter was passed.
.PARAMETER DisplayName
Name of an AD CS template to retrieve.
.PARAMETER Server
FQDN of Active Directory Domain Controller to target for the operation.
When not specified it will search for the nearest Domain Controller.
.EXAMPLE
PS C:\> Get-ADCSTemplate
.EXAMPLE
PS C:\> Get-ADCSTemplate -DisplayName PowerShellCMS
.EXAMPLE
PS C:\> Get-ADCSTemplate | Sort-Object Name | ft Name, Created, Modified
.EXAMPLE
PS C:\> ###View template permissions
(Get-ADCSTemplate pscms).nTSecurityDescriptor
(Get-ADCSTemplate pscms).nTSecurityDescriptor.Sddl
(Get-ADCSTemplate pscms).nTSecurityDescriptor.Access
ConvertFrom-SddlString -Sddl (Get-ADCSTemplate pscms).nTSecurityDescriptor.sddl -Type ActiveDirectoryRights
.NOTES
Requires Enterprise Administrator permissions, since this touches the AD Configuration partition.
#>
Function Get-ADCSTemplate {
param(
[parameter(Position=0)]
[string]
$DisplayName,
[string]
$Server = (Get-ADDomainController -Discover -ForceDiscover -Writable).HostName[0]
)
If ($PSBoundParameters.ContainsKey('DisplayName')) {
$LDAPFilter = "(&(objectClass=pKICertificateTemplate)(displayName=$DisplayName))"
} Else {
$LDAPFilter = '(objectClass=pKICertificateTemplate)'
}
$ConfigNC = $((Get-ADRootDSE -Server $Server).configurationNamingContext)
$TemplatePath = "CN=Certificate Templates,CN=Public Key Services,CN=Services,$ConfigNC"
Get-ADObject -SearchScope Subtree -SearchBase $TemplatePath -LDAPFilter $LDAPFilter -Properties * -Server $Server
}
<#
.SYNOPSIS
Adds an ACL to an Active Directory Certificate template.
.DESCRIPTION
Adds an ACL to an Active Directory Certificate template.
Default permission is read (without the Enroll or AutoEnroll switches).
.PARAMETER DisplayName
Name of an AD CS template to receive the ACL.
.PARAMETER Server
FQDN of Active Directory Domain Controller to target for the operation.
When not specified it will search for the nearest Domain Controller.
.PARAMETER Type
ACL type: Allow or Deny
.PARAMETER Identity
String or string array of Active Directory identities (users or groups)
.PARAMETER Enroll
Set the Enroll permission
.PARAMETER AutoEnroll
Set the AutoEnroll permission
.EXAMPLE
PS C:\> Set-ADCSTemplateACL -DisplayName PowerShellCMS -Type Allow -Identity 'CONTOSO\Servers Group' -Enroll
.EXAMPLE
PS C:\> Set-ADCSTemplateACL -DisplayName PowerShellCMS -Type Allow -Identity 'CONTOSO\Servers Group','CONTOSO\Workstations Group' -Enroll -AutoEnroll
.EXAMPLE
PS C:\> Set-ADCSTemplateACL -DisplayName PowerShellCMS -Type Deny -Identity 'CONTOSO\Servers Group'
.NOTES
Requires Enterprise Administrator permissions, since this touches the AD Configuration partition.
#>
Function Set-ADCSTemplateACL {
param(
[parameter(Mandatory)]
[string]$DisplayName,
[string]$Server = (Get-ADDomainController -Discover -ForceDiscover -Writable).HostName[0],
[ValidateSet('Allow','Deny')]
[string]$Type = 'Allow',
[string[]]$Identity,
[switch]$Enroll,
[switch]$AutoEnroll
)
## Potential issue here that the AD: drive may not be targetting the selected DC in the -SERVER parameter
$TemplatePath = "AD:\" + (Get-ADCSTemplate -DisplayName $DisplayName -Server $Server).DistinguishedName
$acl = Get-ACL $TemplatePath
$InheritedObjectType = [GUID]'00000000-0000-0000-0000-000000000000'
ForEach ($Group in $Identity) {
$account = New-Object System.Security.Principal.NTAccount($Group)
$sid = $account.Translate([System.Security.Principal.SecurityIdentifier])
If ($Type -ne 'Deny') {
# Read, but only if Allow
$ObjectType = [GUID]'00000000-0000-0000-0000-000000000000'
$ace = New-Object System.DirectoryServices.ActiveDirectoryAccessRule `
$sid, 'GenericRead', $Type, $ObjectType, 'None', $InheritedObjectType
$acl.AddAccessRule($ace)
}
If ($Enroll) {
$ObjectType = [GUID]'0e10c968-78fb-11d2-90d4-00c04f79dc55'
$ace = New-Object System.DirectoryServices.ActiveDirectoryAccessRule `
$sid, 'ExtendedRight', $Type, $ObjectType, 'None', $InheritedObjectType
$acl.AddAccessRule($ace)
}
If ($AutoEnroll) {
$ObjectType = [GUID]'a05b8cc2-17bc-4802-a710-e7c15ab866a2'
$ace = New-Object System.DirectoryServices.ActiveDirectoryAccessRule `
$sid, 'ExtendedRight', $Type, $ObjectType, 'None', $InheritedObjectType
$acl.AddAccessRule($ace)
}
}
Set-ACL $TemplatePath -AclObject $acl
}
<#
.SYNOPSIS
Returns a JSON string with the properties of an Active Directory certificate template.
.DESCRIPTION
Returns a JSON string with the properties of an Active Directory certificate template.
By default returns only the PKI-related properties of the object. These properties are
sufficient for passing to the New-ADCSTemplate function.
.PARAMETER DisplayName
DisplayName for the certificate to export.
.PARAMETER Server
FQDN of Active Directory Domain Controller to target for the operation.
When not specified it will search for the nearest Domain Controller.
.PARAMETER Detailed
Includes all ADObject properties of the template. These are not required for
use with the New-ADCSTemplate function.
.NOTES
C.R.U.D. AD CS Template Operations in this module.
No longer have to use the cert GUI to clone a template and build a new one.
Create one manually the first time in the GUI, then export it to JSON.
Pass the JSON in your new environment (file, here string, DSC, etc.) to build from scratch.
Requires Enterprise Administrator permissions, since this touches the AD Configuration partition.
.EXAMPLE
PS C:\> Export-ADCSTemplate -DisplayName PowerShellCMS
.EXAMPLE
PS C:\> Export-ADCSTemplate -DisplayName PowerShellCMS -Detailed
.EXAMPLE
### Backup all the templates to JSON
md C:\ADCSTemplates -ErrorAction SilentlyContinue
cd C:\ADCSTemplates
(Get-ADCSTemplate).name | ForEach-Object {"Exporting $_"; Export-ADCSTemplate -DisplayName $_ | Out-File .\$_.json -Force}
dir
.EXAMPLE
PS C:\> New-ADCSTemplate -DisplayName PowerShellCMS-NEW -JSON (Export-ADCSTemplate -DisplayName PowerShellCMS-OLD)
#>
Function Export-ADCSTemplate {
param(
[parameter(Mandatory)]
[string]$DisplayName,
[string]$Server = (Get-ADDomainController -Discover -ForceDiscover -Writable).HostName[0],
[switch]$Detailed # Detailed output is not required for export/import. Use for documentation/backup purposes.
)
If ($Detailed) {
Get-ADCSTemplate -DisplayName $DisplayName -Server $Server |
ConvertTo-Json
} Else {
Get-ADCSTemplate -DisplayName $DisplayName -Server $Server |
Select-Object -Property name, displayName, objectClass, flags, revision, *pki* |
ConvertTo-Json
}
}
<#
.SYNOPSIS
Creates a new Active Directory Certificate Services template based on a JSON export.
.DESCRIPTION
Creates a new Active Directory Certificate Services template based on a JSON export.
Optionally can permission and publish the template (best practice).
.PARAMETER DisplayName
DisplayName for the certificate template to create. This does not have to match
the original name of the exported template.
.PARAMETER JSON
JSON string output from Export-ADCSTemplate. Defines the template to create.
Can be retrieved from file using Get-Content -Raw.
.PARAMETER Server
FQDN of Active Directory Domain Controller to target for the operation.
When not specified it will search for the nearest Domain Controller.
.PARAMETER Identity
String or string array of Active Directory identities (users or groups).
This is optional for permissioning the template.
.PARAMETER AutoEnroll
Default permission is Read and Enroll. Use this switch to also grant AutoEnroll
to the identity. Only used when Identity parameter is used.
.PARAMETER Publish
Publish the template to *ALL* Certificate Authority issuers. Use with caution
in production environments. You may want to manually publish to only specific
Certificate Authorities in production. In a lab this is ideal.
.NOTES
This function does not use the official (complicated) API for PKI management.
Instead it creates the exact same AD objects that are generated by the API,
including AD forest-specific OIDs.
Requires Enterprise Administrator permissions, since this touches the AD Configuration partition.
.EXAMPLE
PS C:\> New-ADCSTemplate -DisplayName PowerShellCMS -JSON (Get-Content .\pscms.json -Raw)
.EXAMPLE
PS C:\> New-ADCSTemplate -DisplayName PowerShellCMS -JSON (Get-Content .\pscms.json -Raw) -Server dc1.contoso.com -Identity G_DSCNodes -AutoEnroll -Publish
# From a client configured for AD CS autoenrollment:
$Req = @{
Template = 'PowerShellCMS'
Url = 'ldap:'
CertStoreLocation = 'Cert:\LocalMachine\My'
}
Get-Certificate @Req
# Note: If you have the Carbon module installed, it conflicts with Get-Certificate native cmdlet.
$DocEncrCert = (dir Cert:\LocalMachine\My -DocumentEncryptionCert | Sort-Object NotBefore)[-1]
Protect-CmsMessage -To $DocEncrCert -Content "Encrypted with my new cert from the new template!"
.EXAMPLE
PS C:\> New-ADCSTemplate -DisplayName PowerShellCMS-NEW -JSON (Export-ADCSTemplate -DisplayName PowerShellCMS-OLD)
#>
Function New-ADCSTemplate {
param(
[parameter(Mandatory)]
[string]$DisplayName, # name in JSON export is ignored
[parameter(Mandatory)]
[string]$JSON,
[string]$Server = (Get-ADDomainController -Discover -ForceDiscover -Writable).HostName[0],
[string[]]$Identity, # = "$((Get-ADDomain).NetBIOSName)\Domain Computers",
[switch]$AutoEnroll,
[switch]$Publish
)
### Put GroupName and AutoEnroll into a parameter set
# Manually import AD module to get AD: drive used later for permissions
Import-Module ActiveDirectory -Verbose:$false
$ConfigNC = $((Get-ADRootDSE -Server $Server).configurationNamingContext)
#region CREATE OID
<#
CN : 14891906.F2AC4390685318BD1D950A66EDB50FF4
DisplayName : TemplateNameHere
DistinguishedName : CN=14891906.F2AC4390685318BD1D950A66EDB50FF4,CN=OID,CN=Public Key Services,CN=Services,CN=Configuration,DC=contoso,DC=com
dSCorePropagationData : {1/1/1601 12:00:00 AM}
flags : 1
instanceType : 4
msPKI-Cert-Template-OID : 1.3.6.1.4.1.311.21.8.11489019.14294623.5588661.594850.12204198.151.6616009.14891906
Name : 14891906.F2AC4390685318BD1D950A66EDB50FF4
ObjectCategory : CN=ms-PKI-Enterprise-Oid,CN=Schema,CN=Configuration,DC=contoso,DC=com
ObjectClass : msPKI-Enterprise-Oid
#>
$OID = New-TemplateOID -Server $Server -ConfigNC $ConfigNC
$TemplateOIDPath = "CN=OID,CN=Public Key Services,CN=Services,$ConfigNC"
$oa = @{
'DisplayName' = $DisplayName
'flags' = [System.Int32]'1'
'msPKI-Cert-Template-OID' = $OID.TemplateOID
}
New-ADObject -Path $TemplateOIDPath -OtherAttributes $oa -Name $OID.TemplateName -Type 'msPKI-Enterprise-Oid' -Server $Server
#endregion
#region CREATE TEMPLATE
# https://docs.microsoft.com/en-us/powershell/dsc/securemof#certificate-requirements
# https://blogs.technet.microsoft.com/option_explicit/2012/04/09/pki-certificates-and-the-x-509-standard/
# https://technet.microsoft.com/en-us/library/cc776447(v=ws.10).aspx
$import = $JSON | ConvertFrom-Json
$oa = @{ 'msPKI-Cert-Template-OID' = $OID.TemplateOID }
ForEach ($prop in ($import | Get-Member -MemberType NoteProperty)) {
Switch ($prop.Name) {
{ $_ -in 'flags',
'msPKI-Certificate-Name-Flag',
'msPKI-Enrollment-Flag',
'msPKI-Minimal-Key-Size',
'msPKI-Private-Key-Flag',
'msPKI-Template-Minor-Revision',
'msPKI-Template-Schema-Version',
'msPKI-RA-Signature',
'pKIMaxIssuingDepth',
'pKIDefaultKeySpec',
'revision'
} { $oa.Add($_,[System.Int32]$import.$_); break }
{ $_ -in 'msPKI-Certificate-Application-Policy',
'msPKI-RA-Application-Policies',
'pKICriticalExtensions',
'pKIDefaultCSPs',
'pKIExtendedKeyUsage'
} { $oa.Add($_,[Microsoft.ActiveDirectory.Management.ADPropertyValueCollection]$import.$_); break }
{ $_ -in 'msPKI-Supersede-Templates'
} { $oa.Add($_,[Microsoft.ActiveDirectory.Management.ADPropertyValueCollection[]]$import.$_); break }
{ $_ -in 'pKIExpirationPeriod',
'pKIKeyUsage',
'pKIOverlapPeriod'
} { $oa.Add($_,[System.Byte[]]$import.$_); break }
}
}
$TemplatePath = "CN=Certificate Templates,CN=Public Key Services,CN=Services,$ConfigNC"
New-ADObject -Path $TemplatePath -OtherAttributes $oa -Name $DisplayName.Replace(' ','') `
-DisplayName $DisplayName -Type pKICertificateTemplate -Server $Server
#endregion
#region PERMISSIONS
## Potential issue here that the AD: drive may not be targetting the selected DC in the -SERVER parameter
If ($PSBoundParameters.ContainsKey('Identity')) {
If ($AutoEnroll) {
Set-ADCSTemplateACL -DisplayName $DisplayName -Server $Server -Type Allow -Identity $Identity -Enroll -AutoEnroll
} Else {
Set-ADCSTemplateACL -DisplayName $DisplayName -Server $Server -Type Allow -Identity $Identity -Enroll
}
}
#endregion
#region ISSUE
If ($Publish) {
### WARNING: Issues on all available CAs. Test in your environment.
$EnrollmentPath = "CN=Enrollment Services,CN=Public Key Services,CN=Services,$ConfigNC"
$CAs = Get-ADObject -SearchBase $EnrollmentPath -SearchScope OneLevel -Filter * -Server $Server
ForEach ($CA in $CAs) {
Set-ADObject -Identity $CA.DistinguishedName -Add @{certificateTemplates=$DisplayName.Replace(' ','')} -Server $Server
}
}
#endregion
}
<#
.SYNOPSIS
Removes a certificate template from Active Directory.
.DESCRIPTION
Removes the template from any issuers where it is published.
Removes the template itself.
Removes the unique OID object of the template.
.PARAMETER DisplayName
DisplayName for the certificate template to delete.
.PARAMETER Server
FQDN of Active Directory Domain Controller to target for the operation.
When not specified it will search for the nearest Domain Controller.
.EXAMPLE
PS C:\> Remove-ADCSTemplate -DisplayName PowerShellCMS
.EXAMPLE
PS C:\> (Get-ADCSTemplate).name | Where-Object {$_ -like "PowerShellCMS*"} | ForEach-Object {Remove-ADCSTemplate -DisplayName $_ -Verbose}
.NOTES
Use with caution!
Requires Enterprise Administrator permissions, since this touches the AD Configuration partition.
#>
Function Remove-ADCSTemplate {
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')]
param(
[parameter(Mandatory)]
[string]$DisplayName,
[string]$Server = (Get-ADDomainController -Discover -ForceDiscover -Writable).HostName[0]
)
if ($pscmdlet.ShouldProcess($DisplayName, 'Remove certificate template')) {
$ConfigNC = $((Get-ADRootDSE -Server $Server).configurationNamingContext)
$Template = Get-ADCSTemplate -DisplayName $DisplayName -Server $Server
#region REMOVE ISSUE IF IT EXISTS
$EnrollmentPath = "CN=Enrollment Services,CN=Public Key Services,CN=Services,$ConfigNC"
$CAs = Get-ADObject -SearchBase $EnrollmentPath -SearchScope OneLevel -Filter * -Server $Server
ForEach ($CA in $CAs) {
Set-ADObject -Identity $CA.DistinguishedName -Remove @{certificateTemplates=$Template.cn} -Server $Server -Confirm:$false
}
#endregion
#region REMOVE TEMPLATE
Remove-ADObject -Identity $Template.distinguishedName -Server $Server -Confirm:$false
#endregion
#region REMOVE OID
$TemplateOIDPath = "CN=OID,CN=Public Key Services,CN=Services,$ConfigNC"
Get-ADObject -SearchBase $TemplateOIDPath -LDAPFilter "(DisplayName=$DisplayName)" -Server $Server | Remove-ADObject -Confirm:$false
#endregion
}
}
<#
.SYNOPSIS
Maps a PowerShell drive to the Active Directory Certificate Services location.
.DESCRIPTION
Maps a PowerShell drive to the Active Directory Certificate Services location
of the Configuration partition under CN=Public Key Services,CN=Services,... .
The new drive is ADCS:. This is purely for convenience of checking the objects
updated by functions in the ADCSTemplate module.
.PARAMETER Server
FQDN of Active Directory Domain Controller to target for the operation.
When not specified it will search for the nearest Domain Controller.
.EXAMPLE
PS C:\> New-ADCSDrive
PS C:\> Set-Location ADCS:
.EXAMPLE
### Explore templates with drive
New-ADCSDrive
Get-PSDrive
cd ADCS:
dir
# List templates
cd '.\CN=Certificate Templates'
dir
dir | fl *
dir *WebServer*
# list CAs
cd \
cd '.\CN=Enrollment Services'
dir
cd C:
.NOTES
Requires Enterprise Administrator permissions, since this touches the AD Configuration partition.
#>
Function New-ADCSDrive {
param(
[string]$Server = (Get-ADDomainController -Discover -ForceDiscover -Writable).HostName[0]
)
$ConfigNC = $((Get-ADRootDSE -Server $Server).configurationNamingContext)
New-PSDrive -Name ADCS -PSProvider ActiveDirectory -Root "CN=Public Key Services,CN=Services,$ConfigNC" -Server $Server -Scope Global
}
Export-ModuleMember -Function *-ADCS*