diff --git a/server/service/osquery_utils/queries.go b/server/service/osquery_utils/queries.go index 6ad31722ea6..76e47e52614 100644 --- a/server/service/osquery_utils/queries.go +++ b/server/service/osquery_utils/queries.go @@ -14,6 +14,7 @@ import ( "strconv" "strings" "time" + "unicode" "github.com/fleetdm/fleet/v4/pkg/str" "github.com/fleetdm/fleet/v4/server/config" @@ -1760,10 +1761,20 @@ func processProgramFilesScan(mainSoftwareResults, fileScanResults []map[string]s `\program files (x86)\`: {}, } knownPaths := make(map[string]struct{}) + // >>> OPENFRAME(program-files-scan-duplicates): also match by folder name — upstream fleetdm/fleet#59098 + // Many programs entries have no install_location, so their executables are also matched by + // folder name: "C:\Program Files\7-Zip\7z.exe" belongs to "7-Zip 26.06 (x64 edition)". + knownNames := make(map[string]struct{}) + // <<< OPENFRAME(program-files-scan-duplicates) for _, row := range mainSoftwareResults { if row["source"] != "programs" { continue } + // >>> OPENFRAME(program-files-scan-duplicates) + if name := programFolderName(row["name"]); name != "" { + knownNames[name] = struct{}{} + } + // <<< OPENFRAME(program-files-scan-duplicates) p := normalizeWindowsDir(row["installed_path"]) if p == "" { continue @@ -1783,6 +1794,12 @@ func processProgramFilesScan(mainSoftwareResults, fileScanResults []map[string]s continue } + // >>> OPENFRAME(program-files-scan-duplicates) + if skipProgramFilesScanExe(exePath) { + continue + } + // <<< OPENFRAME(program-files-scan-duplicates) + // Extract and normalize the directory containing the exe. exeDir := normalizeWindowsDir(windowsDirname(exePath)) @@ -1796,6 +1813,16 @@ func processProgramFilesScan(mainSoftwareResults, fileScanResults []map[string]s break } } + // >>> OPENFRAME(program-files-scan-duplicates) + if !duplicate { + for _, dir := range programFilesScanDirs(exePath) { + if _, ok := knownNames[programFolderName(dir)]; ok { + duplicate = true + break + } + } + } + // <<< OPENFRAME(program-files-scan-duplicates) if duplicate { continue } @@ -1862,6 +1889,99 @@ func fileScanVersion(row map[string]string) string { return row["file_version"] } +// >>> OPENFRAME(program-files-scan-duplicates) +// programFilesWindowsComponentDirs are folders Windows itself installs under Program Files. Their +// executables are parts of the OS, not applications. +var programFilesWindowsComponentDirs = map[string]struct{}{ + "internet explorer": {}, + "windows defender": {}, + "windows defender advanced threat protection": {}, + "windows mail": {}, + "windows media player": {}, + "windows multimedia platform": {}, + "windows nt": {}, + "windows photo viewer": {}, + "windows portable devices": {}, + "windows security": {}, + "windows sidebar": {}, + "windowspowershell": {}, +} + +// programFilesCommonFilesWindowsDirs are the Windows folders under Program Files\Common Files. The rest of +// Common Files holds third-party components, which are still reported. +var programFilesCommonFilesWindowsDirs = map[string]struct{}{ + "microsoft shared": {}, + "services": {}, + "speechengines": {}, + "system": {}, +} + +// skipProgramFilesScanExe reports whether a scanned executable is never an application of its own: +// uninstallers, and the Windows component folders except MsMpEng.exe, which stands for Windows Defender. +func skipProgramFilesScanExe(exePath string) bool { + path := strings.ToLower(exePath) + filename := path[strings.LastIndex(path, `\`)+1:] + if strings.HasPrefix(filename, "unins") { + return true + } + if filename == "msmpeng.exe" { + return false + } + dirs := programFilesScanDirs(path) + if len(dirs) == 0 { + return false + } + if dirs[0] == "common files" { + if len(dirs) < 2 { + return false + } + _, ok := programFilesCommonFilesWindowsDirs[dirs[1]] + return ok + } + _, ok := programFilesWindowsComponentDirs[dirs[0]] + return ok +} + +// programFilesScanDirs returns the folders between Program Files and the executable, outermost first. +func programFilesScanDirs(exePath string) []string { + _, rel, ok := strings.Cut(strings.ToLower(exePath), `\program files\`) + if !ok { + return nil + } + parts := strings.Split(rel, `\`) + return parts[:len(parts)-1] +} + +// programFolderName reduces a program or folder name to the form both share: lowercase letters and +// digits, without trailing parenthesized parts or versions ("7-Zip 26.06 (x64 edition)" and "7-Zip" +// give "7zip"). Only dotted versions and years count as versions, so "Autodesk 3ds Max 2026" keeps +// "3ds" and "Microsoft 365 Apps" keeps "365". +func programFolderName(name string) string { + name = strings.TrimSpace(name) + for { + trimmed := strings.TrimSpace(programNameTrailingParenthesized.ReplaceAllString(name, "")) + trimmed = strings.TrimSpace(programNameTrailingVersion.ReplaceAllString(trimmed, "")) + if trimmed == name { + break + } + name = trimmed + } + var b strings.Builder + for _, r := range strings.ToLower(name) { + if unicode.IsLetter(r) || unicode.IsDigit(r) { + b.WriteRune(r) + } + } + return b.String() +} + +var ( + programNameTrailingParenthesized = regexp.MustCompile(`\s*\([^()]*\)$`) + programNameTrailingVersion = regexp.MustCompile(`(?i)[\s._-]+v?(\d+(\.\d+)+|(19|20)\d{2})$`) +) + +// <<< OPENFRAME(program-files-scan-duplicates) + // windowsDirname returns the directory portion of a Windows path (everything before the last backslash). func windowsDirname(path string) string { if idx := strings.LastIndex(path, `\`); idx >= 0 { diff --git a/server/service/osquery_utils/queries_test.go b/server/service/osquery_utils/queries_test.go index 4dc9745869b..8aaead05a1a 100644 --- a/server/service/osquery_utils/queries_test.go +++ b/server/service/osquery_utils/queries_test.go @@ -3930,6 +3930,110 @@ func TestWindowsProgramFilesScan(t *testing.T) { }, }, }, + { + name: "exe matched by folder name when the program has no install_location", + mainResults: []map[string]string{ + {"name": "7-Zip 26.06 (x64 edition)", "source": "programs", "installed_path": ""}, + {"name": "Node.js", "source": "programs", "installed_path": ""}, + {"name": "Git", "source": "programs", "installed_path": ""}, + }, + fileScanResults: []map[string]string{ + {"path": `C:\Program Files\7-Zip\7z.exe`, "filename": "7z.exe", "product_version": "26.06", "file_version": "26.6.0.0"}, + {"path": `C:\Program Files\7-Zip\7zFM.exe`, "filename": "7zFM.exe", "product_version": "26.06", "file_version": "26.6.0.0"}, + {"path": `C:\Program Files\nodejs\node.exe`, "filename": "node.exe", "product_version": "26.13.0", "file_version": "26.13.0.0"}, + {"path": `C:\Program Files\Git\cmd\git.exe`, "filename": "git.exe", "product_version": "2.43.0", "file_version": "2.43.0"}, + }, + expected: []map[string]string{ + {"name": "7-Zip 26.06 (x64 edition)", "source": "programs", "installed_path": ""}, + {"name": "Node.js", "source": "programs", "installed_path": ""}, + {"name": "Git", "source": "programs", "installed_path": ""}, + }, + }, + { + name: "folder name must match the whole program name", + mainResults: []map[string]string{ + {"name": "Adobe Acrobat (64-bit)", "source": "programs", "installed_path": ""}, + }, + fileScanResults: []map[string]string{ + {"path": `C:\Program Files\Adobe\DNG Converter\DNGConverter.exe`, "filename": "DNGConverter.exe", "product_version": "16.1", "file_version": "16.1.0.0"}, + }, + expected: []map[string]string{ + {"name": "Adobe Acrobat (64-bit)", "source": "programs", "installed_path": ""}, + { + "name": "DNGConverter.exe", "version": "16.1", "source": "programs", + "vendor": "", "installed_path": `C:\Program Files\Adobe\DNG Converter`, + "extension_id": "", "extension_for": "", "upgrade_code": "", + "release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "", + }, + }, + }, + { + name: "only trailing versions, years and parenthesized parts are trimmed from program names", + mainResults: []map[string]string{ + {"name": "7-Zip-26.06", "source": "programs", "installed_path": ""}, + {"name": "Autodesk 3ds Max 2026", "source": "programs", "installed_path": ""}, + {"name": "Microsoft 365 Apps for enterprise - en-us", "source": "programs", "installed_path": ""}, + {"name": "Acme (CAD) Suite", "source": "programs", "installed_path": ""}, + }, + fileScanResults: []map[string]string{ + {"path": `C:\Program Files\Acme Suite\viewer.exe`, "filename": "viewer.exe", "product_version": "3.0", "file_version": "3.0.0.0"}, + {"path": `C:\Program Files\7-Zip\7z.exe`, "filename": "7z.exe", "product_version": "26.06", "file_version": "26.6.0.0"}, + {"path": `C:\Program Files\Autodesk\AutoCAD 2026\acad.exe`, "filename": "acad.exe", "product_version": "25.1", "file_version": "25.1.0.0"}, + {"path": `C:\Program Files\Microsoft\Tool\tool.exe`, "filename": "tool.exe", "product_version": "1.0", "file_version": "1.0"}, + }, + expected: []map[string]string{ + {"name": "7-Zip-26.06", "source": "programs", "installed_path": ""}, + {"name": "Autodesk 3ds Max 2026", "source": "programs", "installed_path": ""}, + {"name": "Microsoft 365 Apps for enterprise - en-us", "source": "programs", "installed_path": ""}, + {"name": "Acme (CAD) Suite", "source": "programs", "installed_path": ""}, + { + "name": "viewer.exe", "version": "3.0", "source": "programs", + "vendor": "", "installed_path": `C:\Program Files\Acme Suite`, + "extension_id": "", "extension_for": "", "upgrade_code": "", + "release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "", + }, + { + "name": "acad.exe", "version": "25.1", "source": "programs", + "vendor": "", "installed_path": `C:\Program Files\Autodesk\AutoCAD 2026`, + "extension_id": "", "extension_for": "", "upgrade_code": "", + "release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "", + }, + { + "name": "tool.exe", "version": "1.0", "source": "programs", + "vendor": "", "installed_path": `C:\Program Files\Microsoft\Tool`, + "extension_id": "", "extension_for": "", "upgrade_code": "", + "release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "", + }, + }, + }, + { + name: "Windows component folders and uninstallers are skipped, MsMpEng.exe is kept", + mainResults: []map[string]string{}, + fileScanResults: []map[string]string{ + {"path": `C:\Program Files\Windows Defender\MsMpEng.exe`, "filename": "MsMpEng.exe", "product_version": "4.18.25030.2", "file_version": "4.18.25030.2"}, + {"path": `C:\Program Files\Windows Defender\MpCmdRun.exe`, "filename": "MpCmdRun.exe", "product_version": "4.18.25030.2", "file_version": "4.18.25030.2"}, + {"path": `C:\Program Files\Internet Explorer\iexplore.exe`, "filename": "iexplore.exe", "product_version": "11.0.26100.1", "file_version": "11.0.26100.1"}, + {"path": `C:\Program Files\Windows Media Player\wmplayer.exe`, "filename": "wmplayer.exe", "product_version": "12.0.26100.1", "file_version": "12.0.26100.1"}, + {"path": `C:\Program Files\Common Files\microsoft shared\helper.exe`, "filename": "helper.exe", "product_version": "1.0", "file_version": "1.0"}, + {"path": `C:\Program Files\Common Files\Acme\updater.exe`, "filename": "updater.exe", "product_version": "3.1", "file_version": "3.1"}, + {"path": `C:\Program Files\SomeApp\unins000.exe`, "filename": "unins000.exe", "product_version": "1.0", "file_version": "1.0"}, + {"path": `C:\Program Files\SomeApp\Uninstall.exe`, "filename": "Uninstall.exe", "product_version": "1.0", "file_version": "1.0"}, + }, + expected: []map[string]string{ + { + "name": "MsMpEng.exe", "version": "4.18.25030.2", "source": "programs", + "vendor": "", "installed_path": `C:\Program Files\Windows Defender`, + "extension_id": "", "extension_for": "", "upgrade_code": "", + "release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "", + }, + { + "name": "updater.exe", "version": "3.1", "source": "programs", + "vendor": "", "installed_path": `C:\Program Files\Common Files\Acme`, + "extension_id": "", "extension_for": "", "upgrade_code": "", + "release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "", + }, + }, + }, { name: "non-programs entries in main do not affect dedup", mainResults: []map[string]string{