3 Created on Nov 19, 2013
9 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
28 class VMManagerException(Exception):
29 def __init__(self, value):
32 return repr(self.value)
39 def __init__(self, vendorid, productid, revision):
40 self.vendorid = vendorid.lower()
41 self.productid = productid.lower()
42 self.revision = revision.lower()
45 def __eq__(self, other):
46 return self.vendorid == other.vendorid and self.productid == other.productid and self.revision == other.revision
49 return hash(self.vendorid) ^ hash(self.productid) ^ hash(self.revision)
52 return "VendorId = \'" + str(self.vendorid) + "\' ProductId = \'" + str(self.productid) + "\' Revision = \'" + str(self.revision) + "\'"
54 #def __getitem__(self, item):
55 # return self.coords[item]
57 class VMManager(object):
58 vmRootName = "SecurityDVM"
59 systemProperties = None
65 self.systemProperties = self.getSystemProperties()
66 self.machineFolder = self.systemProperties["Default machine folder"]
68 self.rsdHandler = DeviceHandler(self)
69 self.rsdHandler.start()
74 if VMManager._instance == None:
75 VMManager._instance = VMManager()
76 return VMManager._instance
79 if self.rsdHandler != None:
80 self.rsdHandler.stop()
81 self.rsdHandler.join()
82 drives = self.getNetworkDrives()
83 for drive in drives.keys():
84 self.unmapNetworkDrive(drive)
85 for vm in self.listSDVM():
89 # return hosty system properties
90 def getSystemProperties(self):
91 result = checkResult(Cygwin.vboxExecute('list systemproperties'))
94 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 all existing VMs registered with VBox
103 result = checkResult(Cygwin.vboxExecute('list vms'))[1]
104 vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
108 def listRunningVMS(self):
109 result = checkResult(Cygwin.vboxExecute('list runningvms'))[1]
110 vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
113 # list existing SDVMs
118 if vm.startswith(self.vmRootName) and vm != self.vmRootName:
122 # generate valid (not already existing SDVM name). necessary for creating a new VM
123 def generateSDVMName(self):
125 for i in range(0,999):
126 if(not self.vmRootName+str(i) in vms):
127 return self.vmRootName+str(i)
130 # check if the device is mass storage type
132 def isMassStorageDevice(device):
133 keyname = 'SYSTEM\CurrentControlSet\Enum\USB' + '\VID_' + device.vendorid+'&'+'PID_'+ device.productid
134 key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname)
135 #subkeys = _winreg.QueryInfoKey(key)[0]
136 #for i in range(0, subkeys):
137 # print _winreg.EnumKey(key, i)
138 devinfokeyname = _winreg.EnumKey(key, 0)
139 _winreg.CloseKey(key)
141 devinfokey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname+'\\'+devinfokeyname)
142 value = _winreg.QueryValueEx(devinfokey, 'SERVICE')[0]
143 _winreg.CloseKey(devinfokey)
145 return 'USBSTOR' in value
147 # return the RSDs connected to the host
149 def getConnectedRSDS():
150 results = checkResult(Cygwin.vboxExecute('list usbhost'))[1]
151 results = results.split('Host USB Devices:')[1].strip()
153 items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
157 for line in item.splitlines():
159 k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
162 #if 'Product' in props.keys() and props['Product'] == 'Mass Storage':
164 usb_filter = USBFilter( re.search(r"\((?P<vid>[0-9A-Fa-f]+)\)", props['VendorId']).groupdict()['vid'],
165 re.search(r"\((?P<pid>[0-9A-Fa-f]+)\)", props['ProductId']).groupdict()['pid'],
166 re.search(r"\((?P<rev>[0-9A-Fa-f]+)\)", props['Revision']).groupdict()['rev'] )
167 if VMManager.isMassStorageDevice(usb_filter):
168 rsds[props['UUID']] = usb_filter;
169 logger.debug(usb_filter)
172 # return the RSDs attached to all existing SDVMs
173 def getAttachedRSDs(self):
174 vms = self.listSDVM()
175 attached_devices = dict()
177 rsd_filter = self.getUSBFilter(vm)
178 if rsd_filter != None:
179 attached_devices[vm] = rsd_filter
180 return attached_devices
182 # configures hostonly networking and DHCP server. requires admin rights
183 def configureHostNetworking(self):
184 #cmd = 'vboxmanage list hostonlyifs'
185 #Cygwin.vboxExecute(cmd)
186 #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"'
187 #Cygwin.vboxExecute(cmd)
188 #cmd = 'vboxmanage hostonlyif create'
189 #Cygwin.vboxExecute(cmd)
190 checkResult(Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'))
191 #cmd = 'vboxmanage dhcpserver add'
192 #Cygwin.vboxExecute(cmd)
193 checkResult(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'))
195 #create new virtual machine instance based on template vm named SecurityDVM (\SecurityDVM\SecurityDVM.vmdk)
196 def createVM(self, vm_name):
197 hostonly_if = self.getHostOnlyIFs()
198 checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register'))
199 checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat'))
200 checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2'))
203 # attach storage image to controller
204 def storageAttach(self, vm_name):
205 if self.isStorageAttached(vm_name):
206 self.storageDetach(vm_name)
207 checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"'))
209 # return true if storage is attached
210 def isStorageAttached(self, vm_name):
211 info = self.getVMInfo(vm_name)
212 return (info['SATA-0-0']!='none')
214 # detach storage from controller
215 def storageDetach(self, vm_name):
216 if self.isStorageAttached(vm_name):
217 checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none'))
219 def changeStorageType(self, filename, storage_type):
220 checkResult(Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type))
222 # list storage snaphots for VM
223 def updateTemplate(self):
226 self.poweroffVM('SecurityDVM')
227 self.waitShutdown('SecurityDVM')
230 self.genCertificateISO('SecurityDVM')
231 self.attachCertificateISO('SecurityDVM')
233 self.storageDetach('SecurityDVM')
234 results = checkResult(Cygwin.vboxExecute('list hdds'))[1]
235 results = results.replace('Parent UUID', 'Parent')
236 items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
241 for line in item.splitlines():
243 k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
245 snaps[props['UUID']] = props
248 template_storage = self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk'
252 for hdd in snaps.values():
253 if hdd['Location'] == template_storage:
254 template_uuid = hdd['UUID']
255 logger.debug('found parent uuid ' + template_uuid)
258 for hdd in snaps.values():
259 if hdd['Parent'] == template_uuid:
260 #template_uuid = hdd['UUID']
261 logger.debug('removing snapshot ' + hdd['UUID'])
262 checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1]
263 # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
265 self.changeStorageType(template_storage,'normal')
266 self.storageAttach('SecurityDVM')
267 self.startVM('SecurityDVM')
268 self.waitStartup('SecurityDVM')
269 checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
270 checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
271 #self.stopVM('SecurityDVM')
272 self.hibernateVM('SecurityDVM')
273 self.waitShutdown('SecurityDVM')
274 self.storageDetach('SecurityDVM')
275 self.changeStorageType(template_storage,'immutable')
276 self.storageAttach('SecurityDVM')
277 self.rsdHandler = DeviceHandler(self)
278 self.rsdHandler.start()
280 #remove VM from the system. should be used on VMs returned by listSDVMs
281 def removeVM(self, vm_name):
282 logger.info('Removing ' + vm_name)
283 checkResult(Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete'))
284 machineFolder = Cygwin.cygPath(self.machineFolder)
285 checkResult(Cygwin.bashExecute('"/usr/bin/rm -rf ' + machineFolder + '/' + vm_name + '"'))
288 def startVM(self, vm_name):
289 logger.info('Starting ' + vm_name)
290 result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' ))
291 while 'successfully started' not in result[1]:
292 logger.error("Failed to start SDVM: " + vm_name + " retrying")
294 result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless'))
297 # return wether VM is running or not
298 def isVMRunning(self, vm_name):
299 return vm_name in self.listRunningVMS()
302 def stopVM(self, vm_name):
303 logger.info('Sending shutdown signal to ' + vm_name)
304 checkResult(Cygwin.sshExecute( '"sudo shutdown -h now"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' ))
307 def hibernateVM(self, vm_name):
308 logger.info('Sending hibernate-disk signal to ' + vm_name)
309 checkResult(Cygwin.sshExecute( '"sudo hibernate-disk&"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False))
312 def poweroffVM(self, vm_name):
313 if not self.isVMRunning(vm_name):
315 logger.info('Powering off ' + vm_name)
316 return checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff'))
318 #list the hostonly IFs exposed by the VBox host
320 def getHostOnlyIFs():
321 result = Cygwin.vboxExecute('list hostonlyifs')[1]
324 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
327 # return the hostOnly IP for a running guest or the host
329 def getHostOnlyIP(vm_name):
331 logger.info('Gettting hostOnly IP address for Host')
332 return VMManager.getHostOnlyIFs()['IPAddress']
334 logger.info('Gettting hostOnly IP address ' + vm_name)
335 result = checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'))
339 if result.startswith('No value set!'):
341 return result[result.index(':')+1:].strip()
343 # attach removable storage device to VM by provision of filter
344 def attachRSD(self, vm_name, rsd_filter):
345 return checkResult(Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision))
347 # detach removable storage from VM by
348 def detachRSD(self, vm_name):
349 return checkResult(Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name))
351 # return the description set for an existing VM
352 def getVMInfo(self, vm_name):
353 results = checkResult(Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable'))[1]
354 props = dict((k.strip().strip('"'),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
357 # return the configured USB filter for an existing VM
358 def getUSBFilter(self, vm_name):
359 props = self.getVMInfo(vm_name)
360 keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1'])
361 keyset = set(props.keys())
363 if keyset.issuperset(keys):
364 usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1'])
367 #generates ISO containing authorized_keys for use with guest VM
368 def genCertificateISO(self, vm_name):
369 machineFolder = Cygwin.cygPath(self.machineFolder)
370 # remove .ssh folder if exists
371 checkResult(Cygwin.bashExecute('\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'))
372 # remove .ssh folder if exists
373 checkResult(Cygwin.bashExecute('\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"'))
374 # create .ssh folder in vm_name
375 checkResult(Cygwin.bashExecute('\"/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'))
376 # generate dvm_key pair in vm_name / .ssh
377 checkResult(Cygwin.bashExecute('\"/usr/bin/ssh-keygen -q -t rsa -N \\"\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"\"'))
378 # move out private key
379 checkResult(Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"'))
380 # set permissions for private key
381 checkResult(Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"'))
382 # rename public key to authorized_keys
383 checkResult(Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'))
384 # set permissions for authorized_keys
385 checkResult(Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"\"'))
386 # generate iso image with .ssh/authorized keys
387 checkResult(Cygwin.bashExecute('\"/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'))
389 # attaches generated ssh public cert to guest vm
390 def attachCertificateISO(self, vm_name):
391 result = checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + self.machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'))
394 # wait for machine to come up
395 def waitStartup(self, vm_name, timeout_ms = 30000):
396 checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout'))
397 return VMManager.getHostOnlyIP(vm_name)
399 # wait for machine to shutdown
400 def waitShutdown(self, vm_name):
401 while vm_name in self.listRunningVMS():
405 # handles browsing request
406 def handleBrowsingRequest(self):
407 handler = BrowsingHandler(self)
411 #Small function to check the availability of network resource.
412 #def isAvailable(self, path):
413 #return os.path.exists(path)
414 #result = Cygwin.cmdExecute('IF EXIST "' + path + '" echo YES')
415 #return string.find(result[1], 'YES',)
417 #Small function to check if the mention location is a directory
418 def isDirectory(self, path):
419 result = checkResult(Cygwin.cmdExecute('dir ' + path + ' | FIND ".."'))
420 return string.find(result[1], 'DIR',)
422 def mapNetworkDrive(self, drive, networkPath, user, password):
423 self.unmapNetworkDrive(drive)
424 #Check for drive availability
425 if os.path.exists(drive):
426 logger.error("Drive letter is already in use: " + drive)
428 #Check for network resource availability
430 while not os.path.exists(networkPath):
434 logger.info("Path not accessible: " + networkPath + " retrying")
438 command = 'USE ' + drive + ' ' + networkPath + ' /PERSISTENT:NO'
440 command += ' ' + password + ' /User' + user
442 #TODO: Execute 'NET USE' command with authentication
443 result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command))
444 if string.find(result[1], 'successfully',) == -1:
445 logger.error("Failed: NET " + command)
449 def unmapNetworkDrive(self, drive):
450 drives = self.getNetworkDrives()
451 if drive not in drives.keys():
453 result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE ' + drive + ' /DELETE /YES'))
454 if string.find(str(result[1]), 'successfully',) == -1:
455 logger.error(result[2])
459 def getNetworkDrives(self):
460 ip = VMManager.getHostOnlyIP(None)
461 ip = ip[:ip.rindex('.')]
463 result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
464 for line in result[1].splitlines():
467 drives[parts[1]] = parts[2]
470 def genNetworkDrive(self):
471 network_drives = self.getNetworkDrives()
472 logical_drives = VMManager.getLogicalDrives()
473 drives = list(map(chr, range(68, 91)))
475 if drive+':' not in network_drives and drive not in logical_drives:
478 def getNetworkDrive(self, vm_name):
479 ip = self.getHostOnlyIP(vm_name)
480 result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
481 for line in result[1].splitlines():
482 if line != None and ip in line:
486 def getLogicalDrives():
487 drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
488 return list(itertools.compress(string.ascii_uppercase, map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
491 def getDriveType(drive):
492 return ctypes.cdll.kernel32.GetDriveTypeW(u"%s:\\"%drive)
495 def getVolumeInfo(drive):
496 volumeNameBuffer = ctypes.create_unicode_buffer(1024)
497 fileSystemNameBuffer = ctypes.create_unicode_buffer(1024)
499 max_component_length = None
500 file_system_flags = None
502 rc = ctypes.cdll.kernel32.GetVolumeInformationW(
503 #ctypes.c_wchar_p("F:\\"),
506 ctypes.sizeof(volumeNameBuffer),
508 max_component_length,
510 fileSystemNameBuffer,
511 ctypes.sizeof(fileSystemNameBuffer)
514 return volumeNameBuffer.value, fileSystemNameBuffer.value
516 def checkResult(result):
518 logger.error('Command failed:' + ''.join(result[2]))
519 raise OpenSecurityException('Command failed:' + ''.join(result[2]))
522 # handles browsing request
523 class BrowsingHandler(threading.Thread):
525 def __init__(self, vmmanager):
526 threading.Thread.__init__(self)
532 new_sdvm = self.vmm.generateSDVMName()
533 self.vmm.createVM(new_sdvm)
534 self.vmm.storageAttach(new_sdvm)
535 self.vmm.genCertificateISO(new_sdvm)
536 self.vmm.attachCertificateISO(new_sdvm)
537 self.vmm.startVM(new_sdvm)
538 new_ip = self.vmm.waitStartup(new_sdvm)
539 drive = self.vmm.genNetworkDrive()
541 self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\Download', None, None)
542 #browser = '/usr/bin/iceweasel'
543 #browser = '/usr/bin/midori'
544 browser = '/usr/bin/chromium'
545 result = checkResult(Cygwin.sshExecuteX11(browser, new_ip, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + new_sdvm + '/dvm_key'))
547 logger.error("BrowsingHandler failed. Cleaning up")
549 self.vmm.unmapNetworkDrive(drive)
550 self.vmm.poweroffVM(new_sdvm)
551 self.vmm.removeVM(new_sdvm)
553 class DeviceHandler(threading.Thread):
558 def __init__(self, vmmanger):
559 threading.Thread.__init__(self)
566 self.connectedRSDs = dict()
568 tmp_rsds = self.vmm.getConnectedRSDS()
570 self.attachedRSDs = self.vmm.getAttachedRSDs()
571 for vm_name in self.attachedRSDs.keys():
572 if self.attachedRSDs[vm_name] not in tmp_rsds.values():
573 drive = self.vmm.getNetworkDrive(vm_name)
574 self.vmm.unmapNetworkDrive(drive)
575 #self.stopVM(vm_name)
576 self.vmm.detachRSD(vm_name)
577 self.vmm.poweroffVM(vm_name)
578 self.vmm.removeVM(vm_name)
582 if tmp_rsds.keys() == self.connectedRSDs.keys():
583 logger.debug("Nothing's changed. sleep(3)")
587 logger.info("Something's changed")
588 self.connectedRSDs = tmp_rsds
592 #create new vm for attached device if any
593 self.attachedRSDs = self.vmm.getAttachedRSDs()
594 self.connectedRSDs = self.vmm.getConnectedRSDS()
597 for connected_device in self.connectedRSDs.values():
598 if (self.attachedRSDs and False) or (connected_device not in self.attachedRSDs.values()):
599 new_sdvm = self.vmm.generateSDVMName()
600 self.vmm.createVM(new_sdvm)
601 self.vmm.storageAttach(new_sdvm)
602 self.vmm.attachRSD(new_sdvm, connected_device)
603 self.vmm.startVM(new_sdvm)
604 new_ip = self.vmm.waitStartup(new_sdvm)
605 drive = self.vmm.genNetworkDrive()
607 self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\USB', None, None)
609 if __name__ == '__main__':
610 #man = VMManager.getInstance()
612 #print man.getConnectedRSDs()
613 #print man.getNetworkDrives()
614 #man.genNetworkDrive()
615 #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
616 #print list(itertools.compress(string.ascii_uppercase, map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
617 #print list(map(chr, range(68, 91)))
618 #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0]
619 #devices = VMManager.getConnectedRSDS()
622 drives = VMManager.getLogicalDrives()
624 print VMManager.getDriveType("E")
625 print VMManager.getVolumeInfo("E")
626 #for device in devices.values():
628 # if VMManager.isMassStorageDevice(device):
638 #man.genCertificateISO('SecurityDVM0')
639 #man.guestExecute('SecurityDVM0', '/bin/ls -la')
640 #logger = setupLogger('VMManager')
643 #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
644 #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
645 #man.removeVM('SecurityDVM0')
646 #man.netUse('192.168.56.134', 'USB\\')
647 #ip = '192.168.56.139'
649 #man.cygwin_path = 'c:\\cygwin64\\bin\\'
650 #man.handleDeviceChange()
651 #print man.listSDVM()
652 #man.configureHostNetworking()
653 #new_vm = man.generateSDVMName()
654 #man.createVM(new_vm)
657 #man.isAvailable('c:')
658 #ip = man.getHostOnlyIP('SecurityDVM0')
659 #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
661 #man.genCertificateISO(new_vm)
662 #man.attachCertificateISO(new_vm)
664 #man.attachCertificateISO(vm_name)
665 #man.guestExecute(vm_name, "ls")
666 #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
668 #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
669 #man.genCertificateISO('SecurityDVM')
670 #man.attachCertificateISO('SecurityDVM')
671 #man.isStorageAttached('SecurityDVM')
672 #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
673 #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
675 #man.stopVM('SecurityDVM')
676 #man.storageDetach('SecurityDVM')
677 #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
678 #man.storageAttach('SecurityDVM')
681 #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
685 Created on Nov 19, 2013
691 from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
695 from cygwin import Cygwin
696 from environment import Environment
704 from opensecurity_util import logger, setupLogger, OpenSecurityException
710 class VMManagerException(Exception):
711 def __init__(self, value):
714 return repr(self.value)
721 def __init__(self, vendorid, productid, revision):
722 self.vendorid = vendorid.lower()
723 self.productid = productid.lower()
724 self.revision = revision.lower()
727 def __eq__(self, other):
728 return self.vendorid == other.vendorid and self.productid == other.productid and self.revision == other.revision
731 return hash(self.vendorid) ^ hash(self.productid) ^ hash(self.revision)
734 return "VendorId = \'" + str(self.vendorid) + "\' ProductId = \'" + str(self.productid) + "\' Revision = \'" + str(self.revision) + "\'"
736 #def __getitem__(self, item):
737 # return self.coords[item]
739 class VMManager(object):
740 vmRootName = "SecurityDVM"
741 systemProperties = None
749 self.systemProperties = self.getSystemProperties()
750 self.machineFolder = self.systemProperties["Default machine folder"]
752 self.rsdHandler = DeviceHandler(self)
753 self.rsdHandler.start()
758 if VMManager._instance == None:
759 VMManager._instance = VMManager()
760 return VMManager._instance
763 if self.rsdHandler != None:
764 self.rsdHandler.stop()
765 self.rsdHandler.join()
766 drives = self.getNetworkDrives()
767 for drive in drives.keys():
768 self.unmapNetworkDrive(drive)
769 for vm in self.listSDVM():
773 # return hosty system properties
774 def getSystemProperties(self):
775 result = checkResult(Cygwin.vboxExecute('list systemproperties'))
778 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
781 # return the folder containing the guest VMs
782 def getMachineFolder(self):
783 return self.machineFolder
785 # list all existing VMs registered with VBox
787 result = checkResult(Cygwin.vboxExecute('list vms'))[1]
788 vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
792 def listRunningVMS(self):
793 result = checkResult(Cygwin.vboxExecute('list runningvms'))[1]
794 vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
797 # list existing SDVMs
802 if vm.startswith(self.vmRootName) and vm != self.vmRootName:
806 # generate valid (not already existing SDVM name). necessary for creating a new VM
807 def generateSDVMName(self):
809 for i in range(0,999):
810 if(not self.vmRootName+str(i) in vms):
811 return self.vmRootName+str(i)
814 # check if the device is mass storage type
816 def isMassStorageDevice(device):
817 keyname = 'SYSTEM\CurrentControlSet\Enum\USB' + '\VID_' + device.vendorid+'&'+'PID_'+ device.productid
818 key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname)
819 #subkeys = _winreg.QueryInfoKey(key)[0]
820 #for i in range(0, subkeys):
821 # print _winreg.EnumKey(key, i)
822 devinfokeyname = _winreg.EnumKey(key, 0)
823 _winreg.CloseKey(key)
825 devinfokey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname+'\\'+devinfokeyname)
826 value = _winreg.QueryValueEx(devinfokey, 'SERVICE')[0]
827 _winreg.CloseKey(devinfokey)
829 return 'USBSTOR' in value
831 # return the RSDs connected to the host
833 def getConnectedRSDS():
834 results = checkResult(Cygwin.vboxExecute('list usbhost'))[1]
835 results = results.split('Host USB Devices:')[1].strip()
837 items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
841 for line in item.splitlines():
843 k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
846 #if 'Product' in props.keys() and props['Product'] == 'Mass Storage':
848 usb_filter = USBFilter( re.search(r"\((?P<vid>[0-9A-Fa-f]+)\)", props['VendorId']).groupdict()['vid'],
849 re.search(r"\((?P<pid>[0-9A-Fa-f]+)\)", props['ProductId']).groupdict()['pid'],
850 re.search(r"\((?P<rev>[0-9A-Fa-f]+)\)", props['Revision']).groupdict()['rev'] )
851 if VMManager.isMassStorageDevice(usb_filter):
852 rsds[props['UUID']] = usb_filter;
853 logger.debug(usb_filter)
856 # return the RSDs attached to all existing SDVMs
857 def getAttachedRSDs(self):
858 vms = self.listSDVM()
859 attached_devices = dict()
861 rsd_filter = self.getUSBFilter(vm)
862 if rsd_filter != None:
863 attached_devices[vm] = rsd_filter
864 return attached_devices
866 # configures hostonly networking and DHCP server. requires admin rights
867 def configureHostNetworking(self):
868 #cmd = 'vboxmanage list hostonlyifs'
869 #Cygwin.vboxExecute(cmd)
870 #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"'
871 #Cygwin.vboxExecute(cmd)
872 #cmd = 'vboxmanage hostonlyif create'
873 #Cygwin.vboxExecute(cmd)
874 checkResult(Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'))
875 #cmd = 'vboxmanage dhcpserver add'
876 #Cygwin.vboxExecute(cmd)
877 checkResult(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'))
879 #create new virtual machine instance based on template vm named SecurityDVM (\SecurityDVM\SecurityDVM.vmdk)
880 def createVM(self, vm_name):
881 hostonly_if = self.getHostOnlyIFs()
882 checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register'))
883 checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat'))
884 checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2'))
887 # attach storage image to controller
888 def storageAttach(self, vm_name):
889 if self.isStorageAttached(vm_name):
890 self.storageDetach(vm_name)
891 checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"'))
893 # return true if storage is attached
894 def isStorageAttached(self, vm_name):
895 info = self.getVMInfo(vm_name)
896 return (info['SATA-0-0']!='none')
898 # detach storage from controller
899 def storageDetach(self, vm_name):
900 if self.isStorageAttached(vm_name):
901 checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none'))
903 def changeStorageType(self, filename, storage_type):
904 checkResult(Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type))
906 # list storage snaphots for VM
907 def updateTemplate(self):
909 self.poweroffVM('SecurityDVM')
910 self.waitShutdown('SecurityDVM')
913 self.genCertificateISO('SecurityDVM')
914 self.attachCertificateISO('SecurityDVM')
916 self.storageDetach('SecurityDVM')
917 results = checkResult(Cygwin.vboxExecute('list hdds'))[1]
918 results = results.replace('Parent UUID', 'Parent')
919 items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
924 for line in item.splitlines():
926 k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
928 snaps[props['UUID']] = props
931 template_storage = self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk'
935 for hdd in snaps.values():
936 if hdd['Location'] == template_storage:
937 template_uuid = hdd['UUID']
938 logger.debug('found parent uuid ' + template_uuid)
941 for hdd in snaps.values():
942 if hdd['Parent'] == template_uuid:
943 #template_uuid = hdd['UUID']
944 logger.debug('removing snapshot ' + hdd['UUID'])
945 checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1]
946 # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
948 self.changeStorageType(template_storage,'normal')
949 self.storageAttach('SecurityDVM')
950 self.startVM('SecurityDVM')
951 self.waitStartup('SecurityDVM')
952 checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
953 checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
954 #self.stopVM('SecurityDVM')
955 self.hibernateVM('SecurityDVM')
956 self.waitShutdown('SecurityDVM')
957 self.storageDetach('SecurityDVM')
958 self.changeStorageType(template_storage,'immutable')
959 self.storageAttach('SecurityDVM')
960 self.rsdHandler = DeviceHandler(self)
961 self.rsdHandler.start()
963 #remove VM from the system. should be used on VMs returned by listSDVMs
964 def removeVM(self, vm_name):
965 logger.info('Removing ' + vm_name)
966 checkResult(Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete'))
967 vm_file = Cygwin.cygPath(self.machineFolder + '\\' + vm_name)
968 checkResult(Cygwin.bashExecute('rm -rf \'' + vm_file + '\''))
971 def startVM(self, vm_name):
972 logger.info('Starting ' + vm_name)
973 result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' ))
974 while 'successfully started' not in result[1] and _running:
975 logger.error("Failed to start SDVM: " + vm_name + " retrying")
977 result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless'))
980 # return wether VM is running or not
981 def isVMRunning(self, vm_name):
982 return vm_name in self.listRunningVMS()
985 def stopVM(self, vm_name):
986 logger.info('Sending shutdown signal to ' + vm_name)
987 checkResult(Cygwin.sshExecute( '"sudo shutdown -h now"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' ))
990 def hibernateVM(self, vm_name):
991 logger.info('Sending shutdown signal to ' + vm_name)
992 checkResult(Cygwin.sshExecute( '"sudo hibernate-disk&"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False))
995 def poweroffVM(self, vm_name):
996 if not self.isVMRunning(vm_name):
998 logger.info('Powering off ' + vm_name)
999 return checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff'))
1001 #list the hostonly IFs exposed by the VBox host
1003 def getHostOnlyIFs():
1004 result = Cygwin.vboxExecute('list hostonlyifs')[1]
1007 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
1010 # return the hostOnly IP for a running guest or the host
1012 def getHostOnlyIP(vm_name):
1014 logger.info('Gettting hostOnly IP address for Host')
1015 return VMManager.getHostOnlyIFs()['IPAddress']
1017 logger.info('Gettting hostOnly IP address ' + vm_name)
1018 result = checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'))
1022 if result.startswith('No value set!'):
1024 return result[result.index(':')+1:].strip()
1026 # attach removable storage device to VM by provision of filter
1027 def attachRSD(self, vm_name, rsd_filter):
1028 return checkResult(Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision))
1030 # detach removable storage from VM by
1031 def detachRSD(self, vm_name):
1032 return checkResult(Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name))
1034 # return the description set for an existing VM
1035 def getVMInfo(self, vm_name):
1036 results = checkResult(Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable'))[1]
1037 props = dict((k.strip().strip('"'),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
1040 # return the configured USB filter for an existing VM
1041 def getUSBFilter(self, vm_name):
1042 props = self.getVMInfo(vm_name)
1043 keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1'])
1044 keyset = set(props.keys())
1046 if keyset.issuperset(keys):
1047 usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1'])
1050 #generates ISO containing authorized_keys for use with guest VM
1051 def genCertificateISO(self, vm_name):
1052 machineFolder = Cygwin.cygPath(self.machineFolder)
1053 # remove .ssh folder if exists
1054 checkResult(Cygwin.bashExecute('\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'))
1055 # remove .ssh folder if exists
1056 checkResult(Cygwin.bashExecute('\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"'))
1057 # create .ssh folder in vm_name
1058 checkResult(Cygwin.bashExecute('\"/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'))
1059 # generate dvm_key pair in vm_name / .ssh
1060 checkResult(Cygwin.bashExecute('\"/usr/bin/ssh-keygen -q -t rsa -N \\"\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"\"'))
1061 # move out private key
1062 checkResult(Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"'))
1063 # set permissions for private key
1064 checkResult(Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"'))
1065 # rename public key to authorized_keys
1066 checkResult(Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'))
1067 # set permissions for authorized_keys
1068 checkResult(Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"\"'))
1069 # generate iso image with .ssh/authorized keys
1070 checkResult(Cygwin.bashExecute('\"/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'))
1072 # attaches generated ssh public cert to guest vm
1073 def attachCertificateISO(self, vm_name):
1074 result = checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + self.machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'))
1077 # wait for machine to come up
1078 def waitStartup(self, vm_name, timeout_ms = 30000):
1079 checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout'))
1080 return VMManager.getHostOnlyIP(vm_name)
1082 # wait for machine to shutdown
1083 def waitShutdown(self, vm_name):
1084 while vm_name in self.listRunningVMS() and _running:
1088 # handles browsing request
1089 def handleBrowsingRequest(self):
1090 handler = BrowsingHandler(self)
1094 #Small function to check the availability of network resource.
1095 #def isAvailable(self, path):
1096 #return os.path.exists(path)
1097 #result = Cygwin.cmdExecute('IF EXIST "' + path + '" echo YES')
1098 #return string.find(result[1], 'YES',)
1100 #Small function to check if the mention location is a directory
1101 def isDirectory(self, path):
1102 result = checkResult(Cygwin.cmdExecute('dir ' + path + ' | FIND ".."'))
1103 return string.find(result[1], 'DIR',)
1105 def mapNetworkDrive(self, drive, networkPath, user, password):
1106 self.unmapNetworkDrive(drive)
1107 #Check for drive availability
1108 if os.path.exists(drive):
1109 logger.error("Drive letter is already in use: " + drive)
1111 #Check for network resource availability
1113 while not os.path.exists(networkPath):
1117 logger.info("Path not accessible: " + networkPath + " retrying")
1121 command = 'USE ' + drive + ' ' + networkPath + ' /PERSISTENT:NO'
1123 command += ' ' + password + ' /User' + user
1125 #TODO: Execute 'NET USE' command with authentication
1126 result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command))
1127 if string.find(result[1], 'successfully',) == -1:
1128 logger.error("Failed: NET " + command)
1132 def unmapNetworkDrive(self, drive):
1133 drives = self.getNetworkDrives()
1134 if drive not in drives.keys():
1136 result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE ' + drive + ' /DELETE /YES'))
1137 if string.find(str(result[1]), 'successfully',) == -1:
1138 logger.error(result[2])
1142 def getNetworkDrives(self):
1143 ip = VMManager.getHostOnlyIP(None)
1144 ip = ip[:ip.rindex('.')]
1146 result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
1147 for line in result[1].splitlines():
1149 parts = line.split()
1150 drives[parts[1]] = parts[2]
1153 def genNetworkDrive(self):
1154 network_drives = self.getNetworkDrives()
1155 logical_drives = VMManager.getLogicalDrives()
1156 drives = list(map(chr, range(68, 91)))
1157 for drive in drives:
1158 if drive+':' not in network_drives and drive not in logical_drives:
1161 def getNetworkDrive(self, vm_name):
1162 ip = self.getHostOnlyIP(vm_name)
1163 result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
1164 for line in result[1].splitlines():
1165 if line != None and ip in line:
1166 parts = line.split()
1169 def getLogicalDrives():
1170 drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
1171 return list(itertools.compress(string.ascii_uppercase, map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
1174 def getDriveType(drive):
1175 return ctypes.cdll.kernel32.GetDriveTypeW(u"%s:\\"%drive)
1178 def getVolumeInfo(drive):
1179 volumeNameBuffer = ctypes.create_unicode_buffer(1024)
1180 fileSystemNameBuffer = ctypes.create_unicode_buffer(1024)
1181 serial_number = None
1182 max_component_length = None
1183 file_system_flags = None
1185 rc = ctypes.cdll.kernel32.GetVolumeInformationW(
1186 #ctypes.c_wchar_p("F:\\"),
1189 ctypes.sizeof(volumeNameBuffer),
1191 max_component_length,
1193 fileSystemNameBuffer,
1194 ctypes.sizeof(fileSystemNameBuffer)
1197 return volumeNameBuffer.value, fileSystemNameBuffer.value
1201 """stop all running infinite loops now --> needed for gracefull shutdown"""
1206 def checkResult(result):
1208 logger.error('Command failed:' + ''.join(result[2]))
1209 raise OpenSecurityException('Command failed:' + ''.join(result[2]))
1212 # handles browsing request
1213 class BrowsingHandler(threading.Thread):
1215 def __init__(self, vmmanager):
1216 threading.Thread.__init__(self)
1217 self.vmm = vmmanager
1221 new_sdvm = self.vmm.generateSDVMName()
1222 self.vmm.createVM(new_sdvm)
1223 self.vmm.storageAttach(new_sdvm)
1224 self.vmm.genCertificateISO(new_sdvm)
1225 self.vmm.attachCertificateISO(new_sdvm)
1226 self.vmm.startVM(new_sdvm)
1227 new_ip = self.vmm.waitStartup(new_sdvm)
1228 drive = self.vmm.genNetworkDrive()
1230 self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\Download', None, None)
1231 result = checkResult(Cygwin.sshExecuteX11('/usr/bin/iceweasel', new_ip, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + new_sdvm + '/dvm_key'))
1233 logger.error("BrowsingHandler failed. Cleaning up")
1235 self.vmm.unmapNetworkDrive(drive)
1236 self.vmm.poweroffVM(new_sdvm)
1237 self.vmm.removeVM(new_sdvm)
1239 class DeviceHandler(threading.Thread):
1241 #handleDeviceChangeLock = threading.Lock()
1243 connectedRSDs = None
1245 def __init__(self, vmmanger):
1246 threading.Thread.__init__(self)
1250 self.running = False
1253 self.connectedRSDs = dict()
1254 self.attachedRSDs = self.vmm.getAttachedRSDs()
1256 tmp_rsds = self.vmm.getConnectedRSDS()
1257 if tmp_rsds.keys() == self.connectedRSDs.keys():
1258 logger.debug("Nothing's changed. sleep(3)")
1262 logger.info("Something's changed")
1263 self.connectedRSDs = tmp_rsds
1264 self.attachedRSDs = self.vmm.getAttachedRSDs()
1266 for vm_name in self.attachedRSDs.keys():
1267 if self.attachedRSDs[vm_name] not in self.connectedRSDs.values():
1268 drive = self.vmm.getNetworkDrive(vm_name)
1269 self.vmm.unmapNetworkDrive(drive)
1270 #self.stopVM(vm_name)
1271 self.vmm.detachRSD(vm_name)
1272 self.vmm.poweroffVM(vm_name)
1273 self.vmm.removeVM(vm_name)
1274 #create new vm for attached device if any
1275 self.attachedRSDs = self.vmm.getAttachedRSDs()
1276 self.connectedRSDs = self.vmm.getConnectedRSDS()
1279 for connected_device in self.connectedRSDs.values():
1280 if (self.attachedRSDs and False) or (connected_device not in self.attachedRSDs.values()):
1281 new_sdvm = self.vmm.generateSDVMName()
1282 self.vmm.createVM(new_sdvm)
1283 self.vmm.storageAttach(new_sdvm)
1284 self.vmm.attachRSD(new_sdvm, connected_device)
1285 self.vmm.startVM(new_sdvm)
1286 new_ip = self.vmm.waitStartup(new_sdvm)
1287 drive = self.vmm.genNetworkDrive()
1289 self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\USB', None, None)
1291 if __name__ == '__main__':
1292 #man = VMManager.getInstance()
1294 #print man.getConnectedRSDs()
1295 #print man.getNetworkDrives()
1296 #man.genNetworkDrive()
1297 #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
1298 #print list(itertools.compress(string.ascii_uppercase, map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
1299 #print list(map(chr, range(68, 91)))
1300 #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0]
1301 #devices = VMManager.getConnectedRSDS()
1304 drives = VMManager.getLogicalDrives()
1306 print VMManager.getDriveType("E")
1307 print VMManager.getVolumeInfo("E")
1308 #for device in devices.values():
1310 # if VMManager.isMassStorageDevice(device):
1320 #man.genCertificateISO('SecurityDVM0')
1321 #man.guestExecute('SecurityDVM0', '/bin/ls -la')
1322 #logger = setupLogger('VMManager')
1325 #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
1326 #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
1327 #man.removeVM('SecurityDVM0')
1328 #man.netUse('192.168.56.134', 'USB\\')
1329 #ip = '192.168.56.139'
1331 #man.cygwin_path = 'c:\\cygwin64\\bin\\'
1332 #man.handleDeviceChange()
1333 #print man.listSDVM()
1334 #man.configureHostNetworking()
1335 #new_vm = man.generateSDVMName()
1336 #man.createVM(new_vm)
1339 #man.isAvailable('c:')
1340 #ip = man.getHostOnlyIP('SecurityDVM0')
1341 #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
1343 #man.genCertificateISO(new_vm)
1344 #man.attachCertificateISO(new_vm)
1346 #man.attachCertificateISO(vm_name)
1347 #man.guestExecute(vm_name, "ls")
1348 #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
1350 #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
1351 #man.genCertificateISO('SecurityDVM')
1352 #man.attachCertificateISO('SecurityDVM')
1353 #man.isStorageAttached('SecurityDVM')
1354 #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
1355 #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
1357 #man.stopVM('SecurityDVM')
1358 #man.storageDetach('SecurityDVM')
1359 #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
1360 #man.storageAttach('SecurityDVM')
1363 #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""