Skip to content

Instantly share code, notes, and snippets.

@lbarasti
Created September 14, 2021 14:41
Show Gist options
  • Save lbarasti/38ff3a47a515f0b92f9a7bdaae6e5d46 to your computer and use it in GitHub Desktop.
Save lbarasti/38ff3a47a515f0b92f9a7bdaae6e5d46 to your computer and use it in GitHub Desktop.
require "json"
# https://csrc.nist.gov/schema/nvd/feed/1.1/nvd_cve_feed_json_1.1.schema
record CVE_Response, resultsPerPage : Int32, startIndex : Int32,
totalResults : Int32, result : CVE_Result do
include JSON::Serializable
end
record CVE_Result, cve_data_type : String, cve_data_format : String,
cve_data_version : String, cve_data_numberOfCVEs : String?,
cve_data_timestamp : String, cve_Items : Array(CVE_Item) do
include JSON::Serializable
@[JSON::Field(key: "CVE_data_type")]
property cve_data_type : String
@[JSON::Field(key: "CVE_data_format")]
property cve_data_format : String
@[JSON::Field(key: "CVE_data_version")]
property cve_data_version : String
@[JSON::Field(key: "CVE_data_numberOfCVEs")]
property cve_data_numberOfCVEs : String?
@[JSON::Field(key: "CVE_data_timestamp")]
property cve_data_timestamp : String
@[JSON::Field(key: "CVE_Items")]
property cve_Items : Array(CVE_Item)
end
record CVE_Item, cve : JSON::Any, configuration : JSON::Any?,
impact : JSON::Any, publishedDate : String, lastModifiedDate : String do
include JSON::Serializable
end
require "fancyline"
require "http"
require "./cve_records"
fancy = Fancyline.new # Build a shell object
puts "Press Ctrl-C or Ctrl-D to quit."
begin
while input = fancy.readline("$ ") # Ask the user for input
parse(input).try { |cmd|
interpret(cmd)
}
end
rescue err : Fancyline::Interrupt
puts "Bye."
end
record ListCVE, name : String
def parse(input)
if input.starts_with?("ls ")
ListCVE.new input.split[1]
end
end
def interpret(cmd)
case cmd
when ListCVE
url = "https://services.nvd.nist.gov/rest/json/cves/1.0?keyword=#{cmd.name}"
# File.open("resp.json", "w") { |f|
# f.puts(HTTP::Client.get(url).body)
# }
# File.open("resp.json", "r") { |f|
# puts CVE_Response.from_json(f)
# }
r = CVE_Response.from_json(HTTP::Client.get(url).body)
.result.cve_Items.map(&.cve["CVE_data_meta"]["ID"])
puts r
else
puts "Cannot interpret #{cmd}"
end
end
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "run",
"type": "shell",
"command": "crystal ${file}",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment