Skip to content
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

⭐️ Add support for static analysis of Windows systems #4203

Merged
merged 3 commits into from
Jun 6, 2024

Conversation

preslavgerchev
Copy link
Contributor

This PR adds support for identifying Windows disks, mounted on a given FS as assets.

  • Move everything registry related under providers/os/registry. This allows both the connections and the OS MQL resources to use the same code.
  • Cleanup providers/os/resources, the MQL resources now only reference providers/os/registry and use that.
  • Add support for calling RegLoadKeyW and UnLoadRegKeyW syscalls that help us mount static registry files as subkeys under HKLM in the registry.
  • Add detection of version, build, platform, and hostname for Windows filesystems

Copy link
Contributor

github-actions bot commented Jun 6, 2024

Test Results

3 054 tests  +7   3 053 ✅ +7   1m 29s ⏱️ +10s
  355 suites +1       1 💤 ±0 
   26 files   ±0       0 ❌ ±0 

Results for commit edcfb47. ± Comparison against base commit 324535f.

This pull request removes 3 and adds 10 tests. Note that renamed tests count towards both.
go.mondoo.com/cnquery/v11/providers/os/resources/windows ‑ TestWindowsRegistryKeyChildParser
go.mondoo.com/cnquery/v11/providers/os/resources/windows ‑ TestWindowsRegistryKeyItemParser
go.mondoo.com/cnquery/v11/providers/os/resources/windows ‑ TestWindowsRegistryKeyMultiStringParser
go.mondoo.com/cnquery/v11/providers/os/registry ‑ TestBuildSubKey
go.mondoo.com/cnquery/v11/providers/os/registry ‑ TestGetRegistryKeyPath
go.mondoo.com/cnquery/v11/providers/os/registry ‑ TestGetRegistryKeyPath/get_registry_key_path_for_an_registry_that_has_been_loaded
go.mondoo.com/cnquery/v11/providers/os/registry ‑ TestGetRegistryKeyPath/get_registry_key_path_for_an_registry_that_has_not_been_loaded_yet
go.mondoo.com/cnquery/v11/providers/os/registry ‑ TestGetRegistryPath
go.mondoo.com/cnquery/v11/providers/os/registry ‑ TestGetRegistryPath/get_registry_path_for_an_registry_that_has_been_loaded
go.mondoo.com/cnquery/v11/providers/os/registry ‑ TestGetRegistryPath/get_registry_path_for_an_registry_that_has_not_been_loaded_yet
go.mondoo.com/cnquery/v11/providers/os/registry ‑ TestWindowsRegistryKeyChildParser
go.mondoo.com/cnquery/v11/providers/os/registry ‑ TestWindowsRegistryKeyItemParser
go.mondoo.com/cnquery/v11/providers/os/registry ‑ TestWindowsRegistryKeyMultiStringParser

♻️ This comment has been updated with latest results.

…ry files, located on a filesystem

Signed-off-by: Preslav <preslav@mondoo.com>
// The subkey file is indicated by the filepath parameter.
// Only known registry files (see KnownRegistryFiles) can be loaded.
func (r *RegistryHandler) LoadSubkey(registryId, filepath string) error {
if runtime.GOOS != "windows" {
Copy link
Contributor

@afiune afiune Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; why not define this function also inside the files registryhandler_unix.go and registryhandler_windows.go instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also has some additional logic besides making the syscall so I figured it best belongs to the struct which handles the syscalls too (and storing which registries are loaded)

return err
}
ret, _, err := regLoadKey.Call(syscall.HKEY_LOCAL_MACHINE, uintptr(unsafe.Pointer(keyPtr)), uintptr(unsafe.Pointer(pathPtr)))
// the Microsoft docs indicate that the return value is 0 on success
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is funny, what is the err for then? 😂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, what if ret is 0 but err is nil?

Would the caller of LoadRegistrySubkey() be confused if it was a success of failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The err is to be thrown when the ret is not 0. There's always an err, even on success, so we cannot rely on the usual err != nil check:
https://go.dev/src/syscall/dll_windows.go (see 151)

Copy link
Contributor

@afiune afiune left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@afiune afiune left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YESSS!!!!

@afiune afiune merged commit 7b1e2dd into main Jun 6, 2024
15 checks passed
@afiune afiune deleted the preslav/windows-fs branch June 6, 2024 18:32
@github-actions github-actions bot locked and limited conversation to collaborators Jun 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants