-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPKICommands.ps1
309 lines (270 loc) · 11.3 KB
/
PKICommands.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
<#
* Project: RevCacheClear
* CRLRevocationDate setter and getter
* (C) 2018-2019 by PKI Solutions
* Author: @gsamuelhayes
* Contributors: Vadims Podans
* Released under the GPLv3
#>
#requires -Version 3
function Set-CRLRevocationDate {
<#
.SYNOPSIS
Sets or deletes the CRLRevocationDate registry key.
.DESCRIPTION
This Cmdlet has the same effect as the 'certutil -setreg chain\ChainCacheResyncFiletime <date>` utility
except that it can be run across the network to multiple machines on various protocols*.
Returns an object with the details of the operation(s).
.PARAMETER ComputerName
A Computer or Computer array of machines that should have their registry key set.
.PARAMETER ExpirationTime
The date we wish to set for the ChainCacheResyncFiletime registry key.
.PARAMETER Delete
Deletes the registry value.
.PARAMETER AccessMethod
Specifies the remote access method to interact with remote registry. Following access methods are
accepted:
-- NET - .NET remote registry
-- PSRemoting - PowerShell Remoting (WSMAN) protocol
-- WMI - Windows Management Instrumentation
Default is WMI protocol.
.EXAMPLE
Set-CRLRevocationDate -ComputerName workstation1 -ExpirationTime (get-date)
Will expire the cert cache right now.
.EXAMPLE
With the Active Directory cmdlets we could run:
get-adcomputer -filter "name -like '*workstation*'" | Set-CRLRevocationDate -ExpirationTime (get-date)
This will expire all computers that meet the filter (so long as they are online and available with your permissions).
.EXAMPLE
With the Active Directory cmdlets we could run:
get-adcomputer -filter "name -like '*workstation*'" | Set-CRLRevocationDate -ExpirationTime (get-date) -AccessMethod PSRemoting
This will expire all computers that meet the filter (so long as they are online and available with your permissions). Remote computers
will be accessed via PowerShell Remoting protocol.
.EXAMPLE
With the Active Directory cmdlets we could run:
get-adcomputer -filter "name -like '*workstation*'" | Set-CRLRevocationDate -Delete
This will set revocation expiration date and time to its default state.
#>
[CmdletBinding(DefaultParameterSetName = '__setValue')]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Object[]]$ComputerName,
[ValidateSet("NET", "PSRemoting", "WMI")]
[string]$AccessMethod = "WMI",
[Parameter(ParameterSetName = '__setValue', Mandatory = $true)]
[DateTime]$ExpirationTime,
[Parameter(ParameterSetName = '__delValue')]
[switch]$Delete
)
begin {
$RegKey = "SOFTWARE\Microsoft\Cryptography\OID\EncodingType 0\CertDllCreateCertificateChainEngine\Config\"
$RegValue = "ChainCacheResyncFiletime"
$NewValue = switch ($PSCmdlet.ParameterSetName) {
'__setValue' {$ExpirationTime}
'__delValue' {"null"}
}
function __getCompName($InputObject) {
if ($InputObject -is [Microsoft.ActiveDirectory.Management.ADComputer]) {
$InputObject.DNSHostName
} else {
$InputObject
}
}
function __getBinaryDateTime($date) {
$Int64Value = $date.ToFileTime()
[System.BitConverter]::GetBytes($Int64Value)
}
function __setNetAccess($comp, [Byte[]]$binDate) {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $comp)
$key = $Reg.OpenSubKey("SOFTWARE\\Microsoft\\Cryptography\\OID\\EncodingType 0\\CertDllCreateCertificateChainEngine\\Config\\", $true)
$key.SetValue($RegValue, $binDate, [Microsoft.Win32.RegistryValueKind]::Binary)
}
function __setPSRemotingAccess($comp, [Byte[]]$binDate) {
Invoke-Command -ComputerName $comp -ScriptBlock {
$prop = New-ItemProperty -Path "HKLM:\$($using:RegKey)" -Name $using:RegValue -Value $using:binDate -PropertyType Binary -Force
} -ErrorAction Stop
}
function __setWmiAccess($comp, [Byte[]]$binDate) {
$wmi = [wmiclass]"\\$comp\root\DEFAULT:StdRegProv"
[void]$wmi.SetBinaryValue(2147483650, $RegKey, $RegValue, $binDate)
}
function __delNetAccess($comp) {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $comp)
$key = $reg.OpenSubKey($RegKey.Replace("\","\\"), $true)
if ($key.GetValueNames() -contains $RegValue) {
$key.DeleteValue($RegValue)
}
}
function __delPSRemotingAccess($comp) {
Invoke-Command -ComputerName $comp -ScriptBlock {
$prop = Get-ItemProperty -Path "HKLM:\$($using:RegKey)" -Name $using:RegValue -ErrorAction SilentlyContinue
if ($prop -eq $null) {
return
}
[void](Remove-ItemProperty -Path "HKLM:\$($using:RegKey)" -Name $using:RegValue)
} -ErrorAction Stop
}
function __delWmiAccess($comp) {
$wmi = [wmiclass]"\\$comp\root\DEFAULT:StdRegProv"
[void]$wmi.DeleteValue(2147483650, $RegKey, $RegValue)
}
}
process {
$ComputerName | ForEach-Object {
$comp = __getCompName $_
$obj = New-Object psobject -Property @{
Status = "N/A"
CRLRevocationDate = $NewValue
AccessMethod = $AccessMethod
Computer = $comp
Exception = $null
}
try {
switch ($PSCmdlet.ParameterSetName) {
'__setValue' {
$binDate = __getBinaryDateTime $ExpirationTime
switch ($AccessMethod) {
"NET" {__setNetAccess $comp $binDate}
"PSRemoting" {__setPSRemotingAccess $comp $binDate}
"WMI" {__setWmiAccess $comp $binDate}
}
}
'__delValue' {
switch ($AccessMethod) {
"NET" {__delNetAccess $comp}
"PSRemoting" {__delPSRemotingAccess $comp}
"WMI" {__delWmiAccess $comp}
}
}
}
$obj.Status = "Complete"
} catch {
throw $_
$obj.Exception = $_.Exception.GetType().FullName
$obj.Status = "Failed"
}
$obj
}
}
}
function Get-CRLRevocationDate {
<#
.SYNOPSIS
Gets the CRLRevocationDate registry key.
.DESCRIPTION
This Cmdlet has the same effect as the 'certutil -getreg chain\ChainCacheResyncFiletime` command
except that it can be run across the network to multiple machines on various protocols*.
Returns an object with the details of the operation(s).
Note: The command cannot be run against localhost.
.PARAMETER ComputerName
A Computer or Computer array of machines that should have their registry key set.
Accepts Pipeline input.
.PARAMETER AccessMethod
Specifies the remote access method to interact with remote registry. Following access methods are
accepted:
-- NET - .NET remote registry
-- PSRemoting - PowerShell Remoting (WSMAN) protocol
-- WMI - Windows Management Instrumentation
Default is WMI protocol
.EXAMPLE
Get-CRLRevocationDate -ComputerName workstation1
Status CRLRevocationDate ObtainedFrom Computer Exception
------ ----------------- ------------ -------- ---------
Complete 6/2/2018 12:56:08 PM Remote Registry workstation1
.EXAMPLE
With the Active Directory cmdlets we could run:
get-adcomputer -filter "samaccountname -like 'workstation1*'" | Get-CRLRevocationDate
Status CRLRevocationDate ObtainedFrom Computer Exception
------ ----------------- ------------ -------- ---------
Complete 6/2/2018 12:56:08 PM Remote Registry workstation1
This will expire all computers that meet the filter (so long as they are online and available with your permissions).
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Object[]]$ComputerName,
[ValidateSet("NET", "PSRemoting", "WMI")]
[string]$AccessMethod = "WMI"
)
begin {
$RegKey = "SOFTWARE\Microsoft\Cryptography\OID\EncodingType 0\CertDllCreateCertificateChainEngine\Config\"
$RegValue = "ChainCacheResyncFiletime"
$NotSet = "Not set"
function __getCompName($InputObject) {
if ($InputObject -is [Microsoft.ActiveDirectory.Management.ADComputer]) {
$InputObject.DNSHostName
} else {
$InputObject
}
}
function __getBinaryDateTime([Byte[]]$bytes) {
if (!$bytes) {
$false
return
}
$Int64Value = [System.BitConverter]::ToInt64($bytes, 0)
[DateTime]::FromFileTime($Int64Value)
}
# returns:
# false -- reg value not found
# other -- date/time
function __getNetAccess($comp) {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $comp)
$key = $reg.OpenSubKey($RegKey.Replace("\","\\"))
if ($key.GetValueNames() -notcontains $RegValue) {
$false
return
}
$bytes = $key.GetValue($RegValue)
__getBinaryDateTime $bytes
}
# returns date/time object
function __getPSRemotingAccess($comp) {
$value = Invoke-Command -ComputerName $comp -ScriptBlock {
$prop = Get-ItemProperty -Path "HKLM:\$($using:RegKey)" -Name $using:RegValue -ErrorAction SilentlyContinue
if ($prop -eq $null) {
$false
return
}
$prop."$using:RegValue"
} -ErrorAction Stop
__getBinaryDateTime $value
}
# returns date/time object
function __getWmiAccess($comp) {
$wmi = [wmiclass]"\\$comp\root\DEFAULT:StdRegProv"
$prop = $wmi.GetBinaryValue(2147483650,$RegKey,$RegValue)
if ($prop -eq $null) {
$false
return
}
__getBinaryDateTime $prop.uValue
}
}
process {
$ComputerName | ForEach-Object {
$comp = __getCompName $_
$obj = New-Object psobject -Property @{
Status = "N/A"
CRLRevocationDate = "N/A"
AccessMethod = $AccessMethod
Computer = $comp
Exception = $null
}
# trying to use remote registry... safest bet since winrm isn't on by default.
try {
$value = switch ($AccessMethod) {
"NET" {__getNetAccess $comp}
"PSRemoting" {__getPSRemotingAccess $comp}
"WMI" {__getWmiAccess $comp}
}
$obj.CRLRevocationDate = if ($value -is [DateTime]) {$value} else {$NotSet}
$obj.Status = "Complete"
} catch {
$obj.Exception = $_.Exception.GetType().FullName
$obj.Status = "Failed"
}
$obj
}
}
}