cve.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #!/usr/bin/env python
  2. # Copyright (C) 2009 by Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  3. # Copyright (C) 2020 by Gregory CLEMENT <gregory.clement@bootlin.com>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. import datetime
  19. import os
  20. import requests # URL checking
  21. import distutils.version
  22. import time
  23. import gzip
  24. import sys
  25. import operator
  26. try:
  27. import ijson
  28. except ImportError:
  29. sys.stderr.write("You need ijson to parse NVD for CVE check\n")
  30. exit(1)
  31. sys.path.append('utils/')
  32. NVD_START_YEAR = 2002
  33. NVD_JSON_VERSION = "1.1"
  34. NVD_BASE_URL = "https://nvd.nist.gov/feeds/json/cve/" + NVD_JSON_VERSION
  35. ops = {
  36. '>=': operator.ge,
  37. '>': operator.gt,
  38. '<=': operator.le,
  39. '<': operator.lt,
  40. '=': operator.eq
  41. }
  42. # Check if two CPE IDs match each other
  43. def cpe_matches(cpe1, cpe2):
  44. cpe1_elems = cpe1.split(":")
  45. cpe2_elems = cpe2.split(":")
  46. remains = filter(lambda x: x[0] not in ["*", "-"] and x[1] not in ["*", "-"] and x[0] != x[1],
  47. zip(cpe1_elems, cpe2_elems))
  48. return len(list(remains)) == 0
  49. def cpe_product(cpe):
  50. return cpe.split(':')[4]
  51. def cpe_version(cpe):
  52. return cpe.split(':')[5]
  53. class CVE:
  54. """An accessor class for CVE Items in NVD files"""
  55. CVE_AFFECTS = 1
  56. CVE_DOESNT_AFFECT = 2
  57. CVE_UNKNOWN = 3
  58. def __init__(self, nvd_cve):
  59. """Initialize a CVE from its NVD JSON representation"""
  60. self.nvd_cve = nvd_cve
  61. @staticmethod
  62. def download_nvd_year(nvd_path, year):
  63. metaf = "nvdcve-%s-%s.meta" % (NVD_JSON_VERSION, year)
  64. path_metaf = os.path.join(nvd_path, metaf)
  65. jsonf_gz = "nvdcve-%s-%s.json.gz" % (NVD_JSON_VERSION, year)
  66. path_jsonf_gz = os.path.join(nvd_path, jsonf_gz)
  67. # If the database file is less than a day old, we assume the NVD data
  68. # locally available is recent enough.
  69. if os.path.exists(path_jsonf_gz) and os.stat(path_jsonf_gz).st_mtime >= time.time() - 86400:
  70. return path_jsonf_gz
  71. # If not, we download the meta file
  72. url = "%s/%s" % (NVD_BASE_URL, metaf)
  73. print("Getting %s" % url)
  74. page_meta = requests.get(url)
  75. page_meta.raise_for_status()
  76. # If the meta file already existed, we compare the existing
  77. # one with the data newly downloaded. If they are different,
  78. # we need to re-download the database.
  79. # If the database does not exist locally, we need to redownload it in
  80. # any case.
  81. if os.path.exists(path_metaf) and os.path.exists(path_jsonf_gz):
  82. meta_known = open(path_metaf, "r").read()
  83. if page_meta.text == meta_known:
  84. return path_jsonf_gz
  85. # Grab the compressed JSON NVD, and write files to disk
  86. url = "%s/%s" % (NVD_BASE_URL, jsonf_gz)
  87. print("Getting %s" % url)
  88. page_json = requests.get(url)
  89. page_json.raise_for_status()
  90. open(path_jsonf_gz, "wb").write(page_json.content)
  91. open(path_metaf, "w").write(page_meta.text)
  92. return path_jsonf_gz
  93. @classmethod
  94. def read_nvd_dir(cls, nvd_dir):
  95. """
  96. Iterate over all the CVEs contained in NIST Vulnerability Database
  97. feeds since NVD_START_YEAR. If the files are missing or outdated in
  98. nvd_dir, a fresh copy will be downloaded, and kept in .json.gz
  99. """
  100. for year in range(NVD_START_YEAR, datetime.datetime.now().year + 1):
  101. filename = CVE.download_nvd_year(nvd_dir, year)
  102. try:
  103. content = ijson.items(gzip.GzipFile(filename), 'CVE_Items.item')
  104. except: # noqa: E722
  105. print("ERROR: cannot read %s. Please remove the file then rerun this script" % filename)
  106. raise
  107. for cve in content:
  108. yield cls(cve)
  109. def each_product(self):
  110. """Iterate over each product section of this cve"""
  111. for vendor in self.nvd_cve['cve']['affects']['vendor']['vendor_data']:
  112. for product in vendor['product']['product_data']:
  113. yield product
  114. def parse_node(self, node):
  115. """
  116. Parse the node inside the configurations section to extract the
  117. cpe information usefull to know if a product is affected by
  118. the CVE. Actually only the product name and the version
  119. descriptor are needed, but we also provide the vendor name.
  120. """
  121. # The node containing the cpe entries matching the CVE can also
  122. # contain sub-nodes, so we need to manage it.
  123. for child in node.get('children', ()):
  124. for parsed_node in self.parse_node(child):
  125. yield parsed_node
  126. for cpe in node.get('cpe_match', ()):
  127. if not cpe['vulnerable']:
  128. return
  129. product = cpe_product(cpe['cpe23Uri'])
  130. version = cpe_version(cpe['cpe23Uri'])
  131. # ignore when product is '-', which means N/A
  132. if product == '-':
  133. return
  134. op_start = ''
  135. op_end = ''
  136. v_start = ''
  137. v_end = ''
  138. if version != '*' and version != '-':
  139. # Version is defined, this is a '=' match
  140. op_start = '='
  141. v_start = version
  142. else:
  143. # Parse start version, end version and operators
  144. if 'versionStartIncluding' in cpe:
  145. op_start = '>='
  146. v_start = cpe['versionStartIncluding']
  147. if 'versionStartExcluding' in cpe:
  148. op_start = '>'
  149. v_start = cpe['versionStartExcluding']
  150. if 'versionEndIncluding' in cpe:
  151. op_end = '<='
  152. v_end = cpe['versionEndIncluding']
  153. if 'versionEndExcluding' in cpe:
  154. op_end = '<'
  155. v_end = cpe['versionEndExcluding']
  156. yield {
  157. 'id': cpe['cpe23Uri'],
  158. 'v_start': v_start,
  159. 'op_start': op_start,
  160. 'v_end': v_end,
  161. 'op_end': op_end
  162. }
  163. def each_cpe(self):
  164. for node in self.nvd_cve['configurations']['nodes']:
  165. for cpe in self.parse_node(node):
  166. yield cpe
  167. @property
  168. def identifier(self):
  169. """The CVE unique identifier"""
  170. return self.nvd_cve['cve']['CVE_data_meta']['ID']
  171. @property
  172. def affected_products(self):
  173. """The set of CPE products referred by this CVE definition"""
  174. return set(cpe_product(p['id']) for p in self.each_cpe())
  175. def affects(self, name, version, cve_ignore_list, cpeid=None):
  176. """
  177. True if the Buildroot Package object passed as argument is affected
  178. by this CVE.
  179. """
  180. if self.identifier in cve_ignore_list:
  181. return self.CVE_DOESNT_AFFECT
  182. pkg_version = distutils.version.LooseVersion(version)
  183. if not hasattr(pkg_version, "version"):
  184. print("Cannot parse package '%s' version '%s'" % (name, version))
  185. pkg_version = None
  186. # if we don't have a cpeid, build one based on name and version
  187. if not cpeid:
  188. cpeid = "cpe:2.3:*:*:%s:%s:*:*:*:*:*:*:*" % (name, version)
  189. for cpe in self.each_cpe():
  190. if not cpe_matches(cpe['id'], cpeid):
  191. continue
  192. if not cpe['v_start'] and not cpe['v_end']:
  193. return self.CVE_AFFECTS
  194. if not pkg_version:
  195. continue
  196. if cpe['v_start']:
  197. try:
  198. cve_affected_version = distutils.version.LooseVersion(cpe['v_start'])
  199. inrange = ops.get(cpe['op_start'])(pkg_version, cve_affected_version)
  200. except TypeError:
  201. return self.CVE_UNKNOWN
  202. # current package version is before v_start, so we're
  203. # not affected by the CVE
  204. if not inrange:
  205. continue
  206. if cpe['v_end']:
  207. try:
  208. cve_affected_version = distutils.version.LooseVersion(cpe['v_end'])
  209. inrange = ops.get(cpe['op_end'])(pkg_version, cve_affected_version)
  210. except TypeError:
  211. return self.CVE_UNKNOWN
  212. # current package version is after v_end, so we're
  213. # not affected by the CVE
  214. if not inrange:
  215. continue
  216. # We're in the version range affected by this CVE
  217. return self.CVE_AFFECTS
  218. return self.CVE_DOESNT_AFFECT