Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Windows Server

การตรวจสอบวันหมดอายุ SSL/TLS Certificate ด้วย PowerShell

การหมดอายุของ server certificate อย่างไม่คาดคิดอาจก่อให้เกิดปัญหาหลายอย่างให้กับผู้ใช้และลูกค้าของคุณ: พวกเขาอาจไม่สามารถสร้างการเชื่อมต่อที่ปลอดภัยกับไซต์ของคุณได้ เกิดข้อผิดพลาดการรับรองความถูกต้อง การแจ้งเตือนที่รำคาญอาจปรากฏในเบราว์เซอร์ ฯลฯ ในบทความนี้ เราจะแสดงวิธีตรวจสอบวันหมดอายุของ SSL/TLS certificate บนไซต์ระยะไกล หรือดึงรายการ certificates ที่ใกล้หมดอายุใน local certificate store บนเซิร์ฟเวอร์หรือคอมพิวเตอร์ในโดเมนของคุณ

ดูวันหมดอายุของ Website SSL Certificate ด้วย PowerShell

โครงการเว็บหลายโครงการใช้ฟรี Let’s Encrypt SSL certificates เพื่อนำ HTTPS ไปใช้憑證เหล่านี้ออกให้เป็นเวลา 90 วัน และต้องต่ออายุเป็นประจำ ปกติสคริปต์หรือบอทพิเศษจะอัปเดต Let’s Encrypt certificates บนโฮสติ้งหรือฝั่งเซิร์ฟเวอร์ (อาจเป็น WACS ใน Windows หรือ Certbot ใน Linux) อย่างไรก็ตาม บางครั้งการต่ออายุ certificate อัตโนมัติล้มเหลว ผมอยากมีสคริปต์ของตัวเองที่จะตรวจสอบวันหมดอายุ SSL certificate บนเว็บไซต์และแจ้งเตือนผมเมื่อใกล้หมดอายุ ผมใช้ PowerShell สร้างมันขึ้นมา เนื่องจากเราตรวจสอบ certificate ของเว็บไซต์ผ่าน HttpWeb query เราจึงไม่ต้องการสิทธิ์ administrator บนเว็บไซต์/เซิร์ฟเวอร์ระยะไกล

ในสคริปต์ PowerShell ต่อไปนี้ คุณต้องระบุรายการเว็บไซต์ที่คุณต้องการตรวจสอบวันหมดอายุ certificate และอายุของ certificate ที่จะเริ่มแสดงการแจ้งเตือนให้คุณ ($minCertAge) ผมป้อน 80 วันเป็นตัวอย่าง

