Skip to content

Commit

Permalink
Change: make the tag member in the Nvt structure a Vector of (key,val…
Browse files Browse the repository at this point in the history
…) tuples.

Also, converted to the tag string just when needed (like before storing in redis, which expects a single string)
  • Loading branch information
jjnicola committed Nov 23, 2022
1 parent 6dd0a8e commit 260ccb1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
38 changes: 13 additions & 25 deletions rust/nvtcache/src/nvt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub struct Nvt {
solution: String, //Stored in redis under Tag item. Not in use.
solution_type: String, //Stored in redis under Tag item. Not in use.
solution_method: String, //Stored in redis under Tag item. Not in use.
tag: String,
tag: Vec<(String, String)>,
cvss_base: String, //Stored in redis under Tag item. Not in use.
dependencies: Vec<String>,
required_keys: Vec<String>,
Expand Down Expand Up @@ -183,7 +183,7 @@ impl Default for Nvt {
solution: String::new(),
solution_type: String::new(),
solution_method: String::new(),
tag: String::new(),
tag: vec![],
cvss_base: String::new(),
dependencies: vec![],
required_keys: vec![],
Expand Down Expand Up @@ -274,7 +274,7 @@ impl Nvt {
}

/// Set the NVT tag
pub fn set_tag(&mut self, tag: String) {
pub fn set_tag(&mut self, tag: Vec<(String, String)>) {
self.tag = tag;
}

Expand All @@ -289,7 +289,7 @@ impl Nvt {
let deps = list.iter().map(|&d| d.to_string()).collect();
deps
}

/// Set the NVT dependencies
pub fn set_dependencies(&mut self, dependencies: String) {
self.dependencies = self.vec_of_str(&dependencies);
Expand Down Expand Up @@ -355,36 +355,24 @@ impl Nvt {
/// since epoch before added as a tag value.
/// The tag name "cvss_base" will be ignored and not added.
pub fn add_tag(&mut self, name: String, value: String) {
let mut new_value = value;
let current_tag = &self.tag;

match name.as_str() {
"last_modification" => {
//TODO: convert the value to seconds since epoch
new_value = 1234.to_string();
self.tag.push((name, value));
}
"creation_date" => {
//TODO: convert the value to seconds since epoch
new_value = 1234.to_string();
self.tag.push((name, value));
}
"severity_date" => {
//TODO: convert the value to seconds since epoch
new_value = 1234.to_string();
self.tag.push((name, value));
}
// cvss_base is just ignored
"cvss_base" => (),
_ => {
self.tag.push((name, value));
}
"cvss_base" => return,
_ => (),
}
if self.tag.is_empty() {
self.tag = [name, "=".to_string(), new_value].concat();
} else {
self.tag = [
current_tag.to_string(),
"|".to_string(),
name,
"=".to_string(),
new_value,
]
.concat();
}
}

Expand Down Expand Up @@ -470,7 +458,7 @@ impl Nvt {
}

/// Get the NVT tag
pub fn get_tag(&self) -> &str {
pub fn get_tag(&self) -> &Vec<(String, String)> {
&self.tag
}

Expand Down
13 changes: 11 additions & 2 deletions rust/nvtcache/src/redisconnector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ impl RedisCtx {
Ok(ret.v)
}

fn tags_as_single_string(&self, tags: &Vec<(String, String)>) -> String {
let tag: Vec<String> = tags
.iter()
.map(|(key, val)| format!("{}={}", key, val).to_string())
.collect();

tag.iter().as_ref().join("|")
}

pub fn redis_add_nvt(&mut self, nvt: Nvt, filename: String) -> Result<()> {
let oid = nvt.get_oid();
let name = nvt.get_name();
Expand All @@ -197,7 +206,7 @@ impl RedisCtx {
let required_udp_ports = nvt.get_required_udp_ports().concat();
let required_ports = nvt.get_required_ports().concat();
let dependencies = nvt.get_dependencies().concat();
let tags = nvt.get_tag();
let tags = self.tags_as_single_string(nvt.get_tag());
let category = nvt.get_category().to_string();
let family = nvt.get_family();

Expand All @@ -213,7 +222,7 @@ impl RedisCtx {
&required_udp_ports,
&required_ports,
&dependencies,
tags,
&tags,
&cves,
&bids,
&xrefs,
Expand Down
9 changes: 6 additions & 3 deletions rust/nvtcache/tests/nvt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ mod test {
//Add first tag
nvt.add_tag("Tag Name".to_string(), "Tag Value".to_string());
let tag = nvt.get_tag();
assert_eq!(tag, "Tag Name=Tag Value");
let expected = vec![("Tag Name".to_string(), "Tag Value".to_string())];
assert_eq!(tag, &expected);

//Add second tag
nvt.add_tag("Tag Name1".to_string(), "Tag Value1".to_string());
nvt.add_tag("cvss_base".to_string(), "Tag Value1".to_string());
let tag = nvt.get_tag();
assert_eq!(tag, "Tag Name=Tag Value|Tag Name1=Tag Value1");
let expected = vec![("Tag Name".to_string(), "Tag Value".to_string())];

assert_eq!(tag, &expected);
}

#[test]
Expand Down
15 changes: 15 additions & 0 deletions rust/nvtcache/tests/nvtcache_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ mod test {
)?;
fake_nvt.add_pref(pref);

//Add first tag
fake_nvt.add_tag("Tag Name".to_string(), "Tag Value".to_string());
let tag = fake_nvt.get_tag();
let expected = vec![("Tag Name".to_string(), "Tag Value".to_string())];
assert_eq!(tag, &expected);

//Add second tag cvss_base, which is ignored
fake_nvt.add_tag("cvss_base".to_string(), "Tag Value1".to_string());
let tag = fake_nvt.get_tag();
let expected = vec![("Tag Name".to_string(), "Tag Value".to_string())];
assert_eq!(tag, &expected);

let filename = "custom.nasl".to_owned();
match nvtcache.add_nvt(fake_nvt, filename) {
Ok(_) => println!("Nvt successfully added"),
Expand All @@ -108,6 +120,9 @@ mod test {
item = nvtcache.get_nvt_field("1234".to_owned(), KbNvtPos::NvtNamePos)?;
assert_eq!(item, "Custom Script for the vulnerability 1");

item = nvtcache.get_nvt_field("1234".to_owned(), KbNvtPos::NvtTagsPos)?;
assert_eq!(item, "Tag Name=Tag Value");

let _ = nvtcache.reset();

return Ok(());
Expand Down

0 comments on commit 260ccb1

Please sign in to comment.