vmware-detect: fixes for amd64
[grml-scripts.git] / compile / vmware-detect.c
1 #include "string.h"
2 #include "unistd.h"
3 #include "stdio.h"
4 #include "stdlib.h"
5 #include "signal.h"
6
7 #define WRITE(x) write(1, x, strlen(x))
8 #define DWRITE(x) do{ \
9     if(debug) { \
10         WRITE(x); \
11     } \
12 } while(0);
13 #define FALSE 0
14 #define TRUE !FALSE
15
16 /* doc:
17  * vmware IO backdoor: http://chitchat.at.infoseek.co.jp/vmware/backdoor.html
18  * http://www.honeynet.org/papers/bots/botnet-code.html
19  * http://www.codegurus.be/codegurus/Programming/virtualpc&vmware_en.htm
20  */
21
22 // from libowfat {{{
23 static inline char tohex(char c) {
24   return c>=10?c-10+'a':c+'0';
25 }
26
27 unsigned int fmt_xlong(char *dest,unsigned long i) {
28   register unsigned long len,tmp;
29   /* first count the number of bytes needed */
30   for (len=1, tmp=i; tmp>15; ++len) tmp>>=4;
31   if (dest)
32     for (tmp=i, dest+=len; ; ) {
33       *--dest = tohex(tmp&15);
34       if (!(tmp>>=4)) break;
35     }
36   return len;
37 }
38 // }}}
39
40 void printIdtr(const unsigned char* idtr, unsigned size)
41 {
42     unsigned i;
43     for(i=0; i<size; ++i) {
44         char out[4] = {0};
45         fmt_xlong(out, idtr[i]);
46         if(strlen(out) == 1)
47             WRITE("0");
48         WRITE(out);
49     }
50     WRITE("\n");
51 }
52
53 // i386 {{{
54 #if defined (__i386__) || defined (__x86_64__)
55 int checkVmware(const int debug)
56 {
57     unsigned char idtr[10] = {0};
58     asm("sidt %0" : "=m" (idtr));
59     if(debug)
60         printIdtr(idtr, sizeof(idtr));
61     // should normally be the case on amd64, but does not work
62     //return (0xff==idtr[9]) ? 1 : 0;
63     return (0xff==idtr[5]) ? 1 : 0;
64 }
65 int checkVmwareIO()
66 {
67     unsigned int vmaj, vmin, magic, dout = 11;
68     __asm__ __volatile__(
69             "mov $0x564D5868, %%eax; /* magic number */"
70             "mov $0x3c6cf712, %%ebx; /* random number */"
71             "mov $0x0000000A, %%ecx; /* specifies command */"
72             "mov $0x5658, %%edx; /* VMware I/O port */"
73             "in %%dx, %%eax;"
74             "mov %%eax, %0;"
75             "mov %%ebx, %1;"
76             "mov %%ecx, %2;"
77             "mov %%edx, %3;"
78         : "=r"(vmaj), "=r"(magic), "=r"(vmin), "=r"(dout));
79 #ifdef DEBUG
80     fprintf(stderr, "version: major=%x, minor=%x, magic=%x, dout=%x\n",
81             vmaj, vmin, magic, dout);
82 #endif
83     return (0x564D5868 == magic) ? 1 : 0;
84 }
85 // }}}
86
87 // others {{{
88 #else
89 // vmware runs only on the archs above
90 int checkVmware(const int) { return 0; }
91 int checkVmwareIO() { return 0; }
92 #endif
93 // }}}
94
95 static int Killed = FALSE;
96
97 // returns 0 if running inside vmware, 1 otherwise
98 int main(int argc, char* argv[]) {
99     int debug = FALSE;
100     if(argc == 2 && !strcmp(argv[1], "--debug"))
101         debug = TRUE;
102
103     int a, b;
104     // known to be false positives
105     a = checkVmware(debug);
106     DWRITE("idt-check: ")
107     if(!a) {
108         DWRITE("false\n");
109         if(!debug)
110             return EXIT_FAILURE;
111     } else
112         DWRITE("true\n");
113
114     // never returns if not running under vmware
115     void dummy() { Killed=TRUE; DWRITE("false\n"); exit(1); }
116     signal(SIGSEGV, dummy);
117     DWRITE("ioport-check: ");
118     b = checkVmwareIO();
119     if(b) {
120         DWRITE("true\n");
121         return EXIT_SUCCESS;
122     } else {
123         if(!Killed) {
124             // check unuseable or not implemented
125             DWRITE("false\n");
126             DWRITE("Check not implemented, yet!\n");
127             return a ? EXIT_SUCCESS : EXIT_FAILURE;
128         } else {
129             // never reached
130             WRITE("Error: IO check hasn't killed the program but no vmware found either!\n");
131             return EXIT_FAILURE;
132         }
133     }
134 }
135 // vim: foldmethod=marker