common-vulnerability-entry.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/python3
  2. ###############################################################################
  3. ##
  4. ## Functions for downloading and parsing Common Vulnerability DB data
  5. ##
  6. ###############################################################################
  7. import logging, sys
  8. import re
  9. import urllib.request
  10. import datetime
  11. logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
  12. ## Get details of given CVE entry from NIST DB
  13. def fetchCVE(cve_id, cve_base_url, cvss_base_url):
  14. url = ''
  15. logging.info('Fetching CVE: ' + cve_id + '\n')
  16. #Make this subtitution for some reason
  17. cve_id = re.sub('^CAN','CVE',cve_id)
  18. ##
  19. ## get CVE Scores from NVD
  20. ##
  21. url = cvss_base_url + cve_id
  22. try:
  23. req = urllib.request.urlopen(url)
  24. charset = req.info().get_content_charset()
  25. if charset is None:
  26. charset = 'utf-8'
  27. cvss = req.read().decode(charset)
  28. except urllib.error.HTTPError as err:
  29. print('Failed to download CVE: ' + url + '\n')
  30. cvss = ''
  31. # Check for error pages: referenced but unpublished CVEs :-/
  32. if re.compile('.*is valid CVE format, but CVE was not found.*').match(cvss):
  33. print(cve_id + ' does not exist in NIST DB\n')
  34. ##
  35. ## get CVE from MITRE
  36. ##
  37. logging.info('Fetching CVE: ' + cve_id + ' from MITRE\n')
  38. url = cve_base_url + cve_id
  39. try:
  40. req = urllib.request.urlopen(url)
  41. charset = req.info().get_content_charset()
  42. if charset is None:
  43. charset = 'utf-8'
  44. cve = req.read().decode(charset)
  45. except urllib.error.HTTPError as err:
  46. print('Failed to download CVE: ' + url + ' from MITRE\n')
  47. cve = ''
  48. return cve
  49. # Check for error pages: referenced but unpublished CVEs :-/
  50. if re.compile('.*Could not find a CVE entry or candidate named.*').match(cve):
  51. print(cve_id + 'does not exist in MITRE DB\n')
  52. return ''
  53. s = ''
  54. ret = s.join((cvss,cve))
  55. return ret
  56. ###############################################################################
  57. ## Get CVE severity rating and report date, and return
  58. ## (date base-score impact-score exploit-score)
  59. def parseCVE(cve_id, cve):
  60. #use worst case info as defaults
  61. cve_date = datetime.datetime.now()
  62. print(cve_date)
  63. cve_base = 10
  64. cve_impact = 10
  65. cve_exploit = 10
  66. if cve == '':
  67. # No details means we assume worst-case (highest score, recent bug)
  68. if re.compile('LOCAL-(.*)').match(cve_id):
  69. logging.info('Assuming worst-case ratings for LOCAL CVE ' + cve_id)
  70. #test1 = fetchCVE('CVE-2015-2750', 'http://cve.mitre.org/cgi-bin/cvename.cgi?name=', 'http://web.nvd.nist.gov/view/vuln/detail?vulnId=')
  71. #print(test1)
  72. parseCVE('a','b')