blob: 32d6dbdffc36dc0a57939627d8544cb05dc9a4df [file] [log] [blame]
Brad Bishop96ff1982019-08-19 13:50:42 -04001SUMMARY = "Updates the NVD CVE database"
2LICENSE = "MIT"
3
4INHIBIT_DEFAULT_DEPS = "1"
5
6inherit native
7
8deltask do_unpack
9deltask do_patch
10deltask do_configure
11deltask do_compile
12deltask do_install
13deltask do_populate_sysroot
14
15python () {
Andrew Geissler82c905d2020-04-13 13:39:40 -050016 cve_check_db_file = d.getVar("CVE_CHECK_DB_FILE")
17 if not cve_check_db_file:
Brad Bishop96ff1982019-08-19 13:50:42 -040018 raise bb.parse.SkipRecipe("Skip recipe when cve-check class is not loaded.")
Andrew Geissler82c905d2020-04-13 13:39:40 -050019
20 if os.path.exists("%s-journal" % cve_check_db_file ):
21 os.remove("%s-journal" % cve_check_db_file)
22
23 if os.path.exists(cve_check_db_file):
24 os.remove(cve_check_db_file)
Brad Bishop96ff1982019-08-19 13:50:42 -040025}
26
27python do_populate_cve_db() {
28 """
29 Update NVD database with json data feed
30 """
Brad Bishop6dbb3162019-11-25 09:41:34 -050031 import bb.utils
Brad Bishop08902b02019-08-20 09:16:51 -040032 import sqlite3, urllib, urllib.parse, shutil, gzip
Brad Bishop96ff1982019-08-19 13:50:42 -040033 from datetime import date
34
Brad Bishop6dbb3162019-11-25 09:41:34 -050035 bb.utils.export_proxies(d)
36
Andrew Geissler82c905d2020-04-13 13:39:40 -050037 BASE_URL = "https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-"
Brad Bishop96ff1982019-08-19 13:50:42 -040038 YEAR_START = 2002
39
Brad Bishop6dbb3162019-11-25 09:41:34 -050040 db_file = d.getVar("CVE_CHECK_DB_FILE")
41 db_dir = os.path.dirname(db_file)
Brad Bishop96ff1982019-08-19 13:50:42 -040042 json_tmpfile = os.path.join(db_dir, 'nvd.json.gz')
Brad Bishop08902b02019-08-20 09:16:51 -040043
Brad Bishop1d80a2e2019-11-15 16:35:03 -050044 # Don't refresh the database more than once an hour
45 try:
46 import time
47 if time.time() - os.path.getmtime(db_file) < (60*60):
48 return
49 except OSError:
50 pass
51
Brad Bishop96ff1982019-08-19 13:50:42 -040052 cve_f = open(os.path.join(d.getVar("TMPDIR"), 'cve_check'), 'a')
53
54 if not os.path.isdir(db_dir):
55 os.mkdir(db_dir)
56
57 # Connect to database
58 conn = sqlite3.connect(db_file)
59 c = conn.cursor()
60
61 initialize_db(c)
62
63 for year in range(YEAR_START, date.today().year + 1):
64 year_url = BASE_URL + str(year)
65 meta_url = year_url + ".meta"
66 json_url = year_url + ".json.gz"
67
68 # Retrieve meta last modified date
Andrew Geissler82c905d2020-04-13 13:39:40 -050069 try:
70 response = urllib.request.urlopen(meta_url)
71 except urllib.error.URLError as e:
72 cve_f.write('Warning: CVE db update error, Unable to fetch CVE data.\n\n')
73 bb.warn("Failed to fetch CVE data (%s)" % e.reason)
74 return
75
Brad Bishop08902b02019-08-20 09:16:51 -040076 if response:
77 for l in response.read().decode("utf-8").splitlines():
Brad Bishop96ff1982019-08-19 13:50:42 -040078 key, value = l.split(":", 1)
79 if key == "lastModifiedDate":
80 last_modified = value
81 break
82 else:
83 bb.warn("Cannot parse CVE metadata, update failed")
84 return
85
86 # Compare with current db last modified date
87 c.execute("select DATE from META where YEAR = ?", (year,))
88 meta = c.fetchone()
89 if not meta or meta[0] != last_modified:
90 # Clear products table entries corresponding to current year
91 c.execute("delete from PRODUCTS where ID like ?", ('CVE-%d%%' % year,))
92
93 # Update db with current year json file
94 try:
Brad Bishop6dbb3162019-11-25 09:41:34 -050095 response = urllib.request.urlopen(json_url)
Brad Bishop08902b02019-08-20 09:16:51 -040096 if response:
97 update_db(c, gzip.decompress(response.read()).decode('utf-8'))
Brad Bishop96ff1982019-08-19 13:50:42 -040098 c.execute("insert or replace into META values (?, ?)", [year, last_modified])
99 except urllib.error.URLError as e:
100 cve_f.write('Warning: CVE db update error, CVE data is outdated.\n\n')
101 bb.warn("Cannot parse CVE data (%s), update failed" % e.reason)
102 return
103
104 # Update success, set the date to cve_check file.
105 if year == date.today().year:
106 cve_f.write('CVE database update : %s\n\n' % date.today())
107
108 cve_f.close()
109 conn.commit()
110 conn.close()
111}
112
113def initialize_db(c):
114 c.execute("CREATE TABLE IF NOT EXISTS META (YEAR INTEGER UNIQUE, DATE TEXT)")
Brad Bishop6dbb3162019-11-25 09:41:34 -0500115
Brad Bishop96ff1982019-08-19 13:50:42 -0400116 c.execute("CREATE TABLE IF NOT EXISTS NVD (ID TEXT UNIQUE, SUMMARY TEXT, \
117 SCOREV2 TEXT, SCOREV3 TEXT, MODIFIED INTEGER, VECTOR TEXT)")
Brad Bishop6dbb3162019-11-25 09:41:34 -0500118
Brad Bishop96ff1982019-08-19 13:50:42 -0400119 c.execute("CREATE TABLE IF NOT EXISTS PRODUCTS (ID TEXT, \
120 VENDOR TEXT, PRODUCT TEXT, VERSION_START TEXT, OPERATOR_START TEXT, \
121 VERSION_END TEXT, OPERATOR_END TEXT)")
Brad Bishop6dbb3162019-11-25 09:41:34 -0500122 c.execute("CREATE INDEX IF NOT EXISTS PRODUCT_ID_IDX on PRODUCTS(ID);")
Brad Bishop96ff1982019-08-19 13:50:42 -0400123
124def parse_node_and_insert(c, node, cveId):
125 # Parse children node if needed
126 for child in node.get('children', ()):
127 parse_node_and_insert(c, child, cveId)
128
129 def cpe_generator():
130 for cpe in node.get('cpe_match', ()):
131 if not cpe['vulnerable']:
132 return
133 cpe23 = cpe['cpe23Uri'].split(':')
134 vendor = cpe23[3]
135 product = cpe23[4]
136 version = cpe23[5]
137
Andrew Geissler82c905d2020-04-13 13:39:40 -0500138 if version != '*' and version != '-':
Brad Bishop96ff1982019-08-19 13:50:42 -0400139 # Version is defined, this is a '=' match
140 yield [cveId, vendor, product, version, '=', '', '']
Andrew Geissler82c905d2020-04-13 13:39:40 -0500141 elif version == '-':
142 # no version information is available
143 yield [cveId, vendor, product, version, '', '', '']
Brad Bishop96ff1982019-08-19 13:50:42 -0400144 else:
145 # Parse start version, end version and operators
146 op_start = ''
147 op_end = ''
148 v_start = ''
149 v_end = ''
150
151 if 'versionStartIncluding' in cpe:
152 op_start = '>='
153 v_start = cpe['versionStartIncluding']
154
155 if 'versionStartExcluding' in cpe:
156 op_start = '>'
157 v_start = cpe['versionStartExcluding']
158
159 if 'versionEndIncluding' in cpe:
160 op_end = '<='
161 v_end = cpe['versionEndIncluding']
162
163 if 'versionEndExcluding' in cpe:
164 op_end = '<'
165 v_end = cpe['versionEndExcluding']
166
167 yield [cveId, vendor, product, v_start, op_start, v_end, op_end]
168
169 c.executemany("insert into PRODUCTS values (?, ?, ?, ?, ?, ?, ?)", cpe_generator())
170
171def update_db(c, jsondata):
172 import json
173 root = json.loads(jsondata)
174
175 for elt in root['CVE_Items']:
176 if not elt['impact']:
177 continue
178
Andrew Geissler635e0e42020-08-21 15:58:33 -0500179 accessVector = None
Brad Bishop96ff1982019-08-19 13:50:42 -0400180 cveId = elt['cve']['CVE_data_meta']['ID']
181 cveDesc = elt['cve']['description']['description_data'][0]['value']
182 date = elt['lastModifiedDate']
Brad Bishop96ff1982019-08-19 13:50:42 -0400183 try:
Andrew Geissler635e0e42020-08-21 15:58:33 -0500184 accessVector = elt['impact']['baseMetricV2']['cvssV2']['accessVector']
185 cvssv2 = elt['impact']['baseMetricV2']['cvssV2']['baseScore']
186 except KeyError:
187 cvssv2 = 0.0
188 try:
189 accessVector = accessVector or elt['impact']['baseMetricV3']['cvssV3']['attackVector']
Brad Bishop96ff1982019-08-19 13:50:42 -0400190 cvssv3 = elt['impact']['baseMetricV3']['cvssV3']['baseScore']
Andrew Geissler635e0e42020-08-21 15:58:33 -0500191 except KeyError:
192 accessVector = accessVector or "UNKNOWN"
Brad Bishop96ff1982019-08-19 13:50:42 -0400193 cvssv3 = 0.0
194
195 c.execute("insert or replace into NVD values (?, ?, ?, ?, ?, ?)",
196 [cveId, cveDesc, cvssv2, cvssv3, date, accessVector])
197
198 configurations = elt['configurations']['nodes']
199 for config in configurations:
200 parse_node_and_insert(c, config, cveId)
201
202
203addtask do_populate_cve_db before do_fetch
204do_populate_cve_db[nostamp] = "1"
205
206EXCLUDE_FROM_WORLD = "1"