Skip to content

Commit f6a4d42

Browse files
authored
✨ Add InstallLocation to files for Windows packages (#5770)
``` 6: { purl: "pkg:windows/windows/Microsoft%20Edge@138.0.3351.65?arch=AMD64" files: [ 0: pkgFileInfo path="C:\\Program Files (x86)\\Microsoft\\Edge\\Application" ] } ``` Signed-off-by: Christian Zunker <christian@mondoo.com>
1 parent 8f45890 commit f6a4d42

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

providers/os/resources/packages/testdata/windows_2019.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Packages
2-
[commands."powershell -c \"Get-AppxPackage -AllUsers | Select Name, PackageFullName, Architecture, Version, Publisher | ConvertTo-Json\""]
2+
[commands."powershell -c \"Get-AppxPackage -AllUsers | Select Name, PackageFullName, Architecture, Version, Publisher, InstallLocation | ConvertTo-Json\""]
33
stdout="""
44
[
55
{
@@ -197,6 +197,14 @@ stdout="""
197197
"Architecture": 11,
198198
"Version": "6.2.1.0",
199199
"Publisher": "CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US"
200+
},
201+
{
202+
"Name": "Microsoft.MicrosoftEdge.Stable",
203+
"PackageFullName": "Microsoft.MicrosoftEdge.Stable_112.0.1722.39_neutral__8wekyb3d8bbwe",
204+
"Architecture": 11,
205+
"Version": "112.0.1722.39",
206+
"InstallLocation": "C:\\\\Program Files\\\\WindowsApps\\\\Microsoft.MicrosoftEdge.Stable_112.0.1722.39_neutral__8wekyb3d8bbwe",
207+
"Publisher": "CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US"
200208
}
201209
]
202210
"""

providers/os/resources/packages/windows_packages.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,21 @@ Get-ItemProperty (@(
101101
'HKLM:\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*',
102102
'HKCU:\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*'
103103
) | Where-Object { Test-Path $_ }) |
104-
Select-Object -Property DisplayName,DisplayVersion,Publisher,EstimatedSize,InstallSource,UninstallString | ConvertTo-Json -Compress
104+
Select-Object -Property DisplayName,DisplayVersion,Publisher,EstimatedSize,InstallSource,UninstallString,InstallLocation | ConvertTo-Json -Compress
105105
`
106106

107107
var (
108108
WINDOWS_QUERY_HOTFIXES = `Get-HotFix | Select-Object -Property Status, Description, HotFixId, Caption, InstalledOn, InstalledBy | ConvertTo-Json`
109-
WINDOWS_QUERY_APPX_PACKAGES = `Get-AppxPackage -AllUsers | Select Name, PackageFullName, Architecture, Version, Publisher | ConvertTo-Json`
109+
WINDOWS_QUERY_APPX_PACKAGES = `Get-AppxPackage -AllUsers | Select Name, PackageFullName, Architecture, Version, Publisher, InstallLocation | ConvertTo-Json`
110110
)
111111

112112
type winAppxPackages struct {
113-
Name string `json:"Name"`
114-
FullName string `json:"PackageFullName"`
115-
Architecture int `json:"Architecture"`
116-
Version string `json:"Version"`
117-
Publisher string `json:"Publisher"`
113+
Name string `json:"Name"`
114+
FullName string `json:"PackageFullName"`
115+
Architecture int `json:"Architecture"`
116+
Version string `json:"Version"`
117+
Publisher string `json:"Publisher"`
118+
InstallLocation string `json:"InstallLocation"`
118119
// can directly set it to the architecture string, the pwsh script returns it as int (Architecture)
119120
arch string `json:"-"`
120121
}
@@ -137,6 +138,14 @@ func (p winAppxPackages) toPackage(platform *inventory.Platform) Package {
137138
Vendor: p.Publisher,
138139
PUrl: purl.NewPackageURL(platform, purl.TypeAppx, p.Name, p.Version).String(),
139140
}
141+
if p.InstallLocation != "" {
142+
pkg.Files = []FileRecord{
143+
{
144+
Path: p.InstallLocation,
145+
},
146+
}
147+
pkg.FilesAvailable = PkgFilesIncluded
148+
}
140149

141150
if p.Version != "" {
142151
cpeWfns, err := cpe.NewPackage2Cpe(p.Publisher, p.Name, p.Version, "", "")
@@ -452,6 +461,7 @@ func getPackageFromRegistryKeyItems(children []registry.RegistryKeyItem, platfor
452461
var displayName string
453462
var displayVersion string
454463
var publisher string
464+
var installLocation string
455465

456466
for _, i := range children {
457467
switch i.Key {
@@ -463,6 +473,8 @@ func getPackageFromRegistryKeyItems(children []registry.RegistryKeyItem, platfor
463473
displayVersion = i.Value.String
464474
case "Publisher":
465475
publisher = i.Value.String
476+
case "InstallLocation":
477+
installLocation = i.Value.String
466478
}
467479
}
468480

@@ -488,6 +500,14 @@ func getPackageFromRegistryKeyItems(children []registry.RegistryKeyItem, platfor
488500
platform, purl.TypeWindows, displayName, displayVersion,
489501
).String(),
490502
}
503+
if installLocation != "" {
504+
pkg.Files = []FileRecord{
505+
{
506+
Path: installLocation,
507+
},
508+
}
509+
pkg.FilesAvailable = PkgFilesIncluded
510+
}
491511

492512
if displayVersion != "" {
493513
cpeWfns, err := cpe.NewPackage2Cpe(publisher, displayName, displayVersion, "", "")
@@ -561,6 +581,7 @@ func ParseWindowsAppPackages(platform *inventory.Platform, input io.Reader) ([]P
561581
InstallSource string `json:"InstallSource"`
562582
EstimatedSize int `json:"EstimatedSize"`
563583
UninstallString string `json:"UninstallString"`
584+
InstallLocation string `json:"InstallLocation"`
564585
}
565586

566587
var entries []powershellUninstallEntry
@@ -594,7 +615,7 @@ func ParseWindowsAppPackages(platform *inventory.Platform, input io.Reader) ([]P
594615
} else {
595616
log.Debug().Msg("ignored package since information is missing")
596617
}
597-
pkgs = append(pkgs, Package{
618+
pkg := Package{
598619
Name: entry.DisplayName,
599620
Version: entry.DisplayVersion,
600621
Format: "windows/app",
@@ -604,7 +625,15 @@ func ParseWindowsAppPackages(platform *inventory.Platform, input io.Reader) ([]P
604625
PUrl: purl.NewPackageURL(
605626
platform, purl.TypeWindows, entry.DisplayName, entry.DisplayVersion,
606627
).String(),
607-
})
628+
}
629+
if entry.InstallLocation != "" {
630+
pkg.Files = []FileRecord{
631+
{
632+
Path: entry.InstallLocation,
633+
},
634+
}
635+
}
636+
pkgs = append(pkgs, pkg)
608637
}
609638

610639
return pkgs, nil

providers/os/resources/packages/windows_packages_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func TestWindowsAppxPackagesParser(t *testing.T) {
7676
}
7777

7878
pkgs, err := ParseWindowsAppxPackages(pf, c.Stdout)
79-
assert.Nil(t, err)
80-
assert.Equal(t, 28, len(pkgs), "detected the right amount of packages")
79+
require.NoError(t, err)
80+
require.Equal(t, 29, len(pkgs), "detected the right amount of packages")
8181

8282
p := findPkg(pkgs, "Microsoft.Windows.Cortana")
8383
assert.Equal(t, Package{
@@ -94,6 +94,27 @@ func TestWindowsAppxPackagesParser(t *testing.T) {
9494
Vendor: "CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US",
9595
}, p)
9696

97+
p = findPkg(pkgs, "Microsoft.MicrosoftEdge.Stable")
98+
assert.Equal(t, Package{
99+
Name: "Microsoft.MicrosoftEdge.Stable",
100+
Version: "112.0.1722.39",
101+
Arch: "neutral",
102+
Format: "windows/appx",
103+
PUrl: "pkg:appx/windows/Microsoft.MicrosoftEdge.Stable@112.0.1722.39?arch=x86",
104+
// TODO: this is a bug in the CPE generation, we need to extract the publisher from the package
105+
CPEs: []string{
106+
"cpe:2.3:a:cn\\=microsoft_corporation\\,_o\\=microsoft_corporation\\,_l\\=redmond\\,_s\\=washington\\,_c\\=us:microsoft.microsoftedge.stable:112.0.1722.39:*:*:*:*:*:*:*",
107+
"cpe:2.3:a:cn\\=microsoft_corporation\\,_o\\=microsoft_corporation\\,_l\\=redmond\\,_s\\=washington\\,_c\\=us:microsoft.microsoftedge.stable:112.0.1722:*:*:*:*:*:*:*",
108+
},
109+
Vendor: "CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US",
110+
Files: []FileRecord{
111+
{
112+
Path: "C:\\Program Files\\WindowsApps\\Microsoft.MicrosoftEdge.Stable_112.0.1722.39_neutral__8wekyb3d8bbwe",
113+
},
114+
},
115+
FilesAvailable: PkgFilesIncluded,
116+
}, p)
117+
97118
// check empty return
98119
pkgs, err = ParseWindowsAppxPackages(pf, strings.NewReader(""))
99120
assert.Nil(t, err)

0 commit comments

Comments
 (0)