[轉貼] DetectingVM

  1. program DetectingVM;

  2. {$APPTYPE CONSOLE}

  3. uses
  4.   SysUtils;

  5. function IsRunInVirtual:boolean;
  6. function IsRunningVirtualPC: boolean;
  7. asm
  8.   push ebp;
  9.   mov ebp, esp;

  10.   mov ecx, offset @exception_handler;

  11.   push ebx;
  12.   push ecx;

  13.   push dword ptr fs:[0];
  14.   mov dword ptr fs:[0], esp;

  15.   mov ebx, 0; // Flag
  16.   mov eax, 1; // VPC function number

  17.   // call VPC
  18.   db $0F, $3F, $07, $0B

  19.   mov eax, dword ptr ss:[esp];
  20.   mov dword ptr fs:[0], eax;

  21.   add esp, 8;

  22.   test ebx, ebx;

  23.   setz al;

  24.   lea esp, dword ptr ss:[ebp-4];
  25.   mov ebx, dword ptr ss:[esp];
  26.   mov ebp, dword ptr ss:[esp+4];

  27.   add esp, 8;

  28.   jmp @ret1;

  29. @exception_handler:
  30.   mov ecx, [esp+0Ch];
  31.   mov dword ptr [ecx+0A4h], -1; // EBX = -1 ->; not running, ebx = 0 -> running
  32.   add dword ptr [ecx+0B8h], 4;  // ->; skip past the call to VPC
  33.   xor eax, eax;                 // exception is handled

  34. @ret1:
  35. end;
  36. var a, b:cardinal;
  37. begin
  38. a:=0;
  39. try
  40.    asm
  41.       push eax
  42.       push ebx
  43.       push ecx
  44.       push edx

  45.       mov eax, 'VMXh'
  46.       mov ecx, 0Ah
  47.       mov dx, 'VX'

  48.       in eax, dx

  49.       mov a, ebx
  50.       mov b, ecx

  51.       pop edx
  52.       pop ecx
  53.       pop ebx
  54.       pop eax
  55.    end;
  56. except
  57. end;
  58. result:=(a=$564D5868) or IsRunningVirtualPC;
  59. end;
  60. begin
  61.   writeln(isRunInVirtual);
  62.   readln;
  63.   { TODO -oUser -cConsole Main : Insert code here }
  64. end.
複製代碼