-
Notifications
You must be signed in to change notification settings - Fork 571
/
Copy pathmissingkbs.ps1
385 lines (335 loc) · 11.8 KB
/
missingkbs.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<#
.SYNOPSIS
Compiles a list of missing KBs on the current system.
.DESCRIPTION
These missing KBs are determined based either on the online Microsoft Update service or WSUS if configured, or on an offline scanfile (wsusscn2.cab). This scanfile is either provided in the command line or downloaded from the Microsoft Update site. By default, the online Microsoft Update service is used (or WSUS if configured).
.PARAMETER Offline
Perform an offline scan using a scanfile.
.PARAMETER ScanFile
Specify path to the scanfile (wsusscn2.cab). Implies -Offline and -Preserve.
.PARAMETER Preserve
Preserve the scanfile.
.PARAMETER OutputFile
Specify file path to store the results in. By default, the file missing.txt in the current directory will be used.
.PARAMETER Download
Just download the scanfile (don't check for missing KBs). By default, the file will be downloaded to the current directory.
.EXAMPLE
missingkbs.ps1
Determine missing KBs using the online Microsoft Update service (or WSUS if configured)
.EXAMPLE
missingkbs.ps1 -Offline -Preserve
Determine missing KBs downloading the wsusscn2.cab scanfile and preserving it
.EXAMPLE
missingkbs.ps1 -Offline -ScanFile E:\tmp\wsusscn2.cab
Determine missing KBs using the offline wsusscn2.cab scanfile
.EXAMPLE
missingkbs.ps1 -Offline -OutputFile E:\tmp\out.txt
Determine missing KBs downloading the wsusscn2.cab scanfile saving results in out.txt
.EXAMPLE
missingkbs.ps1 -DownloadOnly E:\tmp
Download the scanfile to E:\tmp\
.LINK
https://github.com/bitsadmin/wesng/
https://blog.bitsadmin.com/windows-security-updates-for-hackers
.NOTES
On Windows, it may be required to enable this Activate.ps1 script by setting the execution policy for the user. You can do this by issuing the following PowerShell command:
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
For more information on Execution Policies:
https://go.microsoft.com/fwlink/?LinkID=135170
#>
[CmdletBinding(DefaultParameterSetName = 'Online')]
param(
[Parameter(ParameterSetName = 'Online')]
[switch]$Online,
[Parameter(ParameterSetName = 'Offline')]
[switch]$Offline,
[Parameter(ParameterSetName = 'DownloadOnly')]
[switch]$DownloadOnly,
[Parameter(Mandatory = $false, ParameterSetName = 'Offline')]
[ValidateScript({
if(-not (Test-Path $_ -PathType Leaf))
{
throw 'File does not exist'
}
return $true
})]
[string]$ScanFile,
[Parameter(Mandatory = $false, ParameterSetName = 'Offline')]
[switch]$Preserve,
[Parameter(Mandatory = $false, ParameterSetName = 'Online')]
[Parameter(Mandatory = $false, ParameterSetName = 'Offline')]
[ValidateScript({
$path = Split-Path $_
if(-not (Test-Path $path -PathType Container))
{
throw 'Path does not exist'
}
return $true
})]
[string]$OutputFile = (Join-Path -Path (Get-Location).Path -ChildPath 'missing.txt'),
[Parameter(ParameterSetName = 'DownloadOnly')]
[ValidateScript({
if(-not (Test-Path $_ -PathType Container))
{
throw 'Path does not exist'
}
return $true
})]
[string]$TargetPath = (Get-Location).Path
)
<#
This software is provided under the BSD 3-Clause License.
See the accompanying LICENSE file for more information.
Windows Exploit Suggester - Next Generation
Missing KBs Identifier utility -
Author: Arris Huijgen (@bitsadmin)
Website: https://github.com/bitsadmin
#>
# Application information
$version = 1.0
$appname = "Windows Exploit Suggester: Missing KBs Identifier v$($version.ToString("0.0"))"
$url = 'https://github.com/bitsadmin/wesng/'
$banner = "$appname`n$url`n"
# Show banner
Write-Host $banner
# Online or offline scan?
# Defaults to Online
if($ScanFile)
{
$Offline = $true
}
elseif(-not $Online)
{
if(-not $Offline -and -not $DownloadOnly)
{
$Online = $true
}
}
$specifiedScanfile = $Offline -or $ScanFile
$foundScanfile = $false
$runMode = if($Online){ 'Online' } elseif($Offline){ 'Offline' } elseif($DownloadOnly){ 'DownloadOnly' } else { 'Unknown' }
Write-Host "[I] Running in $runMode mode"
# Only check and download scanfile in case of offline scan
if ($Offline -or $DownloadOnly)
{
# Only download the scanfile
if ($DownloadOnly)
{
# Compile path for scanfile
$scanFilePath = Join-Path -Path $TargetPath -ChildPath 'wsusscn2.cab'
}
# Download and perform scan
else
{
# If scanfile -ScanFile parameter is not specified, check if it already exists
# In case it doesn't exist, download it
if ($ScanFile)
{
$scanFilePath = Resolve-Path $ScanFile
$Preserve = $true
}
else
{
# Set target location to current directory or temp, depending on whether scanfile needs to be preserved
if ($Preserve)
{
$targetDirectory = Get-Location
}
else
{
$targetDirectory = [System.IO.Path]::GetTempPath()
}
# Compile path for scanfile
$scanFilePath = Join-Path -Path $targetDirectory -ChildPath 'wsusscn2.cab'
}
# Check if scanfile exists
if (Test-Path -Path $scanFilePath -PathType Leaf)
{
$objScanFile = Get-Item -Path $scanFilePath
Write-Host "[+] Using scanfile '$scanFilePath' with modification date $($objScanFile.LastWriteTime.ToShortDateString())"
$foundScanfile = $true
}
elseif ($ScanFile)
{
Write-Host "[-] Scanfile '$scanFilePath' does not exist" -ForegroundColor Red
exit
}
}
# Only download if file doesn't exist yet
if (-not (Test-Path -Path $scanFilePath -PathType Leaf))
{
Write-Host '[+] Downloading wsusscn2.cab (+/- 600MB), depending on your Internet speed this may take a while'
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest 'http://download.windowsupdate.com/microsoftupdate/v6/wsusscan/wsusscn2.cab' -OutFile $scanFilePath -UseBasicParsing
$ProgressPreference = 'Continue'
Write-Host "[+] Scanfile saved to '$scanFilePath'"
}
# Scanfile already exists
elseif (-not $foundScanfile)
{
$objScanFile = Get-Item -Path $scanFilePath
Write-Host "[+] File wsusscn2.cab already exists: '$scanFilePath'. Skipping download"
Write-Host "[I] Scanfile modification date: $($objScanFile.LastWriteTime.ToShortDateString())"
$foundScanfile = $true
$Preserve = $true
$scanFileAge = (Get-Date) - $objScanFile.LastWriteTime
if ($scanFileAge.Days -gt 31)
{
Write-Host '[!] Scanfile is more than a month old, consider downloading the latest version for more accurate results' -ForegroundColor Yellow
}
}
}
# Display Windows Update/WSUS settings
else
{
# UseWUServer
$dwUseWUServer = (Get-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU' -Name 'UseWUServer' -ErrorAction SilentlyContinue).UseWUServer
if(-not $dwUseWUServer)
{
$dwUseWUServer = 0
}
# WUServer
if ($dwUseWUServer -eq 0)
{
Write-Host '[I] Windows Update online is used'
}
else
{
$strWSUS = (Get-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate' -Name 'WUServer' -ErrorAction SilentlyContinue).WUServer
if($strWSUS)
{
Write-Host "[I] WSUS with URL '$strWSUS' is used"
}
else
{
Write-Host '[I] WSUS is used, but the WSUS URL could not be read' -ForegroundColor Yellow
}
Write-Host ' Usage of WSUS may cause the missing KB information to be incomplete'
Write-Host ' To use Windows Update ''s online KB information, use the -Offline parameter'
}
}
# Skip checking the current system for missing KBs if the -DownloadOnly parameter is provided
if ($DownloadOnly)
{
Write-Host '[+] Done!'
exit
}
# Validate whether Windows Update (wuauserv) service is not disabled
$wuauserv = Get-CimInstance -ClassName Win32_Service -Filter "Name='wuauserv'"
if ($wuauserv.StartMode -eq 'Disabled')
{
Write-Host "[-] The 'Windows Update' service is disabled" -ForegroundColor Red
exit
}
# Initialize Windows Update and identify missing KBs
Write-Host '[+] Identifying missing KBs...'
try
{
$UpdateSession = New-Object -ComObject 'Microsoft.Update.Session'
}
catch
{
Write-Host "[-] Error initializing Microsoft.Update.Session object: 0x$($Error[0].Exception.HResult.ToString("X"))" -ForegroundColor Red
if ($Error[0].Exception.HResult -eq 0x80040154)
{
Write-Host ' Windows Update Client API missing. Please install the Windows Update Agent' -ForegroundColor Yellow
}
exit
}
$UpdateServiceManager = New-Object -ComObject 'Microsoft.Update.ServiceManager'
# Only use the scanfile in case of an offline scan
if ($Offline)
{
try
{
$UpdateService = $UpdateServiceManager.AddScanPackageService('Offline Sync Service', $scanFilePath, 1)
}
catch
{
Write-Host "[-] Error initializing Windows Update service: 0x$($Error[0].Exception.HResult.ToString("X"))" -ForegroundColor Red
if ($Error[0].Exception.HResult -eq 0x80070005)
{
Write-Host ' Make sure to run this script as an elevated Administrator when running in Offline mode' -ForegroundColor Yellow
}
exit
}
}
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
# In case of online scan the ServerSelection and ServiceID don't need to be set
if ($Offline)
{
$UpdateSearcher.ServerSelection = 3 # ssOthers
$UpdateSearcher.ServiceID = $UpdateService.ServiceID
}
# Perform search for updates
try
{
$SearchResult = $UpdateSearcher.Search('IsInstalled=0')
}
catch
{
Write-Host "[-] Error searching for updates: 0x$($Error[0].Exception.HResult.ToString("X"))" -ForegroundColor Red
if ($Error[0].Exception.HResult -eq 0x8024402C)
{
Write-Host ' Make sure your computer is connected to the Internet' -ForegroundColor Yellow
}
exit
}
$updateCount = $SearchResult.Updates.Count
# List updates
if ($updateCount -eq 0)
{
if ($specifiedScanfile)
{
Write-Host '[+] Based on the provided scanfile no missing KBs were found'
}
else
{
if ($foundScanfile)
{
Write-Host '[+] Based on the scanfile no missing KBs were found'
}
else
{
Write-Host '[+] There are no missing KBs'
}
}
exit
}
# Collect missing updates and show them on the screen
Write-Host '[+] List of missing KBs'
$missingUpdates = @()
for ($i = 0; $i -lt $updateCount; $i++)
{
$update = $SearchResult.Updates.Item($i)
for ($j = 0; $j -lt $update.KBArticleIDs.Count; $j++)
{
$articleId = $update.KBArticleIDs.Item($j)
$missingUpdates += "KB$articleId"
Write-Host "- KB$($articleId): $($update.Title)"
}
}
# Store list of missing KBs
$missingUpdates | Out-File -FilePath $OutputFile -Encoding ASCII
if($?)
{
Write-Host "[+] Saved list of missing updates in '$OutputFile'"
}
else
{
Write-Host '[-] Error storing list of missing updates'
}
# Cleanup scanfile
if ($Offline)
{
if (-not $Preserve)
{
Write-Host '[+] Cleaning up wsusscn2.cab'
Remove-Item -Path $scanFilePath
}
else
{
Write-Host '[+] Skipping cleanup of the scanfile'
}
}
Write-Host '[+] Done!'