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