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 | |
| 15 | python () { |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 16 | cve_check_db_file = d.getVar("CVE_CHECK_DB_FILE") |
| 17 | if not cve_check_db_file: |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 18 | raise bb.parse.SkipRecipe("Skip recipe when cve-check class is not loaded.") |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 19 | |
| 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 Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | python do_populate_cve_db() { |
| 28 | """ |
| 29 | Update NVD database with json data feed |
| 30 | """ |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 31 | import bb.utils |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 32 | import sqlite3, urllib, urllib.parse, shutil, gzip |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 33 | from datetime import date |
| 34 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 35 | bb.utils.export_proxies(d) |
| 36 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 37 | BASE_URL = "https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-" |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 38 | YEAR_START = 2002 |
| 39 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 40 | db_file = d.getVar("CVE_CHECK_DB_FILE") |
| 41 | db_dir = os.path.dirname(db_file) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 42 | json_tmpfile = os.path.join(db_dir, 'nvd.json.gz') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 43 | |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 44 | # 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 Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 52 | 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 69 | 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 Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 76 | if response: |
| 77 | for l in response.read().decode("utf-8").splitlines(): |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 78 | 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 Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 95 | response = urllib.request.urlopen(json_url) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 96 | if response: |
| 97 | update_db(c, gzip.decompress(response.read()).decode('utf-8')) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 98 | 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 | |
| 113 | def initialize_db(c): |
| 114 | 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] | 115 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 116 | c.execute("CREATE TABLE IF NOT EXISTS NVD (ID TEXT UNIQUE, SUMMARY TEXT, \ |
| 117 | SCOREV2 TEXT, SCOREV3 TEXT, MODIFIED INTEGER, VECTOR TEXT)") |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 118 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 119 | 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 Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 122 | 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] | 123 | |
| 124 | def 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 138 | if version != '*' and version != '-': |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 139 | # Version is defined, this is a '=' match |
| 140 | yield [cveId, vendor, product, version, '=', '', ''] |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 141 | elif version == '-': |
| 142 | # no version information is available |
| 143 | yield [cveId, vendor, product, version, '', '', ''] |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 144 | 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 | |
| 171 | def 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 Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 179 | accessVector = None |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 180 | cveId = elt['cve']['CVE_data_meta']['ID'] |
| 181 | cveDesc = elt['cve']['description']['description_data'][0]['value'] |
| 182 | date = elt['lastModifiedDate'] |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 183 | try: |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 184 | 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 Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 190 | cvssv3 = elt['impact']['baseMetricV3']['cvssV3']['baseScore'] |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 191 | except KeyError: |
| 192 | accessVector = accessVector or "UNKNOWN" |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 193 | 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 | |
| 203 | addtask do_populate_cve_db before do_fetch |
| 204 | do_populate_cve_db[nostamp] = "1" |
| 205 | |
| 206 | EXCLUDE_FROM_WORLD = "1" |