2
1

cve.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #!/usr/bin/env python3
  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 distutils.version
  21. import json
  22. import subprocess
  23. import sys
  24. import operator
  25. sys.path.append('utils/')
  26. NVD_START_YEAR = 1999
  27. NVD_BASE_URL = "https://github.com/fkie-cad/nvd-json-data-feeds/"
  28. ops = {
  29. '>=': operator.ge,
  30. '>': operator.gt,
  31. '<=': operator.le,
  32. '<': operator.lt,
  33. '=': operator.eq
  34. }
  35. # Check if two CPE IDs match each other
  36. def cpe_matches(cpe1, cpe2):
  37. cpe1_elems = cpe1.split(":")
  38. cpe2_elems = cpe2.split(":")
  39. remains = filter(lambda x: x[0] not in ["*", "-"] and x[1] not in ["*", "-"] and x[0] != x[1],
  40. zip(cpe1_elems, cpe2_elems))
  41. return len(list(remains)) == 0
  42. def cpe_product(cpe):
  43. return cpe.split(':')[4]
  44. def cpe_version(cpe):
  45. return cpe.split(':')[5]
  46. class CVE:
  47. """An accessor class for CVE Items in NVD files"""
  48. CVE_AFFECTS = 1
  49. CVE_DOESNT_AFFECT = 2
  50. CVE_UNKNOWN = 3
  51. def __init__(self, nvd_cve):
  52. """Initialize a CVE from its NVD JSON representation"""
  53. self.nvd_cve = nvd_cve
  54. @staticmethod
  55. def download_nvd(nvd_git_dir):
  56. print(f"Updating from {NVD_BASE_URL}")
  57. if os.path.exists(nvd_git_dir):
  58. subprocess.check_call(
  59. ["git", "pull", "--depth", "1"],
  60. cwd=nvd_git_dir,
  61. stdout=subprocess.DEVNULL,
  62. stderr=subprocess.DEVNULL,
  63. )
  64. else:
  65. # Create the directory and its parents; git
  66. # happily clones into an empty directory.
  67. os.makedirs(nvd_git_dir)
  68. subprocess.check_call(
  69. ["git", "clone", "--depth", "1", NVD_BASE_URL, nvd_git_dir],
  70. stdout=subprocess.DEVNULL,
  71. stderr=subprocess.DEVNULL,
  72. )
  73. @staticmethod
  74. def sort_id(cve_ids):
  75. def cve_key(cve_id):
  76. year, id_ = cve_id.split('-')[1:]
  77. return (int(year), int(id_))
  78. return sorted(cve_ids, key=cve_key)
  79. @classmethod
  80. def read_nvd_dir(cls, nvd_dir):
  81. """
  82. Iterate over all the CVEs contained in NIST Vulnerability Database
  83. feeds since NVD_START_YEAR. If the files are missing or outdated in
  84. nvd_dir, a fresh copy will be downloaded, and kept in .json.gz
  85. """
  86. nvd_git_dir = os.path.join(nvd_dir, "git")
  87. CVE.download_nvd(nvd_git_dir)
  88. for year in range(NVD_START_YEAR, datetime.datetime.now().year + 1):
  89. for dirpath, _, filenames in os.walk(os.path.join(nvd_git_dir, f"CVE-{year}")):
  90. for filename in filenames:
  91. if filename[-5:] != ".json":
  92. continue
  93. with open(os.path.join(dirpath, filename), "rb") as f:
  94. yield cls(json.load(f))
  95. def each_product(self):
  96. """Iterate over each product section of this cve"""
  97. for vendor in self.nvd_cve['cve']['affects']['vendor']['vendor_data']:
  98. for product in vendor['product']['product_data']:
  99. yield product
  100. def parse_node(self, node):
  101. """
  102. Parse the node inside the configurations section to extract the
  103. cpe information usefull to know if a product is affected by
  104. the CVE. Actually only the product name and the version
  105. descriptor are needed, but we also provide the vendor name.
  106. """
  107. # The node containing the cpe entries matching the CVE can also
  108. # contain sub-nodes, so we need to manage it.
  109. for child in node.get('children', ()):
  110. for parsed_node in self.parse_node(child):
  111. yield parsed_node
  112. for cpe in node.get('cpeMatch', ()):
  113. if not cpe['vulnerable']:
  114. return
  115. product = cpe_product(cpe['criteria'])
  116. version = cpe_version(cpe['criteria'])
  117. # ignore when product is '-', which means N/A
  118. if product == '-':
  119. return
  120. op_start = ''
  121. op_end = ''
  122. v_start = ''
  123. v_end = ''
  124. if version != '*' and version != '-':
  125. # Version is defined, this is a '=' match
  126. op_start = '='
  127. v_start = version
  128. else:
  129. # Parse start version, end version and operators
  130. if 'versionStartIncluding' in cpe:
  131. op_start = '>='
  132. v_start = cpe['versionStartIncluding']
  133. if 'versionStartExcluding' in cpe:
  134. op_start = '>'
  135. v_start = cpe['versionStartExcluding']
  136. if 'versionEndIncluding' in cpe:
  137. op_end = '<='
  138. v_end = cpe['versionEndIncluding']
  139. if 'versionEndExcluding' in cpe:
  140. op_end = '<'
  141. v_end = cpe['versionEndExcluding']
  142. yield {
  143. 'id': cpe['criteria'],
  144. 'v_start': v_start,
  145. 'op_start': op_start,
  146. 'v_end': v_end,
  147. 'op_end': op_end
  148. }
  149. def each_cpe(self):
  150. for nodes in self.nvd_cve.get('configurations', []):
  151. for node in nodes['nodes']:
  152. for cpe in self.parse_node(node):
  153. yield cpe
  154. @property
  155. def identifier(self):
  156. """The CVE unique identifier"""
  157. return self.nvd_cve['id']
  158. @property
  159. def affected_products(self):
  160. """The set of CPE products referred by this CVE definition"""
  161. return set(cpe_product(p['id']) for p in self.each_cpe())
  162. def affects(self, name, version, cve_ignore_list, cpeid=None):
  163. """
  164. True if the Buildroot Package object passed as argument is affected
  165. by this CVE.
  166. """
  167. if self.identifier in cve_ignore_list:
  168. return self.CVE_DOESNT_AFFECT
  169. pkg_version = distutils.version.LooseVersion(version)
  170. if not hasattr(pkg_version, "version"):
  171. print("Cannot parse package '%s' version '%s'" % (name, version))
  172. pkg_version = None
  173. # if we don't have a cpeid, build one based on name and version
  174. if not cpeid:
  175. cpeid = "cpe:2.3:*:*:%s:%s:*:*:*:*:*:*:*" % (name, version)
  176. # if we have a cpeid, use its version instead of the package
  177. # version, as they might be different due to
  178. # <pkg>_CPE_ID_VERSION
  179. else:
  180. pkg_version = distutils.version.LooseVersion(cpe_version(cpeid))
  181. for cpe in self.each_cpe():
  182. if not cpe_matches(cpe['id'], cpeid):
  183. continue
  184. if not cpe['v_start'] and not cpe['v_end']:
  185. return self.CVE_AFFECTS
  186. if not pkg_version:
  187. continue
  188. if cpe['v_start']:
  189. try:
  190. cve_affected_version = distutils.version.LooseVersion(cpe['v_start'])
  191. inrange = ops.get(cpe['op_start'])(pkg_version, cve_affected_version)
  192. except TypeError:
  193. return self.CVE_UNKNOWN
  194. # current package version is before v_start, so we're
  195. # not affected by the CVE
  196. if not inrange:
  197. continue
  198. if cpe['v_end']:
  199. try:
  200. cve_affected_version = distutils.version.LooseVersion(cpe['v_end'])
  201. inrange = ops.get(cpe['op_end'])(pkg_version, cve_affected_version)
  202. except TypeError:
  203. return self.CVE_UNKNOWN
  204. # current package version is after v_end, so we're
  205. # not affected by the CVE
  206. if not inrange:
  207. continue
  208. # We're in the version range affected by this CVE
  209. return self.CVE_AFFECTS
  210. return self.CVE_DOESNT_AFFECT