def create(item: RestrictedElement) -> Asset: os = get_value(item.find(".//tag[@name='operating-system']")) asset, _ = Asset.objects.get_or_create( ip_address=item.find(".//tag[@name='host-ip']").text, mac_address=get_value(item.find(".//tag[@name='mac-address']")), os=os if os else 'Unknown') return asset
def create_element(name, **attrs): # copy.deepcopy() is an order of magnitude faster than creating a new Element() every time if ':' in name: ns, name = name.split(':') name = '{%s}%s' % (ns_translation[ns], name) elem = RestrictedElement(**attrs) elem.tag = name return elem
def create_element(name, attrs=None, nsmap=None): # Python versions prior to 3.6 do not preserve dict or kwarg ordering, so we cannot pull in attrs as **kwargs if we # also want stable XML attribute output. Instead, let callers supply us with an OrderedDict instance. if ':' in name: ns, name = name.split(':') name = '{%s}%s' % (ns_translation[ns], name) elem = RestrictedElement(attrib=attrs, nsmap=nsmap) elem.tag = name return elem
def create_element(name, attrs=None, nsmap=None): # Python versions prior to 3.6 do not preserve dict or kwarg ordering, so we cannot pull in attrs as **kwargs if we # also want stable XML attribute output. Instead, let callers supply us with an OrderedDict instance. if ':' in name: ns, name = name.split(':') name = '{%s}%s' % (ns_translation[ns], name) elem = RestrictedElement(nsmap=nsmap) if attrs: # Try hard to keep attribute order, to ensure deterministic output. This simplifies testing. for k, v in attrs.items(): elem.set(k, v) elem.tag = name return elem
def description(item: RestrictedElement) -> [str, None]: desc_item = item.find('Description') summary = desc_item.find('Description_Summary') if summary is not None: return CWEFactory.remove_whitespaces(summary) return None
def extract_from_node(node: RestrictedElement, *path: str) -> Optional[str]: element_path = "." + "/".join((NS + elem) for elem in path) elem: Optional[RestrictedElement] = node.find(element_path) if elem is None: return None text: str = elem.text return text
def extended_description(item: RestrictedElement) -> [str, None]: desc_item = item.find('Description') extended = desc_item.find('Extended_Description') if extended is not None: text = extended.find('Text') if text is not None: return CWEFactory.remove_whitespaces(text) return None
def references(item: RestrictedElement) -> str: references = item.find( '{http://cpe.mitre.org/dictionary/2.0}references') ref_list = [] if references is not None: for ref in references: ref_list.append({ 'name': ref.text, 'url': ref.attrib.get('href') }) return json.dumps(ref_list)
def create(item: RestrictedElement) -> models.Cpe: name = item.find( '{http://scap.nist.gov/schema/cpe-extension/2.3}cpe23-item').get( 'name') cpe = CpeFactory.get(name) for field in sorted(CpeFactory.FIELD_LIST): parser = getattr(CpeFactory, field, None) if parser: setattr(cpe, field, parser(item)) cpe.save() return cpe
def create(item: RestrictedElement) -> models.Cwe: cwe_id = 'CWE-{}'.format(item.get('ID')) try: cwe = models.Cwe.objects.get(id=cwe_id) except models.Cwe.DoesNotExist: cwe = models.Cwe(id=cwe_id) for field in sorted(CWEFactory.FIELD_LIST): parser = getattr(CWEFactory, field, None) if parser: setattr(cwe, field, parser(item)) if cwe.has_changed: cwe.save() return cwe
def create(item: RestrictedElement): cwe_id = 'CWE-{}'.format(item.get('ID')) old = CweDocument.search().filter( 'term', id=cwe_id).sort('-modified_date')[0].execute() cwe = CweDocument(id=cwe_id) for field in CweDocument.get_fields_name(): parser = getattr(CWEFactory, field, None) if parser: setattr(cwe, field, parser(item)) if old.hits and cwe.has_changed(old.hits[0]): return old.hits[0].update(cwe, weak=True) elif not old.hits: return cwe.save(weak=True) return None
def vendor(item: RestrictedElement) -> str: return CpeFactory.get_field(item.get('name'), CpeFactory.VENDOR)
def create(item: RestrictedElement, config) -> AssetDocument: ip_address = item.find(".//tag[@name='host-ip']").text return AssetDocument.get_or_create(ip_address, config)
def status(item: RestrictedElement) -> str: return item.get('Status')
def weakness_abstraction(item: RestrictedElement) -> str: return item.get('Weakness_Abstraction')
def name(item: RestrictedElement) -> str: return item.get('Name')
def title(item: RestrictedElement) -> str: return item.find('{http://cpe.mitre.org/dictionary/2.0}title').text