diff --git a/rust/src/scannerctl/main.rs b/rust/src/scannerctl/main.rs index 575828605..7d3835fec 100644 --- a/rust/src/scannerctl/main.rs +++ b/rust/src/scannerctl/main.rs @@ -8,7 +8,7 @@ mod execute; mod feed; mod interpret; mod notusupdate; -mod ospd; +mod osp; mod scanconfig; mod syntax; @@ -62,7 +62,7 @@ async fn main() { ); let matches = syntax::extend_args(matches); let matches = scanconfig::extend_args(matches); - let matches = ospd::extend_args(matches); + let matches = osp::extend_args(matches); let matches = execute::extend_args(matches); let matches = notusupdate::scanner::extend_args(matches); let matches = feed::extend_args(matches).get_matches(); @@ -100,7 +100,7 @@ async fn run(matches: &ArgMatches) -> Result<(), CliError> { if let Some(result) = notusupdate::scanner::run(matches).await { return result; } - if let Some(result) = ospd::run(matches).await { + if let Some(result) = osp::run(matches).await { return result; } Err(CliError { diff --git a/rust/src/scannerctl/ospd/mod.rs b/rust/src/scannerctl/osp/mod.rs similarity index 86% rename from rust/src/scannerctl/ospd/mod.rs rename to rust/src/scannerctl/osp/mod.rs index e97d25c60..e2e21eb2c 100644 --- a/rust/src/scannerctl/ospd/mod.rs +++ b/rust/src/scannerctl/osp/mod.rs @@ -6,9 +6,9 @@ use std::io::BufRead; use std::{io::BufReader, path::PathBuf, sync::Arc}; use clap::{arg, value_parser, Arg, ArgAction, Command}; -use scannerlib::models::{Parameter, Scan, VT}; +use scannerlib::models::{self, Parameter, Scan, VT}; use scannerlib::storage::{self, DefaultDispatcher, StorageError}; -use start_scan::StartScan; +use start_scan::{StartScan, VtSelection}; use crate::{CliError, CliErrorKind}; use scannerlib::storage::item::{NVTField, NVTKey}; @@ -18,8 +18,8 @@ mod start_scan; pub fn extend_args(cmd: Command) -> Command { cmd.subcommand(crate::add_verbose( - Command::new("ospd") - .about("Transforms a ospd-start-scan xml to a scan json for openvasd. ") + Command::new("osp") + .about("Transforms a osp start-scan xml to a scan json for openvasd. ") .arg( arg!(-p --path "Path to the feed.") .required(false) @@ -37,7 +37,7 @@ pub fn extend_args(cmd: Command) -> Command { )) } -pub async fn may_start_scan( +pub async fn may_transform_start_scan( print_back: bool, feed: Option, reader: R, @@ -48,7 +48,7 @@ where { match quick_xml::de::from_reader(reader) { Ok(x) if print_back => Some(Ok(format!("{x}"))), - Ok(x) if feed.is_some() => Some(start_scan(feed.unwrap(), x).await), + Ok(x) if feed.is_some() => Some(transform_start_scan(feed.unwrap(), x).await), Ok(_) => Some(Err(CliErrorKind::MissingArguments( vec!["path".to_string()], ))), @@ -56,14 +56,11 @@ where } } -async fn start_scan(feed: S, sc: StartScan) -> Result +async fn transform_vts(feed: S, vts: VtSelection) -> Result, CliErrorKind> where S: storage::Retriever, { - // currently we ignore the previous order as the scanner will reorder - // when scheduling internally anyway. - let svts = sc - .vt_selection + let mut result: Vec<_> = vts .vt_single .into_iter() .flatten() @@ -78,26 +75,16 @@ where .collect(), }) .collect(); - let gvts = sc - .vt_selection - .vt_group - .into_iter() - .flatten() - .filter_map( - |x| match x.filter.split_once('=').map(|(k, v)| (k.trim(), v.trim())) { - Some(("family", v)) => Some(v.to_string()), - filter => { - tracing::warn!(?filter, "only family is supported, ignoring entry"); - None - } - }, - ); - let mut scan = Scan { - scan_id: sc.id.unwrap_or_default(), - scan_preferences: sc.scanner_params.values, - target: sc.targets.target.into(), - vts: svts, - }; + let gvts = vts.vt_group.into_iter().flatten().filter_map(|x| { + match x.filter.split_once('=').map(|(k, v)| (k.trim(), v.trim())) { + Some(("family", v)) => Some(v.to_string()), + filter => { + tracing::warn!(?filter, "only family is supported, ignoring entry"); + None + } + } + }); + // we iterate here to return an error when storage is behaving in an unexpected fashion for family in gvts { let fvts: Vec = match feed.retry_retrieve_by_field( @@ -120,9 +107,24 @@ where } Err(e) => return Err(e.into()), }; - scan.vts.extend(fvts); + result.extend(fvts); } - scan.vts.sort(); + result.sort(); + Ok(result) +} + +async fn transform_start_scan(feed: S, sc: StartScan) -> Result +where + S: storage::Retriever, +{ + // currently we ignore the previous order as the scanner will reorder + // when scheduling internally anyway. + let scan = Scan { + scan_id: sc.id.unwrap_or_default(), + scan_preferences: sc.scanner_params.values, + target: sc.targets.target.into(), + vts: transform_vts(feed, sc.vt_selection).await?, + }; let scan_json = match serde_json::to_string_pretty(&scan) { Ok(s) => s, Err(e) => return Err(e.into()), @@ -160,7 +162,7 @@ pub async fn run(root: &clap::ArgMatches) -> Option> { }; let print_back = args.get_one::("back").cloned().unwrap_or_default(); // currently we just support start scan if that changes chain the options. - let output = may_start_scan(print_back, feed, &mut bufreader).await; + let output = may_transform_start_scan(print_back, feed, &mut bufreader).await; let result = match output { Some(Ok(x)) => { println!("{x}"); @@ -241,7 +243,7 @@ mod tests { dispatch("2", "A"); dispatch("3", "A"); - let output = may_start_scan(false, Some(d), reader) + let output = may_transform_start_scan(false, Some(d), reader) .await .unwrap() .unwrap(); @@ -287,7 +289,7 @@ mod tests { "#; let reader = BufReader::new(Cursor::new(input)); - let output = may_start_scan::<_, DefaultDispatcher>(true, None, reader) + let output = may_transform_start_scan::<_, DefaultDispatcher>(true, None, reader) .await .unwrap() .unwrap(); diff --git a/rust/src/scannerctl/ospd/start_scan.rs b/rust/src/scannerctl/osp/start_scan.rs similarity index 100% rename from rust/src/scannerctl/ospd/start_scan.rs rename to rust/src/scannerctl/osp/start_scan.rs diff --git a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__pare_credential_without_port.snap b/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__pare_credential_without_port.snap deleted file mode 100644 index 174874017..000000000 --- a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__pare_credential_without_port.snap +++ /dev/null @@ -1,37 +0,0 @@ ---- -source: src/scannerctl/ospd/start_scan.rs -expression: sc ---- - - - - 127.0.0.1 - T:80-80,443-443 - - 1 - - localhost - - - PASSWORD - USER - - - - - - - - postgres - - - - - 0 - 5 - /cgi-bin:/scripts - 0 - - - - diff --git a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__parse_without_credential.snap b/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__parse_without_credential.snap deleted file mode 100644 index 88aca68f6..000000000 --- a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__parse_without_credential.snap +++ /dev/null @@ -1,33 +0,0 @@ ---- -source: src/scannerctl/ospd/start_scan.rs -expression: sc ---- - - - - 127.0.0.1 - T:80-80,443-443 - - 1 - - localhost - localhost - - - - - - - postgres - - - - - 0 - 5 - /cgi-bin:/scripts - 0 - - - - diff --git a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__parse_xml.snap b/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__parse_xml.snap deleted file mode 100644 index ec17357b5..000000000 --- a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__parse_xml.snap +++ /dev/null @@ -1,38 +0,0 @@ ---- -source: src/scannerctl/ospd/start_scan.rs -expression: sc ---- - - - - 127.0.0.1 - T:80-80,443-443 - T:80-80,443-443 - - 1 - - localhost - - - PASSWORD - USER - - - - - - - - postgres - - - - - 0 - 5 - /cgi-bin:/scripts - 0 - - - - diff --git a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__parse_xml_with_empty_credentials.snap b/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__parse_xml_with_empty_credentials.snap deleted file mode 100644 index 7d9e6ea49..000000000 --- a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__start_scan__test__parse_xml_with_empty_credentials.snap +++ /dev/null @@ -1,34 +0,0 @@ ---- -source: src/scannerctl/ospd/start_scan.rs -expression: sc ---- - - - - 127.0.0.1 - T:80-80,443-443 - - 1 - - localhost - - - - - - - - - postgres - - - - - 0 - 5 - /cgi-bin:/scripts - 0 - - - - diff --git a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__tests__print_back.snap b/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__tests__print_back.snap deleted file mode 100644 index 2defc56fb..000000000 --- a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__tests__print_back.snap +++ /dev/null @@ -1,38 +0,0 @@ ---- -source: src/scannerctl/ospd/mod.rs -expression: output ---- - - - - 127.0.0.1 - T:80-80,443-443 - T:80-80,443-443 - - 1 - - localhost - - - PASSWORD - USER - - - - - - - - postgres - - - - - 0 - 5 - /cgi-bin:/scripts - 0 - - - - diff --git a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__tests__print_scan_json.snap b/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__tests__print_scan_json.snap deleted file mode 100644 index 8c3e2ebaa..000000000 --- a/rust/src/scannerctl/ospd/snapshots/scannerctl__ospd__tests__print_scan_json.snap +++ /dev/null @@ -1,123 +0,0 @@ ---- -source: src/scannerctl/ospd/mod.rs -expression: output ---- -{ - "scan_id": "36389b56-f5a0-11e9-bba4-482ae354ac4c", - "target": { - "hosts": [ - "127.0.0.1" - ], - "ports": [ - { - "protocol": "tcp", - "range": [ - { - "start": 80, - "end": 80 - } - ] - }, - { - "protocol": "tcp", - "range": [ - { - "start": 443, - "end": 443 - } - ] - } - ], - "excluded_hosts": [ - "localhost" - ], - "credentials": [ - { - "service": "ssh", - "port": 22, - "up": { - "username": "USER", - "password": "PASSWORD" - } - } - ], - "alive_test_ports": [ - { - "protocol": "tcp", - "range": [ - { - "start": 80, - "end": 80 - } - ] - }, - { - "protocol": "tcp", - "range": [ - { - "start": 443, - "end": 443 - } - ] - } - ], - "alive_test_methods": [ - "icmp" - ], - "reverse_lookup_unify": null, - "reverse_lookup_only": null - }, - "scan_preferences": [ - { - "id": "use_mac_addr", - "value": "0" - }, - { - "id": "checks_read_timeout", - "value": "5" - }, - { - "id": "cgi_path", - "value": "/cgi-bin:/scripts" - }, - { - "id": "time_between_request", - "value": "0" - }, - { - "id": "vhosts_ip", - "value": "" - }, - { - "id": "vhosts", - "value": "" - } - ], - "vts": [ - { - "oid": "0", - "parameters": [] - }, - { - "oid": "1", - "parameters": [] - }, - { - "oid": "1.3.6.1.4.1.25623.1.0.100151", - "parameters": [ - { - "id": 1, - "value": "postgres" - } - ] - }, - { - "oid": "2", - "parameters": [] - }, - { - "oid": "3", - "parameters": [] - } - ] -}