From 3b8f552138f723f977a24f9ae055ce7f96a01658 Mon Sep 17 00:00:00 2001 From: Notselwyn <68616630+Notselwyn@users.noreply.github.com> Date: Mon, 17 Jul 2023 17:06:46 +0200 Subject: [PATCH] fix: ported first part of IsVirtualRegistry to use IRegistry --- .../VirtualMachineDetector.cs | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs b/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs index 1cff1b81..48fadf81 100644 --- a/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs +++ b/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs @@ -111,14 +111,16 @@ namespace SafeExamBrowser.SystemComponents * */ IEnumerable hardwareConfigSubkeys; - if (!registry.TryGetSubKeys("HKLM\\SYSTEM\\HardwareConfig", out hardwareConfigSubkeys)) + const string hwConfigParentKey = "HKEY_LOCAL_MACHINE\\SYSTEM\\HardwareConfig"; + if (!registry.TryGetSubKeys(hwConfigParentKey, out hardwareConfigSubkeys)) return false; foreach (string configId in hardwareConfigSubkeys) { logger.Info($"scanning configId: {configId}"); - var configKey = $"HKEY_LOCAL_MACHINE\\SYSTEM\\HardwareConfig\\{configId}"; + var hwConfigKey = $"{hwConfigParentKey}\\{configId}"; + // collect system values for IsVirtualSystemInfo() object biosVendor; object biosVersion; object systemManufacturer; @@ -126,10 +128,10 @@ namespace SafeExamBrowser.SystemComponents bool success = true; - success &= registry.TryRead(configKey, "BIOSVendor", out biosVendor); - success &= registry.TryRead(configKey, "BIOSVersion", out biosVersion); - success &= registry.TryRead(configKey, "SystemManufacturer", out systemManufacturer); - success &= registry.TryRead(configKey, "SystemProductName", out systemProductName); + success &= registry.TryRead(hwConfigKey, "BIOSVendor", out biosVendor); + success &= registry.TryRead(hwConfigKey, "BIOSVersion", out biosVersion); + success &= registry.TryRead(hwConfigKey, "SystemManufacturer", out systemManufacturer); + success &= registry.TryRead(hwConfigKey, "SystemProductName", out systemProductName); if (!success) continue; @@ -139,18 +141,19 @@ namespace SafeExamBrowser.SystemComponents isVirtualMachine |= IsVirtualSystemInfo(biosInfo, (string) systemManufacturer, (string) systemProductName); - // hardware information of profile throughout installation etc. - IEnumerable computerIds; - if (!registry.TryGetSubKeys($"HKLM\\SYSTEM\\HardwareConfig\\{configId}\\ComputerIds", out computerIds)) - return false; + // check even more hardware information + IEnumerable computerIdNames; + var computerIdsKey = $"{hwConfigKey}\\ComputerIds"; + if (!registry.TryGetNames(computerIdsKey, out computerIdNames)) + continue; - foreach (var computerId in computerIds) + foreach (var computerIdName in computerIdNames) { - logger.Info($"computerId: {computerId}"); - // e.g. manufacturer&version&sku&... - object computerSummary; // = (string) computerIds.GetValue(computerId); - - if (!registry.TryRead($"HKLM\\SYSTEM\\HardwareConfig\\{configId}\\ComputerIds", computerId, out computerSummary)) + logger.Info($"computerId: {computerIdName}"); + + // collect computer hardware summary (e.g. manufacturer&version&sku&...) + object computerSummary; + if (!registry.TryRead(computerIdsKey, computerIdName, out computerSummary)) continue; isVirtualMachine |= IsVirtualSystemInfo((string) computerSummary, (string) systemManufacturer, (string) systemProductName);