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

来源:互联网 发布:淘宝层级划分 编辑:程序博客网 时间:2024/05/21 18:47

做了个 检测例子在

http://download.csdn.net/detail/thanklife/9852865

————————————————————————————————————————————————————

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



AntiVM.h

[cpp] view plain copy
  1. #ifndef __DETECT_VM__02222005__    
  2. #define __DETECT_VM__02222005__    
  3.     
  4. bool IsVirtualMachine();    
  5. bool IsInsideVPC();    
  6. bool IsInsideVMWare();    
  7.     
  8. #endif    

AntiVM.cpp


[cpp] view plain copy
  1. /* -----------------------------------------------------------------------------   
  2.  * Created by * lallous <lallousx86@yahoo.com> *  
  3.  * All rights reserved.  
  4.  *   
  5.  * Redistribution and use in source and binary forms, with or without  
  6.  * modification, are permitted provided that the following conditions  
  7.  * are met:  
  8.  * 1. Redistributions of source code must retain the above copyright  
  9.  *    notice, this list of conditions and the following disclaimer.  
  10.  *   
  11.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND  
  12.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  
  14.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE  
  15.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  
  16.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  
  17.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  
  18.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  
  19.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  
  20.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  
  21.  * SUCH DAMAGE.  
  22.  * -----------------------------------------------------------------------------   
  23.   
  24.   
  25.  History  
  26.  ---------  
  27.  09/09/2004  - added IsInsideVPC()  
  28.  02/22/2005  - added IsInsideVMWare()  
  29.  03/26/2005  - . added C++ friendy version of IsInsideVPC() and renamed old IsInsideVPC()  
  30.                . rewritten IsInsideVmWare()  
  31.   
  32.   
  33.  Special thanks to Ken Kato <chitchat-lj@infoseek.jp> for his VMWare Backdoor information  
  34.  (http://chitchat.at.infoseek.co.jp/vmware/vmtools.html)  
  35.   
  36. */    
  37.     
  38. #include "AntiVM.h"    
  39. #include <windows.h>    
  40.     
  41. bool IsVirtualMachine()    
  42. {    
  43.     if(IsInsideVPC())    
  44.         return true;    
  45.         
  46.     if(IsInsideVMWare())    
  47.         return true;    
  48.             
  49.     return false;    
  50. }    
  51.     
  52. // IsInsideVPC's exception filter    
  53. DWORD __forceinline IsInsideVPC_exceptionFilter(LPEXCEPTION_POINTERS ep)    
  54. {    
  55.   PCONTEXT ctx = ep->ContextRecord;    
  56.     
  57.   ctx->Ebx = -1; // Not running VPC    
  58.   ctx->Eip += 4; // skip past the "call VPC" opcodes    
  59.   return EXCEPTION_CONTINUE_EXECUTION; // we can safely resume execution since we skipped faulty instruction    
  60. }    
  61.     
  62. // high level language friendly version of IsInsideVPC()    
  63. bool IsInsideVPC()    
  64. {    
  65.   bool rc = false;    
  66.     
  67.   __try    
  68.   {    
  69.     _asm push ebx    
  70.     _asm mov  ebx, 0 // Flag    
  71.     _asm mov  eax, 1 // VPC function number    
  72.     
  73.     // call VPC     
  74.     _asm __emit 0Fh    
  75.     _asm __emit 3Fh    
  76.     _asm __emit 07h    
  77.     _asm __emit 0Bh    
  78.     
  79.     _asm test ebx, ebx    
  80.     _asm setz [rc]    
  81.     _asm pop ebx    
  82.   }    
  83.   // The except block shouldn't get triggered if VPC is running!!    
  84.   __except(IsInsideVPC_exceptionFilter(GetExceptionInformation()))    
  85.   {    
  86.   }    
  87.     
  88.   return rc;    
  89. }    
  90.     
  91. bool IsInsideVMWare()    
  92. {    
  93.   bool rc = true;    
  94.     
  95.   __try    
  96.   {    
  97.     __asm    
  98.     {    
  99.       push   edx    
  100.       push   ecx    
  101.       push   ebx    
  102.     
  103.       mov    eax, 'VMXh'    
  104.       mov    ebx, 0 // any value but not the MAGIC VALUE    
  105.       mov    ecx, 10 // get VMWare version    
  106.       mov    edx, 'VX' // port number    
  107.     
  108.       in     eax, dx // read port    
  109.                      // on return EAX returns the VERSION    
  110.       cmp    ebx, 'VMXh' // is it a reply from VMWare?    
  111.       setz   [rc] // set return value    
  112.     
  113.       pop    ebx    
  114.       pop    ecx    
  115.       pop    edx    
  116.     }    
  117.   }    
  118.   __except(EXCEPTION_EXECUTE_HANDLER)    
  119.   {    
  120.     rc = false;    
  121.   }    
  122.     
  123.   return rc;    
  124. }    
  125.     
  126. /*  
  127.   
  128. // pure ASM version of IsInsideVPC()  
  129. __declspec(naked) bool IsInsideVPC_asm()  
  130.  
  131.   __asm  
  132.   {  
  133.     push ebp  
  134.     mov  ebp, esp  
  135.   
  136.     mov  ecx, offset exception_handler  
  137.   
  138.     push ebx  
  139.     push ecx  
  140.   
  141.     push dword ptr fs:[0]  
  142.     mov  dword ptr fs:[0], esp  
  143.   
  144.     mov  ebx, 0 // Flag  
  145.     mov  eax, 1 // VPC function number  
  146.   }  
  147.   
  148.     // call VPC   
  149.    _asm __emit 0Fh  
  150.    _asm __emit 3Fh  
  151.    _asm __emit 07h  
  152.    _asm __emit 0Bh  
  153.   
  154.   _asm  
  155.   {  
  156.     mov eax, dword ptr ss:[esp]  
  157.     mov dword ptr fs:[0], eax  
  158.   
  159.     add esp, 8  
  160.   
  161.     test ebx, ebx  
  162.       
  163.     setz al  
  164.   
  165.     lea esp, dword ptr ss:[ebp-4]  
  166.     mov ebx, dword ptr ss:[esp]  
  167.     mov ebp, dword ptr ss:[esp+4]  
  168.   
  169.     add esp, 8  
  170.   
  171.     jmp ret1  
  172.    exception_handler:  
  173.     mov ecx, [esp+0Ch]  
  174.     mov dword ptr [ecx+0A4h], -1 // EBX = -1 -> not running, ebx = 0 -> running  
  175.     add dword ptr [ecx+0B8h], 4 // -> skip past the call to VPC  
  176.     xor eax, eax // exception is handled  
  177.     ret  
  178.    ret1:  
  179.     ret  
  180.   }  
  181.  
  182.   
  183.   
  184. bool IsInsideVMWare_()  
  185.  
  186.   bool r;  
  187.   _asm  
  188.   {  
  189.     push   edx  
  190.     push   ecx  
  191.     push   ebx  
  192.   
  193.     mov    eax, 'VMXh'  
  194.     mov    ebx, 0 // any value but MAGIC VALUE  
  195.     mov    ecx, 10 // get VMWare version  
  196.     mov    edx, 'VX' // port number  
  197.     in     eax, dx // read port  
  198.                    // on return EAX returns the VERSION  
  199.     cmp    ebx, 'VMXh' // is it a reply from VMWare?  
  200.     setz   [r] // set return value  
  201.   
  202.     pop    ebx  
  203.     pop    ecx  
  204.     pop    edx  
  205.   }  
  206.   return r;  
  207.  
  208.   
  209. bool IsInsideVMWare()  
  210.  
  211.   __try  
  212.   {  
  213.     return IsInsideVMWare_();  
  214.   }  
  215.   __except(1) // 1 = EXCEPTION_EXECUTE_HANDLER  
  216.   {  
  217.     return false;  
  218.   }  
  219.  
  220. */    
原创粉丝点击