自建dns獲取域名解析與證書告警

新鈦雲服已累計為您分享813篇技術乾貨
背 景
公司A擁有一套雲上DNS服務,主要用於支援雲中應用和服務的域名解析。為了滿足線下門店之間的服務互聯需求,公司在內網自建一套Windows DNS伺服器,以實現門店之間的高效域名解析。此方案旨在保證內部網路的穩定性與安全性,同時與雲上DNS服務進行有效整合,需要監控證書有效性和域名解析的記錄查詢。
目 標
  1. 自建DNS服務:在不登入window dns伺服器的情況下,透過日誌系統進行查詢解析記錄。
  2. HTTPS安全通訊:為所有域名服務列印域名證書的時間資訊。
  3. 告警系統:監控域名證書狀態,並在出現問題時及時告警。
業務流程
  1. 域名解析測試
    1. 在內部網路的客戶端上測試域名解析,確保所有服務都能正確解析。
    2. 驗證HTTPS證書連線,確保瀏覽器或客戶端能夠安全訪問服務。
  2. 證書和域名監控
    1. 監控證書的有效期,設定告警閾值(如到期前60天)。
    2. 監控HTTPS服務的可用性,確保服務在DNS解析和證書有效的情況下正常執行。
  3. 告警機制
    1. 如果證書即將到期或已過期,發出告警,確保運維人員能夠及時更新證書。
    2. 實現自動化指令碼,當證書即將到期時,生產新的證書進替換

伺服器安裝openssl服務

  1. openssl軟體下載地址
    https://slproweb.com/download/Win64OpenSSL-3_3_2.msi
  2. 軟體下載後直接點選下一步進行安裝,直到安裝完成.
  3. openssl環境變數配置
使用命令查詢openssl  -version版本資訊

指令碼查詢域名的A記錄、CNAME記錄、域名證書資訊

