2 Created on Nov 19, 2013
8 from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
13 from cygwin import Cygwin
14 from environment import Environment
22 from opensecurity_util import logger, setupLogger, OpenSecurityException
26 class VMManagerException(Exception):
27 def __init__(self, value):
30 return repr(self.value)
37 def __init__(self, vendorid, productid, revision):
38 self.vendorid = vendorid.lower()
39 self.productid = productid.lower()
40 self.revision = revision.lower()
43 def __eq__(self, other):
44 return self.vendorid == other.vendorid and self.productid == other.productid and self.revision == other.revision
47 return hash(self.vendorid) ^ hash(self.productid) ^ hash(self.revision)
50 return "VendorId = \'" + str(self.vendorid) + "\' ProductId = \'" + str(self.productid) + "\' Revision = \'" + str(self.revision) + "\'"
52 class VMManager(object):
53 vmRootName = "SecurityDVM"
54 systemProperties = None
55 startNotifications = list()
62 self.systemProperties = self.getSystemProperties()
63 self.machineFolder = self.systemProperties["Default machine folder"]
65 self.attachedRSDs = self.getAttachedRSDs()
66 self.connectedRSDs = self.getConnectedRSDS()
71 if VMManager._instance == None:
72 VMManager._instance = VMManager()
73 return VMManager._instance
76 self.unmapNetworkDrive('G:')
77 self.unmapNetworkDrive('H:')
78 for vm in self.listSDVM():
82 def putStartNotification(self, ip):
83 self.startNotifications.append(ip)
85 def isSDVMStarted(self, ip):
86 return self.startNotifications.contains(ip)
88 # return hosty system properties
89 def getSystemProperties(self):
90 result = Cygwin.vboxExecute('list systemproperties')
93 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
97 # return the folder containing the guest VMs
98 def getMachineFolder(self):
99 return self.machineFolder
101 #list the hostonly IFs exposed by the VBox host
102 def getHostOnlyIFs(self):
103 result = Cygwin.vboxExecute('list hostonlyifs')[1]
106 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
109 # list all existing VMs registered with VBox
111 result = Cygwin.vboxExecute('list vms')[1]
112 vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
116 def listRunningVMS(self):
117 result = Cygwin.vboxExecute('list runningvms')[1]
118 vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
121 # list existing SDVMs
126 if vm.startswith(self.vmRootName) and vm != self.vmRootName:
130 # generate valid (not already existing SDVM name). necessary for creating a new VM
131 def generateSDVMName(self):
133 for i in range(0,999):
134 if(not self.vmRootName+str(i) in vms):
135 return self.vmRootName+str(i)
138 # return the RSDs connected to the host
139 def getConnectedRSDS(self):
140 results = Cygwin.vboxExecute('list usbhost')[1]
141 results = results.split('Host USB Devices:')[1].strip()
143 items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
147 for line in item.splitlines():
149 k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
152 if 'Product' in props.keys() and props['Product'] == 'Mass Storage':
153 usb_filter = USBFilter( re.search(r"\((?P<vid>[0-9A-Fa-f]+)\)", props['VendorId']).groupdict()['vid'],
154 re.search(r"\((?P<pid>[0-9A-Fa-f]+)\)", props['ProductId']).groupdict()['pid'],
155 re.search(r"\((?P<rev>[0-9A-Fa-f]+)\)", props['Revision']).groupdict()['rev'] )
156 rsds[props['UUID']] = usb_filter;
157 logger.debug(usb_filter)
160 # return the RSDs attached to all existing SDVMs
161 def getAttachedRSDs(self):
162 vms = self.listSDVM()
163 attached_devices = dict()
165 rsd_filter = self.getUSBFilter(vm)
166 if rsd_filter != None:
167 attached_devices[vm] = rsd_filter
168 return attached_devices
170 # configures hostonly networking and DHCP server. requires admin rights
171 def configureHostNetworking(self):
172 #cmd = 'vboxmanage list hostonlyifs'
173 #Cygwin.vboxExecute(cmd)
174 #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"'
175 #Cygwin.vboxExecute(cmd)
176 #cmd = 'vboxmanage hostonlyif create'
177 #Cygwin.vboxExecute(cmd)
178 Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0')
179 #cmd = 'vboxmanage dhcpserver add'
180 #Cygwin.vboxExecute(cmd)
181 Cygwin.vboxExecute('dhcpserver modify --ifname \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.200')
183 #create new virtual machine instance based on template vm named SecurityDVM (\SecurityDVM\SecurityDVM.vmdk)
184 def createVM(self, vm_name):
185 hostonly_if = self.getHostOnlyIFs()
186 Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register')
187 Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat')
188 Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2')
191 # attach storage image to controller
192 def storageAttach(self, vm_name):
193 if self.isStorageAttached(vm_name):
194 self.storageDetach(vm_name)
195 Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"')
198 # return true if storage is attached
199 def isStorageAttached(self, vm_name):
200 info = self.getVMInfo(vm_name)
201 return (info['SATA-0-0']!='none')
203 # detach storage from controller
204 def storageDetach(self, vm_name):
205 if self.isStorageAttached(vm_name):
206 Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none')
209 def changeStorageType(self, filename, storage_type):
210 Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type)
213 # list storage snaphots for VM
214 def updateTemplate(self):
216 self.poweroffVM('SecurityDVM')
217 self.waitShutdown('SecurityDVM')
220 self.genCertificateISO('SecurityDVM')
221 self.attachCertificateISO('SecurityDVM')
223 self.storageDetach('SecurityDVM')
224 results = Cygwin.vboxExecute('list hdds')[1]
225 results = results.replace('Parent UUID', 'Parent')
226 items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
231 for line in item.splitlines():
233 k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
235 snaps[props['UUID']] = props
238 template_storage = self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk'
242 for hdd in snaps.values():
243 if hdd['Location'] == template_storage:
244 template_uuid = hdd['UUID']
245 logger.debug('found parent uuid ' + template_uuid)
248 for hdd in snaps.values():
249 if hdd['Parent'] == template_uuid:
250 #template_uuid = hdd['UUID']
251 logger.debug('removing snapshot ' + hdd['UUID'])
252 results = Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete')[1]
253 # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
255 self.changeStorageType(template_storage,'normal')
256 self.storageAttach('SecurityDVM')
257 self.startVM('SecurityDVM')
258 self.waitStartup('SecurityDVM')
259 Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key' )
260 Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key' )
261 self.stopVM('SecurityDVM')
262 #self.hibernateVM('SecurityDVM')
263 self.waitShutdown('SecurityDVM')
264 self.storageDetach('SecurityDVM')
265 self.changeStorageType(template_storage,'immutable')
266 self.storageAttach('SecurityDVM')
267 self.handleDeviceChange()
269 #remove VM from the system. should be used on VMs returned by listSDVMs
270 def removeVM(self, vm_name):
271 logger.info('Removing ' + vm_name)
272 Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete')
273 machineFolder = Cygwin.cygPath(self.machineFolder)
274 Cygwin.bashExecute('"/usr/bin/rm -rf ' + machineFolder + '/' + vm_name + '"')
277 def startVM(self, vm_name):
278 logger.info('Starting ' + vm_name)
279 result = Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' )
280 while not string.find(str(result), 'successfully started',):
281 logger.error("Failed to start SDVM: " + vm_name + " retrying")
283 result = Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless')
286 # return wether VM is running or not
287 def isVMRunning(self, vm_name):
288 return vm_name in self.listRunningVMS()
291 def stopVM(self, vm_name):
292 logger.info('Sending shutdown signal to ' + vm_name)
293 Cygwin.sshExecute( '"sudo shutdown -h now"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' )
296 def hibernateVM(self, vm_name):
297 logger.info('Sending shutdown signal to ' + vm_name)
298 Cygwin.sshExecute( '"sudo hibernate-disk"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' )
301 def poweroffVM(self, vm_name):
302 if not self.isVMRunning(vm_name):
304 logger.info('Powering off ' + vm_name)
305 return Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff')
307 # return the hostOnly IP for a running guest
309 def getHostOnlyIP(vm_name):
310 logger.info('Gettting hostOnly IP address ' + vm_name)
311 result = Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP')
315 if result.startswith('No value set!'):
317 return result[result.index(':')+1:].strip()
319 # attach removable storage device to VM by provision of filter
320 def attachRSD(self, vm_name, rsd_filter):
321 return Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision)
323 # detach removable storage from VM by
324 def detachRSD(self, vm_name):
325 return Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name )
328 # return the description set for an existing VM
329 def getVMInfo(self, vm_name):
330 results = Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable')[1]
331 props = dict((k.strip().strip('"'),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
335 # return the configured USB filter for an existing VM
336 def getUSBFilter(self, vm_name):
337 props = self.getVMInfo(vm_name)
338 keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1'])
339 keyset = set(props.keys())
341 if keyset.issuperset(keys):
342 usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1'])
345 #generates ISO containing authorized_keys for use with guest VM
346 def genCertificateISO(self, vm_name):
347 machineFolder = Cygwin.cygPath(self.machineFolder)
348 # remove .ssh folder if exists
349 cmd = '\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'
350 Cygwin.bashExecute(cmd)
351 # remove .ssh folder if exists
352 Cygwin.bashExecute('\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"')
353 # create .ssh folder in vm_name
354 Cygwin.bashExecute('\"/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"')
355 # generate dvm_key pair in vm_name / .ssh
356 Cygwin.bashExecute('\"/usr/bin/ssh-keygen -q -t rsa -N \\"\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"\"')
357 # move out private key
358 Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"')
359 # set permissions for private key
360 Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"')
361 # rename public key to authorized_keys
362 Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"')
363 # set permissions for authorized_keys
364 Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"\"')
365 # generate iso image with .ssh/authorized keys
366 Cygwin.bashExecute('\"/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"')
368 # attaches generated ssh public cert to guest vm
369 def attachCertificateISO(self, vm_name):
370 result = Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + self.machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"')
374 handleDeviceChangeLock = threading.Lock()
376 # handles device change events
377 def handleDeviceChange(self):
378 if VMManager.handleDeviceChangeLock.acquire(True):
379 #logger.debug("triggered")
380 #VMManager.handleDeviceChangeLock.release()
383 #diff = DictDiffer(self.connectedRSDs, tmp_conn)
386 if self.getConnectedRSDS().keys() == self.connectedRSDs.keys():
387 logger.info("Nothing's changed. Waiting for VBox USB sub-system to update...")
389 self.connectedRSDs = self.getConnectedRSDS()
395 VMManager.handleDeviceChangeLock.release()
398 logger.info("Something's changed")
400 self.attachedRSDs = self.getAttachedRSDs()
402 for vm_name in self.attachedRSDs.keys():
403 if self.attachedRSDs[vm_name] not in self.connectedRSDs.values():
404 self.unmapNetworkDrive('h:')
405 #self.stopVM(vm_name)
406 self.detachRSD(vm_name)
407 self.poweroffVM(vm_name)
408 self.removeVM(vm_name)
409 #create new vm for attached device if any
410 self.attachedRSDs = self.getAttachedRSDs()
411 self.connectedRSDs = self.getConnectedRSDS()
414 for connected_device in self.connectedRSDs.values():
415 if (self.attachedRSDs and False) or (connected_device not in self.attachedRSDs.values()):
416 new_sdvm = self.generateSDVMName()
417 self.createVM(new_sdvm)
418 self.storageAttach(new_sdvm)
419 self.attachRSD(new_sdvm, connected_device)
420 self.startVM(new_sdvm)
421 new_ip = self.waitStartup(new_sdvm)
424 self.mapNetworkDrive('h:', '\\\\' + new_ip + '\\USB', None, None)
425 #TODO: cleanup notifications somwhere else (eg. machine shutdown)
426 self.startNotifications.remove(new_ip)
427 VMManager.handleDeviceChangeLock.release()
430 # wait for machine to come up
431 def waitStartup(self, vm_name):
433 while new_ip == None:
435 new_ip = VMManager.getHostOnlyIP(vm_name)
436 while new_ip not in self.startNotifications:
440 # wait for machine to shutdown
441 def waitShutdown(self, vm_name):
442 while vm_name in self.listRunningVMS():
446 # handles browsing request
447 def handleBrowsingRequest(self):
448 if VMManager.handleDeviceChangeLock.acquire(True):
449 new_sdvm = self.generateSDVMName()
450 self.createVM(new_sdvm)
451 self.storageAttach(new_sdvm)
452 self.genCertificateISO(new_sdvm)
453 self.attachCertificateISO(new_sdvm)
454 self.startVM(new_sdvm)
455 new_ip = self.waitStartup(new_sdvm)
457 self.mapNetworkDrive('g:', '\\\\' + new_ip + '\\Download', None, None)
458 #TODO: cleanup notifications somwhere else (eg. machine shutdown)
459 self.startNotifications.remove(new_ip)
460 VMManager.handleDeviceChangeLock.release()
463 #Small function to check the availability of network resource.
464 #def isAvailable(self, path):
465 #return os.path.exists(path)
466 #result = Cygwin.cmdExecute('IF EXIST "' + path + '" echo YES')
467 #return string.find(result[1], 'YES',)
469 #Small function to check if the mention location is a directory
470 def isDirectory(self, path):
471 result = Cygwin.cmdExecute('dir ' + path + ' | FIND ".."')
472 return string.find(result[1], 'DIR',)
474 def mapNetworkDrive(self, drive, networkPath, user, password):
475 self.unmapNetworkDrive(drive)
476 #Check for drive availability
477 if os.path.exists(drive):
478 logger.error("Drive letter is already in use: " + drive)
480 #Check for network resource availability
481 while not os.path.exists(networkPath):
483 logger.info("Path not accessible: " + networkPath + " retrying")
486 command = 'USE ' + drive + ' ' + networkPath
488 command += ' ' + password + ' /User' + user
490 #TODO: Execute 'NET USE' command with authentication
491 result = Cygwin.execute('C:\\Windows\\system32\\net.exe', command)
492 if string.find(result[1], 'successfully',) == -1:
493 logger.error("Failed: NET " + command)
497 def unmapNetworkDrive(self, drive):
498 if not os.path.exists(drive):
500 result = Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE ' + drive + ' /DELETE /YES')
501 if string.find(str(result), 'successfully',) == -1:
502 logger.error(result[2])
506 class DeviceHandler(threading.Thread):
509 def __init__(self, zahl):
510 threading.Thread.__init__(self)
516 logger.debug("triggered")
522 if __name__ == '__main__':
523 man = VMManager.getInstance()
525 print man.getConnectedRSDs()
531 #man.genCertificateISO('SecurityDVM0')
532 #man.guestExecute('SecurityDVM0', '/bin/ls -la')
533 #logger = setupLogger('VMManager')
536 #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
537 #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
538 #man.removeVM('SecurityDVM0')
539 #man.netUse('192.168.56.134', 'USB\\')
540 #ip = '192.168.56.139'
542 #man.cygwin_path = 'c:\\cygwin64\\bin\\'
543 #man.handleDeviceChange()
544 #print man.listSDVM()
545 #man.configureHostNetworking()
546 #new_vm = man.generateSDVMName()
547 #man.createVM(new_vm)
550 #man.isAvailable('c:')
551 #ip = man.getHostOnlyIP('SecurityDVM0')
552 #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
554 #man.genCertificateISO(new_vm)
555 #man.attachCertificateISO(new_vm)
557 #man.attachCertificateISO(vm_name)
558 #man.guestExecute(vm_name, "ls")
559 #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
561 #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
562 #man.genCertificateISO('SecurityDVM')
563 #man.attachCertificateISO('SecurityDVM')
564 #man.isStorageAttached('SecurityDVM')
565 #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
566 #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
568 #man.stopVM('SecurityDVM')
569 #man.storageDetach('SecurityDVM')
570 #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
571 #man.storageAttach('SecurityDVM')
574 #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""