Пример #1
0
	def get(self, name, disk):
		if self.get_secure_cookie("AuthUsername") == '':
			self.render("NotPermitted.html")
			return
		p= Permission(self.database)
		name=urllib.unquote(name)
		if p.verifyUserToGuestPermission(self.get_secure_cookie("AuthUsername"), name, 6)  == 0:
			self.render("NotPermitted.html")
			return
		try:
			vm = lvconn.lookupByName(name)
			xml = vm.XMLDesc(1)
			xml = minidom.parseString(xml)
			os = xml.getElementsByTagName("os")
			os= os[0]
			devices = xml.getElementsByTagName("devices")
			devices= devices[0]
			boot = xml.getElementsByTagName("boot")
			for tmpboot in boot:
				os.removeChild(tmpboot)
			boot = xml.createElement("boot")
			if disk=="disk":
				disk="hd"
			boot.setAttribute("dev", disk)
			os.appendChild(boot)
			if vm.isActive():
				vm.destroy()
			vm.undefine()
			new = lvconn.defineXML( xml.toxml())
			new.create()
		except:
			print("err")
			pass
		self.redirect('/guest/'+name+'/')
Пример #2
0
def genXML(s):
    """
    Generate a setting file for the container to be defined from s
    """
    doc = Document()
    #root
    domain = doc.createElement("domain")
    domain.setAttribute("type", "lxc")
    doc.appendChild(domain)
    #name
    name=doc.createElement("name")
    node=doc.createTextNode(s["name"]) 
    name.appendChild(node)
    domain.appendChild(name)
    #memory
    memory=doc.createElement("memory")
    node=doc.createTextNode(s["maxMem"]) 
    memory.appendChild(node)
    domain.appendChild(memory)
    
    #currentMemory
    curMem=doc.createElement("currentMemory")
    node=doc.createTextNode(s["curMem"])
    curMem.appendChild(node)
    domain.appendChild(curMem)
    
    #memtune
    memtune=doc.createElement("memtune")
    
    hard_limit=doc.createElement("hard_limit")
    node=doc.createTextNode(s["hard_limit"])
    hard_limit.appendChild(node)
    memtune.appendChild(hard_limit)
    
    soft_limit=doc.createElement("soft_limit")
    node=doc.createTextNode(s["soft_limit"])
    soft_limit.appendChild(node)
    memtune.appendChild(soft_limit)
    
    swap_hard_limit=doc.createElement("swap_hard_limit")
    node=doc.createTextNode(s["swap_hard_limit"])
    swap_hard_limit.appendChild(node)
    memtune.appendChild(swap_hard_limit)
    
    domain.appendChild(memtune)
    
    
    #vcpus
    vcpu=doc.createElement("vcpu")
    node=doc.createTextNode(s["vcpus"]) 
    vcpu.appendChild(node)
    domain.appendChild(vcpu)
    
    #cputune
    cputune=doc.createElement("cputune")
    shares=doc.createElement("shares")
    node=doc.createTextNode(s["shares"])
    shares.appendChild(node)
    cputune.appendChild(shares)
    
    domain.appendChild(cputune)
    
    #os
    os=doc.createElement("os")
    type=doc.createElement("type")
    node=doc.createTextNode("exe") 
    type.appendChild(node)
    os.appendChild(type)
    
    init=doc.createElement("init")
    node=doc.createTextNode("/bin/sh") 
    init.appendChild(node)
    os.appendChild(init)
    
    domain.appendChild(os)
    
    #clock
    clock=doc.createElement("clock")
    clock.setAttribute("offset", "utc")
    domain.appendChild(clock)
    
    #on_poweroff,on_reboot,on_crash
    on_poweroff=doc.createElement("on_poweroff")
    node=doc.createTextNode("destroy") 
    on_poweroff.appendChild(node)
    domain.appendChild(on_poweroff)
    
    on_reboot=doc.createElement("on_reboot")
    node=doc.createTextNode("restart")
    on_reboot.appendChild(node)
    domain.appendChild(on_reboot)
    
    on_crash=doc.createElement("on_crash")
    node=doc.createTextNode("destroy")
    on_crash.appendChild(node)
    domain.appendChild(on_crash)
    
    #devices
    devices=doc.createElement("devices")
    
    console=doc.createElement("console")
    console.setAttribute("type", "pty")
    devices.appendChild(console)
    
    emulator=doc.createElement("emulator")
    node=doc.createTextNode("/usr/lib/libvirt/libvirt_lxc")
    emulator.appendChild(node)
    devices.appendChild(emulator)
    
    #2 network types are provided
    interface=doc.createElement("interface")
    if s["nettype"]=="default":
        interface.setAttribute("type", "network")
    else:
        interface.setAttribute("type", s["nettype"])
    source=doc.createElement("source")
    source.setAttribute("network", s["netlink"])
    interface.appendChild(source)
    devices.appendChild(interface)
    
    domain.appendChild(devices)
    
    
    #Generate XML file
    path="./static/xmls/"+s["name"]+".xml"
    f = open(path,'w')
    f.write(doc.toprettyxml(indent =''))
    f.close()   
    return doc
