打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
测试是否在虚拟机里运行
2014-06-30 22:27 23人阅读 评论(0) 收藏 举报

目录(?)[+]

http://www.codeproject.com/Articles/9823/Detect-if-your-program-is-running-inside-a-Virtual

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:

 Collapse | Copy Code
// IsInsideVPC's exception filterDWORD __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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Virtual?PC?2004?完全手册 - virtualpc - 虚拟机之家
vmware下安装ubuntu会产生很多的vmdk文件
虚拟无限--对虚拟机与虚拟化的简单整理 - ||| 54baishi.126.com ||...
梦山资料馆: Oracle - 关于virtual PC下装LINUX的问题
VMware vCenter Converter 4.0 使用图解
微軟提供開發者免費 IE8 至 IE11、Edge 瀏覽器虛擬機映像檔下載
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服