Minor fixes and Mac > 2 check

This commit is contained in:
diegoara96 2020-05-07 13:21:57 +02:00
parent 1de5848edb
commit 83c82f799f
3 changed files with 24 additions and 16 deletions

View file

@ -44,13 +44,13 @@ namespace SafeExamBrowser.SystemComponents.Contracts
string OperatingSystemInfo { get; } string OperatingSystemInfo { get; }
/// <summary> /// <summary>
/// The MAC Addres of the network addapter /// The MAC address of the network adapter
/// </summary> /// </summary>
string MacAddress { get; } string MacAddress { get; }
/// <summary> /// <summary>
/// Provides the DeviceID information of the user's Plug and Play devices /// Provides the DeviceID information of the user's Plug and Play devices
/// </summary> /// </summary>
string[] DeviceId { get; } string[] PlugAndPlayDeviceIds { get; }
} }
} }

View file

@ -25,7 +25,7 @@ namespace SafeExamBrowser.SystemComponents
public string Name { get; private set; } public string Name { get; private set; }
public OperatingSystem OperatingSystem { get; private set; } public OperatingSystem OperatingSystem { get; private set; }
public string MacAddress { get; private set; } public string MacAddress { get; private set; }
public string[] DeviceId { get; private set; } public string[] PlugAndPlayDeviceIds { get; private set; }
public string OperatingSystemInfo public string OperatingSystemInfo
{ {
@ -133,16 +133,18 @@ namespace SafeExamBrowser.SystemComponents
{ {
return Environment.Is64BitOperatingSystem ? "x64" : "x86"; return Environment.Is64BitOperatingSystem ? "x64" : "x86";
} }
private void InitializeMacAddress() private void InitializeMacAddress()
{ {
using (var searcher = new ManagementObjectSearcher("Select MACAddress from Win32_NetworkAdapterConfiguration WHERE DNSDomain IS NOT NULL")) using (var searcher = new ManagementObjectSearcher("Select MACAddress from Win32_NetworkAdapterConfiguration WHERE DNSDomain IS NOT NULL"))
using (var results = searcher.Get()) using (var results = searcher.Get())
{ {
if (results.Count > 0)
if (results != null && results.Count > 0)
{ {
using (var system = results.Cast<ManagementObject>().First()) using (var networkAdapter = results.Cast<ManagementObject>().First())
{ {
foreach (var property in system.Properties) foreach (var property in networkAdapter.Properties)
{ {
if (property.Name.Equals("MACAddress")) if (property.Name.Equals("MACAddress"))
@ -158,25 +160,27 @@ namespace SafeExamBrowser.SystemComponents
} }
} }
} }
private void InitializePnPDevices() private void InitializePnPDevices()
{ {
List<string> deviceList = new List<string>(); var deviceList = new List<string>();
using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT DeviceID FROM Win32_PnPEntity")) using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT DeviceID FROM Win32_PnPEntity"))
using (var results = searcher.Get()) using (var results = searcher.Get())
{ {
foreach (ManagementObject queryObj in results) foreach (ManagementObject queryObj in results)
{ {
using (queryObj) using (queryObj)
{
foreach (var property in queryObj.Properties) foreach (var property in queryObj.Properties)
{ {
if (property.Name.Equals("DeviceID")) if (property.Name.Equals("DeviceID"))
{ {
Console.WriteLine(Convert.ToString(property.Value));
deviceList.Add(Convert.ToString(property.Value).ToLower()); deviceList.Add(Convert.ToString(property.Value).ToLower());
} }
} }
}
} }
DeviceId = deviceList.ToArray(); PlugAndPlayDeviceIds = deviceList.ToArray();
} }
} }

View file

@ -32,21 +32,25 @@ namespace SafeExamBrowser.SystemComponents
var manufacturer = systemInfo.Manufacturer.ToLower(); var manufacturer = systemInfo.Manufacturer.ToLower();
var model = systemInfo.Model.ToLower(); var model = systemInfo.Model.ToLower();
var macAddress = systemInfo.MacAddress; var macAddress = systemInfo.MacAddress;
var deviceId = systemInfo.DeviceId; var plugAndPlayDeviceIds = systemInfo.PlugAndPlayDeviceIds;
isVirtualMachine |= manufacturer.Contains("microsoft corporation") && !model.Contains("surface"); isVirtualMachine |= manufacturer.Contains("microsoft corporation") && !model.Contains("surface");
isVirtualMachine |= manufacturer.Contains("vmware"); isVirtualMachine |= manufacturer.Contains("vmware");
isVirtualMachine |= manufacturer.Contains("parallels software"); isVirtualMachine |= manufacturer.Contains("parallels software");
isVirtualMachine |= model.Contains("virtualbox"); isVirtualMachine |= model.Contains("virtualbox");
isVirtualMachine |= manufacturer.Contains("qemu"); isVirtualMachine |= manufacturer.Contains("qemu");
isVirtualMachine |= ((byte.Parse(macAddress[1].ToString(), NumberStyles.HexNumber) & 2) == 2 || macAddress.StartsWith("080027")); if (macAddress != null && macAddress.Count() > 2)
{
foreach (var device in deviceId) isVirtualMachine |= ((byte.Parse(macAddress[1].ToString(), NumberStyles.HexNumber) & 2) == 2 || macAddress.StartsWith("080027"));
}
foreach (var device in plugAndPlayDeviceIds)
{ {
isVirtualMachine |= PCI_VENDOR_BLACKLIST.Any(device.ToLower().Contains); isVirtualMachine |= PCI_VENDOR_BLACKLIST.Any(device.ToLower().Contains);
} }
logger.Debug($"Computer '{systemInfo.Name}' appears to {(isVirtualMachine ? "" : "not ")}be a virtual machine."); logger.Debug($"Computer '{systemInfo.Name}' appears to {(isVirtualMachine ? "" : "not ")}be a virtual machine.");
return isVirtualMachine; return isVirtualMachine;