From 9c2baa5e85d1e11be3f46e8756259663d8e070e3 Mon Sep 17 00:00:00 2001 From: Michael Gebetsroither Date: Fri, 9 Mar 2007 17:56:20 +0100 Subject: [PATCH] vmware-detect.c: implemented io-ports based check --- compile/vmware-detect.c | 102 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 94 insertions(+), 8 deletions(-) diff --git a/compile/vmware-detect.c b/compile/vmware-detect.c index f3875a7..4914c8e 100644 --- a/compile/vmware-detect.c +++ b/compile/vmware-detect.c @@ -1,24 +1,110 @@ +#include "string.h" +#include "unistd.h" +#include "stdio.h" +#include "stdlib.h" + +#define WRITE(x) write(1, x, strlen(x)) +#define DWRITE(x) do{ \ + if(debug) { \ + WRITE(x); \ + } \ +} while(0); +#define FALSE 0 +#define TRUE !FALSE + +// from libowfat {{{ +static inline char tohex(char c) { + return c>=10?c-10+'a':c+'0'; +} + +unsigned int fmt_xlong(char *dest,unsigned long i) { + register unsigned long len,tmp; + /* first count the number of bytes needed */ + for (len=1, tmp=i; tmp>15; ++len) tmp>>=4; + if (dest) + for (tmp=i, dest+=len; ; ) { + *--dest = tohex(tmp&15); + if (!(tmp>>=4)) break; + } + return len; +} +// }}} + +void printIdtr(const unsigned char* idtr, unsigned size) +{ + unsigned i; + for(i=0; i<=size; ++i) { + char out[4] = {0}; + fmt_xlong(out, idtr[i]); + WRITE(out); + } + WRITE("\n"); +} + #if defined (__i386__) -int checkVmware() +int checkVmware(const int debug) { - unsigned char idtr[6]; + unsigned char idtr[6] = {0}; asm("sidt %0" : "=m" (idtr)); - return (0xff==idtr[5]) ? 0 : 1; + if(debug) + printIdtr(idtr, 6); + return (0xff==idtr[5]) ? 1 : 0; +} +int checkVmwareIO() +{ + unsigned int vmaj, vmin, magic, dout; + __asm__ __volatile__( + "mov $0x564D5868, %%eax; /* magic number */" + "mov $0x3c6cf712, %%ebx; /* random number */" + "mov $0x0000000A, %%ecx; /* specifies command */" + "mov $0x5658, %%edx; /* VMware I/O port */" + "in %%dx, %%eax;" + "mov %%eax, %0;" + "mov %%ebx, %1;" + "mov %%ecx, %2;" + "mov %%edx, %3;" + : "=r"(vmaj), "=r"(magic), "=r"(vmin), "=r"(dout)); + return (0x564D5868 == magic) ? 1 : 0; } #elif defined (__x86_64__) // only guessed, possible need to check against 0xffff? -int checkVmware() +int checkVmware(const int debug) { unsigned char idtr[10]; asm("sidt %0" : "=m" (idtr)); - return (0xff==idtr[9]) ? 0 : 1; + if(debug) + printIdtr(idtr, 10); + return (0xff==idtr[9]) ? 1 : 0; } #else // vmware runs only on the archs above -int checkVmware() { return 1; } +int checkVmware(const int) { return 0; } #endif -int main() { +int main(int argc, char* argv[]) { + int debug = FALSE; + if(argc == 2 && !strcmp(argv[1], "--debug")) + debug = TRUE; + // returns 0 if running inside vmware, 1 otherwise - return checkVmware(); + int a, b; + a = checkVmware(debug); + DWRITE("idt-check: ") + if(a) { + DWRITE("true\n"); + return 0; + } + DWRITE("false\n"); + + // never returns if not running under vmware + void dummy() { DWRITE("false\n"); exit(1); } + signal(SIGSEGV, dummy); + DWRITE("ioport-check: "); + b = checkVmwareIO(); + if(b) { + DWRITE("true\n"); + return 0; + } + return 1; } +// vim: foldmethod=marker -- 2.1.4