1.1 --- a/server/vmmanager/vmmanager.py Tue Dec 03 18:04:46 2013 +0100
1.2 +++ b/server/vmmanager/vmmanager.py Wed Dec 04 14:30:10 2013 +0100
1.3 @@ -59,6 +59,7 @@
1.4 print res_stderr
1.5 return result, res_stdout, res_stderr
1.6
1.7 + # return hosty system properties
1.8 def getSystemProperties(self):
1.9 cmd = 'VBoxManage list systemproperties'
1.10 result = self.execute(cmd)
1.11 @@ -66,44 +67,12 @@
1.12 return None
1.13 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
1.14 return props
1.15 -
1.16 +
1.17 + # return the folder containing the guest VMs
1.18 def getDefaultMachineFolder(self):
1.19 return self.systemProperties["Default machine folder"]
1.20
1.21 - def createVM(self, vm_name):
1.22 - hostonly_if = self.getHostOnlyIFs()
1.23 - cmd = 'VBoxManage createvm --name ' + vm_name, ' --ostype Debian --register'
1.24 - self.execute(cmd)
1.25 - cmd = 'VBoxManage modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 ' + hostonly_if['Name'] + ' --nic2 nat'
1.26 - self.execute(cmd)
1.27 - cmd = 'VBoxManage storagectl ' + vm_name + ' --name contr1 --add sata --sataportcount 2'
1.28 - self.execute(cmd)
1.29 - cmd = 'VBoxManage storageattach ' + vm_name + ' --storagectl contr1 --port 0 --device 0 --type hdd --mtype normal --medium C:\Users\BarthaM\VirtualBox VMs\SecurityVM\SecurityVM.vdi'
1.30 - self.execute(cmd)
1.31 - return
1.32 -
1.33 - def attachRSD(self, vm_name, filter):
1.34 - cmd = 'VBoxManage usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + filter.vendorid + ' --productid ' + filter.productid + ' --revision ' + filter.revision
1.35 - print self.execute(cmd)
1.36 -
1.37 - def removeVM(self, vm_name):
1.38 - print('removing ' + vm_name)
1.39 - cmd = 'VBoxManage unregistervm', vm_name, '--delete'
1.40 - print self.execute(cmd)
1.41 - machineFolder = self.getDefaultMachineFolder()
1.42 - cmd = self.cygwin_path+'bash.exe --login -c \"rm -rf ' + machineFolder + '\\' + vm_name + '*\"'
1.43 - print self.execute(cmd)
1.44 -
1.45 - def startVM(self, vm_name):
1.46 - print('starting ' + vm_name)
1.47 - cmd = 'VBoxManage startvm ' + vm_name + ' --type headless'
1.48 - print self.execute(cmd)
1.49 -
1.50 - def stopVM(self, vm_name):
1.51 - print('stopping ' + vm_name)
1.52 - cmd = 'VBoxManage controlvm ' + vm_name + ' poweroff'
1.53 - print self.execute(cmd)
1.54 -
1.55 + #list the hostonly IFs exposed by the VBox host
1.56 def getHostOnlyIFs(self):
1.57 cmd = ['VBoxManage list hostonlyifs']
1.58 result = self.execute(cmd)
1.59 @@ -112,15 +81,6 @@
1.60 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
1.61 return props
1.62
1.63 - def getHostOnlyIP(self, vm_name):
1.64 - print('gettting hostOnly IP address ' + vm_name)
1.65 - cmd = 'VBoxManage guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'
1.66 - result = self.execute(cmd)
1.67 - if result=='':
1.68 - return None
1.69 - result = result[1]
1.70 - return result[result.index(':')+1:].strip()
1.71 -
1.72 def listRSDS(self):
1.73 cmd = 'VBoxManage list usbhost'
1.74 results = self.execute(cmd)
1.75 @@ -144,12 +104,14 @@
1.76 print filter
1.77 return rsds
1.78
1.79 + # list all existing VMs registered with VBox
1.80 def listVM(self):
1.81 cmd = 'VBoxManage list vms'
1.82 result = self.execute(cmd)
1.83 vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
1.84 return vms
1.85
1.86 + # list existing SDVMs
1.87 def listSDVM(self):
1.88 vms = self.listVM()
1.89 svdms = []
1.90 @@ -158,6 +120,7 @@
1.91 svdms.append(vm)
1.92 return svdms
1.93
1.94 + # generate valid (not already existing SDVM name). necessary for creating a new VM
1.95 def generateSDVMName(self):
1.96 vms = self.listVM()
1.97 for i in range(0,999):
1.98 @@ -165,12 +128,90 @@
1.99 return self.vmRootName+str(i)
1.100 return ''
1.101
1.102 + # return the RSDs attached to all existing SDVMs
1.103 + def getAttachedRSDs(self):
1.104 + vms = self.listSDVM()
1.105 + attached_devices = dict()
1.106 + for vm in vms:
1.107 + rsd_filter = self.getUSBFilter(vm)
1.108 + if filter != None:
1.109 + attached_devices[vm] = rsd_filter
1.110 + return attached_devices
1.111 +
1.112 + # configures hostonly networking and DHCP server. requires admin rights
1.113 + def configureHostNetworking(self):
1.114 + #cmd = 'vboxmanage list hostonlyifs'
1.115 + #self.execute(cmd)
1.116 + #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"'
1.117 + #self.execute(cmd)
1.118 + #cmd = 'vboxmanage hostonlyif create'
1.119 + #self.execute(cmd)
1.120 + cmd = 'vboxmanage hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'
1.121 + self.execute(cmd)
1.122 + #cmd = 'vboxmanage dhcpserver add'
1.123 + #self.execute(cmd)
1.124 + 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'
1.125 + self.execute(cmd)
1.126 +
1.127 + #create new virtual machine instance based on template vm named SecurityVM (\SecurityVM\SecurityVM.vdi)
1.128 + def createVM(self, vm_name):
1.129 + hostonly_if = self.getHostOnlyIFs()
1.130 + machineFolder = self.getDefaultMachineFolder()
1.131 + cmd = 'VBoxManage createvm --name ' + vm_name, ' --ostype Debian --register'
1.132 + self.execute(cmd)
1.133 + cmd = 'VBoxManage modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 ' + hostonly_if['Name'] + ' --nic2 nat'
1.134 + self.execute(cmd)
1.135 + cmd = 'VBoxManage storagectl ' + vm_name + ' --name contr1 --add sata --sataportcount 2'
1.136 + self.execute(cmd)
1.137 + cmd = 'VBoxManage storageattach ' + vm_name + ' --storagectl contr1 --port 0 --device 0 --type hdd --mtype normal --medium '+ machineFolder + '\SecurityVM\SecurityVM.vdi'
1.138 + self.execute(cmd)
1.139 + return
1.140 +
1.141 + #remove VM from the system. should be used on VMs returned by listSDVMs
1.142 + def removeVM(self, vm_name):
1.143 + print('removing ' + vm_name)
1.144 + cmd = 'VBoxManage unregistervm', vm_name, '--delete'
1.145 + print self.execute(cmd)
1.146 + machineFolder = self.getDefaultMachineFolder()
1.147 + cmd = self.cygwin_path+'bash.exe --login -c \"rm -rf ' + machineFolder + '\\' + vm_name + '*\"'
1.148 + print self.execute(cmd)
1.149 +
1.150 + # start VM
1.151 + def startVM(self, vm_name):
1.152 + print('starting ' + vm_name)
1.153 + cmd = 'VBoxManage startvm ' + vm_name + ' --type headless'
1.154 + print self.execute(cmd)
1.155 +
1.156 + # stop VM
1.157 + def stopVM(self, vm_name):
1.158 + print('stopping ' + vm_name)
1.159 + cmd = 'VBoxManage controlvm ' + vm_name + ' poweroff'
1.160 + print self.execute(cmd)
1.161 +
1.162 + # return the hostOnly IP for a running guest
1.163 + def getHostOnlyIP(self, vm_name):
1.164 + print('gettting hostOnly IP address ' + vm_name)
1.165 + cmd = 'VBoxManage guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'
1.166 + result = self.execute(cmd)
1.167 + if result=='':
1.168 + return None
1.169 + result = result[1]
1.170 + return result[result.index(':')+1:].strip()
1.171 +
1.172 + # attach removable storage device to VM by provision of filter
1.173 + def attachRSD(self, vm_name, rsd_filter):
1.174 + cmd = 'VBoxManage usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision
1.175 + print self.execute(cmd)
1.176 +
1.177 +
1.178 + # return the description set for an existing VM
1.179 def getVMInfo(self, vm_name):
1.180 cmd = 'VBoxManage showvminfo ' + vm_name + ' --machinereadable'
1.181 results = self.execute(cmd)
1.182 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
1.183 return props
1.184
1.185 + # return the configured USB filter for an existing VM
1.186 def getUSBFilter(self, vm_name):
1.187 props = self.getVMInfo(vm_name)
1.188 keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1'])
1.189 @@ -180,15 +221,6 @@
1.190 usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1'])
1.191 return usb_filter
1.192
1.193 - def getAttachedRSDs(self):
1.194 - vms = self.listSDVM()
1.195 - attached_devices = dict()
1.196 - for vm in vms:
1.197 - filter = self.getUSBFilter(vm)
1.198 - if filter != None:
1.199 - attached_devices[vm] = filter
1.200 - return attached_devices
1.201 -
1.202 #generates ISO containing authorized_keys for use with guest VM
1.203 def genCertificateISO(self, vm_name):
1.204 machineFolder = self.getDefaultMachineFolder()
1.205 @@ -239,39 +271,23 @@
1.206 self.startVM(new_sdvm)
1.207
1.208 # executes command over ssh on guest vm
1.209 - def sshGuestExecute(self, vm_name, prog):
1.210 + def sshGuestExecute(self, vm_name, prog, user_name='opensec'):
1.211 # get vm ip
1.212 address = self.getHostOnlyIP(vm_name)
1.213 machineFolder = self.getDefaultMachineFolder()
1.214 # run command
1.215 - cmd = self.cygwin_path+'bash.exe --login -c \"ssh -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" bartham@' + address + ' ' + prog + '\"'
1.216 + cmd = self.cygwin_path+'bash.exe --login -c \"ssh -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" ' + user_name + '@' + address + ' ' + prog + '\"'
1.217 return self.execute(cmd)
1.218
1.219 # executes command over ssh on guest vm with X forwarding
1.220 - def sshGuestX11Execute(self, vm_name, prog):
1.221 + def sshGuestX11Execute(self, vm_name, prog, user_name='opensec'):
1.222 #TODO: verify if X server is running on user account
1.223 #TODO: set DISPLAY accordingly
1.224 address = self.getHostOnlyIP(vm_name)
1.225 machineFolder = self.getDefaultMachineFolder()
1.226 # run command
1.227 - cmd = self.cygwin_path+'bash.exe --login -c \"DISPLAY=:0 ssh -Y -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" bartham@' + address + ' ' + prog + '\"'
1.228 + cmd = self.cygwin_path+'bash.exe --login -c \"DISPLAY=:0 ssh -Y -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" ' + user_name + '@' + address + ' ' + prog + '\"'
1.229 return self.execute(cmd)
1.230 -
1.231 - # configures hostonly networking and DHCP server
1.232 - # requires admin rights
1.233 - def configureHostNetworking(self):
1.234 - #cmd = 'vboxmanage list hostonlyifs'
1.235 - #self.execute(cmd)
1.236 - #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"'
1.237 - #self.execute(cmd)
1.238 - #cmd = 'vboxmanage hostonlyif create'
1.239 - #self.execute(cmd)
1.240 - cmd = 'vboxmanage hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'
1.241 - self.execute(cmd)
1.242 - #cmd = 'vboxmanage dhcpserver add'
1.243 - #self.execute(cmd)
1.244 - 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'
1.245 - self.execute(cmd)
1.246
1.247 # executes NET USE and connects to samba share on guestos
1.248 def netUse(self, vm_name):