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