|
9 | 9 | using System; |
10 | 10 | using System.Linq; |
11 | 11 | using System.Management; |
| 12 | +using SafeExamBrowser.Configuration.Contracts.Integrity; |
12 | 13 | using SafeExamBrowser.Logging.Contracts; |
13 | 14 | using SafeExamBrowser.Monitoring.Contracts; |
14 | 15 | using SafeExamBrowser.SystemComponents.Contracts; |
@@ -42,79 +43,70 @@ public class VirtualMachineDetector : IVirtualMachineDetector |
42 | 43 | "PROD_VIRTUAL_DVD" |
43 | 44 | }; |
44 | 45 |
|
45 | | - private static readonly (string hardware, bool required)[] SystemHardware = |
| 46 | + private static readonly string[] SystemHardware = |
46 | 47 | { |
47 | | - ("CIM_Memory", false), |
48 | | - ("CIM_NumericSensor", false), |
49 | | - ("CIM_Sensor", false), |
50 | | - ("CIM_TemperatureSensor", false), |
51 | | - ("CIM_VoltageSensor", false), |
52 | | - ("Win32_CacheMemory", false), |
53 | | - ("Win32_Fan", false), |
54 | | - ("Win32_PerfRawData_Counters_ThermalZoneInformation", true), |
55 | | - ("Win32_VoltageProbe", false) |
| 48 | + "CIM_Memory", |
| 49 | + "CIM_NumericSensor", |
| 50 | + "CIM_Sensor", |
| 51 | + "CIM_TemperatureSensor", |
| 52 | + "CIM_VoltageSensor", |
| 53 | + "Win32_CacheMemory", |
| 54 | + "Win32_Fan", |
| 55 | + "Win32_VoltageProbe" |
56 | 56 | }; |
57 | 57 |
|
| 58 | + private readonly IIntegrityModule integrityModule; |
58 | 59 | private readonly ILogger logger; |
59 | 60 | private readonly IRegistry registry; |
60 | 61 | private readonly ISystemInfo systemInfo; |
61 | 62 |
|
62 | | - public VirtualMachineDetector(ILogger logger, IRegistry registry, ISystemInfo systemInfo) |
| 63 | + public VirtualMachineDetector(IIntegrityModule integrityModule, ILogger logger, IRegistry registry, ISystemInfo systemInfo) |
63 | 64 | { |
| 65 | + this.integrityModule = integrityModule; |
64 | 66 | this.logger = logger; |
65 | 67 | this.registry = registry; |
66 | 68 | this.systemInfo = systemInfo; |
67 | 69 | } |
68 | 70 |
|
69 | 71 | public bool IsVirtualMachine() |
70 | 72 | { |
71 | | - var isVirtualMachine = false; |
| 73 | + var isVm = false; |
72 | 74 |
|
73 | | - isVirtualMachine |= HasNoSystemHardware(); |
74 | | - isVirtualMachine |= HasVirtualDevice(); |
75 | | - isVirtualMachine |= HasVirtualMacAddress(); |
76 | | - isVirtualMachine |= IsVirtualCpu(); |
77 | | - isVirtualMachine |= IsVirtualRegistry(); |
78 | | - isVirtualMachine |= IsVirtualSystem(systemInfo.BiosInfo, systemInfo.Manufacturer, systemInfo.Model); |
| 75 | + isVm |= HasNoSystemHardware(); |
| 76 | + isVm |= HasVirtualDevice(); |
| 77 | + isVm |= HasVirtualMacAddress(); |
| 78 | + isVm |= IsVirtualCpu(); |
| 79 | + isVm |= IsVirtualRegistry(); |
| 80 | + isVm |= IsVirtualSystem(systemInfo.BiosInfo, systemInfo.Manufacturer, systemInfo.Model); |
| 81 | + isVm |= integrityModule.IsVirtualMachine(out var manufacturer, out var probability); |
79 | 82 |
|
80 | | - logger.Debug($"Computer '{systemInfo.Name}' appears {(isVirtualMachine ? "" : "not ")}to be a virtual machine."); |
| 83 | + logger.Debug($"Computer '{systemInfo.Name}' appears {(isVm ? "" : "not ")}to be a virtual machine{(isVm ? $" ({manufacturer}, {probability}%)" : "")}."); |
81 | 84 |
|
82 | | - return isVirtualMachine; |
| 85 | + return isVm; |
83 | 86 | } |
84 | 87 |
|
85 | 88 | private bool HasNoSystemHardware() |
86 | 89 | { |
87 | | - var hasOther = false; |
88 | | - var hasRequired = true; |
| 90 | + var hasHardware = false; |
89 | 91 |
|
90 | 92 | try |
91 | 93 | { |
92 | | - foreach (var (hardware, required) in SystemHardware) |
| 94 | + foreach (var hardware in SystemHardware) |
93 | 95 | { |
94 | 96 | using (var searcher = new ManagementObjectSearcher($"SELECT * FROM {hardware}")) |
95 | 97 | using (var results = searcher.Get()) |
96 | 98 | { |
97 | | - var hasResults = results.Count > 0; |
98 | | - |
99 | | - if (required) |
100 | | - { |
101 | | - hasRequired &= hasResults; |
102 | | - } |
103 | | - else |
104 | | - { |
105 | | - hasOther |= hasResults; |
106 | | - } |
| 99 | + hasHardware |= results.Count > 0; |
107 | 100 | } |
108 | 101 | } |
109 | 102 | } |
110 | 103 | catch (Exception e) |
111 | 104 | { |
112 | | - hasOther = false; |
113 | | - hasRequired = false; |
| 105 | + hasHardware = false; |
114 | 106 | logger.Error("Failed to query system hardware!", e); |
115 | 107 | } |
116 | 108 |
|
117 | | - return !(hasRequired && hasOther); |
| 109 | + return !hasHardware; |
118 | 110 | } |
119 | 111 |
|
120 | 112 | private bool HasVirtualDevice() |
|
0 commit comments