C++如何检测您的应用程序正在运行的虚拟机具有焦点

标签 c++ focus virtual-machine detect

我有 2 个应用程序,每个应用程序都在各自独立的 MS Windows 7 虚拟机 (VM) 上运行。作为解决方案的一部分,我无法为主机编写软件。我正在使用 Qt 使用 C++ 进行编码。

这可能是不可能的,但我希望能够检测到 VM 窗口何时具有焦点(不是应用程序窗口,而是 VM)。这两个应用程序都是全屏应用程序(至少在 VM 中是全屏),它们始终具有应用程序窗口焦点,但我希望能够检测到用户的键盘输入将要转到哪个 VM,然后再点击键。

有什么想法吗?

最佳答案

没有 100% 的方法可以识别 VM 与否。但是有一些“指标”:

  • 特定“设备”、驱动程序、服务(vmmouse.sys、vmci、VBoxService.exe 等)
  • 特定的注册表项、Windows 产品 ID、MAC 地址

所有这些方法都不可靠但被广泛使用。

VMware 提供正式记录的 VM 检测方法 is here, (with code snippets) . 有一种方法基于使用管理程序端口 0x5658(“VX”)和管理程序魔术 DWORD 0x564D5868,代表“VMXh”

bool IsVMWare()
{
  bool res = true;

  __try
  {
    __asm
    {
      push   edx
      push   ecx
      push   ebx

      mov    eax, 'VMXh'
      mov    ebx, 0 
      mov    ecx, 10 // get VMWare version
      mov    edx, 'VX' // port number

      in     eax, dx // read port
                     // on return EAX returns the VERSION
                     
      cmp    ebx, 'VMXh' // compare with target
      setz   [res] // set return value

      pop    ebx
      pop    ecx
      pop    edx
    }
  }
  __except(EXCEPTION_EXECUTE_HANDLER)
  {
    res = false;
  }

  return res;
}

另一种方法 - 测试 CPUID 管理程序 (HV) 存在位和 HV 供应商的名称:

bool IsVM()
{
    int cpuInfo[4] = {};

    //
    // Upon execution, code should check bit 31 of register ECX
    // (the “hypervisor present bit”). If this bit is set, a hypervisor is present.
    // In a non-virtualized environment, the bit will be clear.
    //
    __cpuid(cpuInfo, 1);
    

    if (!(cpuInfo[2] & (1 << 31)))
        return false;
    
    //
    // A hypervisor is running on the machine. Query the vendor id.
    //
    const auto queryVendorIdMagic = 0x40000000;
    __cpuid(cpuInfo, queryVendorIdMagic);

    const int vendorIdLength = 13;
    using VendorIdStr = char[vendorIdLength];

    VendorIdStr hyperVendorId = {};
    
    memcpy(hyperVendorId + 0, &cpuInfo[1], 4);
    memcpy(hyperVendorId + 4, &cpuInfo[2], 4);
    memcpy(hyperVendorId + 8, &cpuInfo[3], 4);
    hyperVendorId[12] = '\0';

    static const VendorIdStr vendors[]{
    "KVMKVMKVM\0\0\0", // KVM 
    "Microsoft Hv",    // Microsoft Hyper-V or Windows Virtual PC */
    "VMwareVMware",    // VMware 
    "XenVMMXenVMM",    // Xen 
    "prl hyperv  ",    // Parallels
    "VBoxVBoxVBox"     // VirtualBox 
    };

    for (const auto& vendor : vendors)
    {
        if (!memcmp(vendor, hyperVendorId, vendorIdLength))
            return true;
    }

    return false;
}

一些硬件检测方法的实现依赖于 VM 如何模拟 CPU 行为这一事实。例如,使用缓存指令,如 wbinvdinvd,但它们是有特权的。

阅读更多内容的一些链接:

有有趣的文章about VM & Sandbox detection带有一些代码示例。

我找到了 repository有些方法是用C实现的,有些只能在Windows上使用,但也有一些是跨平台的

关于C++如何检测您的应用程序正在运行的虚拟机具有焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41750144/

相关文章:

c++ - 如何从包含空格的文件中读取字符串 C++

C++ "error: multiple types in one declaration"和 "error: expected initializer before ' KeyType'”

Android 键盘在点击/焦点/requestFocus/inputmanager show_forced 时不显示

java - Swing 中 requestFocusInWindow() 和 grabFocus() 的区别

mysql - 从虚拟服务器运行 MySQL,数据库位于主机磁盘上

c++ - friend 类无法访问 protected 成员

c++ - 获取类成员变量的大小(无需运行代码)

javascript - $(window).focus() 在 Chrome 中未正确执行

java - java获取物理mac地址

java - 为每个 Minecraft-Server/Java 应用程序创建一个虚拟机是否有意义?