server/vmmanager/vmmanager.py
author BarthaM
Wed, 04 Dec 2013 14:30:10 +0100
changeset 8 616dca19f52c
parent 7 903480cebdfb
child 11 27aa3b16eebb
permissions -rw-r--r--
small changes in vmmanager.py
     1 '''
     2 Created on Nov 19, 2013
     3 
     4 @author: BarthaM
     5 '''
     6 import os
     7 import os.path
     8 from subprocess import Popen, PIPE, call
     9 import subprocess
    10 import sys
    11 import re
    12 
    13 DEBUG = True
    14 
    15 class USBFilter:
    16     vendorid = ""
    17     productid = ""
    18     revision = ""
    19     
    20     def __init__(self, vendorid, productid, revision):
    21         self.vendorid = vendorid.lower()
    22         self.productid = productid.lower()
    23         self.revision = revision.lower()
    24         return
    25     
    26     def __eq__(self, other):
    27         return self.vendorid == other.vendorid and self.productid == other.productid and self.revision == other.revision
    28     
    29     def __hash__(self):
    30         return hash(self.vendorid) ^ hash(self.productid) ^ hash(self.revision)
    31     
    32     def __repr__(self):
    33         return "VendorId = \'" + str(self.vendorid) + "\' ProductId = \'" + str(self.productid) + "\' Revision = \'" + str(self.revision) + "\'"
    34         
    35 
    36 class VMManager(object):
    37     vmRootName = "SecurityDVM"
    38     systemProperties = None
    39     cygwin_path = 'c:\\cygwin64\\bin\\'
    40     
    41     def __init__(self):
    42         self.systemProperties = self.getSystemProperties()
    43         #TODO: get cygwin path externally
    44         return
    45          
    46     def execute(self, cmd):
    47         if DEBUG:
    48             print('trying to launch: ' + cmd)
    49         process = Popen(cmd, stdout=PIPE, stderr=PIPE)
    50         if DEBUG:
    51             print('launched: ' + cmd)
    52         result = process.wait()
    53         res_stdout = process.stdout.read();
    54         res_stderr = process.stderr.read();
    55         if DEBUG:
    56             if res_stdout != "":
    57                 print res_stdout
    58             if res_stderr != "":
    59                 print res_stderr
    60         return result, res_stdout, res_stderr
    61     
    62     # return hosty system properties
    63     def getSystemProperties(self):
    64         cmd = 'VBoxManage list systemproperties'
    65         result = self.execute(cmd)
    66         if result[1]=='':
    67             return None
    68         props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
    69         return props
    70     
    71     # return the folder containing the guest VMs     
    72     def getDefaultMachineFolder(self):
    73         return self.systemProperties["Default machine folder"]
    74     
    75     #list the hostonly IFs exposed by the VBox host
    76     def getHostOnlyIFs(self):
    77         cmd = ['VBoxManage list hostonlyifs']
    78         result = self.execute(cmd)
    79         if result=='':
    80             return None
    81         props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
    82         return props
    83         
    84     def listRSDS(self):
    85         cmd = 'VBoxManage list usbhost'
    86         results = self.execute(cmd)
    87         results = results.split('Host USB Devices:')[1].strip()
    88         
    89         items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
    90         rsds = dict()   
    91         for item in items:
    92             props = dict()
    93             for line in item.splitlines():
    94                 if line != "":         
    95                     k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
    96                     props[k] = v;
    97             
    98             if 'Product' in props.keys() and props['Product'] == 'Mass Storage':
    99                 usb_filter = USBFilter( re.search(r"\((?P<vid>[0-9A-Fa-f]+)\)", props['VendorId']).groupdict()['vid'], 
   100                                         re.search(r"\((?P<pid>[0-9A-Fa-f]+)\)", props['ProductId']).groupdict()['pid'],
   101                                         re.search(r"\((?P<rev>[0-9A-Fa-f]+)\)", props['Revision']).groupdict()['rev'] )
   102                 rsds[props['UUID']] = usb_filter;
   103                 if DEBUG:
   104                     print filter
   105         return rsds
   106 
   107     # list all existing VMs registered with VBox
   108     def listVM(self):
   109         cmd = 'VBoxManage list vms'
   110         result = self.execute(cmd)
   111         vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
   112         return vms
   113     
   114     # list existing SDVMs
   115     def listSDVM(self):
   116         vms = self.listVM()
   117         svdms = []
   118         for vm in vms:
   119             if vm.startswith(self.vmRootName) and vm != self.vmRootName:
   120                 svdms.append(vm)
   121         return svdms
   122     
   123     # generate valid (not already existing SDVM name). necessary for creating a new VM
   124     def generateSDVMName(self):
   125         vms = self.listVM()
   126         for i in range(0,999):
   127             if(not self.vmRootName+str(i) in vms):
   128                 return self.vmRootName+str(i)
   129         return ''
   130     
   131     # return the RSDs attached to all existing SDVMs
   132     def getAttachedRSDs(self):
   133         vms = self.listSDVM()
   134         attached_devices = dict()
   135         for vm in vms:
   136             rsd_filter = self.getUSBFilter(vm)
   137             if filter != None:
   138                 attached_devices[vm] = rsd_filter
   139         return attached_devices
   140     
   141     # configures hostonly networking and DHCP server. requires admin rights
   142     def configureHostNetworking(self):
   143         #cmd = 'vboxmanage list hostonlyifs'
   144         #self.execute(cmd)
   145         #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"'
   146         #self.execute(cmd)
   147         #cmd = 'vboxmanage hostonlyif create'
   148         #self.execute(cmd)
   149         cmd = 'vboxmanage hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'
   150         self.execute(cmd)
   151         #cmd = 'vboxmanage dhcpserver add'
   152         #self.execute(cmd)
   153         cmd = 'vboxmanage dhcpserver modify --ifname \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0 --lowerip 192.168.56.100 --upperip 192.168.56.255'
   154         self.execute(cmd)
   155     
   156     #create new virtual machine instance based on template vm named SecurityVM (\SecurityVM\SecurityVM.vdi)
   157     def createVM(self, vm_name):
   158         hostonly_if = self.getHostOnlyIFs()
   159         machineFolder = self.getDefaultMachineFolder()
   160         cmd = 'VBoxManage createvm --name ' + vm_name, ' --ostype Debian --register'
   161         self.execute(cmd)
   162         cmd = 'VBoxManage modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 ' + hostonly_if['Name'] + ' --nic2 nat' 
   163         self.execute(cmd)
   164         cmd = 'VBoxManage storagectl ' + vm_name + ' --name contr1 --add sata --sataportcount 2'
   165         self.execute(cmd)
   166         cmd = 'VBoxManage storageattach ' + vm_name + ' --storagectl contr1 --port 0 --device 0 --type hdd --mtype normal --medium '+ machineFolder + '\SecurityVM\SecurityVM.vdi'
   167         self.execute(cmd)
   168         return
   169     
   170     #remove VM from the system. should be used on VMs returned by listSDVMs    
   171     def removeVM(self, vm_name):
   172         print('removing ' + vm_name)
   173         cmd = 'VBoxManage unregistervm', vm_name, '--delete'
   174         print self.execute(cmd)
   175         machineFolder = self.getDefaultMachineFolder()
   176         cmd = self.cygwin_path+'bash.exe --login -c \"rm -rf ' + machineFolder + '\\' + vm_name + '*\"'
   177         print self.execute(cmd)
   178     
   179     # start VM
   180     def startVM(self, vm_name):
   181         print('starting ' +  vm_name)
   182         cmd = 'VBoxManage startvm ' + vm_name + ' --type headless'
   183         print self.execute(cmd)
   184         
   185     # stop VM    
   186     def stopVM(self, vm_name):
   187         print('stopping ' + vm_name)
   188         cmd = 'VBoxManage controlvm ' + vm_name + ' poweroff'
   189         print self.execute(cmd)
   190     
   191     # return the hostOnly IP for a running guest    
   192     def getHostOnlyIP(self, vm_name):
   193         print('gettting hostOnly IP address ' + vm_name)
   194         cmd = 'VBoxManage guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'
   195         result = self.execute(cmd)
   196         if result=='':
   197             return None
   198         result = result[1]
   199         return result[result.index(':')+1:].strip()
   200     
   201     # attach removable storage device to VM by provision of filter
   202     def attachRSD(self, vm_name, rsd_filter):
   203         cmd = 'VBoxManage usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision
   204         print self.execute(cmd)
   205         
   206     
   207     # return the description set for an existing VM
   208     def getVMInfo(self, vm_name):
   209         cmd = 'VBoxManage showvminfo ' + vm_name + ' --machinereadable'
   210         results = self.execute(cmd)
   211         props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
   212         return props
   213     
   214     # return the configured USB filter for an existing VM 
   215     def getUSBFilter(self, vm_name):
   216         props = self.getVMInfo(vm_name)
   217         keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1'])
   218         keyset = set(props.keys())
   219         usb_filter = None
   220         if keyset.issuperset(keys):
   221             usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1'])
   222         return usb_filter
   223     
   224     #generates ISO containing authorized_keys for use with guest VM
   225     def genCertificateISO(self, vm_name):
   226         machineFolder = self.getDefaultMachineFolder()
   227         # create .ssh folder in vm_name
   228         cmd = self.cygwin_path+'bash.exe --login -c \"mkdir -p \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\\"\"'
   229         result = self.execute(cmd)
   230         # generate dvm_key pair in vm_name / .ssh     
   231         cmd = self.cygwin_path+'bash.exe --login -c \"ssh-keygen -q -t rsa -N \\"\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\dvm_key\\\"\"'   #'echo -e "y\\n" |',
   232         result = self.execute(cmd)
   233         # set permissions for keys
   234         #TODO: test without chmod
   235         cmd = self.cygwin_path+'bash.exe --login -c \"chmod 500 \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\*\\\"\"'
   236         result = self.execute(cmd)
   237         # move out private key
   238         cmd = self.cygwin_path+'bash.exe --login -c \"mv \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\dvm_key\\\" \\\"' + machineFolder + '\\' + vm_name + '\\\"'
   239         result = self.execute(cmd)
   240         # rename public key to authorized_keys
   241         cmd = self.cygwin_path+'bash.exe --login -c \"mv \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\dvm_key.pub\\\" \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\authorized_keys\\\"'
   242         result = self.execute(cmd)
   243         # generate iso image with .ssh/authorized keys
   244         cmd = self.cygwin_path+'bash.exe --login -c \"/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\\\" \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\\"\"'
   245         result = self.execute(cmd)
   246     
   247     # attaches generated ssh public cert to guest vm
   248     def attachCertificateISO(self, vm_name):
   249         machineFolder = self.getDefaultMachineFolder()
   250         cmd = 'vboxmanage storageattach ' + vm_name + ' --storagectl contr1 --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'
   251         result = self.execute(cmd)
   252         return result
   253     
   254     # handles device change events
   255     def handleDeviceChange(self):
   256         attached_devices = self.getAttachedRSDs()
   257         connected_devices = self.listRSDS()
   258         for vm_name in attached_devices.keys():
   259             if attached_devices[vm_name] not in connected_devices.values():
   260                 self.stopVM(vm_name)
   261                 self.removeVM(vm_name)
   262         
   263         attached_devices = self.getAttachedRSDs()
   264         for connected_device in connected_devices.values():
   265             if connected_device not in attached_devices.values():
   266                 new_sdvm = self.generateSDVMName()
   267                 self.createVM(new_sdvm)
   268                 self.genCertificateISO(new_sdvm)
   269                 self.attachCertificateISO(new_sdvm)
   270                 self.attachRSD(new_sdvm, connected_device)
   271                 self.startVM(new_sdvm)
   272     
   273     # executes command over ssh on guest vm
   274     def sshGuestExecute(self, vm_name, prog, user_name='opensec'):
   275         # get vm ip
   276         address = self.getHostOnlyIP(vm_name)
   277         machineFolder = self.getDefaultMachineFolder()
   278         # run command
   279         cmd = self.cygwin_path+'bash.exe --login -c \"ssh -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\"  ' + user_name + '@' + address + ' ' + prog + '\"'
   280         return self.execute(cmd)
   281     
   282     # executes command over ssh on guest vm with X forwarding
   283     def sshGuestX11Execute(self, vm_name, prog, user_name='opensec'):
   284         #TODO: verify if X server is running on user account 
   285         #TODO: set DISPLAY accordingly
   286         address = self.getHostOnlyIP(vm_name)
   287         machineFolder = self.getDefaultMachineFolder()
   288         # run command
   289         cmd = self.cygwin_path+'bash.exe --login -c \"DISPLAY=:0 ssh -Y -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\"  '  + user_name + '@' + address + ' ' + prog + '\"'
   290         return self.execute(cmd)    
   291         
   292     # executes NET USE and connects to samba share on guestos 
   293     def netUse(self, vm_name):
   294         ip = self.getHostOnlyIP(vm_name)
   295         cmd = 'net use H: \\' + ip + '\RSD_Device'
   296         return self.execute(cmd)
   297         
   298     
   299 if __name__ == '__main__':
   300     man = VMManager()
   301     man.cygwin_path = 'c:\\cygwin64\\bin\\'
   302     #man.handleDeviceChange()
   303     #print man.listSDVM()
   304     man.configureHostNetworking()
   305     vm_name = "SecurityDVM0"
   306     man.genCertificateISO(vm_name)
   307     #man.attachCertificateISO(vm_name)
   308     #man.sshGuestExecute(vm_name, "ls")
   309     #man.sshGuestX11Execute(vm_name, "iceweasel")
   310     #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
   311     #man.execute(cmd)
   312     
   313     
   314 
   315