Skip to content

Commit 785dfc2

Browse files
Merge branch 'main' into fix/adobe-package-placeholder-cves
2 parents 556a1a4 + eab89b7 commit 785dfc2

2 files changed

Lines changed: 224 additions & 0 deletions

File tree

‎server/service/osquery_utils/queries.go‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strconv"
1515
"strings"
1616
"time"
17+
"unicode"
1718

1819
"github.com/fleetdm/fleet/v4/pkg/str"
1920
"github.com/fleetdm/fleet/v4/server/config"
@@ -1760,10 +1761,20 @@ func processProgramFilesScan(mainSoftwareResults, fileScanResults []map[string]s
17601761
`\program files (x86)\`: {},
17611762
}
17621763
knownPaths := make(map[string]struct{})
1764+
// >>> OPENFRAME(program-files-scan-duplicates): also match by folder name — upstream fleetdm/fleet#59098
1765+
// Many programs entries have no install_location, so their executables are also matched by
1766+
// folder name: "C:\Program Files\7-Zip\7z.exe" belongs to "7-Zip 26.06 (x64 edition)".
1767+
knownNames := make(map[string]struct{})
1768+
// <<< OPENFRAME(program-files-scan-duplicates)
17631769
for _, row := range mainSoftwareResults {
17641770
if row["source"] != "programs" {
17651771
continue
17661772
}
1773+
// >>> OPENFRAME(program-files-scan-duplicates)
1774+
if name := programFolderName(row["name"]); name != "" {
1775+
knownNames[name] = struct{}{}
1776+
}
1777+
// <<< OPENFRAME(program-files-scan-duplicates)
17671778
p := normalizeWindowsDir(row["installed_path"])
17681779
if p == "" {
17691780
continue
@@ -1783,6 +1794,12 @@ func processProgramFilesScan(mainSoftwareResults, fileScanResults []map[string]s
17831794
continue
17841795
}
17851796

1797+
// >>> OPENFRAME(program-files-scan-duplicates)
1798+
if skipProgramFilesScanExe(exePath) {
1799+
continue
1800+
}
1801+
// <<< OPENFRAME(program-files-scan-duplicates)
1802+
17861803
// Extract and normalize the directory containing the exe.
17871804
exeDir := normalizeWindowsDir(windowsDirname(exePath))
17881805

@@ -1796,6 +1813,16 @@ func processProgramFilesScan(mainSoftwareResults, fileScanResults []map[string]s
17961813
break
17971814
}
17981815
}
1816+
// >>> OPENFRAME(program-files-scan-duplicates)
1817+
if !duplicate {
1818+
for _, dir := range programFilesScanDirs(exePath) {
1819+
if _, ok := knownNames[programFolderName(dir)]; ok {
1820+
duplicate = true
1821+
break
1822+
}
1823+
}
1824+
}
1825+
// <<< OPENFRAME(program-files-scan-duplicates)
17991826
if duplicate {
18001827
continue
18011828
}
@@ -1862,6 +1889,99 @@ func fileScanVersion(row map[string]string) string {
18621889
return row["file_version"]
18631890
}
18641891

1892+
// >>> OPENFRAME(program-files-scan-duplicates)
1893+
// programFilesWindowsComponentDirs are folders Windows itself installs under Program Files. Their
1894+
// executables are parts of the OS, not applications.
1895+
var programFilesWindowsComponentDirs = map[string]struct{}{
1896+
"internet explorer": {},
1897+
"windows defender": {},
1898+
"windows defender advanced threat protection": {},
1899+
"windows mail": {},
1900+
"windows media player": {},
1901+
"windows multimedia platform": {},
1902+
"windows nt": {},
1903+
"windows photo viewer": {},
1904+
"windows portable devices": {},
1905+
"windows security": {},
1906+
"windows sidebar": {},
1907+
"windowspowershell": {},
1908+
}
1909+
1910+
// programFilesCommonFilesWindowsDirs are the Windows folders under Program Files\Common Files. The rest of
1911+
// Common Files holds third-party components, which are still reported.
1912+
var programFilesCommonFilesWindowsDirs = map[string]struct{}{
1913+
"microsoft shared": {},
1914+
"services": {},
1915+
"speechengines": {},
1916+
"system": {},
1917+
}
1918+
1919+
// skipProgramFilesScanExe reports whether a scanned executable is never an application of its own:
1920+
// uninstallers, and the Windows component folders except MsMpEng.exe, which stands for Windows Defender.
1921+
func skipProgramFilesScanExe(exePath string) bool {
1922+
path := strings.ToLower(exePath)
1923+
filename := path[strings.LastIndex(path, `\`)+1:]
1924+
if strings.HasPrefix(filename, "unins") {
1925+
return true
1926+
}
1927+
if filename == "msmpeng.exe" {
1928+
return false
1929+
}
1930+
dirs := programFilesScanDirs(path)
1931+
if len(dirs) == 0 {
1932+
return false
1933+
}
1934+
if dirs[0] == "common files" {
1935+
if len(dirs) < 2 {
1936+
return false
1937+
}
1938+
_, ok := programFilesCommonFilesWindowsDirs[dirs[1]]
1939+
return ok
1940+
}
1941+
_, ok := programFilesWindowsComponentDirs[dirs[0]]
1942+
return ok
1943+
}
1944+
1945+
// programFilesScanDirs returns the folders between Program Files and the executable, outermost first.
1946+
func programFilesScanDirs(exePath string) []string {
1947+
_, rel, ok := strings.Cut(strings.ToLower(exePath), `\program files\`)
1948+
if !ok {
1949+
return nil
1950+
}
1951+
parts := strings.Split(rel, `\`)
1952+
return parts[:len(parts)-1]
1953+
}
1954+
1955+
// programFolderName reduces a program or folder name to the form both share: lowercase letters and
1956+
// digits, without trailing parenthesized parts or versions ("7-Zip 26.06 (x64 edition)" and "7-Zip"
1957+
// give "7zip"). Only dotted versions and years count as versions, so "Autodesk 3ds Max 2026" keeps
1958+
// "3ds" and "Microsoft 365 Apps" keeps "365".
1959+
func programFolderName(name string) string {
1960+
name = strings.TrimSpace(name)
1961+
for {
1962+
trimmed := strings.TrimSpace(programNameTrailingParenthesized.ReplaceAllString(name, ""))
1963+
trimmed = strings.TrimSpace(programNameTrailingVersion.ReplaceAllString(trimmed, ""))
1964+
if trimmed == name {
1965+
break
1966+
}
1967+
name = trimmed
1968+
}
1969+
var b strings.Builder
1970+
for _, r := range strings.ToLower(name) {
1971+
if unicode.IsLetter(r) || unicode.IsDigit(r) {
1972+
b.WriteRune(r)
1973+
}
1974+
}
1975+
return b.String()
1976+
}
1977+
1978+
var (
1979+
programNameTrailingParenthesized = regexp.MustCompile(`\s*\([^()]*\)$`)
1980+
programNameTrailingVersion = regexp.MustCompile(`(?i)[\s._-]+v?(\d+(\.\d+)+|(19|20)\d{2})$`)
1981+
)
1982+
1983+
// <<< OPENFRAME(program-files-scan-duplicates)
1984+
18651985
// windowsDirname returns the directory portion of a Windows path (everything before the last backslash).
18661986
func windowsDirname(path string) string {
18671987
if idx := strings.LastIndex(path, `\`); idx >= 0 {

‎server/service/osquery_utils/queries_test.go‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3930,6 +3930,110 @@ func TestWindowsProgramFilesScan(t *testing.T) {
39303930
},
39313931
},
39323932
},
3933+
{
3934+
name: "exe matched by folder name when the program has no install_location",
3935+
mainResults: []map[string]string{
3936+
{"name": "7-Zip 26.06 (x64 edition)", "source": "programs", "installed_path": ""},
3937+
{"name": "Node.js", "source": "programs", "installed_path": ""},
3938+
{"name": "Git", "source": "programs", "installed_path": ""},
3939+
},
3940+
fileScanResults: []map[string]string{
3941+
{"path": `C:\Program Files\7-Zip\7z.exe`, "filename": "7z.exe", "product_version": "26.06", "file_version": "26.6.0.0"},
3942+
{"path": `C:\Program Files\7-Zip\7zFM.exe`, "filename": "7zFM.exe", "product_version": "26.06", "file_version": "26.6.0.0"},
3943+
{"path": `C:\Program Files\nodejs\node.exe`, "filename": "node.exe", "product_version": "26.13.0", "file_version": "26.13.0.0"},
3944+
{"path": `C:\Program Files\Git\cmd\git.exe`, "filename": "git.exe", "product_version": "2.43.0", "file_version": "2.43.0"},
3945+
},
3946+
expected: []map[string]string{
3947+
{"name": "7-Zip 26.06 (x64 edition)", "source": "programs", "installed_path": ""},
3948+
{"name": "Node.js", "source": "programs", "installed_path": ""},
3949+
{"name": "Git", "source": "programs", "installed_path": ""},
3950+
},
3951+
},
3952+
{
3953+
name: "folder name must match the whole program name",
3954+
mainResults: []map[string]string{
3955+
{"name": "Adobe Acrobat (64-bit)", "source": "programs", "installed_path": ""},
3956+
},
3957+
fileScanResults: []map[string]string{
3958+
{"path": `C:\Program Files\Adobe\DNG Converter\DNGConverter.exe`, "filename": "DNGConverter.exe", "product_version": "16.1", "file_version": "16.1.0.0"},
3959+
},
3960+
expected: []map[string]string{
3961+
{"name": "Adobe Acrobat (64-bit)", "source": "programs", "installed_path": ""},
3962+
{
3963+
"name": "DNGConverter.exe", "version": "16.1", "source": "programs",
3964+
"vendor": "", "installed_path": `C:\Program Files\Adobe\DNG Converter`,
3965+
"extension_id": "", "extension_for": "", "upgrade_code": "",
3966+
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
3967+
},
3968+
},
3969+
},
3970+
{
3971+
name: "only trailing versions, years and parenthesized parts are trimmed from program names",
3972+
mainResults: []map[string]string{
3973+
{"name": "7-Zip-26.06", "source": "programs", "installed_path": ""},
3974+
{"name": "Autodesk 3ds Max 2026", "source": "programs", "installed_path": ""},
3975+
{"name": "Microsoft 365 Apps for enterprise - en-us", "source": "programs", "installed_path": ""},
3976+
{"name": "Acme (CAD) Suite", "source": "programs", "installed_path": ""},
3977+
},
3978+
fileScanResults: []map[string]string{
3979+
{"path": `C:\Program Files\Acme Suite\viewer.exe`, "filename": "viewer.exe", "product_version": "3.0", "file_version": "3.0.0.0"},
3980+
{"path": `C:\Program Files\7-Zip\7z.exe`, "filename": "7z.exe", "product_version": "26.06", "file_version": "26.6.0.0"},
3981+
{"path": `C:\Program Files\Autodesk\AutoCAD 2026\acad.exe`, "filename": "acad.exe", "product_version": "25.1", "file_version": "25.1.0.0"},
3982+
{"path": `C:\Program Files\Microsoft\Tool\tool.exe`, "filename": "tool.exe", "product_version": "1.0", "file_version": "1.0"},
3983+
},
3984+
expected: []map[string]string{
3985+
{"name": "7-Zip-26.06", "source": "programs", "installed_path": ""},
3986+
{"name": "Autodesk 3ds Max 2026", "source": "programs", "installed_path": ""},
3987+
{"name": "Microsoft 365 Apps for enterprise - en-us", "source": "programs", "installed_path": ""},
3988+
{"name": "Acme (CAD) Suite", "source": "programs", "installed_path": ""},
3989+
{
3990+
"name": "viewer.exe", "version": "3.0", "source": "programs",
3991+
"vendor": "", "installed_path": `C:\Program Files\Acme Suite`,
3992+
"extension_id": "", "extension_for": "", "upgrade_code": "",
3993+
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
3994+
},
3995+
{
3996+
"name": "acad.exe", "version": "25.1", "source": "programs",
3997+
"vendor": "", "installed_path": `C:\Program Files\Autodesk\AutoCAD 2026`,
3998+
"extension_id": "", "extension_for": "", "upgrade_code": "",
3999+
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
4000+
},
4001+
{
4002+
"name": "tool.exe", "version": "1.0", "source": "programs",
4003+
"vendor": "", "installed_path": `C:\Program Files\Microsoft\Tool`,
4004+
"extension_id": "", "extension_for": "", "upgrade_code": "",
4005+
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
4006+
},
4007+
},
4008+
},
4009+
{
4010+
name: "Windows component folders and uninstallers are skipped, MsMpEng.exe is kept",
4011+
mainResults: []map[string]string{},
4012+
fileScanResults: []map[string]string{
4013+
{"path": `C:\Program Files\Windows Defender\MsMpEng.exe`, "filename": "MsMpEng.exe", "product_version": "4.18.25030.2", "file_version": "4.18.25030.2"},
4014+
{"path": `C:\Program Files\Windows Defender\MpCmdRun.exe`, "filename": "MpCmdRun.exe", "product_version": "4.18.25030.2", "file_version": "4.18.25030.2"},
4015+
{"path": `C:\Program Files\Internet Explorer\iexplore.exe`, "filename": "iexplore.exe", "product_version": "11.0.26100.1", "file_version": "11.0.26100.1"},
4016+
{"path": `C:\Program Files\Windows Media Player\wmplayer.exe`, "filename": "wmplayer.exe", "product_version": "12.0.26100.1", "file_version": "12.0.26100.1"},
4017+
{"path": `C:\Program Files\Common Files\microsoft shared\helper.exe`, "filename": "helper.exe", "product_version": "1.0", "file_version": "1.0"},
4018+
{"path": `C:\Program Files\Common Files\Acme\updater.exe`, "filename": "updater.exe", "product_version": "3.1", "file_version": "3.1"},
4019+
{"path": `C:\Program Files\SomeApp\unins000.exe`, "filename": "unins000.exe", "product_version": "1.0", "file_version": "1.0"},
4020+
{"path": `C:\Program Files\SomeApp\Uninstall.exe`, "filename": "Uninstall.exe", "product_version": "1.0", "file_version": "1.0"},
4021+
},
4022+
expected: []map[string]string{
4023+
{
4024+
"name": "MsMpEng.exe", "version": "4.18.25030.2", "source": "programs",
4025+
"vendor": "", "installed_path": `C:\Program Files\Windows Defender`,
4026+
"extension_id": "", "extension_for": "", "upgrade_code": "",
4027+
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
4028+
},
4029+
{
4030+
"name": "updater.exe", "version": "3.1", "source": "programs",
4031+
"vendor": "", "installed_path": `C:\Program Files\Common Files\Acme`,
4032+
"extension_id": "", "extension_for": "", "upgrade_code": "",
4033+
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
4034+
},
4035+
},
4036+
},
39334037
{
39344038
name: "non-programs entries in main do not affect dedup",
39354039
mainResults: []map[string]string{

0 commit comments

Comments
 (0)