Click here to Skip to main content
11,412,833 members (76,179 online)
Click here to Skip to main content

Detect if your program is running inside a Virtual Machine

, 4 Apr 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
An article showing how you can programmatically check if your code is running inside a virtual machine (such as VPC or VMWare).

Sample Image

Introduction

This article will demonstrate how an application can detect if it is being run from inside a virtual machine software.

The code in this article will detect two well known machine virtualization software:

  • Microsoft's Virtual PC (formally from Connectix).
  • VMWare from VMWare.com

Other virtual machine software such as Bochs or Plex86 are not covered in this article.

It is best that the readers have a general idea about the Intel x86 assembly language to better understand how the code works, however I will do my best to explain the techniques in layman's terms.

Please note that whenever I use the term "Virtual Machine Software", this means I am referring to a software such as Virtual PC or VMWare. When the term "Virtual Machine" is used, this means the emulated machine, usually running an operating system.

A little about virtual machine software

Virtual machine software are software that emulate a given machine's architecture using software (code) instead of relying on hardware, thus allowing a code to be executed in that virtual machine as if it is being run from a real machine.

Till today, these software are far from being perfect, and emulating a given real machine still poses many challenges due to complexities involved when trying to emulate every component of a given machine.

Both Virtual PC and VMWare allow you to install "add-in"s to accelerate emulation, allow drag-n-drop from your real desktop to your virtual desktop, and allow file sharing between your real machine and the virtual machine.

In order to accomplish this task, a communication mechanism between the virtual machine software and the virtual machine itself must exist.

This sort of interfacing is called a "backdoor interfacing", since, using a special/undocumented mechanism, certain commands can be carried and interpreted in a different manner (by the virtual machine software) unlike having them interpreted by the real machine.

Next, I'll be covering how you can tell whether your software is being executed using a real machine or a virtual machine software (covering both Virtual PC and VMWare).

How to detect Virtual PC

As you may already know, every machine has a defined set of instructions commonly referred to as Instruction Set Architecture (ISA).

When an invalid instruction (that is not present in the ISA) is encountered, the machine raises an exception of the type "Invalid Opcode". The software can either handle the exception (using the usual try/catch mechanism), let the operating system handle the exception, or crash the machine in worst cases.

Virtual PC uses a bunch of invalid instructions to allow the interfacing between the virtual machine and the Virtual PC software.

Here's what happens when Virtual PC's virtual machine wants to talk with Virtual PC:

  1. The program sets exception handlers (try/catch blocks).
  2. Set needed parameters before calling the VM software.
  3. Issue a special "Invalid Opcode" instruction.
  4. VM software will recognize this invalid opcode and act accordingly, causing no exception if VPC was present, and an exception if VPC isn't present.
  5. The program's "catch" block will handle the exception and examine the returned parameters for the presence/absence of VM software.

In short, Virtual PC uses the "Invalid Opcode" mechanism as a backdoor.

The following code shows how to detect Virtual PC's presence:

// 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 // It will stay ZERO if VPC is running
    _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;
}

More details on the code:

  1. Install exception handlers.
  2. Prepare input registers "eax" and "ebx".
  3. Issue invalid instruction 0x0F 0x3F 0x07 0x0B. This invalid instruction is like a function designator, it tells Virtual PC what to do exactly. For other functionality, Virtual PC uses another invalid instruction.
  4. Inside the exception handler -> modify registers so to mark VPC's absence (EBX is set to -1 if exception is triggered -> VPC is absent).
  5. Return from exception and resume execution (only if VPC was absent).
  6. Inspect returned registers accordingly.

How to detect VMWare

The Intel x86 provides two instructions to allow you to carry I/O operations, these instructions are the "IN" and "OUT" instructions. These two instructions are privileged instructions and cannot be used in a user-mode (while in protected mode) process unless the necessary privileges are enabled, so using them in normal cases will cause an exception of the type: "EXCEPTION_PRIV_INSTRUCTION".

VMWare uses the "IN" instruction to read from a special port. This port does not effectively exist, however when VMWare is present, that port will be the interface between the virtual machine and VMWare.