Пример #3
0
	def post(self):
		if self.get_secure_cookie("AuthUsername") == '':
			self.render("NotPermitted.html")
			return
        
		guest_name = self.get_secure_cookie("AuthUsername")+"_"+self.get_argument("name")
		folder = conf.images_dir


		domain_xml =  xml.dom.minidom.Document()

		domain = domain_xml.createElement('domain')
		if self.get_argument("arch") == "i686":
			domain.setAttribute('type', 'kvm')
		else:
			domain.setAttribute('type', 'qemu')

		name = domain_xml.createElement('name')
		name.appendChild(domain_xml.createTextNode(guest_name ))
		domain.appendChild(name)

		memory = domain_xml.createElement('memory')
		memory.appendChild(domain_xml.createTextNode(str( int(self.get_argument("ram"))*1024 )))
		domain.appendChild(memory)

		currentMemory = domain_xml.createElement('currentMemory')
		currentMemory.appendChild(domain_xml.createTextNode( str( int(self.get_argument("ram")))))
		
		os = domain_xml.createElement('os')
		
		ostmp = domain_xml.createElement('type')
		ostmp.setAttribute('arch', self.get_argument("arch"))
		ostmp.setAttribute('machine', 'pc')
		
		if self.get_argument("arch") == "i686":
			ostmp.appendChild(domain_xml.createTextNode('hvm'))
		else:
			ostmp.appendChild(domain_xml.createTextNode('linux'))
			
		os.appendChild(ostmp)


		ostmp = domain_xml.createElement('boot')

		if  self.get_argument("hdd_image") == '0':
			ostmp.setAttribute('dev', 'cdrom')
		else:
			ostmp.setAttribute('dev', 'hd')
		os.appendChild(ostmp)

		domain.appendChild(os)

		devices = domain_xml.createElement('devices')

		vnc = domain_xml.createElement('graphics')
		vnc.setAttribute('passwd', str(random.randint(1111,9999)))
		vnc.setAttribute('autoport', 'yes')
		vnc.setAttribute('type', 'vnc')
		devices.appendChild(vnc)

		devicestmp =  domain_xml.createElement('emulator')
		devicestmp.appendChild(domain_xml.createTextNode('/usr/bin/kvm'))
		devices.appendChild(devicestmp)

		if  self.get_argument("hdd_image") == '0':
			disk =  domain_xml.createElement('disk')
			disk.setAttribute('type', 'file')
			disk.setAttribute('device', 'cdrom')

			disktmp = domain_xml.createElement('source')
			disktmp.setAttribute('file', '%s/%s' % (folder, self.get_argument("cdrom")))
			disk.appendChild(disktmp)

			disktmp = domain_xml.createElement('target')
			disktmp.setAttribute('dev', 'hdc')
			disk.appendChild(disktmp)

			disktmp = domain_xml.createElement('readonly')
			disk.appendChild(disktmp)

			devices.appendChild(disk)
		else:
			pass

		disk =  domain_xml.createElement('disk')
		disk.setAttribute('type' , 'file')
		disk.setAttribute('device' , 'disk')

		target = domain_xml.createElement('target')
		target.setAttribute('dev', 'hda')
		target.setAttribute('bus', 'ide')
		disk.appendChild(target)

		driver = domain_xml.createElement('driver')
		driver.setAttribute('name', 'qemu')
		driver.setAttribute('type', 'qcow2')
		disk.appendChild(driver)


		source = domain_xml.createElement('source')
		source.setAttribute('file',  "%s/%s.qcow" % (folder, guest_name))
		disk.appendChild(source)

		if  self.get_argument("hdd_image") == '0':
			cmd = ('/usr/bin/qemu-img create -f qcow2 "%s/%s.qcow" %s' % 
				(folder, guest_name, self.get_argument("disk")))
		else:
			cmd = ('cp %s/%s %s/%s.qcow' % 
				(folder, self.get_argument("hdd_image"), folder, guest_name ))
		print commands.getstatusoutput(cmd)
		
		devices.appendChild(disk)


		interface = domain_xml.createElement('interface')
		interface.setAttribute('type', 'network')

		interfacetmp = domain_xml.createElement('source')
		interfacetmp.setAttribute('network', 'default')
		interface.appendChild(interfacetmp)
		
		interfacetmp=  domain_xml.createElement('model')
		interfacetmp.setAttribute('type', 'e1000')
		interface.appendChild(interfacetmp)

		devices.appendChild(interface)
		  
		domain.appendChild(devices)
	
		try: 
			new = lvconn.defineXML( domain.toxml())
			new.create()
			self.cursor.execute("SELECT CreateGuest('%s', '%s')" %
					(guest_name, self.get_secure_cookie("AuthUsername")))
		except:
			print("ERROR: SELECT CreateGuest('%s', '%s')" %
                   (guest_name, self.get_secure_cookie("AuthUsername")))
			self.database.rollback()
			self.cursor.execute("SELECT * FROM disk;")
			self.database.commit()
			disks = list()
			for disk in self.cursor:
				disk_list = list()
				disk_list.append(disk[0])
				disk_list.append(disk[1])
				
				comando = "ps aux | grep '%s' | grep aria2c | grep -v grep | wc -l" %disk[1]
				if  int(commands.getstatusoutput(comando)[1]) == 0:
					disk_list.append(1)
				else:
					disk_list.append(0)
				disks.append(disk_list)
			self.render("NewGuest.html", error = 1, disks = disks, hdd_images = list())
		else:
			self.database.commit()
			self.redirect('/guests/')
