winform判断Windows 是否运行在 macOS 系统的虚拟机

cs 复制代码
using System;
using System.Management;

namespace DetectMacOSVM
{
    class Program
    {
        static void Main(string[] args)
        {
            if (IsRunningInMacOSVM())
            {
                Console.WriteLine("This Windows system is running in a macOS virtual machine.");
            }
            else
            {
                Console.WriteLine("This Windows system is not running in a macOS virtual machine.");
            }
        }

        static bool IsRunningInMacOSVM()
        {
            try
            {
                using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem"))
                {
                    foreach (var obj in searcher.Get())
                    {
                        string manufacturer = obj["Manufacturer"]?.ToString() ?? string.Empty;
                        string model = obj["Model"]?.ToString() ?? string.Empty;

                        // Check for macOS-related VM indicators
                        if (manufacturer.Contains("Apple") || model.Contains("Mac"))
                        {
                            return true;
                        }
                    }
                }

                using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS"))
                {
                    foreach (var obj in searcher.Get())
                    {
                        string biosVendor = obj["Manufacturer"]?.ToString() ?? string.Empty;

                        // Check for Apple-related BIOS information
                        if (biosVendor.Contains("Apple"))
                        {
                            return true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }

            return false;
        }
    }
}

说明

  1. 通过 WMI 检索系统信息:

使用 Win32_ComputerSystem 检查制造商和模型名称是否包含 "Apple" 或 "Mac"。

使用 Win32_BIOS 检查 BIOS 信息是否与 Apple 相关。

  1. 虚拟机特征:

如果虚拟机运行在 macOS 主机上,虚拟机可能会暴露 Apple 的信息,比如制造商或 BIOS。

  1. 限制与注意事项:

这种方法依赖于虚拟机是否将 macOS 的相关信息暴露给 Windows。

某些虚拟机管理程序(如 VMware 或 Parallels)可能隐藏或自定义这些信息,导致检查失败。

  1. 真实场景中的变通:

可以结合网卡信息、硬件特性进一步判断(如虚拟化软件特定标识:Parallels、VMware 等)。

不支持在Window虚拟机中运行

cs 复制代码
using System;
using System.Management;

namespace VirtualMachineDetector
{
    class Program
    {
        static void Main(string[] args)
        {
            if (IsVirtualMachine())
            {
                Console.WriteLine("The current system is running in a virtual machine.");
            }
            else
            {
                Console.WriteLine("The current system is not running in a virtual machine.");
            }
        }

        static bool IsVirtualMachine()
        {
            try
            {
                // Check for known virtual machine identifiers in Win32_ComputerSystem
                using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem"))
                {
                    foreach (var obj in searcher.Get())
                    {
                        string manufacturer = obj["Manufacturer"]?.ToString() ?? string.Empty;
                        string model = obj["Model"]?.ToString() ?? string.Empty;

                        // Common virtual machine manufacturers and models
                        if (manufacturer.ToLower().Contains("microsoft") && model.ToLower().Contains("virtual"))
                            return true;
                        if (manufacturer.ToLower().Contains("vmware"))
                            return true;
                        if (manufacturer.ToLower().Contains("xen"))
                            return true;
                        if (manufacturer.ToLower().Contains("qemu"))
                            return true;
                        if (manufacturer.ToLower().Contains("parallels"))
                            return true;
                        if (manufacturer.ToLower().Contains("oracle") && model.ToLower().Contains("virtualbox"))
                            return true;
                    }
                }

                // Check for BIOS information in Win32_BIOS
                using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS"))
                {
                    foreach (var obj in searcher.Get())
                    {
                        string biosVersion = obj["SMBIOSBIOSVersion"]?.ToString() ?? string.Empty;
                        string manufacturer = obj["Manufacturer"]?.ToString() ?? string.Empty;

                        // Check for virtual machine specific BIOS information
                        if (biosVersion.ToLower().Contains("vmware"))
                            return true;
                        if (biosVersion.ToLower().Contains("virtualbox"))
                            return true;
                        if (manufacturer.ToLower().Contains("xen"))
                            return true;
                        if (manufacturer.ToLower().Contains("parallels"))
                            return true;
                    }
                }

                // Check for processor information in Win32_Processor
                using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"))
                {
                    foreach (var obj in searcher.Get())
                    {
                        string processorName = obj["Name"]?.ToString() ?? string.Empty;

                        // Virtual processors often have specific names
                        if (processorName.ToLower().Contains("virtual"))
                            return true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred while detecting virtual machine: {ex.Message}");
            }

            return false;
        }
    }
}
相关推荐
怜渠客几秒前
Google Chrome 去除更新 Windows
chrome·windows
Bruce_Liuxiaowei1 小时前
文章题目:利用Adobe Flash Player漏洞:一次针对Windows XP的渗透测试实验
windows·adobe·渗透测试·kali linux·浏览器插件漏洞
CAir22 小时前
openssl编译
windows·编译·openssl
命运之手4 小时前
[ Mac ] HomeBrew Set Sources from China
macos·source·repository·homebrew
鸠摩智首席音效师4 小时前
macOS 如何修改 PATH 环境变量 ?
macos
dingdongkk4 小时前
mac 使用zip2john破解zip压缩包密码
macos·解压缩
总爱写点小BUG7 小时前
macOS 版本对应 Xcode 版本,以及 Xcode 历史版本下载
ide·macos·xcode
步、步、为营7 小时前
C# 与 Windows API 交互的“秘密武器”:结构体和联合体
windows·c#·交互
Narutolxy8 小时前
macOS 使用 FreeRDP 远程访问 Windows:完整指南20250109
windows·macos