Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 1 | SUMMARY = "Updates the NVD CVE database" |
| 2 | LICENSE = "MIT" |
| 3 | |
| 4 | INHIBIT_DEFAULT_DEPS = "1" |
| 5 | |
| 6 | inherit native |
| 7 | |
| 8 | deltask do_unpack |
| 9 | deltask do_patch |
| 10 | deltask do_configure |
| 11 | deltask do_compile |
| 12 | deltask do_install |
| 13 | deltask do_populate_sysroot |
| 14 | |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 15 | NVDCVE_URL ?= "https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-" |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame^] | 16 | # CVE database update interval, in seconds. By default: once a day (24*60*60). |
| 17 | # Use 0 to force the update |
| 18 | CVE_DB_UPDATE_INTERVAL ?= "86400" |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 19 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 20 | python () { |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 21 | if not bb.data.inherits_class("cve-check", d): |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 22 | raise bb.parse.SkipRecipe("Skip recipe when cve-check class is not loaded.") |
| 23 | } |
| 24 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 25 | python do_fetch() { |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 26 | """ |
| 27 | Update NVD database with json data feed |
| 28 | """ |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 29 | import bb.utils |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 30 | import bb.progress |
| 31 | import sqlite3, urllib, urllib.parse, gzip |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 32 | from datetime import date |
| 33 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 34 | bb.utils.export_proxies(d) |
| 35 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 36 | YEAR_START = 2002 |
| 37 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 38 | db_file = d.getVar("CVE_CHECK_DB_FILE") |
| 39 | db_dir = os.path.dirname(db_file) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 40 | |
| 41 | if os.path.exists("{0}-journal".format(db_file)): |
| 42 | # If a journal is present the last update might have been interrupted. In that case, |
| 43 | # just wipe any leftovers and force the DB to be recreated. |
| 44 | os.remove("{0}-journal".format(db_file)) |
| 45 | |
| 46 | if os.path.exists(db_file): |
| 47 | os.remove(db_file) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 48 | |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame^] | 49 | # The NVD database changes once a day, so no need to update more frequently |
| 50 | # Allow the user to force-update |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 51 | try: |
| 52 | import time |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame^] | 53 | update_interval = int(d.getVar("CVE_DB_UPDATE_INTERVAL")) |
| 54 | if (update_interval < 0): |
| 55 | update_interval = 0 |
| 56 | if time.time() - os.path.getmtime(db_file) < update_interval: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 57 | bb.debug(2, "Recently updated, skipping") |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 58 | return |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame^] | 59 | |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 60 | except OSError: |
| 61 | pass |
| 62 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 63 | bb.utils.mkdirhier(db_dir) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 64 | |
| 65 | # Connect to database |
| 66 | conn = sqlite3.connect(db_file) |
| 67 | c = conn.cursor() |
| 68 | |
| 69 | initialize_db(c) |
| 70 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 71 | with bb.progress.ProgressHandler(d) as ph, open(os.path.join(d.getVar("TMPDIR"), 'cve_check'), 'a') as cve_f: |
| 72 | total_years = date.today().year + 1 - YEAR_START |
| 73 | for i, year in enumerate(range(YEAR_START, date.today().year + 1)): |
| 74 | bb.debug(2, "Updating %d" % year) |
| 75 | ph.update((float(i + 1) / total_years) * 100) |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 76 | year_url = (d.getVar('NVDCVE_URL')) + str(year) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 77 | meta_url = year_url + ".meta" |
| 78 | json_url = year_url + ".json.gz" |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 79 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 80 | # Retrieve meta last modified date |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 81 | try: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 82 | response = urllib.request.urlopen(meta_url) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 83 | except urllib.error.URLError as e: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 84 | cve_f.write('Warning: CVE db update error, Unable to fetch CVE data.\n\n') |
| 85 | bb.warn("Failed to fetch CVE data (%s)" % e.reason) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 86 | return |
| 87 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 88 | if response: |
| 89 | for l in response.read().decode("utf-8").splitlines(): |
| 90 | key, value = l.split(":", 1) |
| 91 | if key == "lastModifiedDate": |
| 92 | last_modified = value |
| 93 | break |
| 94 | else: |
| 95 | bb.warn("Cannot parse CVE metadata, update failed") |
| 96 | return |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 97 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 98 | # Compare with current db last modified date |
| 99 | c.execute("select DATE from META where YEAR = ?", (year,)) |
| 100 | meta = c.fetchone() |
| 101 | if not meta or meta[0] != last_modified: |
| 102 | bb.debug(2, "Updating entries") |
| 103 | # Clear products table entries corresponding to current year |
| 104 | c.execute("delete from PRODUCTS where ID like ?", ('CVE-%d%%' % year,)) |
| 105 | |
| 106 | # Update db with current year json file |
| 107 | try: |
| 108 | response = urllib.request.urlopen(json_url) |
| 109 | if response: |
| 110 | update_db(c, gzip.decompress(response.read()).decode('utf-8')) |
| 111 | c.execute("insert or replace into META values (?, ?)", [year, last_modified]) |
| 112 | except urllib.error.URLError as e: |
| 113 | cve_f.write('Warning: CVE db update error, CVE data is outdated.\n\n') |
| 114 | bb.warn("Cannot parse CVE data (%s), update failed" % e.reason) |
| 115 | return |
| 116 | else: |
| 117 | bb.debug(2, "Already up to date (last modified %s)" % last_modified) |
| 118 | # Update success, set the date to cve_check file. |
| 119 | if year == date.today().year: |
| 120 | cve_f.write('CVE database update : %s\n\n' % date.today()) |
| 121 | |
| 122 | conn.commit() |
| 123 | conn.close() |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 126 | do_fetch[lockfiles] += "${CVE_CHECK_DB_FILE_LOCK}" |
| 127 | do_fetch[file-checksums] = "" |
| 128 | do_fetch[vardeps] = "" |
| 129 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 130 | def initialize_db(c): |
| 131 | c.execute("CREATE TABLE IF NOT EXISTS META (YEAR INTEGER UNIQUE, DATE TEXT)") |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 132 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 133 | c.execute("CREATE TABLE IF NOT EXISTS NVD (ID TEXT UNIQUE, SUMMARY TEXT, \ |
| 134 | SCOREV2 TEXT, SCOREV3 TEXT, MODIFIED INTEGER, VECTOR TEXT)") |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 135 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 136 | c.execute("CREATE TABLE IF NOT EXISTS PRODUCTS (ID TEXT, \ |
| 137 | VENDOR TEXT, PRODUCT TEXT, VERSION_START TEXT, OPERATOR_START TEXT, \ |
| 138 | VERSION_END TEXT, OPERATOR_END TEXT)") |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 139 | c.execute("CREATE INDEX IF NOT EXISTS PRODUCT_ID_IDX on PRODUCTS(ID);") |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 140 | |
| 141 | def parse_node_and_insert(c, node, cveId): |
| 142 | # Parse children node if needed |
| 143 | for child in node.get('children', ()): |
| 144 | parse_node_and_insert(c, child, cveId) |
| 145 | |
| 146 | def cpe_generator(): |
| 147 | for cpe in node.get('cpe_match', ()): |
| 148 | if not cpe['vulnerable']: |
| 149 | return |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 150 | cpe23 = cpe.get('cpe23Uri') |
| 151 | if not cpe23: |
| 152 | return |
| 153 | cpe23 = cpe23.split(':') |
| 154 | if len(cpe23) < 6: |
| 155 | return |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 156 | vendor = cpe23[3] |
| 157 | product = cpe23[4] |
| 158 | version = cpe23[5] |
| 159 | |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 160 | if cpe23[6] == '*' or cpe23[6] == '-': |
| 161 | version_suffix = "" |
| 162 | else: |
| 163 | version_suffix = "_" + cpe23[6] |
| 164 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 165 | if version != '*' and version != '-': |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 166 | # Version is defined, this is a '=' match |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 167 | yield [cveId, vendor, product, version + version_suffix, '=', '', ''] |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 168 | elif version == '-': |
| 169 | # no version information is available |
| 170 | yield [cveId, vendor, product, version, '', '', ''] |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 171 | else: |
| 172 | # Parse start version, end version and operators |
| 173 | op_start = '' |
| 174 | op_end = '' |
| 175 | v_start = '' |
| 176 | v_end = '' |
| 177 | |
| 178 | if 'versionStartIncluding' in cpe: |
| 179 | op_start = '>=' |
| 180 | v_start = cpe['versionStartIncluding'] |
| 181 | |
| 182 | if 'versionStartExcluding' in cpe: |
| 183 | op_start = '>' |
| 184 | v_start = cpe['versionStartExcluding'] |
| 185 | |
| 186 | if 'versionEndIncluding' in cpe: |
| 187 | op_end = '<=' |
| 188 | v_end = cpe['versionEndIncluding'] |
| 189 | |
| 190 | if 'versionEndExcluding' in cpe: |
| 191 | op_end = '<' |
| 192 | v_end = cpe['versionEndExcluding'] |
| 193 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 194 | if op_start or op_end or v_start or v_end: |
| 195 | yield [cveId, vendor, product, v_start, op_start, v_end, op_end] |
| 196 | else: |
| 197 | # This is no version information, expressed differently. |
| 198 | # Save processing by representing as -. |
| 199 | yield [cveId, vendor, product, '-', '', '', ''] |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 200 | |
| 201 | c.executemany("insert into PRODUCTS values (?, ?, ?, ?, ?, ?, ?)", cpe_generator()) |
| 202 | |
| 203 | def update_db(c, jsondata): |
| 204 | import json |
| 205 | root = json.loads(jsondata) |
| 206 | |
| 207 | for elt in root['CVE_Items']: |
| 208 | if not elt['impact']: |
| 209 | continue |
| 210 | |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 211 | accessVector = None |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 212 | cveId = elt['cve']['CVE_data_meta']['ID'] |
| 213 | cveDesc = elt['cve']['description']['description_data'][0]['value'] |
| 214 | date = elt['lastModifiedDate'] |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 215 | try: |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 216 | accessVector = elt['impact']['baseMetricV2']['cvssV2']['accessVector'] |
| 217 | cvssv2 = elt['impact']['baseMetricV2']['cvssV2']['baseScore'] |
| 218 | except KeyError: |
| 219 | cvssv2 = 0.0 |
| 220 | try: |
| 221 | accessVector = accessVector or elt['impact']['baseMetricV3']['cvssV3']['attackVector'] |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 222 | cvssv3 = elt['impact']['baseMetricV3']['cvssV3']['baseScore'] |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 223 | except KeyError: |
| 224 | accessVector = accessVector or "UNKNOWN" |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 225 | cvssv3 = 0.0 |
| 226 | |
| 227 | c.execute("insert or replace into NVD values (?, ?, ?, ?, ?, ?)", |
| 228 | [cveId, cveDesc, cvssv2, cvssv3, date, accessVector]) |
| 229 | |
| 230 | configurations = elt['configurations']['nodes'] |
| 231 | for config in configurations: |
| 232 | parse_node_and_insert(c, config, cveId) |
| 233 | |
| 234 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 235 | do_fetch[nostamp] = "1" |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 236 | |
| 237 | EXCLUDE_FROM_WORLD = "1" |