-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzip_cpe.py
84 lines (67 loc) · 2.46 KB
/
unzip_cpe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pprint
import zipfile
from lxml import etree
def check_empty(val):
return None if val == "*" else val
def check_len(val):
return [None, val] if len(val.split(" ")) > 1 else [val, None]
def parse_xml(target_file):
file = zipfile.ZipFile(target_file)
root = etree.parse(file.open(file.namelist()[0])).getroot()
cpe_items = []
vendors = set()
products = set()
for cpe_item in root[1:]:
references = []
for child in cpe_item.getchildren():
if etree.QName(child).localname == "title":
title = child.text
if etree.QName(child).localname == "cpe23-item":
name = child.get("name").split(":")
part = check_empty(name[2].replace("/", ""))
vendor = check_empty(name[3].replace("\\", ""))
product = check_empty(name[4].replace("\\", ""))
version = check_empty(name[5])
update_version = check_empty(name[6])
edition = check_empty(name[7])
lang = check_empty(name[8])
sw_edition = check_empty(name[9])
target_sw = check_empty(name[10])
target_hw = check_empty(name[11])
other = check_empty(name[12])
if etree.QName(child).localname == "references":
refs = child.getchildren()
for reference in refs:
url = reference.attrib.get("href")
ref_type, description = check_len(reference.text)
ref_data = {"url": url, "desc": description, "type": ref_type}
references.append(ref_data)
cpe_data = {
"title": title,
"part": part,
"version": version,
"update_version": update_version,
"version": version,
"update_version": update_version,
"edition": edition,
"lang": lang,
"sw_edition": sw_edition,
"target_sw": target_sw,
"target_hw": target_hw,
"other": other,
"references": references,
"vendor": {
"name": vendor,
},
"product": {
"name": product
},
}
cpe_items.append(cpe_data)
vendors.add(vendor)
products.add(product)
return {
"cpes": cpe_items,
"vendors": list(vendors),
"products": list(products),
}