Skip to content

Commit 854e8e3

Browse files
authored
Fix: insert alive test methods and ports (#2155)
Methods and ports are not inserted and the default icmp methods is always used
1 parent 8c0f005 commit 854e8e3

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

rust/crates/greenbone-scanner-framework/src/models/port.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ impl Display for Protocol {
8484
}
8585
}
8686

87+
impl AsRef<str> for Protocol {
88+
fn as_ref(&self) -> &str {
89+
match self {
90+
Protocol::TCP => "tcp",
91+
Protocol::UDP => "udp",
92+
}
93+
}
94+
}
95+
8796
pub fn ports_to_openvas_port_list(ports: Vec<Port>) -> Option<String> {
8897
fn add_range_to_list(list: &mut String, start: usize, end: Option<usize>) {
8998
// Add range

rust/crates/greenbone-scanner-framework/src/models/target.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ pub enum AliveTestMethodsError {
5454
InvalidValue(u8),
5555
}
5656

57+
impl AsRef<str> for AliveTestMethods {
58+
fn as_ref(&self) -> &str {
59+
match self {
60+
AliveTestMethods::TcpAck => "tcp_ack",
61+
AliveTestMethods::Icmp => "icmp",
62+
AliveTestMethods::Arp => "arp",
63+
AliveTestMethods::ConsiderAlive => "consider_alive",
64+
AliveTestMethods::TcpSyn => "tcp_syn",
65+
}
66+
}
67+
}
68+
5769
impl TryFrom<u8> for AliveTestMethods {
5870
type Error = AliveTestMethodsError;
5971

rust/src/openvasd/database/sqlite/scans.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,13 @@ where
140140
port.range
141141
.clone()
142142
.into_iter()
143-
.map(move |r| (port.protocol, r))
143+
.map(move |r| (port.protocol.as_ref(), r))
144144
}),
145145
|mut b, (protocol, range)| {
146146
b.push_bind(&mapped_id)
147147
.push_bind(match protocol {
148148
None => "udp_tcp",
149-
Some(models::Protocol::TCP) => "tcp",
150-
Some(models::Protocol::UDP) => "udp",
149+
Some(x) => x.as_ref(),
151150
})
152151
.push_bind(range.start as i64)
153152
.push_bind(range.end.map(|x| x as i64));
@@ -158,6 +157,47 @@ where
158157
query.execute(&mut *tx).await?;
159158
}
160159
}
160+
if !scan.target.alive_test_ports.is_empty() {
161+
for ports in scan
162+
.target
163+
.alive_test_ports
164+
.chunks(SQLITE_LIMIT_VARIABLE_NUMBER / 4)
165+
{
166+
let mut builder =
167+
QueryBuilder::new("INSERT INTO ports (id, protocol, start, end, alive)");
168+
builder.push_values(
169+
ports.iter().flat_map(|port| {
170+
port.range
171+
.clone()
172+
.into_iter()
173+
.map(move |r| (port.protocol.as_ref(), r))
174+
}),
175+
|mut b, (protocol, range)| {
176+
b.push_bind(&mapped_id)
177+
.push_bind(match protocol {
178+
None => "udp_tcp",
179+
Some(x) => x.as_ref(),
180+
})
181+
.push_bind(range.start as i64)
182+
.push_bind(range.end.map(|x| x as i64))
183+
.push_bind(true);
184+
},
185+
);
186+
let query = builder.build();
187+
188+
query.execute(&mut *tx).await?;
189+
}
190+
}
191+
192+
if !scan.target.alive_test_methods.is_empty() {
193+
let mut builder = QueryBuilder::new("INSERT INTO alive_methods (id, method)");
194+
builder.push_values(&scan.target.alive_test_methods, |mut b, method| {
195+
b.push_bind(&mapped_id).push_bind(method.as_ref());
196+
});
197+
let query = builder.build();
198+
query.execute(&mut *tx).await?;
199+
}
200+
161201
let mut scan_preferences = scan.scan_preferences.clone();
162202
if scan.target.reverse_lookup_unify.unwrap_or_default() {
163203
scan_preferences.push(models::ScanPreference {

0 commit comments

Comments
 (0)