Fixed mac detection #6 and new Property added

This commit is contained in:
diegoara96 2020-05-06 18:44:08 +02:00
parent 09282a9e95
commit bcdfa36e0d
3 changed files with 40 additions and 8 deletions

View file

@ -42,5 +42,10 @@ namespace SafeExamBrowser.SystemComponents.Contracts
/// Provides detailed version information about the currently running operating system.
/// </summary>
string OperatingSystemInfo { get; }
/// <summary>
/// The MAC Addres of the network addapter
/// </summary>
string MacAddress { get; }
}
}

View file

@ -23,6 +23,7 @@ namespace SafeExamBrowser.SystemComponents
public string Model { get; private set; }
public string Name { get; private set; }
public OperatingSystem OperatingSystem { get; private set; }
public string MacAddress { get; private set; }
public string OperatingSystemInfo
{
@ -34,6 +35,7 @@ namespace SafeExamBrowser.SystemComponents
InitializeBattery();
InitializeMachineInfo();
InitializeOperatingSystem();
InitializeMacAddress();
}
private void InitializeBattery()
@ -128,5 +130,30 @@ namespace SafeExamBrowser.SystemComponents
{
return Environment.Is64BitOperatingSystem ? "x64" : "x86";
}
private void InitializeMacAddress()
{
using (var searcher = new ManagementObjectSearcher("Select MACAddress from Win32_NetworkAdapterConfiguration WHERE DNSDomain IS NOT NULL"))
using (var results = searcher.Get())
{
if (results.Count > 0)
{
using (var system = results.Cast<ManagementObject>().First())
{
foreach (var property in system.Properties)
{
if (property.Name.Equals("MACAddress"))
{
MacAddress = Convert.ToString(property.Value).Replace(":", "").ToUpper();
}
}
}
}
else
{
MacAddress = "000000000000";
}
}
}
}
}

View file

@ -9,17 +9,17 @@
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.SystemComponents.Contracts;
using System.Globalization;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
namespace SafeExamBrowser.SystemComponents
{
public class VirtualMachineDetector : IVirtualMachineDetector
{
private static readonly string[] PCI_VENDOR_BLACKLIST = { "vbox", "80ee", "qemu", "1af4", "1b36" }; //Virtualbox: VBOX, 80EE RedHat: QUEMU, 1AF4, 1B36
private ILogger logger;
private ISystemInfo systemInfo;
private static readonly string[] pciVendorBlacklist = { "vbox", "80ee", "qemu", "1af4", "1b36" }; //Virtualbox: VBOX, 80EE RedHat: QUEMU, 1AF4, 1B36
public VirtualMachineDetector(ILogger logger, ISystemInfo systemInfo)
{
this.logger = logger;
@ -31,6 +31,7 @@ namespace SafeExamBrowser.SystemComponents
var isVirtualMachine = false;
var manufacturer = systemInfo.Manufacturer.ToLower();
var model = systemInfo.Model.ToLower();
var macAddress = systemInfo.MacAddress;
isVirtualMachine |= manufacturer.Contains("microsoft corporation") && !model.Contains("surface");
isVirtualMachine |= manufacturer.Contains("vmware");
@ -38,9 +39,8 @@ namespace SafeExamBrowser.SystemComponents
isVirtualMachine |= model.Contains("virtualbox");
isVirtualMachine |= manufacturer.Contains("qemu");
var macAddr = (from nic in NetworkInterface.GetAllNetworkInterfaces() where nic.OperationalStatus == OperationalStatus.Up select nic.GetPhysicalAddress().ToString()).FirstOrDefault();
isVirtualMachine |= ((byte.Parse(macAddr[1].ToString(), NumberStyles.HexNumber) & 2) == 2 || macAddr.StartsWith("080027"));
isVirtualMachine |= ((byte.Parse(macAddress[1].ToString(), NumberStyles.HexNumber) & 2) == 2 || macAddress.StartsWith("080027"));
/*
using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT DeviceID FROM Win32_PnPEntity"))
foreach (ManagementObject queryObj in searcher.Get())
@ -48,7 +48,7 @@ namespace SafeExamBrowser.SystemComponents
isVirtualMachine |= pciVendorBlacklist.Any(System.Convert.ToString(queryObj.Properties.Cast<PropertyData>().First().Value).ToLower().Contains);
}
*/
logger.Debug($"Computer '{systemInfo.Name}' appears to {(isVirtualMachine ? "" : "not ")}be a virtual machine.");
return isVirtualMachine;