网站首页 > 技术教程 正文
背 景
公司A拥有一套云上DNS服务,主要用于支持云中应用和服务的域名解析。为了满足线下门店之间的服务互联需求,公司在内网自建一套Windows DNS服务器,以实现门店之间的高效域名解析。此方案旨在保证内部网络的稳定性与安全性,同时与云上DNS服务进行有效集成,需要监控证书有效性和域名解析的记录查询。
目 标
- 自建DNS服务:在不登录window dns服务器的情况下,通过日志系统进行查询解析记录。
- HTTPS安全通信:为所有域名服务打印域名证书的时间信息。
- 告警系统:监控域名证书状态,并在出现问题时及时告警。
业务流程
- 域名解析测试
- 在内部网络的客户端上测试域名解析,确保所有服务都能正确解析。
- 验证HTTPS证书连接,确保浏览器或客户端能够安全访问服务。
- 证书和域名监控
- 监控证书的有效期,设置告警阈值(如到期前60天)。
- 监控HTTPS服务的可用性,确保服务在DNS解析和证书有效的情况下正常运行。
- 告警机制
- 如果证书即将到期或已过期,发出告警,确保运维人员能够及时更新证书。
- 实现自动化脚本,当证书即将到期时,生产新的证书进替换
服务器安装openssl服务
- openssl软件下载地址https://slproweb.com/download/Win64OpenSSL-3_3_2.msi
- 软件下载后直接点击下一步进行安装,直到安装完成.
- 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 -AutoSize2.在powershell上查询单个域名的证书信息
$domain = "zi.lu.cn" 
cmd /c "echo | openssl s_client -servername `"$($domain)`" -connect `"$($domain):443`" 2>&1 | openssl x509 -noout -dates | findstr /C:notAfter"3.通过powershell脚本获取域名详细信息
$zoneName = "lu.cn"
# 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 URL
function 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 1
    Invoke-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 ($record in $dnsRecords) {
        $hostName = $record.HostName
        if ($hostName.EndsWith($zoneName, [System.StringComparison]::OrdinalIgnoreCase)) {
            $fullDomain = $hostName
        } else {
            $fullDomain = "$hostName.$zoneName"
        }
        # 修正重复的域名问题
        $fullDomain = $fullDomain -replace '\.lu\.cn\.lu\.cn#39;, '.lu.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 ($domain in $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门户、配置密钥保管库、实施流量回放和监控,我们不仅提高了资产的可见性,还确保了数据的安全性和合规性。随着企业数字化转型的不断深入,资产梳理和安全运营将变得愈发重要。我们鼓励大家定期进行资产审查和监控,以应对不断变化的安全威胁和业务需求。感谢您对安全运营工作的关注与支持,让我们共同努力,提升企业的安全防护水平,确保业务的持续稳定运行。
如有相关问题,请在文章后面给小编留言,小编安排作者第一时间和您联系,为您答疑解惑。
- 上一篇: 内网DNS域名解析(dns域名解析ip)
- 下一篇: 好莱坞明星都痴迷的SUP是神马,国内哪里可以玩?
猜你喜欢
- 2024-11-07 内网DNS域名解析(dns域名解析ip)
- 2024-11-07 DNS域名详细解析过程(最全面,看这一篇就够)
- 2024-11-07 DNS解析是什么?DNS解析在网络通信中作用有哪些?
- 2024-11-07 什么是DNS解析?如何提升DNS解析安全?
- 2024-11-07 DNS分层结构及DNS解析流程(dns按分层管理)
- 2024-11-07 一文读懂DNS解析原理和流程(中科三方)
- 2024-11-07 图解DNS Tunneling 工作原理及防护
- 2024-11-07 DNSPOD动态域名解析教程和软件(自定义ddns动态域名解析)
- 2024-11-07 面试官:如何使用dig/nslookup命令查看dns解析?
- 2024-11-07 Windows设置本地DNS域名解析Hosts文件的步骤和方法
欢迎 你 发表评论:
- 10-23Excel计算工龄和年份之差_excel算工龄的公式year
- 10-23Excel YEARFRAC函数:时间的"年份比例尺"详解
- 10-23最常用的10个Excel函数,中文解读,动图演示,易学易用
- 10-23EXCEL中如何计算截止到今日(两个时间中)的时间
- 10-2390%人不知道的Excel神技:DATEDIF 精准计算年龄,告别手动算错!
- 10-23计算工龄及工龄工资(90%的人搞错了):DATE、DATEDIF组合应用
- 10-23Excel中如何计算工作日天数?用这两个函数轻松计算,附新年日历
- 10-23怎样快速提取单元格中的出生日期?用「Ctrl+E」批量搞定
- 最近发表
- 
- Excel计算工龄和年份之差_excel算工龄的公式year
- Excel YEARFRAC函数:时间的"年份比例尺"详解
- 最常用的10个Excel函数,中文解读,动图演示,易学易用
- EXCEL中如何计算截止到今日(两个时间中)的时间
- 90%人不知道的Excel神技:DATEDIF 精准计算年龄,告别手动算错!
- 计算工龄及工龄工资(90%的人搞错了):DATE、DATEDIF组合应用
- Excel中如何计算工作日天数?用这两个函数轻松计算,附新年日历
- 怎样快速提取单元格中的出生日期?用「Ctrl+E」批量搞定
- Excel日期函数之DATEDIF函数_excel函数datedif在哪里
- Excel函数-DATEDIF求司龄_exceldatedif函数计算年龄
 
- 标签列表
- 
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
 

本文暂时没有评论,来添加一个吧(●'◡'●)