2 Created on Nov 19, 2013
8 from subprocess import Popen, PIPE, call
20 def __init__(self, vendorid, productid, revision):
21 self.vendorid = vendorid.lower()
22 self.productid = productid.lower()
23 self.revision = revision.lower()
26 def __eq__(self, other):
27 return self.vendorid == other.vendorid and self.productid == other.productid and self.revision == other.revision
30 return hash(self.vendorid) ^ hash(self.productid) ^ hash(self.revision)
33 return "VendorId = \'" + str(self.vendorid) + "\' ProductId = \'" + str(self.productid) + "\' Revision = \'" + str(self.revision) + "\'"
36 class VMManager(object):
37 vmRootName = "SecurityDVM"
38 systemProperties = None
39 cygwin_path = 'c:\\cygwin64\\bin\\'
42 self.systemProperties = self.getSystemProperties()
43 #TODO: get cygwin path externally
46 def execute(self, cmd):
48 print('trying to launch: ' + cmd)
49 process = Popen(cmd, stdout=PIPE, stderr=PIPE)
51 print('launched: ' + cmd)
52 result = process.wait()
53 res_stdout = process.stdout.read();
54 res_stderr = process.stderr.read();
60 return result, res_stdout, res_stderr
62 # return hosty system properties
63 def getSystemProperties(self):
64 cmd = 'VBoxManage list systemproperties'
65 result = self.execute(cmd)
68 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
71 # return the folder containing the guest VMs
72 def getDefaultMachineFolder(self):
73 return self.systemProperties["Default machine folder"]
75 #list the hostonly IFs exposed by the VBox host
76 def getHostOnlyIFs(self):
77 cmd = 'VBoxManage list hostonlyifs'
78 result = self.execute(cmd)[1]
81 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
85 cmd = 'VBoxManage list usbhost'
86 results = self.execute(cmd)[1]
87 results = results.split('Host USB Devices:')[1].strip()
89 items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
93 for line in item.splitlines():
95 k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
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;
107 # list all existing VMs registered with VBox
109 cmd = 'VBoxManage list vms'
110 result = self.execute(cmd)[1]
111 vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
114 # list existing SDVMs
119 if vm.startswith(self.vmRootName) and vm != self.vmRootName:
123 # generate valid (not already existing SDVM name). necessary for creating a new VM
124 def generateSDVMName(self):
126 for i in range(0,999):
127 if(not self.vmRootName+str(i) in vms):
128 return self.vmRootName+str(i)
131 # return the RSDs attached to all existing SDVMs
132 def getAttachedRSDs(self):
133 vms = self.listSDVM()
134 attached_devices = dict()
136 rsd_filter = self.getUSBFilter(vm)
137 if rsd_filter != None:
138 attached_devices[vm] = rsd_filter
139 return attached_devices
141 # configures hostonly networking and DHCP server. requires admin rights
142 def configureHostNetworking(self):
143 #cmd = 'vboxmanage list hostonlyifs'
145 #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"'
147 #cmd = 'vboxmanage hostonlyif create'
149 cmd = 'vboxmanage hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'
151 #cmd = 'vboxmanage dhcpserver add'
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'
156 #create new virtual machine instance based on template vm named SecurityDVM (\SecurityDVM\SecurityDVM.vmdk)
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'
162 cmd = 'VBoxManage modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat'
164 cmd = 'VBoxManage storagectl ' + vm_name + ' --name contr1 --add sata --portcount 2'
166 cmd = 'VBoxManage storageattach ' + vm_name + ' --storagectl contr1 --port 0 --device 0 --type hdd --medium \"'+ machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"'
171 #remove VM from the system. should be used on VMs returned by listSDVMs
172 def removeVM(self, vm_name):
173 print('removing ' + vm_name)
174 cmd = 'VBoxManage unregistervm', vm_name, '--delete'
175 print self.execute(cmd)
176 machineFolder = self.getDefaultMachineFolder()
177 cmd = self.cygwin_path+'bash.exe --login -c \"rm -rf ' + machineFolder + '\\' + vm_name + '*\"'
178 print self.execute(cmd)
181 def startVM(self, vm_name):
182 print('starting ' + vm_name)
183 cmd = 'VBoxManage startvm ' + vm_name + ' --type headless'
184 print self.execute(cmd)
187 def stopVM(self, vm_name):
188 print('stopping ' + vm_name)
189 cmd = 'VBoxManage controlvm ' + vm_name + ' poweroff'
190 print self.execute(cmd)
192 # return the hostOnly IP for a running guest
193 def getHostOnlyIP(self, vm_name):
194 print('gettting hostOnly IP address ' + vm_name)
195 cmd = 'VBoxManage guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'
196 result = self.execute(cmd)
200 return result[result.index(':')+1:].strip()
202 # attach removable storage device to VM by provision of filter
203 def attachRSD(self, vm_name, rsd_filter):
204 cmd = 'VBoxManage usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision
205 print self.execute(cmd)
208 # return the description set for an existing VM
209 def getVMInfo(self, vm_name):
210 cmd = 'VBoxManage showvminfo ' + vm_name + ' --machinereadable'
211 results = self.execute(cmd)[1]
212 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
215 # return the configured USB filter for an existing VM
216 def getUSBFilter(self, vm_name):
217 props = self.getVMInfo(vm_name)
218 keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1'])
219 keyset = set(props.keys())
221 if keyset.issuperset(keys):
222 usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1'])
225 #generates ISO containing authorized_keys for use with guest VM
226 def genCertificateISO(self, vm_name):
227 machineFolder = self.getDefaultMachineFolder()
228 # create .ssh folder in vm_name
229 cmd = self.cygwin_path+'bash.exe --login -c \"mkdir -p \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\\"\"'
230 result = self.execute(cmd)
231 # generate dvm_key pair in vm_name / .ssh
232 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" |',
233 result = self.execute(cmd)
234 # set permissions for keys
235 #TODO: test without chmod
236 cmd = self.cygwin_path+'bash.exe --login -c \"chmod 500 \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\*\\\"\"'
237 result = self.execute(cmd)
238 # move out private key
239 cmd = self.cygwin_path+'bash.exe --login -c \"mv \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\dvm_key\\\" \\\"' + machineFolder + '\\' + vm_name + '\\\"'
240 result = self.execute(cmd)
241 # rename public key to authorized_keys
242 cmd = self.cygwin_path+'bash.exe --login -c \"mv \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\dvm_key.pub\\\" \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\authorized_keys\\\"'
243 result = self.execute(cmd)
244 # generate iso image with .ssh/authorized keys
245 cmd = self.cygwin_path+'bash.exe --login -c \"/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\\\" \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\\"\"'
246 result = self.execute(cmd)
248 # attaches generated ssh public cert to guest vm
249 def attachCertificateISO(self, vm_name):
250 machineFolder = self.getDefaultMachineFolder()
251 cmd = 'vboxmanage storageattach ' + vm_name + ' --storagectl contr1 --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'
252 result = self.execute(cmd)
255 # handles device change events
256 def handleDeviceChange(self):
257 attached_devices = self.getAttachedRSDs()
258 connected_devices = self.listRSDS()
259 for vm_name in attached_devices.keys():
260 if connected_devices and attached_devices[vm_name] not in connected_devices.values():
261 # self.netUse(vm_name)
263 self.removeVM(vm_name)
265 attached_devices = self.getAttachedRSDs()
266 for connected_device in connected_devices.values():
267 if attached_devices or connected_device not in attached_devices.values():
268 new_sdvm = self.generateSDVMName()
269 self.createVM(new_sdvm)
270 self.attachRSD(new_sdvm, connected_device)
271 self.startVM(new_sdvm)
272 self.netUse(new_sdvm)
274 def handleBrowsingRequest(self):
275 new_sdvm = self.generateSDVMName()
276 self.createVM(new_sdvm)
277 self.genCertificateISO(new_sdvm)
278 self.attachCertificateISO(new_sdvm)
280 # executes command over ssh on guest vm
281 def sshGuestExecute(self, vm_name, prog, user_name='opensec'):
283 address = self.getHostOnlyIP(vm_name)
284 machineFolder = self.getDefaultMachineFolder()
286 cmd = self.cygwin_path+'bash.exe --login -c \"ssh -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" ' + user_name + '@' + address + ' ' + prog + '\"'
287 return self.execute(cmd)
289 # executes command over ssh on guest vm with X forwarding
290 def sshGuestX11Execute(self, vm_name, prog, user_name='opensec'):
291 #TODO: verify if X server is running on user account
292 #TODO: set DISPLAY accordingly
293 address = self.getHostOnlyIP(vm_name)
294 machineFolder = self.getDefaultMachineFolder()
296 cmd = self.cygwin_path+'bash.exe --login -c \"DISPLAY=:0 ssh -Y -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" ' + user_name + '@' + address + ' ' + prog + '\"'
297 return self.execute(cmd)
299 # executes NET USE and connects to samba share on guestos
300 def netUse(self, vm_name):
301 ip = self.getHostOnlyIP(vm_name)
302 cmd = 'net use H: \\' + ip + '\USB'
303 return self.execute(cmd)
306 if __name__ == '__main__':
308 man.cygwin_path = 'c:\\cygwin64\\bin\\'
309 #man.handleDeviceChange()
310 #print man.listSDVM()
311 #man.configureHostNetworking()
312 new_vm = man.generateSDVMName()
314 man.genCertificateISO(new_vm)
315 man.attachCertificateISO(new_vm)
317 #man.attachCertificateISO(vm_name)
318 #man.sshGuestExecute(vm_name, "ls")
319 #man.sshGuestX11Execute(vm_name, "iceweasel")
320 #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""