1.手動查詢自建window dns的A記錄和CNAME記錄命令資訊
Get-DnsServerResourceRecord -ZoneName "text" | Where-Object { $_.RecordType -eq"CNAME" -or $_.RecordType -eq"A"} | Format-Table -AutoSize
2.在powershell上查詢單個域名的證書資訊
$domain = "text"cmd /c "echo | openssl s_client -servername `"$($domain)`" -connect `"$($domain):443`"2>&1| openssl x509 -noout -dates | findstr /C:notAfter"
3.透過powershell指令碼獲取域名詳細資訊
$zoneName = "text"# domaininput.txt需要排除的域名列表資訊$inputFilePath = "D:\domain\domaininput.txt"# domainoutput.txt需要輸出日誌記錄的域名列表資訊$outputFilePath = "D:\domain\domainoutput.txt"# 飛書webhook$feishuWebhookUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxx"# 替換為你的飛書 Webhook URLfunction Send-FeishuNotification {param ( [string]$message, [string]$color )if ($color-eq"blue") {$formattedMessage = "*Success:* $message" } elseif ($color-eq"black") {$formattedMessage = "*Error:* $message" } else {$formattedMessage = $message }$payload = @{ msg_type = "text" content = @{ text = $formattedMessage } }$jsonPayload = $payload | ConvertTo-Json -Depth 1Invoke-RestMethod -Uri $feishuWebhookUrl -Method Post -Body $jsonPayload -ContentType 'application/json'}function Get-CertificateEndDate {param ( [string]$domain )Write-Host"正在處理域名: $domain"try {# 獲取證書到期資訊$certInfo = cmd /c "echo | openssl s_client -servername `"$($domain)`" -connect `"$($domain):443`" 2>&1 | openssl x509 -noout -dates | findstr /C:notAfter"Write-Host"獲取的證書資訊: $certInfo"if ($certInfo-match"notAfter=(.*)") {$formattedDate = $matches[1].Trim() # 獲取到期時間並去掉空格Write-Host"證書到期時間: $formattedDate"# 修剪多餘空格$formattedDate = $formattedDate-replace'\s+', ' '# 解析日期時,指定格式並轉換為 yyyy-MM-dd$parsedDate = [datetime]::ParseExact($formattedDate, "MMM d HH:mm:ss yyyy 'GMT'", [System.Globalization.CultureInfo]::InvariantCulture)$endDate = $parsedDate.ToString("yyyy-MM-dd")Write-Host"格式化後的日期: $endDate"return$endDate } else {Write-Host"未找到證書資訊: $certInfo"return"2008-08-08"# 預設日期 } } catch {Write-Host"發生錯誤: $_"return"2008-08-08"# 預設日期 }}try {if (-Not (Test-Path$inputFilePath)) {$errorMessage = "輸入檔案不存在:$inputFilePath"$failureMessage = "failed $zoneName$errorMessage" Send-FeishuNotification -message $failureMessage -color "black"exit }$excludedDomains = Get-Content -Path $inputFilePath | Where-Object { $_.Trim() -ne"" }$dnsRecords = Get-DnsServerResourceRecord -ZoneName $zoneName |Where-Object { $_.RecordType -eq"CNAME" -or $_.RecordType -eq"A" }$output = @{}$ignoredDomains = @()foreach ($recordin$dnsRecords) {$hostName = $record.HostNameif ($hostName.EndsWith($zoneName, [System.StringComparison]::OrdinalIgnoreCase)) {$fullDomain = $hostName } else {$fullDomain = "$hostName.$zoneName" }# 修正重複的域名問題$fullDomain = $fullDomain-replace'\.text\.text\.cn$', '.text.cn'if ($excludedDomains-notcontains$fullDomain) {$endDate = Get-CertificateEndDate -domain $fullDomain$key = "$fullDomain|$($record.RecordType)"if (-not$output.ContainsKey($key)) {$output[$key] = @{ RecordType = $record.RecordType domain = $fullDomain EndDate = $endDate CimInstanceProperties = $record.RecordData } } } else {$ignoredDomains += $fullDomain } }$outputArray = $output.Values$outputLines = $outputArray | ForEach-Object {$_ | ConvertTo-Json -Depth 1 -Compress }$outputLines | Set-Content -Path $outputFilePath -Encoding utf8$lineCount = $outputLines.Count$uniqueIgnoredDomains = $ignoredDomains | Select-Object -Unique$ignoredJson = @{}foreach ($domainin$uniqueIgnoredDomains) {$ignoredJson["domain_$($ignoredJson.Count + 1)"] = $domain }$ignoredJsonString = $ignoredJson | ConvertTo-Json -Depth 10$successMessage = "domain:$zoneName path:$outputFilePath row:$lineCount ignore: `n$ignoredJsonString" Send-FeishuNotification -message $successMessage -color "blue"} catch {$errorMessage = $_.Exception.Message$failureMessage = "failed $zoneName$errorMessage" Send-FeishuNotification -message $failureMessage -color "black"throw}
#domainoutput.txt檔案內容輸出JSON LOG資訊{"CimInstanceProperties":{"CimClass":"root/Microsoft/Windows/DNS:DnsServerResourceRecordA","CimInstanceProperties":"IPv4Address = \"20.20.20.20\"","CimSystemProperties":"Microsoft.Management.Infrastructure.CimSystemProperties"},"RecordType":"A","domain":"view.lu.cn","EndDate":"2008-08-08"}{"CimInstanceProperties":{"CimClass":"root/Microsoft/Windows/DNS:DnsServerResourceRecordCName","CimInstanceProperties\"":"HostNameAlias = \"alb-cs.cn","CimSystemProperties":"Microsoft.Management.Infrastructure.CimSystemProperties"},"RecordType":"CNAME","domain":"pa.lu.cn","EndDate":"2025-05-31"}
使用json-hand外掛進行格式化輸出展示
4.飛書輸出資訊提示,成功的域名地址、路徑、域名多少數量、排除的域名是那幾個
配置日誌接入與查詢SQL
1.配置阿里雲sls接入window dns單層json日誌資訊
window dns檔案的路徑地址為D:\domain\domainoutput.txt
2.透過sls查詢sql輸出window dns日誌資訊
* | SELECT "content.domain", "content.RecordType", "content.CimInstanceProperties.CimInstanceProperties" , "content.EndDate"from log LIMIT 1000
配置告警輸出
根據查詢的sls sql語句進行針對性的配置告警和行動策略通知聚道(釘釘、簡訊、電話等等)
在本文中,我們詳細探討了如何透過自建DNS實現域名解析與證書告警的管理。透過建立Purview門戶、配置金鑰保管庫、實施流量回放和監控,我們不僅提高了資產的可見性,還確保了資料的安全性和合規性。隨著企業數字化轉型的不斷深入,資產梳理和安全運營將變得愈發重要。我們鼓勵大家定期進行資產審查和監控,以應對不斷變化的安全威脅和業務需求。感謝您對安全運營工作的關注與支援,讓我們共同努力,提升企業的安全防護水平,確保業務的持續穩定執行。
如有相關問題,請在文章後面給小編留言,小編安排作者第一時間和您聯絡,為您答疑解惑。


相關文章