Skip to content

fix(scanner/windows): allow only cab file scan for offline scan #2236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 42 additions & 33 deletions scanner/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,47 +1155,56 @@ func (w *windows) scanKBs() (*models.WindowsKB, error) {
}
}

var searcher string
switch c := w.getServerInfo().Windows; c.ServerSelection {
case 3: // https://learn.microsoft.com/en-us/windows/win32/wua_sdk/using-wua-to-scan-for-updates-offline
searcher = fmt.Sprintf(`$UpdateSession = (New-Object -ComObject Microsoft.Update.Session); $UpdateServiceManager = (New-Object -ComObject Microsoft.Update.ServiceManager); $UpdateService = $UpdateServiceManager.AddScanPackageService("Offline Sync Service", "%s"); $UpdateSearcher = $UpdateSession.CreateUpdateSearcher(); $UpdateSearcher.ServerSelection = %d; $UpdateSearcher.ServiceID = $UpdateService.ServiceID;`, c.CabPath, c.ServerSelection)
default:
searcher = fmt.Sprintf("$UpdateSession = (New-Object -ComObject Microsoft.Update.Session); $UpdateSearcher = $UpdateSession.CreateUpdateSearcher(); $UpdateSearcher.ServerSelection = %d;", c.ServerSelection)
}
if r := w.exec(w.translateCmd(fmt.Sprintf(`%s $UpdateSearcher.search("IsInstalled = 1 and RebootRequired = 0 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs`, searcher)), noSudo); r.isSuccess() {
kbs, err := w.parseWindowsUpdaterSearch(r.Stdout)
if err != nil {
return nil, xerrors.Errorf("Failed to parse Windows Update Search. err: %w", err)
if err := func() error {
var searcher string
switch c := w.getServerInfo(); c.Windows.ServerSelection {
case 3: // https://learn.microsoft.com/en-us/windows/win32/wua_sdk/using-wua-to-scan-for-updates-offline
searcher = fmt.Sprintf(`$UpdateSession = (New-Object -ComObject Microsoft.Update.Session); $UpdateServiceManager = (New-Object -ComObject Microsoft.Update.ServiceManager); $UpdateService = $UpdateServiceManager.AddScanPackageService("Offline Sync Service", "%s"); $UpdateSearcher = $UpdateSession.CreateUpdateSearcher(); $UpdateSearcher.ServerSelection = %d; $UpdateSearcher.ServiceID = $UpdateService.ServiceID;`, c.Windows.CabPath, c.Windows.ServerSelection)
default:
if c.Mode.IsOffline() {
return nil
}
searcher = fmt.Sprintf("$UpdateSession = (New-Object -ComObject Microsoft.Update.Session); $UpdateSearcher = $UpdateSession.CreateUpdateSearcher(); $UpdateSearcher.ServerSelection = %d;", c.Windows.ServerSelection)
}
for _, kb := range kbs {
applied[kb] = struct{}{}
if r := w.exec(w.translateCmd(fmt.Sprintf(`%s $UpdateSearcher.search("IsInstalled = 1 and RebootRequired = 0 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs`, searcher)), noSudo); r.isSuccess() {
kbs, err := w.parseWindowsUpdaterSearch(r.Stdout)
if err != nil {
return xerrors.Errorf("Failed to parse Windows Update Search. err: %w", err)
}
for _, kb := range kbs {
applied[kb] = struct{}{}
}
}
}

if r := w.exec(w.translateCmd(fmt.Sprintf(`%s $UpdateSearcher.search("IsInstalled = 0 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs`, searcher)), noSudo); r.isSuccess() {
kbs, err := w.parseWindowsUpdaterSearch(r.Stdout)
if err != nil {
return nil, xerrors.Errorf("Failed to parse Windows Update Search. err: %w", err)
}
for _, kb := range kbs {
unapplied[kb] = struct{}{}
if r := w.exec(w.translateCmd(fmt.Sprintf(`%s $UpdateSearcher.search("IsInstalled = 0 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs`, searcher)), noSudo); r.isSuccess() {
kbs, err := w.parseWindowsUpdaterSearch(r.Stdout)
if err != nil {
return xerrors.Errorf("Failed to parse Windows Update Search. err: %w", err)
}
for _, kb := range kbs {
unapplied[kb] = struct{}{}
}
}
}

if r := w.exec(w.translateCmd(fmt.Sprintf(`%s $UpdateSearcher.search("IsInstalled = 1 and RebootRequired = 1 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs`, searcher)), noSudo); r.isSuccess() {
kbs, err := w.parseWindowsUpdaterSearch(r.Stdout)
if err != nil {
return nil, xerrors.Errorf("Failed to parse Windows Update Search. err: %w", err)
}
for _, kb := range kbs {
unapplied[kb] = struct{}{}
if r := w.exec(w.translateCmd(fmt.Sprintf(`%s $UpdateSearcher.search("IsInstalled = 1 and RebootRequired = 1 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs`, searcher)), noSudo); r.isSuccess() {
kbs, err := w.parseWindowsUpdaterSearch(r.Stdout)
if err != nil {
return xerrors.Errorf("Failed to parse Windows Update Search. err: %w", err)
}
for _, kb := range kbs {
unapplied[kb] = struct{}{}
}
}
}

if w.getServerInfo().Windows.ServerSelection == 3 {
if r := w.exec(w.translateCmd(`$UpdateServiceManager = (New-Object -ComObject Microsoft.Update.ServiceManager); $UpdateServiceManager.Services | Where-Object {$_.Name -eq "Offline Sync Service"} | ForEach-Object { $UpdateServiceManager.RemoveService($_.ServiceID) };`), noSudo); !r.isSuccess() {
return nil, xerrors.Errorf("Failed to remove Windows Update Offline Sync Service: %v", r)
if w.getServerInfo().Windows.ServerSelection == 3 {
if r := w.exec(w.translateCmd(`$UpdateServiceManager = (New-Object -ComObject Microsoft.Update.ServiceManager); $UpdateServiceManager.Services | Where-Object {$_.Name -eq "Offline Sync Service"} | ForEach-Object { $UpdateServiceManager.RemoveService($_.ServiceID) };`), noSudo); !r.isSuccess() {
return xerrors.Errorf("Failed to remove Windows Update Offline Sync Service: %v", r)
}
}

return nil
}(); err != nil {
return nil, xerrors.Errorf("Failed to check Windows Update Serach. err: %w", err)
}

if r := w.exec(w.translateCmd("$UpdateSearcher = (New-Object -ComObject Microsoft.Update.Session).CreateUpdateSearcher(); $HistoryCount = $UpdateSearcher.GetTotalHistoryCount(); $UpdateSearcher.QueryHistory(0, $HistoryCount) | Sort-Object -Property Date | Format-List -Property Title, Operation, ResultCode"), noSudo); r.isSuccess() {
Expand Down
Loading