Skip to content

Commit

Permalink
Make Port an explicit type with FromNaslValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Dec 9, 2024
1 parent 2a256f4 commit ff9acfa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
21 changes: 14 additions & 7 deletions rust/src/nasl/builtin/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,19 @@ pub fn get_kb_item_str(context: &Context, name: &str) -> Result<String, FnError>
.ok_or_else(|| KBError(format!("KB key {} is not set", name)).into())
}

pub fn verify_port(port: i64) -> Result<u16, ArgumentError> {
if !(0..=65535).contains(&port) {
return Err(ArgumentError::WrongArgument(format!(
"{} is not a valid port number",
port
)));
struct Port(u16);

impl FromNaslValue<'_> for Port {
fn from_nasl_value(value: &NaslValue) -> Result<Self, FnError> {
let port = i64::from_nasl_value(value)?;
if !(0..=65535).contains(&port) {
return Err(ArgumentError::WrongArgument(format!(
"{} is not a valid port number",
port
))
.into());
} else {
Ok(Port(port as u16))
}
}
Ok(port as u16)
}
9 changes: 4 additions & 5 deletions rust/src/nasl/builtin/network/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use std::{net::IpAddr, process::Command};

use super::mtu;
use super::socket::SocketError;
use super::{mtu, Port};
use super::{
network_utils::{get_netmask_by_local_ip, get_source_ip, ipstr2ipaddr, islocalhost},
verify_port, DEFAULT_PORT,
DEFAULT_PORT,
};
use crate::function_set;
use crate::nasl::utils::{Context, FnError};
Expand Down Expand Up @@ -134,14 +134,13 @@ fn islocalnet(context: &Context) -> Result<bool, SocketError> {

/// Declares an open port on the target host
#[nasl_function(named(port, proto))]
fn scanner_add_port(context: &Context, port: i64, proto: Option<&str>) -> Result<(), FnError> {
let port = verify_port(port)?;
fn scanner_add_port(context: &Context, port: Port, proto: Option<&str>) -> Result<(), FnError> {
let protocol = proto.unwrap_or("tcp");

context.dispatcher().dispatch(
context.key(),
Field::KB(Kb {
key: format!("Port/{}/{}", protocol, port),
key: format!("Port/{}/{}", protocol, port.0),
value: Primitive::Number(1),
expire: None,
}),
Expand Down
33 changes: 14 additions & 19 deletions rust/src/nasl/builtin/network/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::{
tcp::TcpConnection,
tls::create_tls_client,
udp::UdpConnection,
verify_port, OpenvasEncaps,
OpenvasEncaps, Port,
};

#[derive(Debug, Error)]
Expand Down Expand Up @@ -431,15 +431,14 @@ impl NaslSockets {
fn open_sock_tcp(
&mut self,
context: &Context,
port: i64,
port: Port,
timeout: Option<i64>,
transport: Option<i64>,
bufsz: Option<i64>,
// TODO: Extract information from custom priority string
// priority: Option<&str>,
) -> Result<NaslValue, FnError> {
// Get port
let port = verify_port(port)?;
let transport = transport.unwrap_or(-1);

let addr = ipstr2ipaddr(context.target())?;
Expand All @@ -457,7 +456,7 @@ impl NaslSockets {
let sockets: Vec<Option<NaslSocket>> = vhosts
.iter()
.map(|vhost| {
Self::open_sock_tcp_vhost(context, addr, timeout, bufsz, port, vhost, transport)
Self::open_sock_tcp_vhost(context, addr, timeout, bufsz, port.0, vhost, transport)
})
.collect::<Result<_, _>>()?;

Expand Down Expand Up @@ -491,11 +490,10 @@ impl NaslSockets {

/// Open a UDP socket to the target host
#[nasl_function]
fn open_sock_udp(&mut self, context: &Context, port: i64) -> Result<NaslValue, FnError> {
let port = verify_port(port)?;
fn open_sock_udp(&mut self, context: &Context, port: Port) -> Result<NaslValue, FnError> {
let addr = ipstr2ipaddr(context.target())?;

let socket = NaslSocket::Udp(UdpConnection::new(addr, port)?);
let socket = NaslSocket::Udp(UdpConnection::new(addr, port.0)?);
let fd = self.add(socket);

Ok(NaslValue::Number(fd as i64))
Expand Down Expand Up @@ -525,28 +523,25 @@ impl NaslSockets {
fn open_priv_sock(
&mut self,
addr: IpAddr,
dport: i64,
sport: Option<i64>,
dport: Port,
sport: Option<Port>,
tcp: bool,
) -> Result<NaslValue, FnError> {
let dport = verify_port(dport)?;

if let Some(sport) = sport {
let sport = verify_port(sport)?;
return Ok(self.connect_priv_sock(addr, sport, dport as u16, tcp)?);
return Ok(self.connect_priv_sock(addr, sport.0, dport.0 as u16, tcp)?);
}

for sport in (1..=1023).rev() {
let fd = if tcp {
// TODO: set timeout to global recv timeout when available
let timeout = Duration::from_secs(10);
self.wait_before_next_probe();
if let Ok(tcp) = TcpConnection::connect_priv(addr, sport, dport, timeout) {
if let Ok(tcp) = TcpConnection::connect_priv(addr, sport, dport.0, timeout) {
self.add(NaslSocket::Tcp(Box::new(tcp)))
} else {
continue;
}
} else if let Ok(udp) = UdpConnection::new_priv(addr, sport, dport) {
} else if let Ok(udp) = UdpConnection::new_priv(addr, sport, dport.0) {
self.add(NaslSocket::Udp(udp))
} else {
continue;
Expand All @@ -566,8 +561,8 @@ impl NaslSockets {
fn open_priv_sock_tcp(
&mut self,
context: &Context,
dport: i64,
sport: Option<i64>,
dport: Port,
sport: Option<Port>,
) -> Result<NaslValue, FnError> {
let addr = ipstr2ipaddr(context.target())?;
self.open_priv_sock(addr, dport, sport, true)
Expand All @@ -582,8 +577,8 @@ impl NaslSockets {
fn open_priv_sock_udp(
&mut self,
context: &Context,
dport: i64,
sport: Option<i64>,
dport: Port,
sport: Option<Port>,
) -> Result<NaslValue, FnError> {
let addr = ipstr2ipaddr(context.target())?;
self.open_priv_sock(addr, dport, sport, false)
Expand Down

0 comments on commit ff9acfa

Please sign in to comment.