Skip to content

Instantly share code, notes, and snippets.

@sbs2001
Created June 26, 2020 11:47
Show Gist options
  • Select an option

  • Save sbs2001/62c7453067328d51a29622031ecbb186 to your computer and use it in GitHub Desktop.

Select an option

Save sbs2001/62c7453067328d51a29622031ecbb186 to your computer and use it in GitHub Desktop.
import requests
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup
class CVRFParser:
def __init__(self, xml_doc: ET.ElementTree):
self.cvrf_doc = xml_doc.getroot()
self.product_tree = None
self.vulnerabilities = []
for i in self.cvrf_doc :
if i.tag.endswith('ProductTree'):
self.product_tree = i
if i.tag.endswith('Vulnerability'):
self.vulnerabilities.append(i)
def get_products(self):
products = set()
for child in self.product_tree :
if child.attrib.get('Name') and child.attrib.get('Type'):
if child.attrib['Type'] == 'Product Version' :
products.add(child.attrib['Name'])
return products
def get_vulnerablities(self):
vulnerabilities = []
for vulnerability in self.vulnerabilities:
vuln_entry = {
'cve_id':None,
'reference_urls': [],
}
for details in vulnerability :
if details.tag.endswith('CVE'):
vuln_entry['cve_id'] = details.text
if details.tag.endswith('References'):
for child in details.iter() :
if child.tag.endswith('URL'):
vuln_entry['reference_urls'].append(child.text)
vulnerabilities.append(vuln_entry)
return vulnerabilities
def get_data(self):
return {"packages":self.get_products(),
"vulnerabilities":self.get_vulnerablities(),
}
def get_urls_of_xmls_from_page(base_url):
r = requests.get(base_url)
soup = BeautifulSoup(r.content, 'lxml')
for a_tag in soup.find_all('a', href=True):
if a_tag['href'].endswith('.xml'):
yield base_url + a_tag['href']
for url in get_urls_of_xmls_from_page("http://ftp.suse.com/pub/projects/security/cvrf/"):
resp = requests.get(url).content
z = ET.ElementTree(ET.fromstring(resp.decode('utf-8')))
vb = CVRFParser(z)
print(vb.get_products())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment