2022-05-30 12:18:40 -05:00
|
|
|
require 'nokogiri'
|
|
|
|
|
|
|
|
class OvalParser
|
2022-05-30 21:38:20 -05:00
|
|
|
attr_accessor :data, :doc, :root
|
|
|
|
def initialize(data)
|
2022-05-30 12:18:40 -05:00
|
|
|
@data = data
|
|
|
|
@doc = Nokogiri::XML(data)
|
2022-05-30 21:38:20 -05:00
|
|
|
@root = doc.root
|
2022-05-30 12:18:40 -05:00
|
|
|
end
|
|
|
|
|
2022-05-30 21:38:20 -05:00
|
|
|
def get_definitions
|
|
|
|
root.xpath("//xmlns:definition")
|
2022-05-30 12:18:40 -05:00
|
|
|
end
|
|
|
|
|
2022-05-30 21:38:20 -05:00
|
|
|
def get_def_ids
|
|
|
|
definitions = get_definitions
|
|
|
|
definitions.map { |d| d.xpath(".//@id").text }
|
|
|
|
end
|
2022-05-30 12:18:40 -05:00
|
|
|
|
2022-05-30 21:38:20 -05:00
|
|
|
def oval_defs_for_id(id)
|
|
|
|
definitions = get_definitions
|
|
|
|
definitions.select do |definition|
|
|
|
|
definition if definition.attributes.dig('id').value == id
|
|
|
|
end
|
|
|
|
end
|
2022-05-30 12:18:40 -05:00
|
|
|
|
2022-05-30 21:38:20 -05:00
|
|
|
def get_cve_def_info
|
|
|
|
# largerly used this code here as a guide:
|
|
|
|
# https://github.com/OpenSCAP/openscap-daemon/blob/1b9e9d4849573e1ce09728cc61c4564e5d605a8e/openscap_daemon/cve_scanner/generate_summary.py#L83-L104
|
|
|
|
def_ids = get_def_ids
|
|
|
|
def_ids.map do |id|
|
|
|
|
oval_defs = oval_defs_for_id(id)
|
|
|
|
oval_defs.map do |oval_def|
|
|
|
|
title = oval_def.xpath(".//xmlns:title").text
|
|
|
|
cve_meta = oval_def.xpath(".//xmlns:reference").select { |n| n.attributes.dig('source').value == 'CVE' }
|
|
|
|
cve_ids = cve_meta.map { |cve_m| cve_m.attributes.dig('ref_id').value || 'None' }
|
|
|
|
cve_urls = cve_meta.map { |cve_m| cve_m.attributes.dig('ref_url').value || 'None' }
|
|
|
|
description = oval_def.xpath(".//xmlns:description").text
|
|
|
|
severity = oval_def.xpath(".//xmlns:severity").text
|
|
|
|
my_sample_data_hash = {
|
|
|
|
:title => title,
|
|
|
|
:cve_ids => cve_ids,
|
|
|
|
:cve_urls => cve_urls,
|
|
|
|
:description => description,
|
|
|
|
:severity => severity
|
|
|
|
}
|
|
|
|
binding.pry
|
|
|
|
my_sample_data_hash
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|