Skip to content

Commit 1d920b1

Browse files
committed
Fix: Reverse lookup options
There was a whitespace at the end of the `reverse_lookup_unify` option. Also now always sets the defaults, also they are not present in the target config. Additionally extendet the tests for the preference handling to cover all preference handling steps.
1 parent bfd0ad2 commit 1d920b1

File tree

2 files changed

+177
-44
lines changed

2 files changed

+177
-44
lines changed

rust/src/openvas/openvas_redis.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception
44

5-
use crate::storage::item::Nvt;
5+
use crate::nasl::syntax::ACT;
6+
use crate::storage::item::{Nvt, NvtPreference, PreferenceType};
67
use crate::storage::redis::{DbError, RedisCtx, RedisGetNvt, RedisStorageResult, RedisWrapper};
8+
use std::collections::BTreeMap;
79
use std::{
810
collections::HashMap,
911
sync::{Arc, Mutex, MutexGuard},
@@ -133,8 +135,39 @@ impl FakeRedis {
133135
}
134136

135137
impl VtHelper for FakeRedis {
136-
fn get_vt(&self, _: &str) -> RedisStorageResult<Option<Nvt>> {
137-
Ok(None)
138+
fn get_vt(&self, oid: &str) -> RedisStorageResult<Option<Nvt>> {
139+
match oid {
140+
"123" => Ok(Some(Nvt {
141+
oid: "123".to_string(),
142+
name: "test".to_string(),
143+
filename: "test.nasl".to_string(),
144+
tag: BTreeMap::new(),
145+
dependencies: Vec::new(),
146+
required_keys: Vec::new(),
147+
mandatory_keys: Vec::new(),
148+
excluded_keys: Vec::new(),
149+
required_ports: Vec::new(),
150+
required_udp_ports: Vec::new(),
151+
references: Vec::new(),
152+
preferences: vec![
153+
NvtPreference {
154+
id: Some(1),
155+
class: PreferenceType::CheckBox,
156+
name: "test1".to_string(),
157+
default: "no".to_string(),
158+
},
159+
NvtPreference {
160+
id: Some(2),
161+
class: PreferenceType::Entry,
162+
name: "test2".to_string(),
163+
default: "".to_string(),
164+
},
165+
],
166+
category: ACT::Init,
167+
family: "test".to_string(),
168+
})),
169+
_ => Ok(None),
170+
}
138171
}
139172
}
140173

rust/src/openvas/pref_handler.rs

Lines changed: 141 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,17 @@ where
9797

9898
// prepare vt preferences
9999
for pref in &vt.parameters {
100-
if let Some((prefid, class, name, _value)) =
101-
nvt.preferences.iter().find_map(|p| {
102-
if let Some(i) = p.id {
103-
if i as u16 == pref.id {
104-
Some(p.into())
105-
} else {
106-
None
107-
}
100+
if let Some((prefid, class, name, _)) = nvt.preferences.iter().find_map(|p| {
101+
if let Some(i) = p.id {
102+
if i as u16 == pref.id {
103+
Some(p.into())
108104
} else {
109105
None
110106
}
111-
})
112-
{
107+
} else {
108+
None
109+
}
110+
}) {
113111
let value_aux: String = if class == *"checkbox" {
114112
bool_to_str(&pref.value)
115113
} else {
@@ -293,30 +291,31 @@ where
293291
async fn prepare_reverse_lookup_opt_for_openvas(&mut self) -> RedisStorageResult<()> {
294292
let mut lookup_opts: Vec<String> = vec![];
295293

296-
if let Some(reverse_lookup_only) = self.scan_config.target.reverse_lookup_only {
297-
if reverse_lookup_only {
298-
lookup_opts.push("reverse_lookup_only|||yes".to_string());
299-
} else {
300-
lookup_opts.push("reverse_lookup_only|||no".to_string());
301-
}
302-
}
303-
304-
if let Some(reverse_lookup_unify) = self.scan_config.target.reverse_lookup_unify {
305-
if reverse_lookup_unify {
306-
lookup_opts.push("reverse_lookup_unify|||yes".to_string());
307-
} else {
308-
lookup_opts.push("reverse_lookup_unify|||no ".to_string());
309-
}
294+
if self
295+
.scan_config
296+
.target
297+
.reverse_lookup_only
298+
.is_some_and(|x| x)
299+
{
300+
lookup_opts.push("reverse_lookup_only|||yes".to_string());
301+
} else {
302+
lookup_opts.push("reverse_lookup_only|||no".to_string());
310303
}
311304

312-
if !lookup_opts.is_empty() {
313-
self.redis_connector.push_kb_item(
314-
format!("internal/{}/scanprefs", self.scan_config.scan_id.clone()).as_str(),
315-
lookup_opts,
316-
)?
305+
if self
306+
.scan_config
307+
.target
308+
.reverse_lookup_unify
309+
.is_some_and(|x| x)
310+
{
311+
lookup_opts.push("reverse_lookup_unify|||yes".to_string());
312+
} else {
313+
lookup_opts.push("reverse_lookup_unify|||no".to_string());
317314
}
318-
319-
Ok(())
315+
self.redis_connector.push_kb_item(
316+
format!("internal/{}/scanprefs", self.scan_config.scan_id.clone()).as_str(),
317+
lookup_opts,
318+
)
320319
}
321320

322321
async fn prepare_target_for_openvas(&mut self) -> RedisStorageResult<()> {
@@ -549,6 +548,7 @@ mod tests {
549548
scan_id: "123-456".to_string(),
550549
..Default::default()
551550
};
551+
scan.target.hosts = vec!["127.0.0.1".to_string(), "10.0.0.1".to_string()];
552552
scan.target.alive_test_methods = vec![AliveTestMethods::Icmp, AliveTestMethods::TcpSyn];
553553
scan.target.credentials = vec![Credential {
554554
service: Service::SSH,
@@ -573,48 +573,148 @@ mod tests {
573573
},
574574
],
575575
}];
576+
scan.scan_preferences = vec![
577+
crate::models::ScanPreference {
578+
id: "testParam1".to_string(),
579+
value: "1".to_string(),
580+
},
581+
crate::models::ScanPreference {
582+
id: "testParam2".to_string(),
583+
value: "abc".to_string(),
584+
},
585+
];
586+
scan.vts = vec![crate::models::VT {
587+
oid: "123".to_string(),
588+
parameters: vec![
589+
crate::models::Parameter {
590+
id: 1,
591+
value: "yes".to_string(),
592+
},
593+
crate::models::Parameter {
594+
id: 2,
595+
value: "abc".to_string(),
596+
},
597+
],
598+
}];
576599

577600
let mut rc = FakeRedis {
578601
data: HashMap::new(),
579602
};
580603

581604
let mut prefh = PreferenceHandler::new(scan, &mut rc);
582605
assert_eq!(prefh.redis_connector.kb_id().unwrap(), 3);
606+
// Prepare and test Scan ID
583607
assert!(prefh.prepare_scan_id_for_openvas().await.is_ok());
584608
assert!(prefh
585609
.redis_connector
586610
.item_exists("internal/scanid", "123-456"));
587611
assert!(prefh.redis_connector.item_exists("internal/123-456", "new"));
588612

589-
assert!(prefh.prepare_main_kbindex_for_openvas().await.is_ok());
613+
// Prepare and test Target
614+
assert!(prefh.prepare_target_for_openvas().await.is_ok());
590615
assert!(prefh
591616
.redis_connector
592-
.item_exists("internal/123-456/scanprefs", "ov_maindbid|||3"));
617+
.item_exists("internal/123-456/scanprefs", "TARGET|||127.0.0.1,10.0.0.1"));
593618

594-
assert!(prefh.prepare_boreas_alive_test().await.is_ok());
619+
// Prepare and test Ports
620+
assert!(prefh.prepare_ports_for_openvas().await.is_ok());
621+
assert!(prefh.redis_connector.item_exists(
622+
"internal/123-456/scanprefs",
623+
"port_range|||T:22,23,24,25,80,"
624+
));
625+
626+
// Prepare and test Credentials
627+
assert!(prefh.prepare_credentials_for_openvas().await.is_ok());
628+
assert!(prefh.redis_connector.item_exists(
629+
"internal/123-456/scanprefs",
630+
"1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!):|||pass"
631+
));
632+
assert!(prefh.redis_connector.item_exists(
633+
"internal/123-456/scanprefs",
634+
"1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||user"
635+
));
636+
637+
// Prepare and test Plugins
638+
assert!(prefh.prepare_plugins_for_openvas().await.is_ok());
595639
assert!(prefh
596640
.redis_connector
597-
.item_exists("internal/123-456/scanprefs", "ALIVE_TEST|||18"));
641+
.item_exists("internal/123-456/scanprefs", "plugin_set|||123"));
598642

643+
// Prepare and test Main KB Index
644+
assert!(prefh.prepare_main_kbindex_for_openvas().await.is_ok());
645+
assert!(prefh
646+
.redis_connector
647+
.item_exists("internal/123-456/scanprefs", "ov_maindbid|||3"));
648+
649+
// Prepare and test Host Options
599650
assert!(prefh.prepare_host_options_for_openvas().await.is_ok());
600651
assert!(prefh
601652
.redis_connector
602653
.item_exists("internal/123-456/scanprefs", "exclude_hosts|||127.0.0.1"));
603654

604-
assert!(prefh.prepare_credentials_for_openvas().await.is_ok());
655+
// Prepare and test Scan Params
656+
assert!(prefh.prepare_scan_params_for_openvas().await.is_ok());
657+
assert!(prefh
658+
.redis_connector
659+
.item_exists("internal/123-456/scanprefs", "testParam1|||1"));
660+
assert!(prefh
661+
.redis_connector
662+
.item_exists("internal/123-456/scanprefs", "testParam2|||abc"));
663+
664+
// Prepare and test Reverse Lookup Options
665+
assert!(prefh.prepare_reverse_lookup_opt_for_openvas().await.is_ok());
666+
assert!(prefh
667+
.redis_connector
668+
.item_exists("internal/123-456/scanprefs", "reverse_lookup_only|||no"));
669+
assert!(prefh
670+
.redis_connector
671+
.item_exists("internal/123-456/scanprefs", "reverse_lookup_unify|||no"));
672+
673+
// Prepare Alive Test Options
674+
// To test this options we have to call prepare_nvt_preferences first
675+
assert!(prefh.prepare_alive_test_option_for_openvas().await.is_ok());
676+
677+
// Prepare NVT Preferences
678+
assert!(prefh.prepare_nvt_preferences().await.is_ok());
679+
680+
// Test Alive Test Options
605681
assert!(prefh.redis_connector.item_exists(
606682
"internal/123-456/scanprefs",
607-
"1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!):|||pass"
683+
"1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||yes"
608684
));
609685
assert!(prefh.redis_connector.item_exists(
610686
"internal/123-456/scanprefs",
611-
"1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||user"
687+
"1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping|||no"
612688
));
613-
614-
assert!(prefh.prepare_ports_for_openvas().await.is_ok());
615689
assert!(prefh.redis_connector.item_exists(
616690
"internal/123-456/scanprefs",
617-
"port_range|||T:22,23,24,25,80,"
691+
"1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||yes"
692+
));
693+
assert!(prefh.redis_connector.item_exists(
694+
"internal/123-456/scanprefs",
695+
"1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes"
696+
));
697+
assert!(prefh.redis_connector.item_exists(
698+
"internal/123-456/scanprefs",
699+
"1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no"
700+
));
701+
assert!(prefh.redis_connector.item_exists(
702+
"internal/123-456/scanprefs",
703+
"1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unreachable Hosts as dead (not scanning)|||yes"
618704
));
705+
706+
// Test NVT Preferences
707+
assert!(prefh
708+
.redis_connector
709+
.item_exists("internal/123-456/scanprefs", "123:1:checkbox:test1|||yes"));
710+
assert!(prefh
711+
.redis_connector
712+
.item_exists("internal/123-456/scanprefs", "123:2:entry:test2|||abc"));
713+
714+
// Prepare Boreas Alive Test
715+
assert!(prefh.prepare_boreas_alive_test().await.is_ok());
716+
assert!(prefh
717+
.redis_connector
718+
.item_exists("internal/123-456/scanprefs", "ALIVE_TEST|||18"));
619719
}
620720
}

0 commit comments

Comments
 (0)