Skip to content

Commit c7cdd69

Browse files
fix(software): report the running Windows Defender platform version CU-17tkuw67fq5
Defender platform updates install the running MsMpEng.exe under C:\ProgramData\Microsoft\Windows Defender\Platform\<version>\, while C:\Program Files\Windows Defender\MsMpEng.exe keeps the in-box version. Scan the platform folder too and keep only the newest MsMpEng.exe, so Windows Defender no longer matches CVEs the running platform has fixed (e.g. CVE-2023-36422). Upstream: fleetdm#58895, fleetdm#58896
1 parent 05b9368 commit c7cdd69

3 files changed

Lines changed: 107 additions & 7 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixed Windows Defender showing the in-box version from `C:\Program Files\Windows Defender` instead of the running platform version, which caused false positive CVEs such as CVE-2023-36422.

‎server/service/osquery_utils/queries.go‎

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/fleetdm/fleet/v4/server/ptr"
3030
"github.com/fleetdm/fleet/v4/server/service/async"
31+
"github.com/fleetdm/fleet/v4/server/vulnerabilities/nvd/tools/cvefeed/nvd"
3132
"github.com/google/uuid"
3233
"github.com/spf13/cast"
3334
)
@@ -1702,7 +1703,9 @@ var SoftwareOverrideQueries = map[string]DetailQuery{
17021703
// (Vendor\app.exe) and depth 1 (Vendor\Subfolder\app.exe), excluding WindowsApps (already
17031704
// covered by the programs table). Deduplication against programs entries is handled server-side
17041705
// via SoftwareProcessResults. Also enables detection of Windows Defender (MsMpEng.exe) which
1705-
// does not create registry entries (#42878).
1706+
// does not create registry entries (#42878). Defender platform updates install the running
1707+
// MsMpEng.exe under C:\ProgramData\Microsoft\Windows Defender\Platform\<version>, so that
1708+
// folder is scanned too and processProgramFilesScan keeps only the newest MsMpEng.exe.
17061709
"windows_program_files_scan": {
17071710
Description: "A software override query[^1] to detect Windows software installed to Program Files without registry entries.",
17081711
Platforms: []string{"windows"},
@@ -1716,6 +1719,7 @@ FROM file
17161719
WHERE (
17171720
path LIKE 'C:\Program Files\%\%.exe'
17181721
OR path LIKE 'C:\Program Files\%\%\%.exe'
1722+
OR path LIKE 'C:\ProgramData\Microsoft\Windows Defender\Platform\%\MsMpEng.exe'
17191723
)
17201724
AND path NOT LIKE 'C:\Program Files\WindowsApps\%'`,
17211725
SoftwareProcessResults: processProgramFilesScan,
@@ -1739,6 +1743,8 @@ func processProgramFilesScan(mainSoftwareResults, fileScanResults []map[string]s
17391743
return mainSoftwareResults
17401744
}
17411745

1746+
fileScanResults = keepNewestWindowsDefender(fileScanResults)
1747+
17421748
// Build a set of normalized installed paths from existing programs entries.
17431749
// normalizeWindowsDir ensures consistent trailing backslash and lowercase.
17441750
//
@@ -1794,14 +1800,9 @@ func processProgramFilesScan(mainSoftwareResults, fileScanResults []map[string]s
17941800
continue
17951801
}
17961802

1797-
version := row["product_version"]
1798-
if version == "" {
1799-
version = row["file_version"]
1800-
}
1801-
18021803
mainSoftwareResults = append(mainSoftwareResults, map[string]string{
18031804
"name": row["filename"],
1804-
"version": version,
1805+
"version": fileScanVersion(row),
18051806
"source": "programs",
18061807
"vendor": "",
18071808
"installed_path": windowsDirname(exePath),
@@ -1818,6 +1819,49 @@ func processProgramFilesScan(mainSoftwareResults, fileScanResults []map[string]s
18181819
return mainSoftwareResults
18191820
}
18201821

1822+
// keepNewestWindowsDefender drops every MsMpEng.exe row except the one with the highest version.
1823+
//
1824+
// C:\Program Files\Windows Defender\MsMpEng.exe is the in-box copy that ships with Windows and is
1825+
// not updated by Defender platform updates. The running copy lives under
1826+
// C:\ProgramData\Microsoft\Windows Defender\Platform\<version>\, and older platform folders can
1827+
// remain after an update. Reporting the in-box version makes Windows Defender look outdated and
1828+
// matches CVEs that the running platform has already fixed (e.g. CVE-2023-36422), so only the
1829+
// newest copy is kept. When no platform folder exists, the in-box copy is still reported.
1830+
func keepNewestWindowsDefender(fileScanResults []map[string]string) []map[string]string {
1831+
newest := -1
1832+
count := 0
1833+
for i, row := range fileScanResults {
1834+
if !strings.EqualFold(row["filename"], "MsMpEng.exe") {
1835+
continue
1836+
}
1837+
count++
1838+
if newest == -1 || nvd.SmartVerCmp(fileScanVersion(row), fileScanVersion(fileScanResults[newest])) > 0 {
1839+
newest = i
1840+
}
1841+
}
1842+
if count <= 1 {
1843+
return fileScanResults
1844+
}
1845+
1846+
filtered := make([]map[string]string, 0, len(fileScanResults)-count+1)
1847+
for i, row := range fileScanResults {
1848+
if i != newest && strings.EqualFold(row["filename"], "MsMpEng.exe") {
1849+
continue
1850+
}
1851+
filtered = append(filtered, row)
1852+
}
1853+
return filtered
1854+
}
1855+
1856+
// fileScanVersion returns the version of a windows_program_files_scan row: product_version,
1857+
// falling back to file_version.
1858+
func fileScanVersion(row map[string]string) string {
1859+
if v := row["product_version"]; v != "" {
1860+
return v
1861+
}
1862+
return row["file_version"]
1863+
}
1864+
18211865
// windowsDirname returns the directory portion of a Windows path (everything before the last backslash).
18221866
func windowsDirname(path string) string {
18231867
if idx := strings.LastIndex(path, `\`); idx >= 0 {

‎server/service/osquery_utils/queries_test.go‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,6 +3773,61 @@ func TestWindowsProgramFilesScan(t *testing.T) {
37733773
},
37743774
},
37753775
},
3776+
{
3777+
name: "Windows Defender reports the newest platform MsMpEng.exe, not the in-box copy",
3778+
mainResults: []map[string]string{},
3779+
fileScanResults: []map[string]string{
3780+
{"path": `C:\Program Files\Windows Defender\MsMpEng.exe`, "filename": "MsMpEng.exe", "product_version": "4.18.1909.6", "file_version": "4.18.1909.6"},
3781+
{"path": `C:\ProgramData\Microsoft\Windows Defender\Platform\4.18.26090.3-0\MsMpEng.exe`, "filename": "MsMpEng.exe", "product_version": "4.18.26090.3", "file_version": "4.18.26090.3"},
3782+
{"path": `C:\ProgramData\Microsoft\Windows Defender\Platform\4.18.26100.4-0\MsMpEng.exe`, "filename": "MsMpEng.exe", "product_version": "4.18.26100.4", "file_version": "4.18.26100.4"},
3783+
{"path": `C:\Program Files\SomeApp\app.exe`, "filename": "app.exe", "product_version": "1.0", "file_version": "1.0.0.0"},
3784+
},
3785+
expected: []map[string]string{
3786+
{
3787+
"name": "MsMpEng.exe", "version": "4.18.26100.4", "source": "programs",
3788+
"vendor": "", "installed_path": `C:\ProgramData\Microsoft\Windows Defender\Platform\4.18.26100.4-0`,
3789+
"extension_id": "", "extension_for": "", "upgrade_code": "",
3790+
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
3791+
},
3792+
{
3793+
"name": "app.exe", "version": "1.0", "source": "programs",
3794+
"vendor": "", "installed_path": `C:\Program Files\SomeApp`,
3795+
"extension_id": "", "extension_for": "", "upgrade_code": "",
3796+
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
3797+
},
3798+
},
3799+
},
3800+
{
3801+
name: "Windows Defender keeps the in-box MsMpEng.exe when no platform folder exists",
3802+
mainResults: []map[string]string{},
3803+
fileScanResults: []map[string]string{
3804+
{"path": `C:\Program Files\Windows Defender\MsMpEng.exe`, "filename": "MsMpEng.exe", "product_version": "4.18.1909.6", "file_version": "4.18.1909.6"},
3805+
},
3806+
expected: []map[string]string{
3807+
{
3808+
"name": "MsMpEng.exe", "version": "4.18.1909.6", "source": "programs",
3809+
"vendor": "", "installed_path": `C:\Program Files\Windows Defender`,
3810+
"extension_id": "", "extension_for": "", "upgrade_code": "",
3811+
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
3812+
},
3813+
},
3814+
},
3815+
{
3816+
name: "Windows Defender version comparison uses file_version when product_version is empty",
3817+
mainResults: []map[string]string{},
3818+
fileScanResults: []map[string]string{
3819+
{"path": `C:\ProgramData\Microsoft\Windows Defender\Platform\4.18.26100.4-0\msmpeng.exe`, "filename": "msmpeng.exe", "product_version": "", "file_version": "4.18.26100.4"},
3820+
{"path": `C:\Program Files\Windows Defender\MsMpEng.exe`, "filename": "MsMpEng.exe", "product_version": "4.18.1909.6", "file_version": "4.18.1909.6"},
3821+
},
3822+
expected: []map[string]string{
3823+
{
3824+
"name": "msmpeng.exe", "version": "4.18.26100.4", "source": "programs",
3825+
"vendor": "", "installed_path": `C:\ProgramData\Microsoft\Windows Defender\Platform\4.18.26100.4-0`,
3826+
"extension_id": "", "extension_for": "", "upgrade_code": "",
3827+
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
3828+
},
3829+
},
3830+
},
37763831
{
37773832
name: "version falls back to file_version when product_version is empty",
37783833
mainResults: []map[string]string{},

0 commit comments

Comments
 (0)