-
Notifications
You must be signed in to change notification settings - Fork 346
/
vmcontrol.py
executable file
·566 lines (434 loc) · 18.1 KB
/
vmcontrol.py
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#!/usr/bin/python
#!c:\\python\\python.exe # noqa: E265
import getopt
import os
import sys
import time
from boofuzz import pedrpc
if os.name != "nt":
print("[!] This only works on windows!")
sys.exit(1)
try:
# noinspection PyUnresolvedReferences
from win32api import GetShortPathName # pytype: disable=import-error
# noinspection PyUnresolvedReferences
from win32com.shell import shell # pytype: disable=import-error
except ImportError:
if os.name == "nt":
print("[!] Failed to import win32api/win32com modules, please install these! Bailing...")
sys.exit(1)
PORT = 26003
def err(msg):
return sys.stderr.write("ERR> " + msg + "\n") or sys.exit(1)
USAGE = (
"USAGE: vmcontrol.py"
"\n <-x|--vmx FILENAME|NAME> path to VMX to control or name of VirtualBox image"
"\n <-r|--vmrun FILENAME> path to vmrun.exe or VBoxManage"
"\n <-s|--snapshot NAME> set the snapshot name"
"\n [-l|--log_level LEVEL] log level (default 1), increase for more verbosity"
"\n [-i|--interactive] Interactive mode, prompts for input values"
"\n [--port PORT] TCP port to bind this agent to"
"\n [--vbox] control an Oracle VirtualBox VM"
)
class VMControlPedrpcServer(pedrpc.Server):
def __init__(self, host, port, vmrun, vmx, snap_name=None, log_level=1, interactive=False):
"""
@type host: str
@param host: Hostname or IP address to bind server to
@type port: int
@param port: Port to bind server to
@type vmrun: str
@param vmrun: Path to VMWare vmrun.exe
@type vmx: str
@param vmx: Path to VMX file
@type snap_name: str
@param snap_name: (Optional, def=None) Snapshot name to revert to on restart
@type log_level: int
@param log_level: (Optional, def=1) Log output level, increase for more verbosity
@type interactive: bool
@param interactive: (Option, def=False) Interactive mode, prompts for input values
"""
# initialize the PED-RPC server.
pedrpc.Server.__init__(self, host, port)
self.host = host
self.port = port
self.interactive = interactive
if interactive:
print("[*] Entering interactive mode...")
# get vmrun path
try:
while 1:
print("[*] Please browse to the folder containing vmrun.exe...")
pidl, disp, imglist = shell.SHBrowseForFolder(
0, None, "Please browse to the folder containing vmrun.exe:"
)
fullpath = shell.SHGetPathFromIDList(pidl)
file_list = os.listdir(fullpath)
if "vmrun.exe" not in file_list:
print("[!] vmrun.exe not found in selected folder, please try again")
else:
vmrun = fullpath + "\\vmrun.exe"
print("[*] Using %s" % vmrun)
break
except Exception:
print("[!] Error while trying to find vmrun.exe. Try again without -i.")
sys.exit(1)
# get vmx path
try:
while 1:
print("[*] Please browse to the folder containing the .vmx file...")
pidl, disp, imglist = shell.SHBrowseForFolder(
0, None, "Please browse to the folder containing the .vmx file:"
)
fullpath = shell.SHGetPathFromIDList(pidl)
file_list = os.listdir(fullpath)
exists = False
for filename in file_list:
idx = filename.find(".vmx")
if idx == len(filename) - 4:
exists = True
vmx = fullpath + "\\" + filename
print("[*] Using %s" % vmx)
if exists:
break
else:
print("[!] No .vmx file found in the selected folder, please try again")
except Exception:
print("[!] Error while trying to find the .vmx file. Try again without -i.")
sys.exit(1)
# Grab snapshot name and log level if we're in interactive mode
if interactive:
snap_name = input("[*] Please enter the snapshot name: ")
log_level = input("[*] Please enter the log level (default 1): ")
if log_level:
log_level = int(log_level)
else:
log_level = 1
# if we're on windows, get the DOS path names
if os.name == "nt":
self.vmrun = GetShortPathName(r"%s" % vmrun)
self.vmx = GetShortPathName(r"%s" % vmx)
else:
self.vmrun = vmrun
self.vmx = vmx
self.snap_name = snap_name
self.log_level = log_level
self.interactive = interactive
self.log("VMControl PED-RPC server initialized:")
self.log("\t vmrun: %s" % self.vmrun)
self.log("\t vmx: %s" % self.vmx)
self.log("\t snap name: %s" % self.snap_name)
self.log("\t log level: %d" % self.log_level)
self.log("Awaiting requests...")
# noinspection PyMethodMayBeStatic
def alive(self):
"""
Returns True. Useful for PED-RPC clients who want to see if the PED-RPC connection is still alive.
"""
return True
def log(self, msg="", level=1):
"""
If the supplied message falls under the current log level, print the specified message to screen.
@type msg: str
@param msg: Message to log
"""
if self.log_level >= level:
print("[%s] %s" % (time.strftime("%I:%M.%S"), msg))
def set_vmrun(self, vmrun):
self.log("setting vmrun to %s" % vmrun, 2)
self.vmrun = vmrun
def set_vmx(self, vmx):
self.log("setting vmx to %s" % vmx, 2)
self.vmx = vmx
def set_snap_name(self, snap_name):
self.log("setting snap_name to %s" % snap_name, 2)
self.snap_name = snap_name
def vmcommand(self, command):
"""
Execute the specified command, keep trying in the event of a failure.
@type command: str
@param command: VMRun command to execute
"""
out = None
while 1:
self.log("executing: %s" % command, 5)
pipe = os.popen(command)
out = pipe.readlines()
try:
pipe.close()
except IOError:
self.log("IOError trying to close pipe")
if not out:
break
elif not out[0].lower().startswith("close failed"):
break
self.log("failed executing command '%s' (%s). will try again." % (command, out))
time.sleep(1)
return "".join(out)
def delete_snapshot(self, snap_name=None):
if not snap_name:
snap_name = self.snap_name
self.log("deleting snapshot: %s" % snap_name, 2)
command = self.vmrun + " deleteSnapshot " + self.vmx + " " + '"' + snap_name + '"'
return self.vmcommand(command)
def list(self):
self.log("listing running images", 2)
command = self.vmrun + " list"
return self.vmcommand(command)
def list_snapshots(self):
self.log("listing snapshots", 2)
command = self.vmrun + " listSnapshots " + self.vmx
return self.vmcommand(command)
def reset(self):
self.log("resetting image", 2)
command = self.vmrun + " reset " + self.vmx
return self.vmcommand(command)
def revert_to_snapshot(self, snap_name=None):
if not snap_name:
snap_name = self.snap_name
self.log("reverting to snapshot: %s" % snap_name, 2)
command = self.vmrun + " revertToSnapshot " + self.vmx + " " + '"' + snap_name + '"'
return self.vmcommand(command)
def snapshot(self, snap_name=None):
if not snap_name:
snap_name = self.snap_name
self.log("taking snapshot: %s" % snap_name, 2)
command = self.vmrun + " snapshot " + self.vmx + " " + '"' + snap_name + '"'
return self.vmcommand(command)
def start(self):
self.log("starting image", 2)
command = self.vmrun + " start " + self.vmx
return self.vmcommand(command)
def stop(self):
self.log("stopping image", 2)
command = self.vmrun + " stop " + self.vmx
return self.vmcommand(command)
def suspend(self):
self.log("suspending image", 2)
command = self.vmrun + " suspend " + self.vmx
return self.vmcommand(command)
def restart_target(self):
self.log("restarting virtual machine...")
# revert to the specified snapshot and start the image.
self.revert_to_snapshot()
self.start()
# wait for the snapshot to come alive.
self.wait()
def is_target_running(self):
# sometimes vmrun reports that the VM is up while it's still reverting.
time.sleep(10)
for line in self.list().lower().split("\n"):
if os.name == "nt":
try:
line = GetShortPathName(line)
# skip invalid paths.
except Exception:
continue
if self.vmx.lower() == line.lower():
return True
return False
def wait(self):
self.log("waiting for vmx to come up: %s" % self.vmx)
while 1:
if self.is_target_running():
break
class VBoxControlPedrpcServer(VMControlPedrpcServer):
def __init__(self, host, port, vmrun, vmx, snap_name=None, log_level=1, interactive=False):
"""
Controls an Oracle VirtualBox Virtual Machine
@type host: str
@param host: Hostname or IP address to bind server to
@type port: int
@param port: Port to bind server to
@type vmrun: str
@param vmrun: Path to VBoxManage
@type vmx: str
@param vmx: Name of the virtualbox VM to control (no quotes)
@type snap_name: str
@param snap_name: (Optional, def=None) Snapshot name to revert to on restart
@type log_level: int
@param log_level: (Optional, def=1) Log output level, increase for more verbosity
@type interactive: bool
@param interactive: (Option, def=False) Interactive mode, prompts for input values
"""
# initialize the PED-RPC server.
pedrpc.Server.__init__(self, host, port)
self.host = host
self.port = port
self.interactive = interactive
if interactive:
print("[*] Entering interactive mode...")
# get vmrun path
try:
while 1:
print("[*] Please browse to the folder containing VBoxManage.exe...")
pidl, disp, imglist = shell.SHBrowseForFolder(
0, None, "Please browse to the folder containing VBoxManage.exe"
)
fullpath = shell.SHGetPathFromIDList(pidl)
file_list = os.listdir(fullpath)
if "VBoxManage.exe" not in file_list:
print("[!] VBoxManage.exe not found in selected folder, please try again")
else:
vmrun = fullpath + "\\VBoxManage.exe"
print("[*] Using %s" % vmrun)
break
except Exception:
print("[!] Error while trying to find VBoxManage.exe. Try again without -I.")
sys.exit(1)
# Grab vmx, snapshot name and log level if we're in interactive mode
if interactive:
vmx = input("[*] Please enter the VirtualBox virtual machine name: ")
snap_name = input("[*] Please enter the snapshot name: ")
log_level = input("[*] Please enter the log level (default 1): ")
if log_level:
log_level = int(log_level)
else:
log_level = 1
# if we're on windows, get the DOS path names
if os.name == "nt":
self.vmrun = GetShortPathName(r"%s" % vmrun)
self.vmx = GetShortPathName(r"%s" % vmx)
else:
self.vmrun = vmrun
self.vmx = vmx
self.snap_name = snap_name
self.log_level = log_level
self.interactive = interactive
self.log("VirtualBox PED-RPC server initialized:")
self.log("\t vboxmanage: %s" % self.vmrun)
self.log("\t machine name: %s" % self.vmx)
self.log("\t snap name: %s" % self.snap_name)
self.log("\t log level: %d" % self.log_level)
self.log("Awaiting requests...")
def delete_snapshot(self, snap_name=None):
if not snap_name:
snap_name = self.snap_name
self.log("deleting snapshot: %s" % snap_name, 2)
command = self.vmrun + " snapshot " + self.vmx + " delete " + snap_name + '"'
return self.vmcommand(command)
def list(self):
self.log("listing running images", 2)
command = self.vmrun + " list runningvms"
return self.vmcommand(command)
def list_snapshots(self):
self.log("listing snapshots", 2)
command = self.vmrun + " snapshot " + self.vmx + " list"
return self.vmcommand(command)
def reset(self):
self.log("resetting image", 2)
command = self.vmrun + " controlvm " + self.vmx + " reset"
return self.vmcommand(command)
def pause(self):
self.log("pausing image", 2)
command = self.vmrun + " controlvm " + self.vmx + " pause"
return self.vmcommand(command)
def resume(self):
self.log("resuming image", 2)
command = self.vmrun + " controlvm " + self.vmx + " resume"
return self.vmcommand(command)
def revert_to_snapshot(self, snap_name=None):
if not snap_name:
snap_name = self.snap_name
# VirtualBox flips out if you try to do this with a running VM
if self.is_target_running():
self.stop()
self.log("reverting to snapshot: %s" % snap_name, 2)
command = self.vmrun + " snapshot " + self.vmx + " restore " + snap_name
return self.vmcommand(command)
def snapshot(self, snap_name=None):
if not snap_name:
snap_name = self.snap_name
# VirtualBox flips out if you try to do this with a running VM
if self.is_target_running():
self.pause()
self.log("taking snapshot: %s" % snap_name, 2)
command = self.vmrun + " snapshot " + self.vmx + " take " + snap_name
return self.vmcommand(command)
def start(self):
self.log("starting image", 2)
command = self.vmrun + " startvm " + self.vmx
# TODO: we may want to do more here with headless, gui, etc...
return self.vmcommand(command)
def stop(self):
self.log("stopping image", 2)
command = self.vmrun + " controlvm " + self.vmx + " poweroff"
return self.vmcommand(command)
def suspend(self):
self.log("suspending image", 2)
command = self.vmrun + " controlvm " + self.vmx + " pause"
return self.vmcommand(command)
# added a function here to get vminfo... useful for parsing stuff out later
def get_vminfo(self):
self.log("getting vminfo", 2)
command = self.vmrun + " showvminfo " + self.vmx + " --machinereadable"
return self.vmcommand(command)
def restart_target(self):
self.log("restarting virtual machine...")
# VirtualBox flips out if you try to do this with a running VM
if self.is_target_running():
self.stop()
# revert to the specified snapshot and start the image.
self.revert_to_snapshot()
self.start()
# wait for the snapshot to come alive.
self.wait()
def is_target_running(self):
# sometimes vmrun reports that the VM is up while it's still reverting.
time.sleep(10)
for line in self.get_vminfo().split("\n"):
if line == 'VMState="running"':
return True
return False
def is_target_paused(self):
time.sleep(10)
for line in self.get_vminfo().split("\n"):
if line == 'VMState="paused"':
return True
return False
if __name__ == "__main__":
opts = None
vmrun_arg = None
vmx_arg = None
snap_name_arg = None
log_level_arg = 1
interactive_arg = False
virtualbox_arg = False
port_arg = None
# parse command line options.
try:
opts, args = getopt.getopt(
sys.argv[1:], "x:r:s:l:i", ["vmx=", "vmrun=", "snapshot=", "log_level=", "interactive", "port=", "vbox"]
)
except getopt.GetoptError:
err(USAGE)
for opt, arg in opts:
if opt in ("-x", "--vmx"):
vmx_arg = arg
if opt in ("-r", "--vmrun"):
vmrun_arg = arg
if opt in ("-s", "--snapshot"):
snap_name_arg = arg
if opt in ("-l", "--log_level"):
log_level_arg = int(arg)
if opt in ("-i", "--interactive"):
interactive_arg = True
if opt in ("-p", "--port"):
port_arg = int(arg)
if opt in ("-v", "--vbox"):
virtualbox_arg = True
# OS check
if interactive_arg and not os.name == "nt":
print("[!] Interactive mode currently only works on Windows operating systems.")
err(USAGE)
if (not vmx_arg or not vmrun_arg or not snap_name_arg) and not interactive_arg:
err(USAGE)
if not virtualbox_arg:
servlet = VMControlPedrpcServer(
"0.0.0.0", port_arg, vmrun_arg, vmx_arg, snap_name_arg, log_level_arg, interactive_arg
)
else:
servlet = VBoxControlPedrpcServer(
"0.0.0.0", port_arg, vmrun_arg, vmx_arg, snap_name_arg, log_level_arg, interactive_arg
)
servlet.serve_forever()