Fixed mac detection #6 and new Property added
This commit is contained in:
		
							parent
							
								
									09282a9e95
								
							
						
					
					
						commit
						bcdfa36e0d
					
				
					 3 changed files with 40 additions and 8 deletions
				
			
		| 
						 | 
					@ -42,5 +42,10 @@ namespace SafeExamBrowser.SystemComponents.Contracts
 | 
				
			||||||
		/// Provides detailed version information about the currently running operating system.
 | 
							/// Provides detailed version information about the currently running operating system.
 | 
				
			||||||
		/// </summary>
 | 
							/// </summary>
 | 
				
			||||||
		string OperatingSystemInfo { get; }
 | 
							string OperatingSystemInfo { get; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							/// <summary>
 | 
				
			||||||
 | 
							/// The MAC Addres of the network addapter
 | 
				
			||||||
 | 
							/// </summary>
 | 
				
			||||||
 | 
							string MacAddress { get; }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,6 +23,7 @@ namespace SafeExamBrowser.SystemComponents
 | 
				
			||||||
		public string Model { get; private set; }
 | 
							public string Model { get; private set; }
 | 
				
			||||||
		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 OperatingSystemInfo
 | 
							public string OperatingSystemInfo
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
| 
						 | 
					@ -34,6 +35,7 @@ namespace SafeExamBrowser.SystemComponents
 | 
				
			||||||
			InitializeBattery();
 | 
								InitializeBattery();
 | 
				
			||||||
			InitializeMachineInfo();
 | 
								InitializeMachineInfo();
 | 
				
			||||||
			InitializeOperatingSystem();
 | 
								InitializeOperatingSystem();
 | 
				
			||||||
 | 
								InitializeMacAddress();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private void InitializeBattery()
 | 
							private void InitializeBattery()
 | 
				
			||||||
| 
						 | 
					@ -128,5 +130,30 @@ namespace SafeExamBrowser.SystemComponents
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			return Environment.Is64BitOperatingSystem ? "x64" : "x86";
 | 
								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";
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,17 +9,17 @@
 | 
				
			||||||
using SafeExamBrowser.Logging.Contracts;
 | 
					using SafeExamBrowser.Logging.Contracts;
 | 
				
			||||||
using SafeExamBrowser.SystemComponents.Contracts;
 | 
					using SafeExamBrowser.SystemComponents.Contracts;
 | 
				
			||||||
using System.Globalization;
 | 
					using System.Globalization;
 | 
				
			||||||
using System.Linq;
 | 
					
 | 
				
			||||||
using System.Management;
 | 
					 | 
				
			||||||
using System.Net.NetworkInformation;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace SafeExamBrowser.SystemComponents
 | 
					namespace SafeExamBrowser.SystemComponents
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	public class VirtualMachineDetector : IVirtualMachineDetector
 | 
						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 ILogger logger;
 | 
				
			||||||
		private ISystemInfo systemInfo;
 | 
							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)
 | 
							public VirtualMachineDetector(ILogger logger, ISystemInfo systemInfo)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			this.logger = logger;
 | 
								this.logger = logger;
 | 
				
			||||||
| 
						 | 
					@ -31,6 +31,7 @@ namespace SafeExamBrowser.SystemComponents
 | 
				
			||||||
			var isVirtualMachine = false;
 | 
								var isVirtualMachine = false;
 | 
				
			||||||
			var manufacturer = systemInfo.Manufacturer.ToLower();
 | 
								var manufacturer = systemInfo.Manufacturer.ToLower();
 | 
				
			||||||
			var model = systemInfo.Model.ToLower();
 | 
								var model = systemInfo.Model.ToLower();
 | 
				
			||||||
 | 
								var macAddress = systemInfo.MacAddress;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			isVirtualMachine |= manufacturer.Contains("microsoft corporation") && !model.Contains("surface");
 | 
								isVirtualMachine |= manufacturer.Contains("microsoft corporation") && !model.Contains("surface");
 | 
				
			||||||
			isVirtualMachine |= manufacturer.Contains("vmware");
 | 
								isVirtualMachine |= manufacturer.Contains("vmware");
 | 
				
			||||||
| 
						 | 
					@ -38,9 +39,8 @@ namespace SafeExamBrowser.SystemComponents
 | 
				
			||||||
			isVirtualMachine |= model.Contains("virtualbox");
 | 
								isVirtualMachine |= model.Contains("virtualbox");
 | 
				
			||||||
			isVirtualMachine |= manufacturer.Contains("qemu");
 | 
								isVirtualMachine |= manufacturer.Contains("qemu");
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
			var macAddr = (from nic in NetworkInterface.GetAllNetworkInterfaces() where nic.OperationalStatus == OperationalStatus.Up select nic.GetPhysicalAddress().ToString()).FirstOrDefault();
 | 
								isVirtualMachine |= ((byte.Parse(macAddress[1].ToString(), NumberStyles.HexNumber) & 2) == 2 || macAddress.StartsWith("080027"));
 | 
				
			||||||
			isVirtualMachine |= ((byte.Parse(macAddr[1].ToString(), NumberStyles.HexNumber) & 2) == 2 || macAddr.StartsWith("080027"));
 | 
								/*
 | 
				
			||||||
 | 
					 | 
				
			||||||
			using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT DeviceID FROM Win32_PnPEntity"))
 | 
								using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT DeviceID FROM Win32_PnPEntity"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				foreach (ManagementObject queryObj in searcher.Get())
 | 
									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);
 | 
									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.");
 | 
								logger.Debug($"Computer '{systemInfo.Name}' appears to {(isVirtualMachine ? "" : "not ")}be a virtual machine.");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			return isVirtualMachine;
 | 
								return isVirtualMachine;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue