#!/usr/bin/env python3 import json import os import urllib from urllib import request import gzip import io import dateutil from dateutil import parser # Grab and consume the latest CVE feed from NIST headers ={'User-agent': 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'} nvdrecent = urllib.request.Request('https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-recent.json.gz', headers = headers) response = urllib.request.urlopen(nvdrecent) buf = io.BytesIO(response.read()) f = gzip.GzipFile(fileobj=buf) data = json.load(f) # Pull the list of items together and grab the fields we need list(data) cves = data['CVE_Items'] cvenum = int(data['CVE_data_numberOfCVEs']) date_posted = dateutil.parser.parse(data['CVE_data_timestamp']) # Format the date the way we want it date_posted = date_posted.strftime("%B %d %Y") goodcves = [] # Sort the list sortedcves = sorted(cves, key=lambda k: dateutil.parser.parse(k.get('publishedDate', '')), reverse=True) # Remove rejected items goodcves = [] for cve in sortedcves: description = cve.get('cve')['description']['description_data'][0]['value'] if 'REJECT' in description: pass else: goodcves.append(cve) # Snag the latest CVES from our list lastfivecves = goodcves[0:6] # Set up the array output = [] # Iterate through the CVEs to get the data we want for cve in lastfivecves[0:5]: zdate = cve.get('publishedDate') cvedate = dateutil.parser.parse((cve.get('publishedDate'))) cvedate = cvedate.strftime("%B %d %Y") description = cve.get('cve')['description']['description_data'][0]['value'] CVE_id = cve.get('cve')['CVE_data_meta']['ID'] description = description + " This is " + CVE_id + " published on " + cvedate redirectionurl = "https://cve.mitre.org/cgi-bin/cvename.cgi?name=" + CVE_id # Place all of our output data into a variable export_dict = {"uid": CVE_id,"updateDate": zdate, "titleText": CVE_id, "mainText": description, "redirectionUrl": redirectionurl} # Write variable data into our array output.append(export_dict) # Write everything out to our json file. with open('cve.json','w') as json_file: json.dump(output, json_file, indent=4)