diff options
Diffstat (limited to 'bin/common/srtool_utils.py')
-rwxr-xr-x | bin/common/srtool_utils.py | 163 |
1 files changed, 159 insertions, 4 deletions
diff --git a/bin/common/srtool_utils.py b/bin/common/srtool_utils.py index 0ab29a4e..33405c67 100755 --- a/bin/common/srtool_utils.py +++ b/bin/common/srtool_utils.py @@ -29,6 +29,7 @@ from datetime import datetime, date import time import re import subprocess +import json # load the srt.sqlite schema indexes dir_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) @@ -904,6 +905,9 @@ def fix_defects_to_products(): # # +# Fix MITRE reserved CVEs that were mistakenly set at "New" instead of +# "New-Reserved" due to column ordering issue in the MITRE "Init" routine. +# def fix_bad_mitre_init(): conn = sqlite3.connect(srtDbName) cur = conn.cursor() @@ -961,19 +965,22 @@ def fix_bad_mitre_init(): pass if (0 == i % 200): # conn.commit() - #print('') + print('') pass # Development/debug support if cmd_skip and (i < cmd_skip): continue if cmd_count and ((i - cmd_skip) > cmd_count): break - print("3CVE NEW_COUNT=%d, mitre=%d, name=%s, database=%s" % (new_count,mitre_count,cve_name,srtDbName)) + print("\nCVE NEW_COUNT=%d, mitre=%d, name=%s, database=%s" % (new_count,mitre_count,cve_name,srtDbName)) # conn.commit() # +# Fix MITRE CVEs that are missing a description in the top level CVE +# records due to column ordering issue in the MITRE "Init" routine. +# def foo_fix_bad_mitre_init(): conn = sqlite3.connect(srtDbName) cur = conn.cursor() @@ -1073,6 +1080,151 @@ def foo_fix_bad_mitre_init(): # conn.commit() +# +# Fix CVE records with missing 'cvssV2_severity' values in the top-level CVE records, due to +# CVE imports before a patch was sent upstream +# +def fix_v2_severity(datasource_list): + conn = sqlite3.connect(srtDbName) + cur_ds = conn.cursor() + cur_cs = conn.cursor() + cur_cve = conn.cursor() + + cve_count = 0 + fix_count = 0 + + # Find NIST data sources + cur_ds.execute('SELECT * FROM orm_datasource WHERE source = "nist" ORDER BY key ASC;') + for i,ds in enumerate(cur_ds): + # Development/debug support + if cmd_count and ((cve_count - cmd_skip) > cmd_count): + break + + if ds[ORM.DATASOURCE_DESCRIPTION] in ['NIST Common Weakness Enumeration Data']: + continue + elif "ALL" == datasource_list: + pass + elif not ds[ORM.DATASOURCE_DESCRIPTION] in datasource_list.split(','): + continue + print("NIST Source:%s" % ds[ORM.DATASOURCE_DESCRIPTION]) + + # Scan the NIST datasource file and extract required values into a map + # (bin/nist/srtool_nist.py --download-only --source='NIST 2002' --file=data/nvdcve-1.1-2002.json --url-file=nvdcve-1.1-2002.json.gz --url-meta=nvdcve-1.1-2002.meta) + cve_source_file = '' + for param in ds[ORM.DATASOURCE_LOOKUP].split(' '): + if param.startswith('--file='): + cve_source_file = param.replace('--file=','') + print(" File:%s" % cve_source_file) + break + nist_data_map = {} + nist_file = os.path.join(srtool_basepath,cve_source_file) + try: + f = open(nist_file, 'r') + source_dct = json.load(f) + for item in source_dct["CVE_Items"]: + if not 'cve' in item: + continue + if not 'CVE_data_meta' in item['cve']: + continue + if not 'ID' in item['cve']['CVE_data_meta']: + continue + cve_name = item['cve']['CVE_data_meta']['ID'] + + + if cve_name == "CVE-2019-15031": + print("BAR1:%s" % (item['impact']['baseMetricV3'])) + + cvssV3_baseScore = '' + cvssV3_baseSeverity = '' + cvssV2_baseScore = '' + cvssV2_severity = '' + if ('impact' in item) and ('baseMetricV3' in item['impact']): + cvssV3_baseScore = "%.1f" % float(item['impact']['baseMetricV3']['cvssV3']['baseScore']) + cvssV3_baseSeverity = item['impact']['baseMetricV3']['cvssV3']['baseSeverity'] + if ('impact' in item) and ('baseMetricV2' in item['impact']): + cvssV2_baseScore = "%.1f" % float(item['impact']['baseMetricV2']['cvssV2']['baseScore']) + cvssV2_severity = item['impact']['baseMetricV2']['severity'] + +# print(" Name:%s,cvssV2_severity=%s" % (cve_name,cvssV2_severity)) + nist_data_map[cve_name] = [cvssV3_baseScore,cvssV3_baseSeverity,cvssV2_baseScore,cvssV2_severity] + + if cve_name == "CVE-2019-15031": + print("BAR2:%s" % str(nist_data_map[cve_name])) + + + except Exception as e: + print("ERROR:%s (%s)" % (e,item['impact']['baseMetricV3'])) + return + + + # Find all CVEs with this datasource + cur_cs.execute('SELECT * FROM orm_cvesource WHERE datasource_id = %d' % ds[ORM.DATASOURCE_ID]) + for j,cvesource in enumerate(cur_cs): + # Development/debug support + cve_count += 1 + if cmd_skip and (cve_count < cmd_skip): continue + if cmd_count and ((cve_count - cmd_skip) > cmd_count): break + + cur_cve.execute('SELECT * FROM orm_cve WHERE id = %d' % cvesource[ORM.CVESOURCE_CVE_ID]) + cve = cur_cve.fetchone() + if not cve: + print("WARNING: MISSING CVE in orm_cvesource '%d:%d' : %s" % (cvesource[ORM.CVESOURCE_CVE_ID],cvesource[ORM.CVESOURCE_DATASOURCE_ID],ds[ORM.DATASOURCE_DESCRIPTION])) + continue + cve_name = cve[ORM.CVE_NAME] + if cve_name in nist_data_map: + fix_count += 1 + if (nist_data_map[cve_name][0] != cve[ORM.CVE_CVSSV3_BASESCORE]) or (nist_data_map[cve_name][1] != cve[ORM.CVE_CVSSV3_BASESEVERITY]): + print("WARNING: diff V3 for %s (%s->%s) (%s->%s)" % (cve_name,nist_data_map[cve_name][0],cve[ORM.CVE_CVSSV3_BASESCORE],nist_data_map[cve_name][1],cve[ORM.CVE_CVSSV3_BASESEVERITY])) + + if force: + sql = ''' UPDATE orm_cve + SET cvssV3_baseScore = ?, cvssV3_baseSeverity = ?, cvssV2_baseScore = ?, cvssV2_severity = ? + WHERE id = ?''' + cur_cve.execute(sql, (nist_data_map[cve_name][0],nist_data_map[cve_name][1],nist_data_map[cve_name][2],nist_data_map[cve_name][3],cve[ORM.CVE_ID],)) +# print('%05d: %-20s = %-20s' % (j,cve_name,nist_data_map[cve_name])) + else: + print("ERROR:CVE_NAME '%s' NOT MAPPED" % cve_name) + + # Progress indicator support + if (0 == cve_count % 1000): + print('%05d: %-20s\r' % (cve_count,cve_name), end='') + if force: conn.commit() + print('') + pass + + print("CVE COUNT=%d, fix_count=%d" % (cve_count,fix_count)) + if force: conn.commit() + + +# Sample code that does a CVE lookup data fetch and CVE update +#def example_datasource_lookup(cve,nist_ds,cvesource,cur): +# if force: +# if nist_ds: +# lookup_command = nist_lookup[ cvesource[ORM.CVESOURCE_DATASOURCE_ID] ].replace('%command%','--cve-detail=%s' % cve[ORM.CVE_NAME]) +# result_returncode,result_stdout,result_stderr = execute_process(lookup_command.split(' ')) +# if 0 != result_returncode: +# print("ERROR_LOOKUP:%s" % lookup_command) +# return(1) +# cvssV2_severity = '' +# for line in result_stdout.decode("utf-8").splitlines(): +# try: +# name = line[:line.index('=')] +# value = line[line.index('=')+1:].replace("[EOL]","\n") +# except: +# continue +# if name == 'cvssV2_severity': +# cvssV2_severity = value +# break +# if cvssV2_severity: +# fix_count += 1 +# sql = ''' UPDATE orm_cve +# SET cvssV2_severity = ? +# WHERE id = ?''' +# cur.execute(sql, (cvssV2_severity,cve[ORM.CVE_ID],)) +# print('%05d: %-20s = %-20s' % (i,cve[ORM.CVE_NAME],cvssV2_severity)) +## return(0) + + ################################# # find_multiple_defects # @@ -1345,6 +1497,7 @@ def main(argv): parser.add_argument('--fix-remove-bulk-cve-history', action='store_const', const='fix_remove_bulk_cve_history', dest='command', help='foo') parser.add_argument('--fix-bad-mitre-init', action='store_const', const='fix_bad_mitre_init', dest='command', help='foo') parser.add_argument('--fix-bad-new', action='store_const', const='fix_bad_new', dest='command', help='foo') + parser.add_argument('--fix-v2-severity', dest='fix_v2_severity', help='foo') parser.add_argument('--find-empty-status', action='store_const', const='find_empty_status', dest='command', help='foo') @@ -1364,7 +1517,7 @@ def main(argv): args = parser.parse_args() - master_log = open(os.path.join(script_pathname, "update_logs/master_log.txt"), "a") + master_log = open(os.path.join(srtool_basepath, "update_logs/master_log.txt"), "a") verbose = args.verbose if None != args.skip: @@ -1420,6 +1573,8 @@ def main(argv): fix_bad_mitre_init() elif 'fix_bad_new' == args.command: fix_bad_new() + elif args.fix_v2_severity: + fix_v2_severity(args.fix_v2_severity) elif 'find_multiple_defects' == args.command: find_multiple_defects() @@ -1436,5 +1591,5 @@ def main(argv): master_log.close() if __name__ == '__main__': - script_pathname = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))) + srtool_basepath = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))) main(sys.argv[1:]) |