VC++检测VM、VPC虚拟机代码

C++检测VM、VPC虚拟机代码,添加后只需要调用IsVirtualMachine即可判断是否在虚拟机运行!



AntiVM.h

#ifndef __DETECT_VM__02222005__  
#define __DETECT_VM__02222005__  
  
bool IsVirtualMachine();  
bool IsInsideVPC();  
bool IsInsideVMWare();  
  
#endif  

AntiVM.cpp


/* -----------------------------------------------------------------------------  
 * Created by * lallous <lallousx86@yahoo.com> * 
 * All rights reserved. 
 *  
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 *  
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 * SUCH DAMAGE. 
 * -----------------------------------------------------------------------------  
 
 
 History 
 --------- 
 09/09/2004  - added IsInsideVPC() 
 02/22/2005  - added IsInsideVMWare() 
 03/26/2005  - . added C++ friendy version of IsInsideVPC() and renamed old IsInsideVPC() 
               . rewritten IsInsideVmWare() 
 
 
 Special thanks to Ken Kato <chitchat-lj@infoseek.jp> for his VMWare Backdoor information 
 (http://chitchat.at.infoseek.co.jp/vmware/vmtools.html) 
 
*/  
  
#include "AntiVM.h"  
#include <windows.h>  
  
bool IsVirtualMachine()  
{  
    if(IsInsideVPC())  
        return true;  
      
    if(IsInsideVMWare())  
        return true;  
          
    return false;  
}  
  
// IsInsideVPC's exception filter  
DWORD __forceinline IsInsideVPC_exceptionFilter(LPEXCEPTION_POINTERS ep)  
{  
  PCONTEXT ctx = ep->ContextRecord;  
  
  ctx->Ebx = -1; // Not running VPC  
  ctx->Eip += 4; // skip past the "call VPC" opcodes  
  return EXCEPTION_CONTINUE_EXECUTION; // we can safely resume execution since we skipped faulty instruction  
}  
  
// high level language friendly version of IsInsideVPC()  
bool IsInsideVPC()  
{  
  bool rc = false;  
  
  __try  
  {  
    _asm push ebx  
    _asm mov  ebx, 0 // Flag  
    _asm mov  eax, 1 // VPC function number  
  
    // call VPC   
    _asm __emit 0Fh  
    _asm __emit 3Fh  
    _asm __emit 07h  
    _asm __emit 0Bh  
  
    _asm test ebx, ebx  
    _asm setz [rc]  
    _asm pop ebx  
  }  
  // The except block shouldn't get triggered if VPC is running!!  
  __except(IsInsideVPC_exceptionFilter(GetExceptionInformation()))  
  {  
  }  
  
  return rc;  
}  
  
bool IsInsideVMWare()  
{  
  bool rc = true;  
  
  __try  
  {  
    __asm  
    {  
      push   edx  
      push   ecx  
      push   ebx  
  
      mov    eax, 'VMXh'  
      mov    ebx, 0 // any value but not the MAGIC VALUE  
      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' // is it a reply from VMWare?  
      setz   [rc] // set return value  
  
      pop    ebx  
      pop    ecx  
      pop    edx  
    }  
  }  
  __except(EXCEPTION_EXECUTE_HANDLER)  
  {  
    rc = false;  
  }  
  
  return rc;  
}  
  
/* 
 
// pure ASM version of IsInsideVPC() 
__declspec(naked) bool IsInsideVPC_asm() 
{ 
  __asm 
  { 
    push ebp 
    mov  ebp, esp 
 
    mov  ecx, offset exception_handler 
 
    push ebx 
    push ecx 
 
    push dword ptr fs:[0] 
    mov  dword ptr fs:[0], esp 
 
    mov  ebx, 0 // Flag 
    mov  eax, 1 // VPC function number 
  } 
 
    // call VPC  
   _asm __emit 0Fh 
   _asm __emit 3Fh 
   _asm __emit 07h 
   _asm __emit 0Bh 
 
  _asm 
  { 
    mov eax, dword ptr ss:[esp] 
    mov dword ptr fs:[0], eax 
 
    add esp, 8 
 
    test ebx, ebx 
     
    setz al 
 
    lea esp, dword ptr ss:[ebp-4] 
    mov ebx, dword ptr ss:[esp] 
    mov ebp, dword ptr ss:[esp+4] 
 
    add esp, 8 
 
    jmp ret1 
   exception_handler: 
    mov ecx, [esp+0Ch] 
    mov dword ptr [ecx+0A4h], -1 // EBX = -1 -> not running, ebx = 0 -> running 
    add dword ptr [ecx+0B8h], 4 // -> skip past the call to VPC 
    xor eax, eax // exception is handled 
    ret 
   ret1: 
    ret 
  } 
} 
 
 
bool IsInsideVMWare_() 
{ 
  bool r; 
  _asm 
  { 
    push   edx 
    push   ecx 
    push   ebx 
 
    mov    eax, 'VMXh' 
    mov    ebx, 0 // any value but MAGIC VALUE 
    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' // is it a reply from VMWare? 
    setz   [r] // set return value 
 
    pop    ebx 
    pop    ecx 
    pop    edx 
  } 
  return r; 
} 
 
bool IsInsideVMWare() 
{ 
  __try 
  { 
    return IsInsideVMWare_(); 
  } 
  __except(1) // 1 = EXCEPTION_EXECUTE_HANDLER 
  { 
    return false; 
  } 
} 
*/  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Win10 VPC (Windows 10 虚拟机)是在Windows 10操作系统上创建的一种虚拟环境。虚拟机是一种模拟计算机操作的软件技术,它可以在一台计算机上运行多个操作系统,并且每个操作系统都有自己的独立环境和资源。 Win10 VPC虚拟机的使用可以带来许多好处。首先,它允许用户在同一台计算机上同时运行多个操作系统,例如在Windows 10上运行Linux或其他Windows版本。这使得用户能够在一个系统内同时使用多个操作系统的功能和应用程序,而不需要购买和维护额外的硬件设备。 其次,Win10 VPC虚拟机提供了一个安全的环境,用户可以在其中尝试新的操作系统、应用程序或配置设置,而不会影响主机操作系统。这使得用户可以随意进行实验和测试,而不必担心对主机系统造成损坏或不稳定。 此外,Win10 VPC虚拟机还提供了灵活性和便捷性。用户可以轻松地创建、启动和关闭虚拟机,以及配置虚拟机的硬件资源和网络连接。这意味着用户可以根据实际需求调整虚拟机的性能和运行方式,从而最大程度地提高效率和工作效果。 总而言之,Win10 VPC虚拟机为用户提供了运行多个操作系统的方便方式,并且具有安全性、灵活性和便捷性。无论是为了开发和测试软件,还是为了与不同操作系统的应用程序和工具进行兼容,Win10 VPC虚拟机都是一种极其有用的技术。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值