def process_running_on_configuration(self, node): simple_cpes = defaultdict(lambda: defaultdict(set)) running_on = defaultdict(lambda: defaultdict(set)) if 'children' not in node: # TODO if this happens, we would need to add a constraint in FMR saying that X cpe needs Y cpe and viceversa. for complex_cpe in node['cpe_match']: aux = CPE(complex_cpe['cpe23Uri']) simple_cpes[aux.get_vendor()[0]][aux.get_product()[0]].update( self.expand(complex_cpe)) else: for subnode in node['children']: isVulnerable = subnode['cpe_match'][0]['vulnerable'] if isVulnerable: # simple cpe for complex_cpe in subnode['cpe_match']: aux = CPE(complex_cpe['cpe23Uri']) simple_cpes[aux.get_vendor()[0]][ aux.get_product()[0]].update( self.expand(complex_cpe)) else: # running on for complex_cpe in subnode['cpe_match']: aux = CPE(complex_cpe['cpe23Uri']) running_on[aux.get_vendor()[0]][ aux.get_product()[0]].update( self.expand(complex_cpe)) return simple_cpes, running_on
def test_get_apache_http_server_cpe(self): self.task._port.service.cpe = 'cpe:2.3:a:apache:http_server:2.4.18:*:*:*:*:*:*:*' expected = [ CPE('cpe:2.3:a:apache:http_server:2.4.18:*:*:*:*:*:*:*'), CPE('cpe:2.3:a:apache:httpd:2.4.18:*:*:*:*:*:*:*') ] result = self.task.get_cpes() self.assertEqual(result, expected)
def test_unique_cpe(self): cpe_1 = CPE('cpe:2.3:o:cisco:ios:12.2\(52\)se:*:*:*:*:*:*:*') cpe_2 = CPE('cpe:2.3:o:cisco:ios:12.2\(52\)se:*:*:*:*:*:*:*') expected = [cpe_1] result = self.task._unique_cpes([cpe_1, cpe_2]) self.assertCountEqual(result, expected)
def convert_cpe23_to_cpe22(cpe: str) -> Tuple[str, bool]: """Convert a CPE v2.3 to a CPE v2.2 returns the CPE v2.2 and True if no product version is given """ # MAKE ME BETTER!!! cpe = CPE(cpe) any_version = False if cpe.get_version()[0] == '*': any_version = True return ( str(CPE(cpe.as_uri_2_3(), CPE.VERSION_2_2)).replace('CPE v2.2: ', ''), any_version, )
def main(): global API parser = argparse.ArgumentParser( description='Giving the CVEs that affect a given CPE', add_help=True) parser.add_argument('--cpe', '-c', action="store", default=None, help="Give one CPE string to the tool") parser.add_argument('--file', '-f', action="store", default=None, help="Import multiple CPE from a file (one per line)") parser.add_argument( '--output', '-o', action="store", default=None, help= "Write results in an output file. Extension is read to choice between PDF and CSV. Default is CSV" ) args = parser.parse_args() cpe = [] print("[*] Check if CPE is well formed ...") try: if args.cpe: cpe.append(CPE(args.cpe)) elif args.file: with open(args.file) as f: for line in f.readlines(): #remove space and newlines char from each line l = line.lower().strip('\n\r') cpe.append(CPE(l)) else: print( "[!] indicate at least a CPE (--cpe) or an input file with one CPE per line (--file)" ) exit(1) except Exception as e: print(f"[!] Bad CPE format: {e}") exit(1) print("[+] Valid CPE") print(f"[*] Searching vulnerabilities for the {len(cpe)} CPE given") vulns = cpe2cve(cpe, API) print("[+] Vulnerabilities computed") print(f"[*] Export to {args.output if args.output else 'stdout'}") export(vulns, args.output) print("[+] Export completed !")
def get_description(self, address, name, cpe): '''Define a description based on hostname and CPE''' if name: return name else: c = CPE(cpe[0], CPE.VERSION_2_3) vendor = c.get_vendor()[0] if self.tacacs and vendor == 'cisco': try: client = SSHClient() client.set_missing_host_key_policy(AutoAddPolicy()) client.connect(address, username=self.tacacs['user'], password=self.tacacs['password']) stdin, stdout, stderr = client.exec_command( self.tacacs['command']) return '{}:{}'.format( vendor.lower(), re.search(self.tacacs['regex'], str(stdout.read().decode('utf-8'))).group( self.tacacs['regroup'])) except (AuthenticationException, SSHException, NoValidConnectionsError, TimeoutError, ConnectionResetError): pass return '{}.{}.{}'.format(c.get_vendor()[0], c.get_product()[0], c.get_version()[0])
def get_description(self, address, name, cpe): '''Define a description based on hostname and CPE''' if name: return name else: c = CPE(cpe[0], CPE.VERSION_2_3) vendor = c.get_vendor()[0].upper() if vendor in self.devs: try: client = SSHClient() client.set_missing_host_key_policy(AutoAddPolicy()) client.connect(address, username=self.devs[vendor]['USER'], password=self.devs[vendor]['PASSWORD']) stdin, stdout, stderr = client.exec_command( self.devs[vendor]['COMMAND']) return '{}:{}'.format( vendor.lower(), re.search(self.devs[vendor]['REGEX'], str(stdout.read().decode('utf-8'))).group( self.devs[vendor]['REGROUP'])) except (AuthenticationException, SSHException, NoValidConnectionsError, TimeoutError, ConnectionResetError): pass return '{}.{}.{}'.format(c.get_vendor()[0], c.get_product()[0], c.get_version()[0])
async def load(self) -> None: """Load HassOS data.""" try: if not self.sys_host.info.cpe: raise NotImplementedError() cpe = CPE(self.sys_host.info.cpe) if cpe.get_product()[0] != "hassos": raise NotImplementedError() except NotImplementedError: _LOGGER.debug("Found no HassOS") return else: self._available = True # Store meta data self._version = cpe.get_version()[0] self._board = cpe.get_target_hardware()[0] await self.sys_dbus.rauc.update() _LOGGER.info("Detect HassOS %s / BootSlot %s", self.version, self.sys_dbus.rauc.boot_slot) with suppress(DockerAPIError): await self.instance.attach(tag="latest")
def config_cpe_match(self, cm): if all("$.vulnerable", cm)[0]: v = PLATFORM.VulnerableConfiguration else: v = PLATFORM.NotVulnerableConfiguration subject = BNode() cveStr = all("$.cpe23Uri", cm)[0] self.triples(subject, v, [(PLATFORM.hasPlatform, cpeURI(cveStr))] + \ self.versionStartExcluding(cm) + self.versionStartIncluding(cm) + self.versionEndExcluding(cm) + self.versionEndIncluding(cm)) #print(cveStr) c = CPE(cveStr) if c.is_hardware(): self.g.add((cpeURI(cveStr), RDF.type, PLATFORM.HardwarePlatform)) elif c.is_application(): self.g.add( (cpeURI(cveStr), RDF.type, PLATFORM.ApplicationPlatform)) elif c.is_operating_system(): self.g.add( (cpeURI(cveStr), RDF.type, PLATFORM.OperatingSystemPlatform)) vendor = "" for i in c.get_vendor(): self.g.add((cpeURI(cveStr), PLATFORM.vendor, self.plEnt(i, "Vendor_", cls=PLATFORM.Vendor))) vendor = i for i in c.get_product(): self.g.add((cpeURI(cveStr), PLATFORM.product, self.plEnt(i, "Product_" + vendor + "_", cls=PLATFORM.Product))) for i in c.get_edition(): self.g.add((cpeURI(cveStr), PLATFORM.edition, self.plEnt(i, "Edition_", cls=PLATFORM.Edition))) for i in c.get_language(): self.g.add((cpeURI(cveStr), PLATFORM.language, self.plEnt(i, "Language_", cls=PLATFORM.Language))) for i in c.get_other(): self.g.add((cpeURI(cveStr), PLATFORM.other, self.plEnt(i, "Other_", cls=PLATFORM.Other))) for i in c.get_software_edition(): self.g.add((cpeURI(cveStr), PLATFORM.softwareEdition, self.plEnt(i, "SoftwareEdition_", cls=PLATFORM.SoftwareEdition))) for i in c.get_target_hardware(): self.g.add((cpeURI(cveStr), PLATFORM.targetHardware, self.plEnt(i, "Hardware_", cls=CORE.Hardware))) for i in c.get_target_software(): self.g.add((cpeURI(cveStr), PLATFORM.targetSoftware, self.plEnt(i, "Software_", cls=CORE.Software))) for i in c.get_update(): if not i == "-": self.g.add((cpeURI(cveStr), PLATFORM.update, Literal(i))) for i in c.get_version(): if not i == "-": self.g.add((cpeURI(cveStr), PLATFORM.version, Literal(i))) return subject
def process_basic_configuration(container: dict) -> dict: ''' Extracts, expands and conditionally expand the CPEs records of a Configuration Container of type BASIC. :param container: A valid container wrapping information about a configuration ''' # First, we need to check the arguments if not container or type(container) is not dict: raise ValueError("container must be a non-empty Parsed JSON Object") if container["configType"] != ConfigType.BASIC.name or container[ 'cpes'] is None: raise ValueError( "container must be a valid structure of type: {} ".format( ConfigType.BASIC.name)) res = defaultdict(lambda: defaultdict(set)) for complex_cpe in container['cpes']: aux = CPE(complex_cpe['cpe23Uri']) res[aux.get_vendor()[0]][aux.get_product()[0]].update( expand(complex_cpe)) return res
def process_basic_configuration(self, node): res = defaultdict(lambda: defaultdict(set)) for complex_cpe in node['cpe_match']: aux = CPE(complex_cpe['cpe23Uri']) res[aux.get_vendor()[0]][aux.get_product()[0]].update( self.expand(complex_cpe)) return res
def process_advanced_configuration(container: dict) -> dict: ''' Extracts, expands and conditionally expand the CPEs records of a Configuration Container of type ADVANCED. ADVANCED containers usually store information recursively, so we need to iterate over all the underlying containers to extract all CPEs. :param container: A valid container wrapping information about a configuration ''' # First, we need to check the arguments if not container or type(container) is not dict: raise ValueError("container must be a non-empty Parsed JSON Object") if container["configType"] != ConfigType.ADVANCED.name or container[ 'containers'] is None: raise ValueError( "container must be a valid structure of type: {} ".format( ConfigType.ADVANCED.name)) res = defaultdict(lambda: defaultdict(set)) for subcontainer in container['containers']: res.update(process_advanced_configuration(subcontainer)) for complex_cpe in container['cpes']: aux = CPE(complex_cpe['cpe23Uri']) res[aux.get_vendor()[0]][aux.get_product()[0]].update( expand(complex_cpe)) return res
async def load(self) -> None: """Load HassOS data.""" try: if not self.sys_host.info.cpe: raise NotImplementedError() cpe = CPE(self.sys_host.info.cpe) os_name = cpe.get_product()[0] if os_name not in ("hassos", "haos"): raise NotImplementedError() except NotImplementedError: _LOGGER.info("No Home Assistant Operating System found") return # Store meta data self._available = True self.sys_host.supported_features.cache_clear() self._version = AwesomeVersion(cpe.get_version()[0]) self._board = cpe.get_target_hardware()[0] self._os_name = cpe.get_product()[0] await self.sys_dbus.rauc.update() await self.datadisk.load() _LOGGER.info( "Detect Home Assistant Operating System %s / BootSlot %s", self.version, self.sys_dbus.rauc.boot_slot, )
def manage_vulner_script(self, test, dupes, script_element, endpoint): for component_element in script_element.findall('table'): component_cpe = CPE(component_element.attrib['key']) for vuln in component_element.findall('table'): description = "### Vulnerability\n\n" description += "**CPE**: " + str(component_cpe) + "\n" vuln_attributes = dict() for elem in vuln.findall('elem'): vuln_attributes[elem.attrib['key'].lower()] = elem.text description += "**" + elem.attrib['key'] + "**: " + elem.text + "\n" cve = vuln_attributes['id'] severity = self.convert_cvss_score(vuln_attributes['cvss']) dupe_key = cve if dupe_key in dupes: find = dupes[dupe_key] if description is not None: find.description += description else: find = Finding(title=cve, cve=cve, test=test, description=description, severity=severity, mitigation="N/A", impact="No impact provided", component_name=component_cpe.get_product()[0] if len(component_cpe.get_product()) > 0 else '', component_version=component_cpe.get_version()[0] if len(component_cpe.get_version()) > 0 else '', ) find.unsaved_endpoints = list() dupes[dupe_key] = find find.unsaved_endpoints.append(endpoint)
def getinfo(filename): if os.path.exists(filename + '.bin'): print(filename, "exists -- taking in") with open(filename + '.bin', 'rb') as reader: return eval(reader.read()) print(filename, "does not exist, parsing") f = open(filename) tree = etree.parse(f) f.close() vulns = [] entry_nodes = tree.xpath( '//prefix:entry', namespaces={ 'prefix': 'http://scap.nist.gov/schema/feed/vulnerability/2.0' }) for entry in entry_nodes: thisVuln = {} thisVuln['id'] = entry.find(prefixed('vuln', 'cve-id')).text vulnSoftware = entry.find(prefixed('vuln', 'vulnerable-software-list')) if vulnSoftware is not None: for v in vulnSoftware: try: myCPE = CPE(v.text) except NotImplementedError: print("Could not parse") #logging.warning("Unable to parse CPE '%s'" % v.text) else: thisVuln['part'] = myCPE.get_part()[0] thisVuln['vendor'] = myCPE.get_vendor()[0] if 'linux' in thisVuln['vendor']: thisVuln['vendor'] = 'linux' thisVuln['product'] = myCPE.get_product()[0] if 'linux' in thisVuln['product']: thisVuln['vendor'] = 'linux' thisVuln['version'] = myCPE.get_version()[0] thisVuln['update'] = myCPE.get_update()[0] thisVuln['edition'] = myCPE.get_edition()[0] thisVuln['language'] = myCPE.get_language()[0] cvss = entry.find(prefixed('vuln', 'cvss')) if cvss is not None: thisVuln['score'] = cvss.getchildren()[0].getchildren()[0].text thisVuln['accessVector'] = cvss.getchildren()[0].getchildren( )[1].text thisVuln['accessComplexity'] = cvss.getchildren()[0].getchildren( )[2].text thisVuln['auth'] = cvss.getchildren()[0].getchildren()[3].text thisVuln['impactConf'] = cvss.getchildren()[0].getchildren( )[4].text thisVuln['impactInt'] = cvss.getchildren()[0].getchildren()[5].text thisVuln['impactAvail'] = cvss.getchildren()[0].getchildren( )[6].text vulns.append(thisVuln) with open(filename + '.bin', 'wrb+') as myFile: myFile.write(bytes(vulns))
def owasp_dependency_checker_task(source_code_location: str) -> List[Dict]: """ Run OWASP dependency-check and storage all vulnerabilities in an unified format in Redis """ command = ['dependency-check --project "{loc}" --scan', ' "{loc}" -f "XML" -o {loc}', ' --enableExperimental'] # Install dependencies is VERY important os.system("npm install") # We use os.system instead of subprocess.call because because the OWASP # Dependency check tool can take te CVE wordlist downloaded from the OS # context. Using subprocess.call this is not possible. This implies # that without the context no vulnerabilities was detected os.system("".join(command).format(loc=source_code_location)) tree = ET.parse('{}/dependency-check-report.xml'. \ format(source_code_location)) root = tree.getroot() SCHEME = "{https://jeremylong.github.io/DependencyCheck/dependency" \ "-check.1.3.xsd}" results = [] for dependency in root.iterfind(".//{}dependency".format(SCHEME)): # Get dep info vulnerabilities = dependency.findall(".//{}vulnerability".format(SCHEME)) for vulnerability in vulnerabilities: advisory = getattr( vulnerability.find("{}name".format(SCHEME)), "text", "") severity = getattr( vulnerability.find("{}severity".format(SCHEME)), "text", "") summary = getattr( vulnerability.find("{}description".format( SCHEME)), "text", "") for vulnerable_version in vulnerability.findall( ".//{}vulnerableSoftware/{}software[" "@allPreviousVersion='true']".format( SCHEME, SCHEME)): cpe = CPE(vulnerable_version.text) library = cpe.get_product()[0] version = cpe.get_version()[0] results.append(dict(library=library, version=version, severity=severity, summary=summary, advisory=advisory)) return results
def cpe(self): cpe_string = ['cpe'] cpe_string.append('2.3') cpe_string.append('a') cpe_string.append('*') cpe_string.append(self.name()) cpe_string.append(self.version()) cpe_fs = ":".join(cpe_string) + ":*:*:*:*:*:*:*" return CPE(cpe_fs, CPE.VERSION_2_3)
def get_cpes(self): """ Get cpe based on port service Returns: list - list of CPEs """ cpes = [] detailed_cpes = [] if self.port.apps: cpes = [app.cpe for app in self.port.apps if app.cpe is not None] elif isinstance(self.port, PhysicalPort) and self.port.node.os.cpe is not None: cpes = [self.port.node.os.cpe] elif isinstance(self.port, Port) and self.port.service.cpe is not None: cpes = [self.port.service.cpe] for cpe in self._unique_cpes(cpes): if not cpe.get_version()[0]: log.debug("CVE search: CPE without version is not supported") continue if cpe.get_vendor()[0] == self.VENDOR_APACHE: if cpe.get_product()[0] == self.APACHE_HTTPD: detailed_cpes.extend([ cpe, CPE(cpe.as_uri_2_3().replace(self.APACHE_HTTPD, self.APACHE_HTTP_SERVER)) ]) continue elif cpe.get_product()[0] == self.APACHE_HTTP_SERVER: detailed_cpes.extend([ cpe, CPE(cpe.as_uri_2_3().replace(self.APACHE_HTTP_SERVER, self.APACHE_HTTPD)) ]) continue detailed_cpes.append(cpe) return self._unique_cpes(detailed_cpes)
def process_running_on_configuration(container: dict) -> (dict, dict): ''' Extracts, expands and conditionally expand the CPEs records of a Configuration Container of type RUNNING_ON. Generates two lists, one containing the affected CPEs and the other the platforms/execution environment on which they must run. :param container: A valid container wrapping information about a configuration ''' # First, we need to check the arguments if not container or type(container) is not dict: raise ValueError("container must be a non-empty Parsed JSON Object") if container["configType"] != ConfigType.RUNNING_ON.name or container[ 'containers'] is None: raise ValueError( "container must be a valid structure of type: {} ".format( ConfigType.RUNNING_ON.name)) simple_cpes = defaultdict(lambda: defaultdict(set)) running_on = defaultdict(lambda: defaultdict(set)) for subcontainer in container['containers']: if subcontainer['cpeListType'] == "VULNERABLE": for complex_cpe in subcontainer['cpes']: aux = CPE(complex_cpe['cpe23Uri']) simple_cpes[aux.get_vendor()[0]][aux.get_product()[0]].update( expand(complex_cpe)) else: # NON_VULNERABLE for complex_cpe in subcontainer['cpes']: aux = CPE(complex_cpe['cpe23Uri']) running_on[aux.get_vendor()[0]][aux.get_product()[0]].update( expand(complex_cpe)) return simple_cpes, running_on
def test_init(self): expected = { 'name': 'test_name', 'version': '2.4\(23\)', '_cpe': CPE('cpe:2.3:a:apache:http_server:2.4\(23\):*:*:*:*:*:*:*') } result = self.service.__dict__ self.assertDictEqual(result, expected)
def manage_vulner_script(self, test, dupes, script_element, endpoint, report_date=None): for component_element in script_element.findall('table'): component_cpe = CPE(component_element.attrib['key']) for vuln in component_element.findall('table'): # convert elements in dict vuln_attributes = dict() for elem in vuln.findall('elem'): vuln_attributes[elem.attrib['key'].lower()] = elem.text vuln_id = vuln_attributes['id'] description = "### Vulnerability\n\n" description += "**ID**: `" + str(vuln_id) + "`\n" description += "**CPE**: " + str(component_cpe) + "\n" for attribute in vuln_attributes: description += "**" + attribute + "**: `" + vuln_attributes[ attribute] + "`\n" severity = self.convert_cvss_score(vuln_attributes['cvss']) finding = Finding( title=vuln_id, test=test, description=description, severity=severity, component_name=component_cpe.get_product()[0] if len(component_cpe.get_product()) > 0 else '', component_version=component_cpe.get_version()[0] if len(component_cpe.get_version()) > 0 else '', vuln_id_from_tool=vuln_id, nb_occurences=1, ) finding.unsaved_endpoints = [endpoint] # manage if CVE is in metadata if "type" in vuln_attributes and "cve" == vuln_attributes[ "type"]: finding.cve = vuln_attributes["id"] if report_date: finding.date = report_date dupe_key = finding.vuln_id_from_tool if dupe_key in dupes: find = dupes[dupe_key] if description is not None: find.description += "\n-----\n\n" + finding.description # fives '-' produces an horizontal line find.unsaved_endpoints.extend(finding.unsaved_endpoints) find.nb_occurences += finding.nb_occurences else: dupes[dupe_key] = finding
def cpe_check(instance): """Checks to see if provided cpe is a valid CPE v2.3 entry """ if 'cpe' not in instance: return try: CPE(instance['cpe'], CPE.VERSION_2_3) except NotImplementedError: yield JSONError( "Provided CPE value '%s' is not CPE v2.3 compliant." % instance['cpe'], instance['id'], )
def parse(self): for cpe in self.cpes: try: _cpe = CPE(cpe) part, vendor, product, version = self.get_cpe_info(_cpe) if part == 'h': self.hws.append((vendor, product)) elif part == 'o': self.oss.append((vendor, product)) elif part == 'a': self.apps.append((vendor, product)) except NotImplementedError: self.log.error('Something is wrong with cpe: {}'.format(cpe))
def extract_cpe(cpe_vector): """Extract vendor and product strings from CPE vector.""" vendor = None product = None try: c = CPE(cpe_vector) vendor = c.get_vendor()[0] product = c.get_product()[0] print("-->", c, vendor, product) except Exception as e: print(e) return vendor, product
def cpe_check(instance): """Checks to see if provided cpe is a valid CPE v2.3 entry """ for key, obj in instance['objects'].items(): if 'cpe' not in obj: continue try: CPE(obj['cpe'], CPE.VERSION_2_3) except NotImplementedError: yield JSONError( "Provided CPE value of object '%s' ('%s') is not CPE v2.3" "compliant." % (key, obj['cpe']), instance['id'], )
async def test_cpe(self): self.port_info.store_vulnerability = MagicMock() self.port_info.scan_only = True future = Future() future.set_result(ElementTree.fromstring(self.XML_CPE)) self.port_info.command.async_call = MagicMock(return_value=future) self.port_info.prepare_args = MagicMock() await self.port_info() result = self.port_info.store_vulnerability.call_args[0][ 0].port.service.cpe expected = CPE('cpe:/a:apache:http_server:2.4.23') self.assertEqual(result, expected)
def __init__(self, cve_entry, thread: ThreadPool = None): self.id = cve_entry.find(prefixed('vuln', 'cve-id')).text self.vulnsoftware = cve_entry.find( prefixed('vuln', 'vulnerable-software-list')) self.part = [] self.vendor = [] self.version = [] self.update = [] self.edition = [] self.language = [] self.product = [] self.year_path = os.path.join("CVE_Detail", self.get_year()) self.cvssscore = None self.accessVector = None self.accessComplexity = None self.auth = None self.impactConf = None self.impactInt = None self.impactAvail = None if self.vulnsoftware is not None: for product in self.vulnsoftware: try: mycpe = CPE(product.text) except NotImplementedError as e: print(e) else: self.part.append(mycpe.get_part()[0]) self.vendor.append(mycpe.get_vendor()[0]) self.version.append(mycpe.get_version()[0]) self.update.append(mycpe.get_update()[0]) self.edition.append(mycpe.get_edition()[0]) self.language.append(mycpe.get_language()[0]) self.product.append(mycpe.get_product()[0]) cvss = cve_entry.find(prefixed('vuln', 'cvss')) if cvss is not None: self.cvssscore = cvss.getchildren()[0].getchildren()[0].text self.accessVector = cvss.getchildren()[0].getchildren()[1].text self.accessComplexity = cvss.getchildren()[0].getchildren()[2].text self.auth = cvss.getchildren()[0].getchildren()[3].text self.impactConf = cvss.getchildren()[0].getchildren()[4].text self.impactInt = cvss.getchildren()[0].getchildren()[5].text self.impactAvail = cvss.getchildren()[0].getchildren()[6].text self.summery = cve_entry.find(prefixed('vuln', 'summary')).text if thread != None: thread.apply_async(self.get_from_pycvesearch)
async def test_getting_nodes_os_direct(self, mock_cpe, http_client): self.req_future.set_result(MagicMock(body=self.NODE_WITH_OS_DIRECT)) http_client.instance().get.return_value = self.req_future mock_cpe.return_value = 'cpe:2.3:a:b:c:d:*:*:*:*:*:*:*' nodes = await self.topdis.get_all_nodes() self.assertEqual(len(nodes), 1) result = list(nodes)[0] self.assertEqual(result.os.name, 'test_name') self.assertEqual(result.os.version, '11') self.assertEqual(result.os.cpe, CPE(mock_cpe.return_value)) mock_cpe.assert_called_once_with(product='test_name', version='11', part=CPEType.OS)
async def load(self): """Load HassOS data.""" try: assert self.sys_host.info.cpe is not None cpe = CPE(self.sys_host.info.cpe) assert cpe.get_product()[0] == 'hassos' except (NotImplementedError, IndexError, AssertionError): _LOGGER.info("Can't detect HassOS") return # Store meta data self._available = True self._version = cpe.get_version()[0] self._board = cpe.get_target_hardware()[0] _LOGGER.info("Detect HassOS %s on host system", self.version)
def run(mappings): """ Import the Vendors and Products list. """ header("Importing CPE list...") # Download the XML file with timed_operation("Downloading {}...".format(NVD_CPE_URL)): resp = requests.get(NVD_CPE_URL).content # Parse the XML elements with timed_operation("Parsing XML elements..."): raw = gzip.GzipFile(fileobj=BytesIO(resp)).read() obj = untangle.parse(raw.decode("utf-8")) items = obj.cpe_list.cpe_item del obj # Create the objects with timed_operation("Creating list of mappings..."): for item in items: obj = CPE(item.cpe_23_cpe23_item["name"]) vendor = obj.get_vendor()[0] product = obj.get_product()[0] if vendor not in mappings["vendors"].keys(): mappings["vendors"][vendor] = dict(id=get_uuid(), name=vendor) if get_slug(vendor, product) not in mappings["products"].keys(): mappings["products"][get_slug(vendor, product)] = dict( id=get_uuid(), name=product, vendor_id=mappings["vendors"][vendor]["id"], ) del items # Insert the objects in database with timed_operation("Inserting Vendors and Products..."): db.session.bulk_insert_mappings(Vendor, mappings["vendors"].values()) db.session.bulk_insert_mappings(Product, mappings["products"].values()) db.session.commit() info( "{} vendors and {} products imported.".format( len(mappings["vendors"]), len(mappings["products"]) ) ) del mappings