added some one time import stuff to nvd downloader and have cve_info searching through gzipped files now for cve id
This commit is contained in:
parent
8a0757c10e
commit
63a62fac79
2 changed files with 51 additions and 13 deletions
|
@ -1,16 +1,48 @@
|
||||||
class CveReport
|
class CveReport
|
||||||
|
attr_accessor :filename, :file
|
||||||
|
# 1st way:
|
||||||
|
# expects a hash with the following key:values # {:filename => "nvdcve-1.0-2002.json.gz",
|
||||||
|
# :json => {"CVE_data_type"=>"CVE",
|
||||||
|
# "CVE_Items"= etc...etc...etc...}
|
||||||
|
# 2nd way:
|
||||||
|
# a json gz compressed file of cve metadata downloaded from nvd
|
||||||
|
# TO ADD: 1st way.
|
||||||
|
|
||||||
def cves
|
def initialize(filename)
|
||||||
# return a list of cve data from the given filename in an array.
|
@filename = filename
|
||||||
self.from_file(filename)
|
@file = from_file
|
||||||
end
|
end
|
||||||
|
|
||||||
def from_file(filename)
|
def cve_ids
|
||||||
# lookup info from the given json.gz filename of cve information.
|
# return a list of cve data from the given filename in an array.
|
||||||
|
file["CVE_Items"].map do |item|
|
||||||
|
item["cve"]["CVE_data_meta"]["ID"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def cve_id?(cve_id)
|
||||||
|
cve_ids.include?(cve_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def cve(cve_id)
|
||||||
|
find(cve_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def cves
|
||||||
|
file["CVE_Items"].map do |cve|
|
||||||
|
cve
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def from_file
|
||||||
|
Zlib::GzipReader.open(filename) do |gz|
|
||||||
|
JSON.parse(gz.read)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def find(cve_id)
|
def find(cve_id)
|
||||||
# this should look in the returned array of cve information from self.cves
|
cves.select do |cve|
|
||||||
# and return the given information for the cve_id
|
cve["cve"]["CVE_data_meta"]["ID"] == cve_id
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -53,17 +53,23 @@ module NvdTools
|
||||||
JSON.parse(json_string)
|
JSON.parse(json_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
def one_time_import
|
def one_time_import(to_file=false)
|
||||||
# experimenting with how i want to do this.
|
# experimenting with how i want to do this.
|
||||||
# right now its a loop through the filenames
|
# right now its a loop through the filenames
|
||||||
# and parse all into a json string and store in an array
|
# and parse all into a json string and store in an array
|
||||||
self.filenames_json.map do |filename|
|
self.filenames_json.map do |filename|
|
||||||
r = client.get(filename)
|
r = client.get(filename)
|
||||||
sleep(1)
|
json_string = read_gzip_stream(r.body)
|
||||||
|
parsed_json = parse_json(json_string)
|
||||||
|
|
||||||
gzip_stream = r.body
|
{ :filename => filename,
|
||||||
json_string = read_gzip_stream(gzip_stream)
|
:json => parsed_json
|
||||||
parse_json(json_string)
|
}
|
||||||
|
|
||||||
|
if to_file
|
||||||
|
filepath = "../data/cve/#{filename}"
|
||||||
|
write_to_file(r.body, filepath)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
# this should be a method that does a one-time import
|
# this should be a method that does a one-time import
|
||||||
# of all of the json.gz from each year + recent + modified json feeds
|
# of all of the json.gz from each year + recent + modified json feeds
|
||||||
|
|
Loading…
Add table
Reference in a new issue