离线场景下扫描与修复系统缺失补丁解决方案
前言
在系统运维中,手动打补丁的低效与复杂性(如漏洞对应多补丁、补丁覆盖关系混乱)一直是痛点。笔者因亲身经历逐枚手打补丁的繁琐,深知其对运维效率与系统安全的负面影响。为帮助读者快速定位缺失补丁、简化补丁安装流程,特撰此文,分享高效补丁管理的方法与工具,助力突破手动操作瓶颈,提升系统安全维护的效率。
如何快速定位系统缺少哪些补丁
- 在离线模式下(Offline),系统可使用Windows Update Agent(WUA),通过保存到本地的Wsusscn2.cab文件对系统所需的补丁进行扫描
关于Wsusscn2.cab文件:
Wsusscn2.cab文件是由Microsoft签名的cabinet文件。此文件包含有关Microsoft发布的安全相关更新的信息。可以扫描未连接到Internet的计算机,以查看是否存在或需要这些与安全相关的更新。Wsusscn2.cab文件不包含安全更新本身,因此您必须通过其他方式获取和安装任何所需的安全相关更新。随着Windows Update站点上发布、删除或修订与安全相关的更新,会定期发布Wsusscn2.cab文件的新版本。
关于微软对Wsusscn2.cab文件更多的描述及其下载链接:
Using WUA to Scan for Updates Offline - Win32 apps | Microsoft Learn
使用.cab文件进行系统所需补丁扫描
- 微软官方给出了两种脚本:VBS、PS1
个人推荐使用对老旧系统的兼容性更高的VBS脚本:
Set UpdateSession = CreateObject("Microsoft.Update.Session")
Set UpdateServiceManager = CreateObject("Microsoft.Update.ServiceManager")
Set UpdateService = UpdateServiceManager.AddScanPackageService("Offline Sync Service", "c:\wsusscn2.cab")
Set UpdateSearcher = UpdateSession.CreateUpdateSearcher()
WScript.Echo "Searching for updates..." & vbCRLF
UpdateSearcher.ServerSelection = 3 ' ssOthers
UpdateSearcher.ServiceID = UpdateService.ServiceID
Set SearchResult = UpdateSearcher.Search("IsInstalled=0")
Set Updates = SearchResult.Updates
If searchResult.Updates.Count = 0 ThenWScript.Echo "There are no applicable updates."WScript.Quit
End If
WScript.Echo "List of applicable items on the machine when using wssuscan.cab:" & vbCRLF
For I = 0 to searchResult.Updates.Count-1Set update = searchResult.Updates.Item(I)WScript.Echo I + 1 & "> " & update.Title
Next
WScript.Quit
同时附上PS1脚本:
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateServiceManager = New-Object -ComObject Microsoft.Update.ServiceManager
$UpdateService = $UpdateServiceManager.AddScanPackageService("Offline Sync Service", "c:\wsusscn2.cab")
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
Write-Host "Searching for updates..."
$UpdateSearcher.ServerSelection = 3 # ssOthers
$UpdateSearcher.ServiceID = [string] $UpdateService.ServiceID
$SearchResult = $UpdateSearcher.Search("IsInstalled=0")
$Updates = $SearchResult.Updates
If ($SearchResult.Updates.Count -eq 0) {Write-Host "There are no applicable updates."Exit
}
Write-Host "List of applicable items on the machine when using wssuscan.cab:"
For ($i = 0; $i -lt $SearchResult.Updates.Count; $i++) {$update = $SearchResult.Updates.Item($i)Write-Host ($i + 1) "> " $update.Title
}
关于开源项目kbupdate对离线场景下对更新系统补丁的帮助
Github项目链接:
GitHub - potatoqualitee/kbupdate: 🛡 KB Viewer, Saver, Installer and Uninstaller
- 项目克隆
git clone https://github.com/potatoqualitee/kbupdate.git
在本地导入项目模块
在以管理员身份运行的Powershell中执行以下命令
- 在线场景(Online)
Install-Module kbupdate
- 离线场景(Offline)
Save-Module kbupdate -Path C:\项目文件夹路径\dir\
将Wsusscn2.cab文件与该项目结合使用
- 在操作机中以管理员身份运行Powershell
#使用本地保存的cab文件获取server01上所需的所有补丁
$scanfile = Save-KbScanFile -Path \\server01\c$\temp
Get-KbNeededUpdate -ComputerName server01 -ScanFilePath $scanfile
#获取server01上所需的补丁,然后选择要安装的补丁并安装
Get-KbNeededUpdate -ComputerName server01 | Out-GridView -Passthru | Install-KbUpdate
- 从可联网的操作机中下载需要的补丁
#下载所需补丁到本地目录下
Get-KBNeededUpdate -ComputerName server01 | Save-KbUpdate -Path C:\temp
#下载所需补丁到远程目录下
Get-KBNeededUpdate -ComputerName server01 | Save-KbUpdate -Path '\\server01\c$\temp'
若需要更新补丁的服务器无法或不便开通WinRM服务
- 通过wsusscn2.cab离线文件扫描到的补丁号进行手动记录
- 制备为记事本文件以便记录和使用
在可连接互联网的操作机中开始匹配和下载补丁
Get-KbUpdate -Name KB2966826, KB2894856, KB2973114, KB2972213, KB2894852, KB2966828, KB2968296, KB2972103, KB3023266, KB3019978, KB3045999, KB3045685, KB3037576, KB3023219, KB3032663, KB3059317, KB3061512, KB3072307, KB3046017, KB3071756, KB3082089, KB3084135, KB3074228, KB3074545, KB3086255, KB3042058, KB3097992, KB3110329, KB3133043, KB3127222, KB3126434, KB3126587, KB3122651, KB3139914, KB3156059, KB3159398, KB3161949, KB3178539, KB4014987, KB4019111, KB4041092, KB4041085, KB4099639, KB4340006, KB4339284, KB4345681, KB4462901, KB4468323, KB4471983, KB4487123, KB4486459, KB4490128, KB4498963, KB4501226, KB4507413, KB4514599, KB4534978, KB4556405, KB4566468, KB4580469, KB5012326, KB5013839, KB5017220, KB5021081, KB5022785, KB5027533, KB5029568, KB5030184, KB5030175, KB5030329 -Latest -Simple -Architecture x64 | Out-GridView -Passthru | Save-KbUpdate -Path C:\Users\cy0hp\Desktop\NewPatch3
- 该命令中的-Latest参数会自动查找覆盖旧补丁的最新补丁
- 需要注意的是,下方的补丁列表窗口并不能选择下载哪些补丁,仅用于展示作用,默认会将列表中的所有补丁下载到本地
- 将补丁文件逐个导入服务器后,进行图形化逐一安装即可,过程中提示补丁安装完成需要重启,可以先将补丁全部安装完成后再进行重启