-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGet-NetCertificateHealth.ps1
143 lines (137 loc) · 6.48 KB
/
Get-NetCertificateHealth.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
<#
.Synopsis
Retrieve certificate from remote system and validates it against criteria
.DESCRIPTION
Retrieve certificate from remote system and validates it against criteria for days until expiration, algorithm, and key size
.EXAMPLE
Get-NetCertificateHealth -IP 8.8.8.8
Queries 8.8.8.8 on the default port of 443 and verfies certificate doesn't expire with 60 days, uses at least SHA256RSA and has at least 2048 key size
It will return warning or critical results for any failing validations
.NOTES
Adapted by: Jason Wasser
Original code by: Rob VandenBrink
Inspiration
https://isc.sans.edu/forums/diary/Assessing+Remote+Certificates+with+Powershell/20645/
Modified: 1/9/2020 02:16:05 PM
Modified: 11/10/2020
Updated help from source function of Save-NetCertificate
Updated Verbiage in verbose statements for expired certificates
Added CmdletBinding So that the verbose statements are useful
#>
function Get-NetCertificateHealth {
[CmdletBinding()]
Param (
[Alias('IP')]
$ComputerName,
[int]$Port = 443,
[int]$WarningDays = 60,
[int]$CriticalDays = 30,
[string[]]$WarningAlgorithm = ('sha1RSA'),
[string[]]$CriticalAlgorithm = ('md5RSA'),
[int]$CriticalKeySize = 1024,
[int]$WarningKeySize = 2048
)
$NetCertificate = Get-NetCertificate -ComputerName $ComputerName -Port $Port
$CertificateProperties = @{
ComputerName = $ComputerName + ':' + $Port
FileName = 'N/A'
Subject = $NetCertificate.Subject
SignatureAlgorithm = $NetCertificate.SignatureAlgorithm.FriendlyName
NotBefore = $NetCertificate.NotBefore
NotAfter = $NetCertificate.NotAfter
Days = ($NetCertificate.NotAfter - (Get-Date)).Days
Thumbprint = $NetCertificate.Thumbprint
KeySize = $NetCertificate.PublicKey.Key.KeySize
}
$Certificate = New-Object -TypeName PSObject -Property $CertificateProperties
#region Check certificate expiration
# Check certificate is within $WarningDays
if ($Certificate.NotAfter -le (Get-Date).AddDays($WarningDays) -and $Certificate.NotAfter -gt (Get-Date).AddDays($CriticalDays)) {
Write-Verbose "Certificate is expiring within $WarningDays days."
$ValidityPeriodStatus = 'Warning'
$ValidityPeriodStatusMessage = "Certificate expiring in $($Certificate.Days) days."
}
# Check certificate is within $CriticalDays
elseif ($Certificate.NotAfter -le (Get-Date).AddDays($CriticalDays) -and $Certificate.NotAfter -gt (Get-Date)) {
Write-Verbose "Certificate is expiring within $CriticalDays days."
$ValidityPeriodStatus = 'Critical'
$ValidityPeriodStatusMessage = "Certificate expiring in $($Certificate.Days) days."
}
# Check certificate is expired
elseif ($Certificate.NotAfter -le (Get-Date)) {
Write-Verbose "Certificate expired: $($Certificate.Days) days."
$ValidityPeriodStatus = 'Critical'
$ValidityPeriodStatusMessage = "Certificate expired: $($Certificate.Days) days."
}
# Certificate validity period is healthy.
else {
Write-Verbose "Certificate is within validity period."
$ValidityPeriodStatus = 'OK'
$ValidityPeriodStatusMessage = "Certificate expires in $($Certificate.Days) days."
}
#endregion
#region Check certificate algorithm
if ($CriticalAlgorithm -contains $Certificate.SignatureAlgorithm) {
Write-Verbose "Certificate uses critical algorithm."
$AlgorithmStatus = 'Critical'
$AlgorithmStatusMessage = "Certificate uses a vulnerable algorithm $($Certificate.SignatureAlgorithm)."
}
elseif ($WarningAlgorithm -contains $Certificate.SignatureAlgorithm) {
Write-Verbose "Certificate uses warning algorithm."
$AlgorithmStatus = 'Warning'
$AlgorithmStatusMessage = "Certificate uses the deprecated algorithm $($Certificate.SignatureAlgorithm)."
}
else {
Write-Verbose "Certificate uses acceptable algorithm."
$AlgorithmStatus = 'OK'
$AlgorithmStatusMessage = "Certificate uses valid algorithm $($Certificate.SignatureAlgorithm)."
}
#endregion
#region Check MinimumKeySize
Write-Verbose 'Checking minimum key length.'
if ($Certificate.KeySize -lt $CriticalKeySize) {
# Key Size is critical
Write-Verbose 'Certificate key length is critical.'
$KeySizeStatus = 'Critical'
$KeySizeStatusMessage = "Certificate key size $($Certificate.KeySize) is less than $CriticalKeySize."
}
elseif ($Certificate.KeySize -lt $WarningKeySize -and $Certificate.KeySize -ge $CriticalKeySize) {
# Key Size is warning
Write-Verbose 'Certificate key length is warning.'
$KeySizeStatus = 'Warning'
$KeySizeStatusMessage = "Certificate key size $($Certificate.KeySize) is less than $WarningKeySize."
}
elseif ($Certificate.KeySize -ge $WarningKeySize) {
# Key Size is OK
Write-Verbose 'Certificate key length is OK.'
$KeySizeStatus = 'OK'
$KeySizeStatusMessage = "Certificate key size $($Certificate.KeySize) is greater than or equal to $WarningKeySize."
}
else {
# Key Size is OK
Write-Verbose 'Certificate key length is Unknown.'
$KeySizeStatus = 'Unknown'
$KeySizeStatusMessage = "Certificate key size is unknown."
}
#endregion
Write-Verbose 'Adding additional properties to the certificate object.'
$CertificateProperties = [ordered]@{
ComputerName = $ComputerName + ':' + $Port
FileName = $Certificate.FileName
Subject = $Certificate.Subject
SignatureAlgorithm = $Certificate.SignatureAlgorithm
NotBefore = $Certificate.NotBefore
NotAfter = $Certificate.NotAfter
Days = $Certificate.Days
Thumbprint = $Certificate.Thumbprint
ValidityPeriodStatus = $ValidityPeriodStatus
ValidityPeriodStatusMessage = $ValidityPeriodStatusMessage
AlgorithmStatus = $AlgorithmStatus
AlgorithmStatusMessage = $AlgorithmStatusMessage
KeySize = $Certificate.KeySize
KeySizeStatus = $KeySizeStatus
KeySizeStatusMessage = $KeySizeStatusMessage
}
$Certificate = New-Object -TypeName PSObject -Property $CertificateProperties
$Certificate
}