gen-missing-cpe 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. import argparse
  3. import sys
  4. import json
  5. import subprocess
  6. import os
  7. from cpedb import CPEDB, CPE
  8. def gen_update_xml_reports(cpeids, cpedb, output):
  9. cpe_need_update = []
  10. for cpe in cpeids:
  11. result = cpedb.find(cpe)
  12. if not result:
  13. result = cpedb.find_partial(CPE.no_version(cpe))
  14. if result:
  15. cpe_need_update.append(cpe)
  16. else:
  17. print("WARNING: no match found for '%s'" % cpe)
  18. for cpe in cpe_need_update:
  19. xml = cpedb.gen_update_xml(cpe)
  20. fname = CPE.product(cpe) + '-' + CPE.version(cpe) + '.xml'
  21. print("Generating %s" % fname)
  22. with open(os.path.join(output, fname), 'w+') as fp:
  23. fp.write(xml)
  24. print("Generated %d update files out of %d CPEs" % (len(cpe_need_update), len(cpeids)))
  25. def get_cpe_ids():
  26. print("Getting list of CPE for enabled packages")
  27. cmd = ["make", "--no-print-directory", "show-info"]
  28. js = json.loads(subprocess.check_output(cmd).decode("utf-8"))
  29. return set([v["cpe-id"] for k, v in js.items() if "cpe-id" in v])
  30. def resolvepath(path):
  31. return os.path.abspath(os.path.expanduser(path))
  32. def parse_args():
  33. parser = argparse.ArgumentParser()
  34. parser.add_argument('--output', dest='output',
  35. help='Path to the output CPE update files', type=resolvepath, required=True)
  36. parser.add_argument('--nvd-path', dest='nvd_path',
  37. help='Path to the local NVD database', type=resolvepath, required=True)
  38. return parser.parse_args()
  39. def __main__():
  40. args = parse_args()
  41. if not os.path.isdir(args.output):
  42. print("ERROR: output directory %s does not exist" % args.output)
  43. sys.exit(1)
  44. cpedb = CPEDB(args.nvd_path)
  45. cpedb.get_xml_dict()
  46. cpeids = get_cpe_ids()
  47. gen_update_xml_reports(cpeids, cpedb, args.output)
  48. if __name__ == "__main__":
  49. __main__()