Here's the code:

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;
}
  1. The program sets exception handlers (in case VMWare isn't present, we just discard its presence).
  2. Set up into the EAX register the magic number 0x564D5868 (or 'VMXh').
  3. Set EBX register to any value but the magic number.
  4. Set ECX register to the "function number" value. The value 10 means Get VMWare version, other codes means other functionality.
  5. Set into DX the magic port number 0x5658 (or 'VX'), this special port number allows interfacing with VMWare when it is present.
  6. Read from that port into EAX.
    1. When VMWare is not present, an exception will occur and we discard VMWare's presence.
    2. Otherwise, the code flow continues.
  7. EBX should now read the magic number value.
  8. If so, then VMWare is present.

Using the code

This article comes with a sample GUI written in C#/VB.NET and VC++, allowing you to detect the presence of either VMWare or Virtual PC.

The C++ code uses "DetectVM.cpp/.h" which exports the following functions:

  bool IsInsideVPC();
  bool IsInsideVMWare();

The C#/VB.NET code uses the following read-only properties:

  System::Boolean IsInsideVPC
  System::Boolean IsInsideVMWare

Closing words

Hope you enjoyed and learned from reading this article and using the code. Thanks to CodeProject members for their continuous support and quality articles/code.

Special thanks goes to Mr. Ken Kato, for his work that enabled me to learn about VMWare's backdoor interface.

History

  • 03/14/2005 - Initial version of article packed with only VC++ sample code.
  • 03/30/2005
    • Updated the article's IsInsideVPC explanation.
    • Rewritten both IsInsideVPC() and IsInsideVMWare() using less ASM code.
    • Added .NET samples (thanks to unruledboy for his .NET code contribution).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Elias Bachaalany
Web Developer
United States United States
Elias (aka lallousx86, @0xeb) has always been interested in the making of things and their inner workings.

His computer interests include system programming, reverse engineering, writing libraries, tutorials and articles.

In his free time, and apart from researching, his favorite reading topics include: dreams, metaphysics, philosophy, psychology and any other human/mystical science.

Former employee of Hex-Rays (the creators of IDA Pro), was responsible about many debugger plugins, IDAPython project ownership and what not.

Elias currently works at Microsoft as a software security engineer.

More articles and blog posts can be found here:

- http://lallousx86.wordpress.com/
- http://0xeb.wordpress.com/
- http://www.hexblog.com/?author=3

Comments and Discussions

 
GeneralVery nice Pin
S.A.Esmaeily at 8-Mar-15 22:45
professionalS.A.Esmaeily8-Mar-15 22:45 
QuestionInteresting Pin
jfriedman at 12-Feb-15 15:26
professionaljfriedman12-Feb-15 15:26 
QuestionHow can I detect VM/ VMWare in x64 bit Windows Pin
kalyani.homkar at 2-Sep-14 21:42
memberkalyani.homkar2-Sep-14 21:42 
Questionhow about detecting proxmox ? Pin
bgsjust at 17-Jan-14 13:46
professionalbgsjust17-Jan-14 13:46 
Questionupdated link and correction Pin
Dio PM at 2-Aug-13 3:33
memberDio PM2-Aug-13 3:33 
QuestionESET-NOD32 detects the name IsInsideVPC() function as a variant of "Win32/Packed.VMDetector.A" when exposed from a DLL. [modified] Pin
anony-user at 14-Jul-13 21:46
memberanony-user14-Jul-13 21:46 
QuestionHow to use this code on x64 project ?? Pin
imyoungcheol at 16-Jun-13 22:14
memberimyoungcheol16-Jun-13 22:14 
GeneralMy vote of 5 Pin
filepang at 22-May-13 6:07
memberfilepang22-May-13 6:07 
QuestionCool codes.Furthermore,how to get the vmware version if it's running in vmware? Pin
Eric Yung at 4-Jun-12 18:11
memberEric Yung4-Jun-12 18:11 
GeneralMy vote of 5 Pin
forgodssakejustregister at 1-Jun-12 10:47
memberforgodssakejustregister1-Jun-12 10:47 
GeneralMy vote of 5 Pin
manoj kumar choubey at 24-Feb-12 3:44
membermanoj kumar choubey24-Feb-12 3:44 
QuestionProgram could not detect accurately for 64 bit processor machine Pin
Anthony_Yio at 2-Jan-12 23:27
memberAnthony_Yio2-Jan-12 23:27 
I tried a couple of machines running 64 bit OS (or with 64 bit processor)
It seems like it is not working correctly.
AnswerRe: Program could not detect accurately for 64 bit processor machine Pin
firehawk at 25-Apr-12 18:13
memberfirehawk25-Apr-12 18:13 
BugI Can't Compile the library on windows 7 (x64 bit) Pin
koko07 at 24-Oct-11 4:49
memberkoko0724-Oct-11 4:49 
GeneralRe: I Can't Compile the library on windows 7 (x64 bit) Pin
mdanh2002 at 6-Mar-12 4:25
membermdanh20026-Mar-12 4:25 
GeneralMy vote of 5 Pin
koko07 at 24-Oct-11 4:31
memberkoko0724-Oct-11 4:31 
Generalin case you have Hyper-V Pin
fryezz at 18-May-11 12:42
memberfryezz18-May-11 12:42 
GeneralRe: in case you have Hyper-V Pin
Khurram_riaz at 25-May-11 5:33
memberKhurram_riaz25-May-11 5:33 
GeneralPerfect, thx Pin
Dalroi at 1-Mar-11 7:46
memberDalroi1-Mar-11 7:46 
Generaluseful article Pin
CIDev at 6-Oct-10 6:42
memberCIDev6-Oct-10 6:42 
QuestionCan not run on Windows Server 2008 64Bit. Pin
Member 4429983 at 4-Feb-10 17:38
memberMember 44299834-Feb-10 17:38 
AnswerRe: Can not run on Windows Server 2008 64Bit. Pin
Khurram_riaz at 25-May-11 5:22
memberKhurram_riaz25-May-11 5:22 
GeneralIs it possible to detect Parallels ( Virtual Machine under Mac) Pin
tomasen at 4-Jun-09 8:13
membertomasen4-Jun-09 8:13 
QuestionHow about late binding? Pin
HStrix at 24-Mar-09 8:09
memberHStrix24-Mar-09 8:09 
GeneralFor Virtual PC, try this... Pin
leandrobecker at 28-Jan-09 3:27
memberleandrobecker28-Jan-09 3:27 
QuestionHow about ESXI Pin
Colin Maharaj at 21-Nov-08 13:23
memberColin Maharaj21-Nov-08 13:23 
Questionporting in RHEL (UNIX) [modified] Pin
vikramaditya234 at 17-Nov-08 2:34
membervikramaditya23417-Nov-08 2:34 
QuestionI need to know from API Windows if my appIication is running in a virtual machine or not. Pin
piermario at 1-Sep-08 0:24
memberpiermario1-Sep-08 0:24 
GeneralNot popping registers when exception thrown Pin
LeoMaheo at 18-Jun-08 5:27
memberLeoMaheo18-Jun-08 5:27 
GeneralVB6 Pin
bitpusher1010 at 22-Apr-08 9:30
memberbitpusher101022-Apr-08 9:30 
GeneralRe: VB6 Pin
lallous at 23-Apr-08 3:39
memberlallous23-Apr-08 3:39 
GeneralRe: VB6 Pin
RICARDO MARTINEZ MONJE at 4-Mar-11 2:16
memberRICARDO MARTINEZ MONJE4-Mar-11 2:16 
Generalanother approach... Pin
NikoTanghe at 17-Dec-07 5:42
memberNikoTanghe17-Dec-07 5:42 
GeneralVS 2008 Project Pin
great_scandinavian at 16-Dec-07 11:52
membergreat_scandinavian16-Dec-07 11:52 
GeneralThis is not best practice approach at all Pin
nbk33r1 at 24-Jun-07 18:53
membernbk33r124-Jun-07 18:53 
GeneralRe: This is not best practice approach at all Pin
Yogesh P. Dhakad at 12-Oct-07 23:19
memberYogesh P. Dhakad12-Oct-07 23:19 
GeneralRe: This is not best practice approach at all Pin
cgurtler at 10-Feb-09 10:29
membercgurtler10-Feb-09 10:29 
GeneralRe: This is not best practice approach at all Pin
Elmue at 15-Sep-10 11:38
mvpElmue15-Sep-10 11:38 
GeneralRe: This is not best practice approach at all Pin
Witch_Dr at 18-Jul-11 22:35
memberWitch_Dr18-Jul-11 22:35 
GeneralDetecting number of Virtual PC Pin
Fergal Cassidy at 29-Mar-07 2:16
memberFergal Cassidy29-Mar-07 2:16 
GeneralRe: Detecting number of Virtual PC Pin
lallous at 29-Mar-07 2:31
memberlallous29-Mar-07 2:31 
QuestionVirtualPC asm version problem Pin
J.B. at 22-Aug-06 17:41
memberJ.B.22-Aug-06 17:41 
GeneralAnother wholly different way... Pin
Antoine Leca at 21-Dec-05 4:53
memberAntoine Leca21-Dec-05 4:53 
GeneralRe: Another wholly different way... Pin
lallous at 21-Dec-05 21:46
memberlallous21-Dec-05 21:46 
GeneralRe: Another wholly different way... Pin
Antoine Leca at 25-Dec-05 23:25
memberAntoine Leca25-Dec-05 23:25 
GeneralDoesn't seem to work under VPC7.01 Pin
rssmsvc at 25-Apr-05 10:41
memberrssmsvc25-Apr-05 10:41 
GeneralRe: Doesn't seem to work under VPC7.01 Pin
rssmsvc at 25-Apr-05 20:04
memberrssmsvc25-Apr-05 20:04 
GeneralRe: Doesn't seem to work under VPC7.01 Pin
rssmsvc at 25-Apr-05 20:57
memberrssmsvc25-Apr-05 20:57 
GeneralOf course yes Pin
Maxim Alekseikin at 17-May-06 3:08
memberMaxim Alekseikin17-May-06 3:08 
GeneralRe: Doesn't seem to work under VPC7.01 Pin
rssmsvc at 25-Apr-05 21:04
memberrssmsvc25-Apr-05 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 5 Apr 2005
Article Copyright 2005 by Elias Bachaalany
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid