forked from tithomas1/vdhh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy_paste.c
233 lines (194 loc) · 5.6 KB
/
copy_paste.c
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
/*
* Copyright (C) 2016 Veertu Inc,
*
* 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 or
* (at your option) version 3 of the License.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/io.h>
typedef uint64_t UINT64;
typedef uint32_t UINT32;
typedef uint8_t UINT8;
typedef uint32_t DWORD;
typedef int BOOL;
char gbuffer[4096 * 1024];
void SetPortVal(uint32_t port, uint32_t val, int size)
{
outl(val, port);
}
void GetPortVal(uint32_t port, uint32_t *val, int size)
{
*val = (uint32_t)inl(port);
}
BOOL SyncVMXPageNoSet(UINT64 page_addr, DWORD command)
{
DWORD val;
SetPortVal(0x1854, command, 4);
GetPortVal(0x1854, &val, 4);
if (val)
return 0;
return 1;
}
BOOL SyncVMXPage(UINT64 page_addr, DWORD command)
{
DWORD val;
memset((void *)page_addr, 0, 4096);
SetPortVal(0x1854, command, 4);
GetPortVal(0x1854, &val, 4);
if (val)
return 0;
return 1;
}
BOOL SyncVMXPageVal(UINT64 page_addr, DWORD command, DWORD nval)
{
DWORD val;
memset((void *)page_addr, 0, 4096);
*(UINT32*)page_addr = nval;
SetPortVal(0x1854, command, 4);
GetPortVal(0x1854, &val, 4);
if (val)
return 0;
return 1;
}
static UINT32 VMX_MIN(UINT32 a, UINT32 b)
{
if (a > b)
return b;
return a;
}
int grab_sync = 0;
int ungrab_sync = 0;
int need_to_copy = 0;
int copy_index = 0;
int copyed = 0;
int CopyFromVmxFirst(UINT64 gva)
{
int size;
UINT32 *ptr;
SyncVMXPage(gva, 4);
ptr = (UINT32 *)gva;
grab_sync = ptr[1];
ungrab_sync = ptr[2];
if (grab_sync) {
size = (int)ptr[3];
need_to_copy = size - VMX_MIN(size, 4096 - 1024);
copyed = copy_index = VMX_MIN(size, 4096 - 1024);
} else {
need_to_copy = copyed = copy_index = 0;
}
return copyed;
}
int CopyFromVmxSeconed(UINT64 gva)
{
UINT32 *ptr;
SyncVMXPageVal(gva, 5, copy_index);
ptr = (UINT32 *)gva;
copyed = VMX_MIN(need_to_copy, 4096);
need_to_copy -= copyed;
copy_index += copyed;
return copyed;
}
int CopyToVmxFirst(UINT64 gva, UINT8 *buffer, int size)
{
UINT32 *ptr = (UINT32 *)gva;
ptr[0] = size;
copyed = 0;
memcpy((void *)(gva + 1024), buffer, VMX_MIN(size, 4096 - 1024));
if (SyncVMXPageNoSet(gva, 6)) {
copyed = copy_index = VMX_MIN(size, 4096 - 1024);
need_to_copy = size - copyed;
}
return copyed;
}
int CopyToVmxSeconed(UINT64 gva, UINT8 *buffer)
{
copyed = 0;
memcpy((void *)gva, buffer + copy_index, VMX_MIN(need_to_copy, 4096));
if (SyncVMXPageNoSet(gva, 7)) {
copyed = VMX_MIN(need_to_copy, 4096);
copy_index += copyed;
need_to_copy -= copyed;
}
return copyed;
}
void SyncVM(UINT64 gva)
{
UINT32 *ptr = (UINT32 *)gva;
while (1)
{
CopyFromVmxFirst(gva);
if (grab_sync) {
char *cpy_ptr;
char *hMem = malloc(ptr[3] + 1024);
strcpy(hMem, "echo \"");
cpy_ptr = hMem + strlen(hMem);
memcpy(cpy_ptr, (char *)gva + 1024, copyed);
if (need_to_copy) {
cpy_ptr = (char *)(((UINT8 *)cpy_ptr) + copyed);
while (need_to_copy) {
if (CopyFromVmxSeconed(gva)) {
memcpy(cpy_ptr, (char *)gva, copyed);
cpy_ptr = (char *)(((UINT8 *)cpy_ptr) + copyed);
}
}
}
strcat(hMem ,"\" | xclip -selection clipboard");
system(hMem);
} else if (ungrab_sync) {
if (1) {
int x = 0;
FILE *file = popen("xclip -out", "r");
if (gbuffer) {
while (!feof(file) && x < (4096 * 1024 - 1))
gbuffer[x++] = fgetc(file);
gbuffer[x-1] = '\0';
if (CopyToVmxFirst(gva, (UINT8 *)gbuffer, strlen(gbuffer) + 1)) {
while (need_to_copy) {
CopyToVmxSeconed(gva, (UINT8 *)gbuffer);
}
}
}
pclose(file);
}
ungrab_sync = 0;
}
usleep(50000);
}
}
UINT64 AllocRegisterVMXPage()
{
void *ptr;
UINT64 val;
ptr = aligned_alloc(4096, 4096);
if (!ptr)
exit(1);
val = (UINT64)ptr;
SetPortVal(0x1854, 2, 4);
SetPortVal(0x1854, (UINT32)val, 4);
SetPortVal(0x1854, (UINT32)(val >> 32), 4);
return (UINT64)ptr;
}
int main()
{
uint64_t gva;
setuid(0);
if (ioperm(0x1854, 4, 1)) {perror("ioperm"); exit(1);}
gva = AllocRegisterVMXPage();
SyncVM(gva);
/* We don't need the ports anymore */
if (ioperm(0x1854, 4, 0)) {perror("ioperm"); exit(1);}
exit(0);
}