1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::env;
use std::fs::{self, File};
use std::io::BufReader;
use std::path::{Path, PathBuf};
use anyhow::{Result, Error};
use platform_dirs::AppDirs;
use pushover::API;
use pushover::requests::message::SendMessage;
use serde_derive::{Serialize, Deserialize};
#[cfg(target_os="macos")]
use mac_notification_sys::*;
const KEV_JSON_URL: &str = "https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json";
const KEV_CATALOG_URL: &str = "https://www.cisa.gov/known-exploited-vulnerabilities-catalog";
#[derive(Debug, Serialize, Deserialize)]
pub struct Kev {
#[serde(rename = "title")]
pub(crate) title: String,
#[serde(rename = "catalogVersion")]
pub(crate) catalog_version: Option<String>,
#[serde(rename = "dateReleased")]
pub(crate) date_released: String,
#[serde(rename = "count")]
pub(crate) count: Option<i64>,
#[serde(rename = "vulnerabilities")]
pub(crate) vulnerabilities: Option<Vec<Vulnerability>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Vulnerability {
#[serde(rename = "cveID")]
pub(crate) cve_id: String,
#[serde(rename = "vendorProject")]
pub(crate) vendor_project: String,
#[serde(rename = "product")]
pub(crate) product: String,
#[serde(rename = "vulnerabilityName")]
pub(crate) vulnerability_name: String,
#[serde(rename = "dateAdded")]
pub(crate) date_added: String,
#[serde(rename = "shortDescription")]
pub(crate) short_description: String,
#[serde(rename = "requiredAction")]
pub(crate) required_action: String,
#[serde(rename = "dueDate")]
pub(crate) due_date: String,
#[serde(rename = "notes")]
pub(crate) notes: String,
}
pub fn read_kev_cache_from_file<P: AsRef<Path>>(path: P) -> Result<Kev, Error> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let kev = serde_json::from_reader(reader)?;
Ok(kev)
}
pub fn create_kev_cache_file<P: AsRef<Path>>(kev_cache_file_path: P) -> Result<File, std::io::Error> {
File::create(kev_cache_file_path)
}
pub fn read_kev_from_cisa() -> Result<Kev, reqwest::Error> {
reqwest::blocking::get(KEV_JSON_URL)?.json::<Kev>()
}
pub fn notify() {
if let Ok(token) = env::var("PUSHOVER_APP") {
if let Ok(user_key) = env::var("PUSHOVER_USER") {
let api = API::new();
let msg = SendMessage::new(token, user_key, format!("New KEV Release! {}", KEV_CATALOG_URL));
#[cfg(target_os="macos")]
{
let bundle = get_bundle_identifier_or_default("com.apple.Terminal");
set_application(&bundle).unwrap();
let _ = send_notification("New KEV Release!", None, format!("Visit {} for more info.", KEV_CATALOG_URL).as_str(), None).unwrap();
}
if let Err(response) = api.send(&msg) {
eprintln!("{:?}", response)
}
} else {
eprintln!("PUSHOVER_USER environment variable is not set!")
}
} else {
eprintln!("PUSHOVER_APP environment variable is not set!")
}
}
pub fn run() -> Result<(), Error> {
let app_dirs: AppDirs = AppDirs::new(Some("kev-cache"), true).expect("Error idenfitying app dir");
let kev_cache_file_path: PathBuf = app_dirs.cache_dir.join("kev.json");
fs::create_dir_all(&app_dirs.cache_dir)?;
if kev_cache_file_path.is_file() {
let old_kev: Kev = read_kev_cache_from_file(kev_cache_file_path.as_path())?;
let new_kev: Kev = read_kev_from_cisa()?;
if old_kev.date_released != new_kev.date_released {
let kev_cache: File = create_kev_cache_file(kev_cache_file_path)?;
serde_json::to_writer_pretty(&kev_cache, &new_kev)?;
notify();
}
} else {
let kev: Kev = read_kev_from_cisa()?;
let kev_cache: File = create_kev_cache_file(kev_cache_file_path)?;
serde_json::to_writer_pretty(&kev_cache, &kev)?
};
Ok(())
}