$minCertAge = 80
$timeoutMs = 10000
$sites = @(
"https://testsite1.com/",
"https://testsite2.com/",
"https://woshub.com/"
)
# Disable certificate validation
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
foreach ($site in $sites)
{
Write-Host Check $site -f Green
$req = [Net.HttpWebRequest]::Create($site)
$req.Timeout = $timeoutMs
try {$req.GetResponse() |Out-Null} catch {Write-Host URL check error $site`: $_ -f Red}
$expDate = $req.ServicePoint.Certificate.GetExpirationDateString()
$certExpDate = [datetime]::ParseExact($expDate, “dd/MM/yyyy HH:mm:ss”, $null)
[int]$certExpiresIn = ($certExpDate - $(get-date)).Days
$certName = $req.ServicePoint.Certificate.GetName()
$certThumbprint = $req.ServicePoint.Certificate.GetCertHashString()
$certEffectiveDate = $req.ServicePoint.Certificate.GetEffectiveDateString()
$certIssuer = $req.ServicePoint.Certificate.GetIssuerName()
if ($certExpiresIn -gt $minCertAge)
{Write-Host The $site certificate expires in $certExpiresIn days [$certExpDate] -f Green}
else
{
$message= "The $site certificate expires in $certExpiresIn days"
$messagetitle= "Renew certificate"
Write-Host $message [$certExpDate]. Details:`n`nCert name: $certName`Cert thumbprint: $certThumbprint`nCert effective date: $certEffectiveDate`nCert issuer: $certIssuer -f Red
#Displays a pop-up notification and sends an email to the administrator
#ShowNotification $messagetitle $message
# Send-MailMessage -From powershell@woshub.com -To admin@woshub.com -Subject $messagetitle -body $message -SmtpServer gwsmtp.woshub.com -Encoding UTF8
}
write-host "________________" `n
}

สคริปต์ PowerShell นี้จะตรวจสอบ SSL certificates ของเว็บไซต์ทั้งหมดในรายการ หากพบ certificate ที่ใกล้หมดอายุ จะถูกเน้นในการแจ้งเตือน

การตรวจสอบวันหมดอายุ SSL/TLS Certificate ด้วย PowerShell

เพื่อแจ้งเตือนผู้ดูแลระบบว่า SSL certificate ใกล้หมดอายุ คุณสามารถเพิ่ม popup notification ได้ ในการทำเช่นนั้น ให้ uncomment บรรทัดสคริปต์ “ShowNotification $messagetitle $message” และเพิ่มฟังก์ชันต่อไปนี้:

Function ShowNotification ($MsgTitle, $MsgText) {
Add-Type -AssemblyName System.Windows.Forms
$global:balmsg = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balmsg.BalloonTipText = $MsgText
$balmsg.BalloonTipTitle = $MsgTitle
$balmsg.Visible = $true
$balmsg.ShowBalloonTip(10000)
}

คุณยังสามารถส่งการแจ้งเตือนอีเมลโดยใช้ Send-MailMessage

จากนั้น หากพบ certificates ที่หมดอายุหรือใกล้หมดอายุ คุณจะได้รับการแจ้งเตือนทางอีเมลและ popup message

การตรวจสอบวันหมดอายุ SSL/TLS Certificate ด้วย PowerShell

จากนั้นสร้าง automatic task สำหรับ Task Scheduler ให้รันหนึ่งหรือสองครั้งต่อสัปดาห์ และรันสคริปต์ PowerShell เพื่อตรวจสอบวันหมดอายุของ HTTPS website certificates ของคุณ (คุณสามารถสร้าง task ใน Task Scheduler ให้รันไฟล์ PS1 script โดยใช้คอมมานด์เลต Register-ScheduledTask)

วิธีตรวจสอบ Expired Certificates ใน Windows Certificate Store แบบระยะไกล

คุณอาจต้องการสคริปต์ PowerShell ตรวจสอบวันหมดอายุของ certificates ที่ใช้โดย cryptographic services บนโดเมนเซิร์ฟเวอร์ของคุณ (เช่น RDP/RDS, Exchange, SharePoint, LDAPS certificates ฯลฯ) หรือคอมพิวเตอร์ของผู้ใช้

บนคอมพิวเตอร์ท้องถิ่น คุณสามารถดูรายการ certificates โดยใช้คำสั่ง:

Get-ChildItem -Path cert

Powershell 3.0 มี argument พิเศษ -ExpiringInDays:

Get-ChildItem -Path cert: -Recurse -ExpiringInDays 30

ใน PowerShell 2.0 คำสั่งเดียวกันมีลักษณะดังนี้:

Get-ChildItem -Path cert: -Recurse | where { $_.notafter -le (get-date).AddDays(30) -AND $_.notafter -gt (get-date)} | select thumbprint, subject

เพื่อตรวจสอบเฉพาะ certificates ของคุณเอง ให้ใช้ container Cert:\LocalMachine\My แทน Cert: ในโฟลเดอร์ราก ดังนั้นคุณจะไม่ตรวจสอบ Windows trusted root certificates และ commercial certificates

เพื่อหา certificates ที่จะหมดอายุใน 30 วันข้างหน้าบนโดเมนเซิร์ฟเวอร์ทั้งหมด ให้ใช้สคริปต์ PowerShell นี้:

$servers= (Get-ADComputer -LDAPFilter "(&(objectCategory=computer)(operatingSystem=Windows Server*) (!serviceprincipalname=*MSClusterVirtualServer*) (!(userAccountControl:1.2.840.113556.1.4.803:=2)))").Name
$result=@()
foreach ($server in $servers)
{
$ErrorActionPreference="SilentlyContinue"
$getcert=Invoke-Command -ComputerName $server { Get-ChildItem -Path Cert:\LocalMachine\My -Recurse -ExpiringInDays 30}
foreach ($cert in $getcert) {
$result+=New-Object -TypeName PSObject -Property ([ordered]@{
'Server'=$server;
'Certificate'=$cert.Issuer;
'Expires'=$cert.NotAfter
})
}
}
Write-Output $result

การตรวจสอบวันหมดอายุ SSL/TLS Certificate ด้วย PowerShell

คุณจะได้รายการ server certificates ที่ใกล้หมดอายุและคุณจะมีเวลาเพียงพอในการต่ออายุพวกมัน