Пример #4
0
def exportNmapXML(pcap, out='nmapout-p0f3p.xml', retdom=False):
    import time
    doc = Document()
    pyobj = processCaptures(pcap)
    root = doc.createElement('nmaprun')
    pi = doc.createProcessingInstruction(
        'xml-stylesheet',
        'type="text/xsl" href="file:///usr/local/bin/../share/nmap/nmap.xsl"')
    first = doc.firstChild
    doc.insertBefore(pi, first)
    root.setAttribute('scanner', 'p0fplus')
    t = int(time.time())
    root.setAttribute('start', str(t))
    ftime = time.ctime(t)
    root.setAttribute('startstr', ftime.replace('  ', ' '))
    doc.appendChild(root)
    for k, v in pyobj.items():
        host = doc.createElement('host')
        root.appendChild(host)
        addr = doc.createElement('address')
        addr.setAttribute('addrtype', 'ipv4')
        k = convB2Str(k)
        addr.setAttribute('ipaddr', k)
        host.appendChild(addr)
        hostnames = doc.createElement('hostnames')
        host.appendChild(hostnames)
        if v['hostnames'] is not None:
            for h in v['hostnames']:
                hostname = doc.createElement('hostname')
                if h[-1] == '.':
                    h = h[:-1]
                hostname.setAttribute('name', h)
                hostnames.appendChild(hostname)
        ports = None
        if v['applications'] is not None:
            for app in v['applications']:
                if 'sport' in app:
                    ports = doc.createElement('ports')
                    host.appendChild(ports)
                    port = doc.createElement('port')
                    port.setAttribute(
                        'protocol',
                        'tcp')  # FIXME: change when UDP is supported
                    port.setAttribute('portid', str(app['sport']))
                    ports.appendChild(port)
                    state = doc.createElement('state')
                    state.setAttribute('state', 'open')
                    state.setAttribute('reason', 'push-ack')
                    port.appendChild(state)
                    service = doc.createElement('service')
                    if 'appname' in app:
                        service.setAttribute('product', app['appname'])
                    if 'version' in app:
                        service.setAttribute('version', app['version'])
                    if 'extrainf' in app:
                        service.setAttribute('extrainfo', app['extrainf'])
                    port.appendChild(service)
        if v['otherports'] is not None:
            for portk, value in v['otherports'].items():
                if value['matched'] is False:
                    if ports is None:
                        ports = doc.createElement('ports')
                        host.appendChild(ports)
                    port = doc.createElement('port')
                    port.setAttribute('protocol', 'tcp')
                    port.setAttribute('portid', str(portk))
                    ports.appendChild(port)
                    state = doc.createElement('state')
                    state.setAttribute('state', 'open')
                    reasonstr = flag2str(value['flag'])
                    if reasonstr is not None:
                        state.setAttribute('reason', reasonstr)
                    port.appendChild(state)
        if 'guesses' in v['matches']:
            os = doc.createElement('os')
            host.appendChild(os)
            osmatch = doc.createElement('osmatch')
            for guess in v['matches']['guesses']:
                if guess['type'] != '!':  #is a system
                    osmatch = doc.createElement('osmatch')
                    namematch = ' '.join(
                        guess['label'].split(':')[2:]).replace('.x', '')
                    osmatch.setAttribute('name', namematch)
                    accuracy = str(100 -
                                   guess['distance'])  # best formula ever!
                    osmatch.setAttribute('accuracy', accuracy)
                    os.appendChild(osmatch)
                    osclass = doc.createElement('osclass')
                    osclass.setAttribute('osfamily', guess['os'])
                    osclass.setAttribute('osgen', guess['version'])
                    osclass.setAttribute('accuracy', accuracy)
                    osmatch.appendChild(osclass)
    if retdom is True:
        return doc  # return only the minidom object
    else:
        doc.writexml(open(out, 'w'), indent="  ", addindent="  ", newl='\n')