diff -r bfd41c38d156 -r a26757850ea9 OpenSecurity/bin/vmmanager.pyw --- a/OpenSecurity/bin/vmmanager.pyw Fri Mar 07 14:32:12 2014 +0100 +++ b/OpenSecurity/bin/vmmanager.pyw Mon Mar 10 13:01:08 2014 +0100 @@ -1,672 +1,681 @@ -''' -Created on Nov 19, 2013 - -@author: BarthaM -''' -import os -import os.path -from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess -import sys -import re - -from cygwin import Cygwin -from environment import Environment -import threading -import time -import string - -import shutil -import stat -import tempfile -from opensecurity_util import logger, setupLogger, OpenSecurityException -import ctypes -import itertools -import _winreg -DEBUG = True - -class VMManagerException(Exception): - def __init__(self, value): - self.value = value - def __str__(self): - return repr(self.value) - -class USBFilter: - vendorid = "" - productid = "" - revision = "" - - def __init__(self, vendorid, productid, revision): - self.vendorid = vendorid.lower() - self.productid = productid.lower() - self.revision = revision.lower() - return - - def __eq__(self, other): - return self.vendorid == other.vendorid and self.productid == other.productid and self.revision == other.revision - - def __hash__(self): - return hash(self.vendorid) ^ hash(self.productid) ^ hash(self.revision) - - def __repr__(self): - return "VendorId = \'" + str(self.vendorid) + "\' ProductId = \'" + str(self.productid) + "\' Revision = \'" + str(self.revision) + "\'" - - #def __getitem__(self, item): - # return self.coords[item] - -class VMManager(object): - vmRootName = "SecurityDVM" - systemProperties = None - _instance = None - machineFolder = '' - rsdHandler = None - - def __init__(self): - self.systemProperties = self.getSystemProperties() - self.machineFolder = self.systemProperties["Default machine folder"] - self.cleanup() - self.rsdHandler = DeviceHandler(self) - self.rsdHandler.start() - return - - @staticmethod - def getInstance(): - if VMManager._instance == None: - VMManager._instance = VMManager() - return VMManager._instance - - def cleanup(self): - if self.rsdHandler != None: - self.rsdHandler.stop() - self.rsdHandler.join() - drives = self.getNetworkDrives() - for drive in drives.keys(): - self.unmapNetworkDrive(drive) - for vm in self.listSDVM(): - self.poweroffVM(vm) - self.removeVM(vm) - - # return hosty system properties - def getSystemProperties(self): - result = checkResult(Cygwin.vboxExecute('list systemproperties')) - if result[1]=='': - return None - props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines())) - return props - - # return the folder containing the guest VMs - def getMachineFolder(self): - return self.machineFolder - - # list all existing VMs registered with VBox - def listVM(self): - result = checkResult(Cygwin.vboxExecute('list vms'))[1] - vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines())) - return vms - - # list running VMs - def listRunningVMS(self): - result = checkResult(Cygwin.vboxExecute('list runningvms'))[1] - vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines())) - return vms - - # list existing SDVMs - def listSDVM(self): - vms = self.listVM() - svdms = [] - for vm in vms: - if vm.startswith(self.vmRootName) and vm != self.vmRootName: - svdms.append(vm) - return svdms - - # generate valid (not already existing SDVM name). necessary for creating a new VM - def generateSDVMName(self): - vms = self.listVM() - for i in range(0,999): - if(not self.vmRootName+str(i) in vms): - return self.vmRootName+str(i) - return '' - - # check if the device is mass storage type - @staticmethod - def isMassStorageDevice(device): - keyname = 'SYSTEM\CurrentControlSet\Enum\USB' + '\VID_' + device.vendorid+'&'+'PID_'+ device.productid - key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname) - #subkeys = _winreg.QueryInfoKey(key)[0] - #for i in range(0, subkeys): - # print _winreg.EnumKey(key, i) - devinfokeyname = _winreg.EnumKey(key, 0) - _winreg.CloseKey(key) - - devinfokey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname+'\\'+devinfokeyname) - value = _winreg.QueryValueEx(devinfokey, 'SERVICE')[0] - _winreg.CloseKey(devinfokey) - - return 'USBSTOR' in value - - # return the RSDs connected to the host - @staticmethod - def getConnectedRSDS(): - results = checkResult(Cygwin.vboxExecute('list usbhost'))[1] - results = results.split('Host USB Devices:')[1].strip() - - items = list( "UUID:"+result for result in results.split('UUID:') if result != '') - rsds = dict() - for item in items: - props = dict() - for line in item.splitlines(): - if line != "": - k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip() - props[k] = v - - #if 'Product' in props.keys() and props['Product'] == 'Mass Storage': - - usb_filter = USBFilter( re.search(r"\((?P[0-9A-Fa-f]+)\)", props['VendorId']).groupdict()['vid'], - re.search(r"\((?P[0-9A-Fa-f]+)\)", props['ProductId']).groupdict()['pid'], - re.search(r"\((?P[0-9A-Fa-f]+)\)", props['Revision']).groupdict()['rev'] ) - if VMManager.isMassStorageDevice(usb_filter): - rsds[props['UUID']] = usb_filter; - logger.debug(usb_filter) - return rsds - - # return the RSDs attached to all existing SDVMs - def getAttachedRSDs(self): - vms = self.listSDVM() - attached_devices = dict() - for vm in vms: - rsd_filter = self.getUSBFilter(vm) - if rsd_filter != None: - attached_devices[vm] = rsd_filter - return attached_devices - - # configures hostonly networking and DHCP server. requires admin rights - def configureHostNetworking(self): - #cmd = 'vboxmanage list hostonlyifs' - #Cygwin.vboxExecute(cmd) - #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"' - #Cygwin.vboxExecute(cmd) - #cmd = 'vboxmanage hostonlyif create' - #Cygwin.vboxExecute(cmd) - checkResult(Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0')) - #cmd = 'vboxmanage dhcpserver add' - #Cygwin.vboxExecute(cmd) - 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')) - - #create new virtual machine instance based on template vm named SecurityDVM (\SecurityDVM\SecurityDVM.vmdk) - def createVM(self, vm_name): - hostonly_if = self.getHostOnlyIFs() - checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register')) - checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat')) - checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2')) - return - - # attach storage image to controller - def storageAttach(self, vm_name): - if self.isStorageAttached(vm_name): - self.storageDetach(vm_name) - checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"')) - - # return true if storage is attached - def isStorageAttached(self, vm_name): - info = self.getVMInfo(vm_name) - return (info['SATA-0-0']!='none') - - # detach storage from controller - def storageDetach(self, vm_name): - if self.isStorageAttached(vm_name): - checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none')) - - def changeStorageType(self, filename, storage_type): - checkResult(Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type)) - - # list storage snaphots for VM - def updateTemplate(self): - self.cleanup() - self.poweroffVM('SecurityDVM') - self.waitShutdown('SecurityDVM') - - # check for updates - self.genCertificateISO('SecurityDVM') - self.attachCertificateISO('SecurityDVM') - - self.storageDetach('SecurityDVM') - results = checkResult(Cygwin.vboxExecute('list hdds'))[1] - results = results.replace('Parent UUID', 'Parent') - items = list( "UUID:"+result for result in results.split('UUID:') if result != '') - - snaps = dict() - for item in items: - props = dict() - for line in item.splitlines(): - if line != "": - k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip() - props[k] = v; - snaps[props['UUID']] = props - - - template_storage = self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk' - - # find template uuid - template_uuid = '' - for hdd in snaps.values(): - if hdd['Location'] == template_storage: - template_uuid = hdd['UUID'] - logger.debug('found parent uuid ' + template_uuid) - - # remove snapshots - for hdd in snaps.values(): - if hdd['Parent'] == template_uuid: - #template_uuid = hdd['UUID'] - logger.debug('removing snapshot ' + hdd['UUID']) - checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1] - # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% - - self.changeStorageType(template_storage,'normal') - self.storageAttach('SecurityDVM') - self.startVM('SecurityDVM') - self.waitStartup('SecurityDVM') - checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key')) - checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key')) - #self.stopVM('SecurityDVM') - self.hibernateVM('SecurityDVM') - self.waitShutdown('SecurityDVM') - self.storageDetach('SecurityDVM') - self.changeStorageType(template_storage,'immutable') - self.storageAttach('SecurityDVM') - self.rsdHandler = DeviceHandler(self) - self.rsdHandler.start() - - #remove VM from the system. should be used on VMs returned by listSDVMs - def removeVM(self, vm_name): - logger.info('Removing ' + vm_name) - checkResult(Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete')) - machineFolder = Cygwin.cygPath(self.machineFolder) - checkResult(Cygwin.bashExecute('"/usr/bin/rm -rf ' + machineFolder + '/' + vm_name + '"')) - - # start VM - def startVM(self, vm_name): - logger.info('Starting ' + vm_name) - result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' )) - while 'successfully started' not in result[1]: - logger.error("Failed to start SDVM: " + vm_name + " retrying") - time.sleep(1) - result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless')) - return result[0] - - # return wether VM is running or not - def isVMRunning(self, vm_name): - return vm_name in self.listRunningVMS() - - # stop VM - def stopVM(self, vm_name): - logger.info('Sending shutdown signal to ' + vm_name) - checkResult(Cygwin.sshExecute( '"sudo shutdown -h now"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' )) - - # stop VM - def hibernateVM(self, vm_name): - logger.info('Sending shutdown signal to ' + vm_name) - checkResult(Cygwin.sshExecute( '"sudo hibernate-disk&"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False)) - - # poweroff VM - def poweroffVM(self, vm_name): - if not self.isVMRunning(vm_name): - return - logger.info('Powering off ' + vm_name) - return checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff')) - - #list the hostonly IFs exposed by the VBox host - @staticmethod - def getHostOnlyIFs(): - result = Cygwin.vboxExecute('list hostonlyifs')[1] - if result=='': - return None - props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines())) - return props - - # return the hostOnly IP for a running guest or the host - @staticmethod - def getHostOnlyIP(vm_name): - if vm_name == None: - logger.info('Gettting hostOnly IP address for Host') - return VMManager.getHostOnlyIFs()['IPAddress'] - else: - logger.info('Gettting hostOnly IP address ' + vm_name) - result = checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP')) - if result=='': - return None - result = result[1] - if result.startswith('No value set!'): - return None - return result[result.index(':')+1:].strip() - - # attach removable storage device to VM by provision of filter - def attachRSD(self, vm_name, rsd_filter): - return checkResult(Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision)) - - # detach removable storage from VM by - def detachRSD(self, vm_name): - return checkResult(Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name)) - - # return the description set for an existing VM - def getVMInfo(self, vm_name): - results = checkResult(Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable'))[1] - props = dict((k.strip().strip('"'),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines())) - return props - - # return the configured USB filter for an existing VM - def getUSBFilter(self, vm_name): - props = self.getVMInfo(vm_name) - keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1']) - keyset = set(props.keys()) - usb_filter = None - if keyset.issuperset(keys): - usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1']) - return usb_filter - - #generates ISO containing authorized_keys for use with guest VM - def genCertificateISO(self, vm_name): - machineFolder = Cygwin.cygPath(self.machineFolder) - # remove .ssh folder if exists - checkResult(Cygwin.bashExecute('\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"')) - # remove .ssh folder if exists - checkResult(Cygwin.bashExecute('\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"')) - # create .ssh folder in vm_name - checkResult(Cygwin.bashExecute('\"/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"')) - # generate dvm_key pair in vm_name / .ssh - checkResult(Cygwin.bashExecute('\"/usr/bin/ssh-keygen -q -t rsa -N \\"\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"\"')) - # move out private key - checkResult(Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"')) - # set permissions for private key - checkResult(Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"')) - # rename public key to authorized_keys - checkResult(Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"')) - # set permissions for authorized_keys - checkResult(Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"\"')) - # generate iso image with .ssh/authorized keys - checkResult(Cygwin.bashExecute('\"/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"')) - - # attaches generated ssh public cert to guest vm - def attachCertificateISO(self, vm_name): - 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\"')) - return result - - # wait for machine to come up - def waitStartup(self, vm_name, timeout_ms = 30000): - checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout')) - return VMManager.getHostOnlyIP(vm_name) - - # wait for machine to shutdown - def waitShutdown(self, vm_name): - while vm_name in self.listRunningVMS(): - time.sleep(1) - return - - # handles browsing request - def handleBrowsingRequest(self): - handler = BrowsingHandler(self) - handler.start() - return 'ok' - - #Small function to check the availability of network resource. - #def isAvailable(self, path): - #return os.path.exists(path) - #result = Cygwin.cmdExecute('IF EXIST "' + path + '" echo YES') - #return string.find(result[1], 'YES',) - - #Small function to check if the mention location is a directory - def isDirectory(self, path): - result = checkResult(Cygwin.cmdExecute('dir ' + path + ' | FIND ".."')) - return string.find(result[1], 'DIR',) - - def mapNetworkDrive(self, drive, networkPath, user, password): - self.unmapNetworkDrive(drive) - #Check for drive availability - if os.path.exists(drive): - logger.error("Drive letter is already in use: " + drive) - return -1 - #Check for network resource availability - retry = 5 - while not os.path.exists(networkPath): - time.sleep(1) - if retry == 0: - return -1 - logger.info("Path not accessible: " + networkPath + " retrying") - retry-=1 - #return -1 - - command = 'USE ' + drive + ' ' + networkPath + ' /PERSISTENT:NO' - if user != None: - command += ' ' + password + ' /User' + user - - #TODO: Execute 'NET USE' command with authentication - result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command)) - if string.find(result[1], 'successfully',) == -1: - logger.error("Failed: NET " + command) - return -1 - return 1 - - def unmapNetworkDrive(self, drive): - drives = self.getNetworkDrives() - if drive not in drives.keys(): - return 1 - result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE ' + drive + ' /DELETE /YES')) - if string.find(str(result[1]), 'successfully',) == -1: - logger.error(result[2]) - return -1 - return 1 - - def getNetworkDrives(self): - ip = VMManager.getHostOnlyIP(None) - ip = ip[:ip.rindex('.')] - drives = dict() - result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE')) - for line in result[1].splitlines(): - if ip in line: - parts = line.split() - drives[parts[1]] = parts[2] - return drives - - def genNetworkDrive(self): - network_drives = self.getNetworkDrives() - logical_drives = VMManager.getLogicalDrives() - drives = list(map(chr, range(68, 91))) - for drive in drives: - if drive+':' not in network_drives and drive not in logical_drives: - return drive+':' - - def getNetworkDrive(self, vm_name): - ip = self.getHostOnlyIP(vm_name) - result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE')) - for line in result[1].splitlines(): - if line != None and ip in line: - parts = line.split() - return parts[0] - @staticmethod - def getLogicalDrives(): - drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives() - return list(itertools.compress(string.ascii_uppercase, map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1]))) - - @staticmethod - def getDriveType(drive): - return ctypes.cdll.kernel32.GetDriveTypeW(u"%s:\\"%drive) - - @staticmethod - def getVolumeInfo(drive): - volumeNameBuffer = ctypes.create_unicode_buffer(1024) - fileSystemNameBuffer = ctypes.create_unicode_buffer(1024) - serial_number = None - max_component_length = None - file_system_flags = None - - rc = ctypes.cdll.kernel32.GetVolumeInformationW( - #ctypes.c_wchar_p("F:\\"), - u"%s:\\"%drive, - volumeNameBuffer, - ctypes.sizeof(volumeNameBuffer), - serial_number, - max_component_length, - file_system_flags, - fileSystemNameBuffer, - ctypes.sizeof(fileSystemNameBuffer) - ) - - return volumeNameBuffer.value, fileSystemNameBuffer.value - -def checkResult(result): - if result[0] != 0: - logger.error('Command failed:' + ''.join(result[2])) - raise OpenSecurityException('Command failed:' + ''.join(result[2])) - return result - -# handles browsing request -class BrowsingHandler(threading.Thread): - vmm = None - def __init__(self, vmmanager): - threading.Thread.__init__(self) - self.vmm = vmmanager - - def run(self): - try: - new_sdvm = self.vmm.generateSDVMName() - self.vmm.createVM(new_sdvm) - self.vmm.storageAttach(new_sdvm) - self.vmm.genCertificateISO(new_sdvm) - self.vmm.attachCertificateISO(new_sdvm) - self.vmm.startVM(new_sdvm) - new_ip = self.vmm.waitStartup(new_sdvm) - drive = self.vmm.genNetworkDrive() - if new_ip != None: - self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\Download', None, None) - result = checkResult(Cygwin.sshExecuteX11('/usr/bin/iceweasel', new_ip, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + new_sdvm + '/dvm_key')) - except: - logger.error("BrowsingHandler failed. Cleaning up") - - self.vmm.unmapNetworkDrive(drive) - self.vmm.poweroffVM(new_sdvm) - self.vmm.removeVM(new_sdvm) - -class DeviceHandler(threading.Thread): - vmm = None - #handleDeviceChangeLock = threading.Lock() - attachedRSDs = None - connectedRSDs = None - running = True - def __init__(self, vmmanger): - threading.Thread.__init__(self) - self.vmm = vmmanger - - def stop(self): - self.running = False - - def run(self): - self.connectedRSDs = dict() - self.attachedRSDs = self.vmm.getAttachedRSDs() - while self.running: - tmp_rsds = self.vmm.getConnectedRSDS() - if tmp_rsds.keys() == self.connectedRSDs.keys(): - logger.debug("Nothing's changed. sleep(3)") - time.sleep(3) - continue - - logger.info("Something's changed") - self.connectedRSDs = tmp_rsds - self.attachedRSDs = self.vmm.getAttachedRSDs() - - for vm_name in self.attachedRSDs.keys(): - if self.attachedRSDs[vm_name] not in self.connectedRSDs.values(): - drive = self.vmm.getNetworkDrive(vm_name) - self.vmm.unmapNetworkDrive(drive) - #self.stopVM(vm_name) - self.vmm.detachRSD(vm_name) - self.vmm.poweroffVM(vm_name) - self.vmm.removeVM(vm_name) - #create new vm for attached device if any - self.attachedRSDs = self.vmm.getAttachedRSDs() - self.connectedRSDs = self.vmm.getConnectedRSDS() - - new_ip = None - for connected_device in self.connectedRSDs.values(): - if (self.attachedRSDs and False) or (connected_device not in self.attachedRSDs.values()): - new_sdvm = self.vmm.generateSDVMName() - self.vmm.createVM(new_sdvm) - self.vmm.storageAttach(new_sdvm) - self.vmm.attachRSD(new_sdvm, connected_device) - self.vmm.startVM(new_sdvm) - new_ip = self.vmm.waitStartup(new_sdvm) - drive = self.vmm.genNetworkDrive() - if new_ip != None: - self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\USB', None, None) - -if __name__ == '__main__': - #man = VMManager.getInstance() - #man.listVM() - #print man.getConnectedRSDs() - #print man.getNetworkDrives() - #man.genNetworkDrive() - #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives() - #print list(itertools.compress(string.ascii_uppercase, map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1]))) - #print list(map(chr, range(68, 91))) - #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0] - #devices = VMManager.getConnectedRSDS() - #print devices - - drives = VMManager.getLogicalDrives() - print drives - print VMManager.getDriveType("E") - print VMManager.getVolumeInfo("E") - #for device in devices.values(): - # #print device - # if VMManager.isMassStorageDevice(device): - # print device - - - - #time.sleep(-1) - #man.listVM() - #man.listVM() - #man.listVM() - #man.listVM() - #man.genCertificateISO('SecurityDVM0') - #man.guestExecute('SecurityDVM0', '/bin/ls -la') - #logger = setupLogger('VMManager') - #c = Cygwin() - - #man.sshExecute('/bin/ls -la', 'SecurityDVM0') - #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0') - #man.removeVM('SecurityDVM0') - #man.netUse('192.168.56.134', 'USB\\') - #ip = '192.168.56.139' - - #man.cygwin_path = 'c:\\cygwin64\\bin\\' - #man.handleDeviceChange() - #print man.listSDVM() - #man.configureHostNetworking() - #new_vm = man.generateSDVMName() - #man.createVM(new_vm) - - #print Cygwin.cmd() - #man.isAvailable('c:') - #ip = man.getHostOnlyIP('SecurityDVM0') - #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None) - - #man.genCertificateISO(new_vm) - #man.attachCertificateISO(new_vm) - - #man.attachCertificateISO(vm_name) - #man.guestExecute(vm_name, "ls") - #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel') - #time.sleep(60) - #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*") - #man.genCertificateISO('SecurityDVM') - #man.attachCertificateISO('SecurityDVM') - #man.isStorageAttached('SecurityDVM') - #man.guestExecute('SecurityDVM', 'sudo apt-get -y update') - #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' ) - - #man.stopVM('SecurityDVM') - #man.storageDetach('SecurityDVM') - #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable') - #man.storageAttach('SecurityDVM') - - - #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\"" - #man.execute(cmd) +''' +Created on Nov 19, 2013 + +@author: BarthaM +''' +import os +import os.path +from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess +import sys +import re + +from cygwin import Cygwin +from environment import Environment +import threading +import time +import string + +import shutil +import stat +import tempfile +from opensecurity_util import logger, setupLogger, OpenSecurityException +import ctypes +import itertools +import _winreg +DEBUG = True + +class VMManagerException(Exception): + def __init__(self, value): + self.value = value + def __str__(self): + return repr(self.value) + +class USBFilter: + vendorid = "" + productid = "" + revision = "" + + def __init__(self, vendorid, productid, revision): + self.vendorid = vendorid.lower() + self.productid = productid.lower() + self.revision = revision.lower() + return + + def __eq__(self, other): + return self.vendorid == other.vendorid and self.productid == other.productid and self.revision == other.revision + + def __hash__(self): + return hash(self.vendorid) ^ hash(self.productid) ^ hash(self.revision) + + def __repr__(self): + return "VendorId = \'" + str(self.vendorid) + "\' ProductId = \'" + str(self.productid) + "\' Revision = \'" + str(self.revision) + "\'" + + #def __getitem__(self, item): + # return self.coords[item] + +class VMManager(object): + vmRootName = "SecurityDVM" + systemProperties = None + _instance = None + machineFolder = '' + rsdHandler = None + _running = True + + def __init__(self): + self._running = True + self.systemProperties = self.getSystemProperties() + self.machineFolder = self.systemProperties["Default machine folder"] + self.cleanup() + self.rsdHandler = DeviceHandler(self) + self.rsdHandler.start() + return + + @staticmethod + def getInstance(): + if VMManager._instance == None: + VMManager._instance = VMManager() + return VMManager._instance + + def cleanup(self): + if self.rsdHandler != None: + self.rsdHandler.stop() + self.rsdHandler.join() + drives = self.getNetworkDrives() + for drive in drives.keys(): + self.unmapNetworkDrive(drive) + for vm in self.listSDVM(): + self.poweroffVM(vm) + self.removeVM(vm) + + # return hosty system properties + def getSystemProperties(self): + result = checkResult(Cygwin.vboxExecute('list systemproperties')) + if result[1]=='': + return None + props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines())) + return props + + # return the folder containing the guest VMs + def getMachineFolder(self): + return self.machineFolder + + # list all existing VMs registered with VBox + def listVM(self): + result = checkResult(Cygwin.vboxExecute('list vms'))[1] + vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines())) + return vms + + # list running VMs + def listRunningVMS(self): + result = checkResult(Cygwin.vboxExecute('list runningvms'))[1] + vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines())) + return vms + + # list existing SDVMs + def listSDVM(self): + vms = self.listVM() + svdms = [] + for vm in vms: + if vm.startswith(self.vmRootName) and vm != self.vmRootName: + svdms.append(vm) + return svdms + + # generate valid (not already existing SDVM name). necessary for creating a new VM + def generateSDVMName(self): + vms = self.listVM() + for i in range(0,999): + if(not self.vmRootName+str(i) in vms): + return self.vmRootName+str(i) + return '' + + # check if the device is mass storage type + @staticmethod + def isMassStorageDevice(device): + keyname = 'SYSTEM\CurrentControlSet\Enum\USB' + '\VID_' + device.vendorid+'&'+'PID_'+ device.productid + key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname) + #subkeys = _winreg.QueryInfoKey(key)[0] + #for i in range(0, subkeys): + # print _winreg.EnumKey(key, i) + devinfokeyname = _winreg.EnumKey(key, 0) + _winreg.CloseKey(key) + + devinfokey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname+'\\'+devinfokeyname) + value = _winreg.QueryValueEx(devinfokey, 'SERVICE')[0] + _winreg.CloseKey(devinfokey) + + return 'USBSTOR' in value + + # return the RSDs connected to the host + @staticmethod + def getConnectedRSDS(): + results = checkResult(Cygwin.vboxExecute('list usbhost'))[1] + results = results.split('Host USB Devices:')[1].strip() + + items = list( "UUID:"+result for result in results.split('UUID:') if result != '') + rsds = dict() + for item in items: + props = dict() + for line in item.splitlines(): + if line != "": + k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip() + props[k] = v + + #if 'Product' in props.keys() and props['Product'] == 'Mass Storage': + + usb_filter = USBFilter( re.search(r"\((?P[0-9A-Fa-f]+)\)", props['VendorId']).groupdict()['vid'], + re.search(r"\((?P[0-9A-Fa-f]+)\)", props['ProductId']).groupdict()['pid'], + re.search(r"\((?P[0-9A-Fa-f]+)\)", props['Revision']).groupdict()['rev'] ) + if VMManager.isMassStorageDevice(usb_filter): + rsds[props['UUID']] = usb_filter; + logger.debug(usb_filter) + return rsds + + # return the RSDs attached to all existing SDVMs + def getAttachedRSDs(self): + vms = self.listSDVM() + attached_devices = dict() + for vm in vms: + rsd_filter = self.getUSBFilter(vm) + if rsd_filter != None: + attached_devices[vm] = rsd_filter + return attached_devices + + # configures hostonly networking and DHCP server. requires admin rights + def configureHostNetworking(self): + #cmd = 'vboxmanage list hostonlyifs' + #Cygwin.vboxExecute(cmd) + #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"' + #Cygwin.vboxExecute(cmd) + #cmd = 'vboxmanage hostonlyif create' + #Cygwin.vboxExecute(cmd) + checkResult(Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0')) + #cmd = 'vboxmanage dhcpserver add' + #Cygwin.vboxExecute(cmd) + 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')) + + #create new virtual machine instance based on template vm named SecurityDVM (\SecurityDVM\SecurityDVM.vmdk) + def createVM(self, vm_name): + hostonly_if = self.getHostOnlyIFs() + checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register')) + checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat')) + checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2')) + return + + # attach storage image to controller + def storageAttach(self, vm_name): + if self.isStorageAttached(vm_name): + self.storageDetach(vm_name) + checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"')) + + # return true if storage is attached + def isStorageAttached(self, vm_name): + info = self.getVMInfo(vm_name) + return (info['SATA-0-0']!='none') + + # detach storage from controller + def storageDetach(self, vm_name): + if self.isStorageAttached(vm_name): + checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none')) + + def changeStorageType(self, filename, storage_type): + checkResult(Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type)) + + # list storage snaphots for VM + def updateTemplate(self): + self.cleanup() + self.poweroffVM('SecurityDVM') + self.waitShutdown('SecurityDVM') + + # check for updates + self.genCertificateISO('SecurityDVM') + self.attachCertificateISO('SecurityDVM') + + self.storageDetach('SecurityDVM') + results = checkResult(Cygwin.vboxExecute('list hdds'))[1] + results = results.replace('Parent UUID', 'Parent') + items = list( "UUID:"+result for result in results.split('UUID:') if result != '') + + snaps = dict() + for item in items: + props = dict() + for line in item.splitlines(): + if line != "": + k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip() + props[k] = v; + snaps[props['UUID']] = props + + + template_storage = self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk' + + # find template uuid + template_uuid = '' + for hdd in snaps.values(): + if hdd['Location'] == template_storage: + template_uuid = hdd['UUID'] + logger.debug('found parent uuid ' + template_uuid) + + # remove snapshots + for hdd in snaps.values(): + if hdd['Parent'] == template_uuid: + #template_uuid = hdd['UUID'] + logger.debug('removing snapshot ' + hdd['UUID']) + checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1] + # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% + + self.changeStorageType(template_storage,'normal') + self.storageAttach('SecurityDVM') + self.startVM('SecurityDVM') + self.waitStartup('SecurityDVM') + checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key')) + checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key')) + #self.stopVM('SecurityDVM') + self.hibernateVM('SecurityDVM') + self.waitShutdown('SecurityDVM') + self.storageDetach('SecurityDVM') + self.changeStorageType(template_storage,'immutable') + self.storageAttach('SecurityDVM') + self.rsdHandler = DeviceHandler(self) + self.rsdHandler.start() + + #remove VM from the system. should be used on VMs returned by listSDVMs + def removeVM(self, vm_name): + logger.info('Removing ' + vm_name) + checkResult(Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete')) + vm_file = Cygwin.cygPath(self.machineFolder + '\\' + vm_name) + checkResult(Cygwin.bashExecute('rm -rf \'' + vm_file + '\'')) + + # start VM + def startVM(self, vm_name): + logger.info('Starting ' + vm_name) + result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' )) + while 'successfully started' not in result[1] and _running: + logger.error("Failed to start SDVM: " + vm_name + " retrying") + time.sleep(1) + result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless')) + return result[0] + + # return wether VM is running or not + def isVMRunning(self, vm_name): + return vm_name in self.listRunningVMS() + + # stop VM + def stopVM(self, vm_name): + logger.info('Sending shutdown signal to ' + vm_name) + checkResult(Cygwin.sshExecute( '"sudo shutdown -h now"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' )) + + # stop VM + def hibernateVM(self, vm_name): + logger.info('Sending shutdown signal to ' + vm_name) + checkResult(Cygwin.sshExecute( '"sudo hibernate-disk&"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False)) + + # poweroff VM + def poweroffVM(self, vm_name): + if not self.isVMRunning(vm_name): + return + logger.info('Powering off ' + vm_name) + return checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff')) + + #list the hostonly IFs exposed by the VBox host + @staticmethod + def getHostOnlyIFs(): + result = Cygwin.vboxExecute('list hostonlyifs')[1] + if result=='': + return None + props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines())) + return props + + # return the hostOnly IP for a running guest or the host + @staticmethod + def getHostOnlyIP(vm_name): + if vm_name == None: + logger.info('Gettting hostOnly IP address for Host') + return VMManager.getHostOnlyIFs()['IPAddress'] + else: + logger.info('Gettting hostOnly IP address ' + vm_name) + result = checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP')) + if result=='': + return None + result = result[1] + if result.startswith('No value set!'): + return None + return result[result.index(':')+1:].strip() + + # attach removable storage device to VM by provision of filter + def attachRSD(self, vm_name, rsd_filter): + return checkResult(Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision)) + + # detach removable storage from VM by + def detachRSD(self, vm_name): + return checkResult(Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name)) + + # return the description set for an existing VM + def getVMInfo(self, vm_name): + results = checkResult(Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable'))[1] + props = dict((k.strip().strip('"'),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines())) + return props + + # return the configured USB filter for an existing VM + def getUSBFilter(self, vm_name): + props = self.getVMInfo(vm_name) + keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1']) + keyset = set(props.keys()) + usb_filter = None + if keyset.issuperset(keys): + usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1']) + return usb_filter + + #generates ISO containing authorized_keys for use with guest VM + def genCertificateISO(self, vm_name): + machineFolder = Cygwin.cygPath(self.machineFolder) + # remove .ssh folder if exists + checkResult(Cygwin.bashExecute('\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"')) + # remove .ssh folder if exists + checkResult(Cygwin.bashExecute('\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"')) + # create .ssh folder in vm_name + checkResult(Cygwin.bashExecute('\"/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"')) + # generate dvm_key pair in vm_name / .ssh + checkResult(Cygwin.bashExecute('\"/usr/bin/ssh-keygen -q -t rsa -N \\"\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"\"')) + # move out private key + checkResult(Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"')) + # set permissions for private key + checkResult(Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"')) + # rename public key to authorized_keys + checkResult(Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"')) + # set permissions for authorized_keys + checkResult(Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"\"')) + # generate iso image with .ssh/authorized keys + checkResult(Cygwin.bashExecute('\"/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"')) + + # attaches generated ssh public cert to guest vm + def attachCertificateISO(self, vm_name): + 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\"')) + return result + + # wait for machine to come up + def waitStartup(self, vm_name, timeout_ms = 30000): + checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout')) + return VMManager.getHostOnlyIP(vm_name) + + # wait for machine to shutdown + def waitShutdown(self, vm_name): + while vm_name in self.listRunningVMS() and _running: + time.sleep(1) + return + + # handles browsing request + def handleBrowsingRequest(self): + handler = BrowsingHandler(self) + handler.start() + return 'ok' + + #Small function to check the availability of network resource. + #def isAvailable(self, path): + #return os.path.exists(path) + #result = Cygwin.cmdExecute('IF EXIST "' + path + '" echo YES') + #return string.find(result[1], 'YES',) + + #Small function to check if the mention location is a directory + def isDirectory(self, path): + result = checkResult(Cygwin.cmdExecute('dir ' + path + ' | FIND ".."')) + return string.find(result[1], 'DIR',) + + def mapNetworkDrive(self, drive, networkPath, user, password): + self.unmapNetworkDrive(drive) + #Check for drive availability + if os.path.exists(drive): + logger.error("Drive letter is already in use: " + drive) + return -1 + #Check for network resource availability + retry = 5 + while not os.path.exists(networkPath): + time.sleep(1) + if retry == 0: + return -1 + logger.info("Path not accessible: " + networkPath + " retrying") + retry-=1 + #return -1 + + command = 'USE ' + drive + ' ' + networkPath + ' /PERSISTENT:NO' + if user != None: + command += ' ' + password + ' /User' + user + + #TODO: Execute 'NET USE' command with authentication + result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command)) + if string.find(result[1], 'successfully',) == -1: + logger.error("Failed: NET " + command) + return -1 + return 1 + + def unmapNetworkDrive(self, drive): + drives = self.getNetworkDrives() + if drive not in drives.keys(): + return 1 + result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE ' + drive + ' /DELETE /YES')) + if string.find(str(result[1]), 'successfully',) == -1: + logger.error(result[2]) + return -1 + return 1 + + def getNetworkDrives(self): + ip = VMManager.getHostOnlyIP(None) + ip = ip[:ip.rindex('.')] + drives = dict() + result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE')) + for line in result[1].splitlines(): + if ip in line: + parts = line.split() + drives[parts[1]] = parts[2] + return drives + + def genNetworkDrive(self): + network_drives = self.getNetworkDrives() + logical_drives = VMManager.getLogicalDrives() + drives = list(map(chr, range(68, 91))) + for drive in drives: + if drive+':' not in network_drives and drive not in logical_drives: + return drive+':' + + def getNetworkDrive(self, vm_name): + ip = self.getHostOnlyIP(vm_name) + result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE')) + for line in result[1].splitlines(): + if line != None and ip in line: + parts = line.split() + return parts[0] + @staticmethod + def getLogicalDrives(): + drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives() + return list(itertools.compress(string.ascii_uppercase, map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1]))) + + @staticmethod + def getDriveType(drive): + return ctypes.cdll.kernel32.GetDriveTypeW(u"%s:\\"%drive) + + @staticmethod + def getVolumeInfo(drive): + volumeNameBuffer = ctypes.create_unicode_buffer(1024) + fileSystemNameBuffer = ctypes.create_unicode_buffer(1024) + serial_number = None + max_component_length = None + file_system_flags = None + + rc = ctypes.cdll.kernel32.GetVolumeInformationW( + #ctypes.c_wchar_p("F:\\"), + u"%s:\\"%drive, + volumeNameBuffer, + ctypes.sizeof(volumeNameBuffer), + serial_number, + max_component_length, + file_system_flags, + fileSystemNameBuffer, + ctypes.sizeof(fileSystemNameBuffer) + ) + + return volumeNameBuffer.value, fileSystemNameBuffer.value + + @staticmethod + def stop(): + """stop all running infinite loops now --> needed for gracefull shutdown""" + _running = False + + + +def checkResult(result): + if result[0] != 0: + logger.error('Command failed:' + ''.join(result[2])) + raise OpenSecurityException('Command failed:' + ''.join(result[2])) + return result + +# handles browsing request +class BrowsingHandler(threading.Thread): + vmm = None + def __init__(self, vmmanager): + threading.Thread.__init__(self) + self.vmm = vmmanager + + def run(self): + try: + new_sdvm = self.vmm.generateSDVMName() + self.vmm.createVM(new_sdvm) + self.vmm.storageAttach(new_sdvm) + self.vmm.genCertificateISO(new_sdvm) + self.vmm.attachCertificateISO(new_sdvm) + self.vmm.startVM(new_sdvm) + new_ip = self.vmm.waitStartup(new_sdvm) + drive = self.vmm.genNetworkDrive() + if new_ip != None: + self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\Download', None, None) + result = checkResult(Cygwin.sshExecuteX11('/usr/bin/iceweasel', new_ip, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + new_sdvm + '/dvm_key')) + except: + logger.error("BrowsingHandler failed. Cleaning up") + + self.vmm.unmapNetworkDrive(drive) + self.vmm.poweroffVM(new_sdvm) + self.vmm.removeVM(new_sdvm) + +class DeviceHandler(threading.Thread): + vmm = None + #handleDeviceChangeLock = threading.Lock() + attachedRSDs = None + connectedRSDs = None + running = True + def __init__(self, vmmanger): + threading.Thread.__init__(self) + self.vmm = vmmanger + + def stop(self): + self.running = False + + def run(self): + self.connectedRSDs = dict() + self.attachedRSDs = self.vmm.getAttachedRSDs() + while self.running: + tmp_rsds = self.vmm.getConnectedRSDS() + if tmp_rsds.keys() == self.connectedRSDs.keys(): + logger.debug("Nothing's changed. sleep(3)") + time.sleep(3) + continue + + logger.info("Something's changed") + self.connectedRSDs = tmp_rsds + self.attachedRSDs = self.vmm.getAttachedRSDs() + + for vm_name in self.attachedRSDs.keys(): + if self.attachedRSDs[vm_name] not in self.connectedRSDs.values(): + drive = self.vmm.getNetworkDrive(vm_name) + self.vmm.unmapNetworkDrive(drive) + #self.stopVM(vm_name) + self.vmm.detachRSD(vm_name) + self.vmm.poweroffVM(vm_name) + self.vmm.removeVM(vm_name) + #create new vm for attached device if any + self.attachedRSDs = self.vmm.getAttachedRSDs() + self.connectedRSDs = self.vmm.getConnectedRSDS() + + new_ip = None + for connected_device in self.connectedRSDs.values(): + if (self.attachedRSDs and False) or (connected_device not in self.attachedRSDs.values()): + new_sdvm = self.vmm.generateSDVMName() + self.vmm.createVM(new_sdvm) + self.vmm.storageAttach(new_sdvm) + self.vmm.attachRSD(new_sdvm, connected_device) + self.vmm.startVM(new_sdvm) + new_ip = self.vmm.waitStartup(new_sdvm) + drive = self.vmm.genNetworkDrive() + if new_ip != None: + self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\USB', None, None) + +if __name__ == '__main__': + #man = VMManager.getInstance() + #man.listVM() + #print man.getConnectedRSDs() + #print man.getNetworkDrives() + #man.genNetworkDrive() + #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives() + #print list(itertools.compress(string.ascii_uppercase, map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1]))) + #print list(map(chr, range(68, 91))) + #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0] + #devices = VMManager.getConnectedRSDS() + #print devices + + drives = VMManager.getLogicalDrives() + print drives + print VMManager.getDriveType("E") + print VMManager.getVolumeInfo("E") + #for device in devices.values(): + # #print device + # if VMManager.isMassStorageDevice(device): + # print device + + + + #time.sleep(-1) + #man.listVM() + #man.listVM() + #man.listVM() + #man.listVM() + #man.genCertificateISO('SecurityDVM0') + #man.guestExecute('SecurityDVM0', '/bin/ls -la') + #logger = setupLogger('VMManager') + #c = Cygwin() + + #man.sshExecute('/bin/ls -la', 'SecurityDVM0') + #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0') + #man.removeVM('SecurityDVM0') + #man.netUse('192.168.56.134', 'USB\\') + #ip = '192.168.56.139' + + #man.cygwin_path = 'c:\\cygwin64\\bin\\' + #man.handleDeviceChange() + #print man.listSDVM() + #man.configureHostNetworking() + #new_vm = man.generateSDVMName() + #man.createVM(new_vm) + + #print Cygwin.cmd() + #man.isAvailable('c:') + #ip = man.getHostOnlyIP('SecurityDVM0') + #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None) + + #man.genCertificateISO(new_vm) + #man.attachCertificateISO(new_vm) + + #man.attachCertificateISO(vm_name) + #man.guestExecute(vm_name, "ls") + #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel') + #time.sleep(60) + #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*") + #man.genCertificateISO('SecurityDVM') + #man.attachCertificateISO('SecurityDVM') + #man.isStorageAttached('SecurityDVM') + #man.guestExecute('SecurityDVM', 'sudo apt-get -y update') + #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' ) + + #man.stopVM('SecurityDVM') + #man.storageDetach('SecurityDVM') + #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable') + #man.storageAttach('SecurityDVM') + + + #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\"" + #man.execute(cmd)