chore: change varnames (and declarations), and fix registry bug

This commit is contained in:
Notselwyn 2023-07-18 14:32:54 +02:00
parent bc30e56e38
commit f2917f69a6
2 changed files with 36 additions and 38 deletions

View file

@ -89,23 +89,23 @@ namespace SafeExamBrowser.SystemComponents.Registry
return success; return success;
} }
public bool TryGetNames(string key, out IEnumerable<string> names) public bool TryGetNames(string keyName, out IEnumerable<string> names)
{ {
names = default; names = default;
if (!TryOpenKey(key, out var keyObj)) if (!TryOpenKey(keyName, out var key))
return false; return false;
bool success = true; bool success = true;
using (keyObj) using (key)
{ {
try try
{ {
names = keyObj.GetValueNames(); names = key.GetValueNames();
} }
catch (Exception e) catch (Exception e)
{ {
logger.Error($"Failed to get registry value names '{key}'!", e); logger.Error($"Failed to get registry value names '{keyName}'!", e);
success = false; success = false;
// persist keyObj dispose operation by finishing using() {} // persist keyObj dispose operation by finishing using() {}
} }
@ -115,23 +115,23 @@ namespace SafeExamBrowser.SystemComponents.Registry
return success; return success;
} }
public bool TryGetSubKeys(string key, out IEnumerable<string> subKeys) public bool TryGetSubKeys(string keyName, out IEnumerable<string> subKeys)
{ {
subKeys = default; subKeys = default;
if (!TryOpenKey(key, out var keyObj)) if (!TryOpenKey(keyName, out var key))
return false; return false;
bool success = true; bool success = true;
using (keyObj) using (key)
{ {
try try
{ {
subKeys = keyObj.GetSubKeyNames(); subKeys = key.GetSubKeyNames();
} }
catch (Exception e) catch (Exception e)
{ {
logger.Error($"Failed to get registry value names '{key}'!", e); logger.Error($"Failed to get registry value names '{keyName}'!", e);
success = false; success = false;
// persist keyObj dispose operation by finishing using() {} // persist keyObj dispose operation by finishing using() {}
} }
@ -234,25 +234,25 @@ namespace SafeExamBrowser.SystemComponents.Registry
/// <summary> /// <summary>
/// Tries to open a key and outputs a RegistryKey object. Does not raise Exceptions, but returns false/true. /// Tries to open a key and outputs a RegistryKey object. Does not raise Exceptions, but returns false/true.
/// </summary> /// </summary>
private bool TryOpenKey(string key, out RegistryKey keyObj) private bool TryOpenKey(string keyName, out RegistryKey key)
{ {
keyObj = default; key = default;
try try
{ {
if (!GetBaseKeyFromKeyName(key, out var hiveObj, out var subHiveKey)) if (!GetBaseKeyFromKeyName(keyName, out var hiveObj, out var subHiveKey))
return false; return false;
keyObj = hiveObj.OpenSubKey(subHiveKey); key = hiveObj.OpenSubKey(subHiveKey);
if (keyObj == null) if (key == null)
{ {
keyObj = default; key = default;
return false; return false;
} }
} }
catch (Exception e) catch (Exception e)
{ {
logger.Error($"Failed to open registry key '{key}'!", e); logger.Error($"Failed to open registry key '{keyName}'!", e);
return false; return false;
} }

View file

@ -119,7 +119,7 @@ namespace SafeExamBrowser.SystemComponents
/// </summary> /// </summary>
private bool IsVirtualRegistryHardwareConfig() private bool IsVirtualRegistryHardwareConfig()
{ {
bool isVirtualMachine = false; var isVirtualMachine = false;
/** /**
* scanned registry format: * scanned registry format:
@ -135,19 +135,18 @@ namespace SafeExamBrowser.SystemComponents
foreach (string configId in hardwareConfigSubkeys) foreach (string configId in hardwareConfigSubkeys)
{ {
var hwConfigKey = $"{hwConfigParentKey}\\{configId}"; var hwConfigKey = $"{hwConfigParentKey}\\{configId}";
bool didReadKeys = true;
// collect system values for IsVirtualSystemInfo() // collect system values for IsVirtualSystemInfo()
bool success = true; didReadKeys &= registry.TryRead(hwConfigKey, "BIOSVendor", out var biosVendor);
success &= registry.TryRead(hwConfigKey, "BIOSVendor", out var biosVendor); didReadKeys &= registry.TryRead(hwConfigKey, "BIOSVersion", out var biosVersion);
success &= registry.TryRead(hwConfigKey, "BIOSVersion", out var biosVersion); didReadKeys &= registry.TryRead(hwConfigKey, "SystemManufacturer", out var systemManufacturer);
success &= registry.TryRead(hwConfigKey, "SystemManufacturer", out var systemManufacturer); didReadKeys &= registry.TryRead(hwConfigKey, "SystemProductName", out var systemProductName);
success &= registry.TryRead(hwConfigKey, "SystemProductName", out var systemProductName); if (!didReadKeys)
if (!success)
continue; continue;
// reconstruct the systemInfo.biosInfo string // reconstruct the systemInfo.biosInfo string
string biosInfo = $"{(string) biosVendor} {(string) biosVersion}"; var biosInfo = $"{(string) biosVendor} {(string) biosVersion}";
isVirtualMachine |= IsVirtualSystemInfo(biosInfo, (string) systemManufacturer, (string) systemProductName); isVirtualMachine |= IsVirtualSystemInfo(biosInfo, (string) systemManufacturer, (string) systemProductName);
@ -174,31 +173,30 @@ namespace SafeExamBrowser.SystemComponents
/// </summary> /// </summary>
private bool IsVirtualRegistryDeviceCache() private bool IsVirtualRegistryDeviceCache()
{ {
bool isVirtualMachine = false; var isVirtualMachine = false;
// device cache contains hardware about other devices logged into as well, so lock onto this device in case an innocent VM was logged into. // device cache contains hardware about other devices logged into as well, so lock onto this device in case an innocent VM was logged into.
// in the future, try to improve this check somehow since DeviceCache only gives ComputerName // in the future, try to improve this check somehow since DeviceCache only gives ComputerName
var deviceName = System.Environment.GetEnvironmentVariable("COMPUTERNAME"); var deviceName = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
// check Windows timeline caches for current hardware config // check Windows timeline caches for current hardware config
const string deviceCacheParentKey = "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\TaskFlow\\DeviceCache"; const string deviceCacheParentKey = "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\TaskFlow\\DeviceCache";
bool has_dc_keys = registry.TryGetSubKeys(deviceCacheParentKey, out var deviceCacheKeys); var hasDeviceCacheKeys = registry.TryGetSubKeys(deviceCacheParentKey, out var deviceCacheKeys);
if (deviceName != null && has_dc_keys) if (deviceName != null && hasDeviceCacheKeys)
{ {
foreach (string cacheId in deviceCacheKeys) foreach (var cacheId in deviceCacheKeys)
{ {
var cacheIdKey = $"{deviceCacheParentKey}\\{cacheId}"; var cacheIdKey = $"{deviceCacheParentKey}\\{cacheId}";
var didReadKeys = true;
bool success = true; didReadKeys &= registry.TryRead(cacheIdKey, "DeviceName", out var cacheDeviceName);
success &= registry.TryRead(cacheIdKey, "DeviceName", out var cacheDeviceName); if (!didReadKeys || deviceName.ToLower() != ((string) cacheDeviceName).ToLower())
if (!success || deviceName.ToLower() != ((string) cacheDeviceName).ToLower())
continue; continue;
success &= registry.TryRead(cacheIdKey, "DeviceMake", out var cacheDeviceManufacturer); didReadKeys &= registry.TryRead(cacheIdKey, "DeviceMake", out var cacheDeviceManufacturer);
success &= registry.TryRead(cacheIdKey, "DeviceModel", out var cacheDeviceModel); didReadKeys &= registry.TryRead(cacheIdKey, "DeviceModel", out var cacheDeviceModel);
if (!success) if (!didReadKeys)
continue; continue;
isVirtualMachine |= IsVirtualSystemInfo("", (string) cacheDeviceManufacturer, (string) cacheDeviceModel); isVirtualMachine |= IsVirtualSystemInfo("", (string) cacheDeviceManufacturer, (string) cacheDeviceModel);