-
Notifications
You must be signed in to change notification settings - Fork 650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add: nasl function support for IPv6 packet forgery, TCP and UDP over IPv6 and ICMPv6 #1661
base: main
Are you sure you want to change the base?
Conversation
e6c6cd6
to
13a7fc8
Compare
🔍 Vulnerabilities of
|
digest | sha256:c213edb64058e381d92a5c0d98109a9a8b5d95efd37d1471dc0b18c1061d4450 |
vulnerabilities | |
size | 121 MB |
packages | 261 |
📦 Base Image debian:stable-20250224-slim
also known as |
|
digest | sha256:068a6b5298e3d27d1fcfbf71209831568be8516a030a41ae13dacb3e1f42feb1 |
vulnerabilities |
Description
Description
Description
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
Description
Description
Description
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
Description
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
13a7fc8
to
f84b30c
Compare
f84b30c
to
be723cc
Compare
Waiting for rust-lang/socket2#518 |
be723cc
to
daad0d3
Compare
Dependency ReviewThe following issues were found:
|
waiting for rust-lang/socket2#535 |
daad0d3
to
1892854
Compare
11dc609
to
8f898fe
Compare
8f898fe
to
5603f82
Compare
5603f82
to
956332b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments before I do a detailed review:
I think nasl function argument handling could be used to remove a ton of boilerplate in this code and make the actual logic stand out more. This primarily means moving the primitive types (flags etc) to arguments given by nasl_function
and removing all of the custom register.get(...)
calls which are boilerplaty and have error handling that is less helpful and inconsistent with the majority of the builtins.
For example this code:
#[nasl_function]
fn get_udp_element(register: &Register) -> Result<NaslValue, FnError> {
let buf = match register.named("udp") {
Some(ContextType::Value(NaslValue::Data(d))) => d.clone(),
_ => {
return Err(FnError::missing_argument("udp"));
}
};
let ip = packet::ipv4::Ipv4Packet::new(&udp)
.ok_or_else(|| error("No possible to create a packet from buffer".to_string()))?;
let udp = packet::udp::UdpPacket::new(ip.payload())
.ok_or_else(|| error("No possible to create a packet from buffer".to_string()))?;
...
}
could be replaced (as a first step) by
#[nasl_function(named(udp))]
fn get_udp_element(udp: &[u8], ...) -> Result<NaslValue, FnError> {
let ip = packet::ipv4::Ipv4Packet::new(&udp)
.ok_or_else(|| error("No possible to create a packet from buffer".to_string()))?;
let udp = packet::udp::UdpPacket::new(ip.payload())
.ok_or_else(|| error("No possible to create a packet from buffer".to_string()))?;
...
}
However, I think we can go further than this and do the rusty thing of moving the parsing logic to the type system via something like this (note that UdpPacket
uses the owned version here to avoid some borrow checker difficulties, but I think this could be fixed if it is a performance issue and we decide to go this way):
impl<'a> FromNaslValue<'a> for Ipv4Packet<'a> {
fn from_nasl_value(val: &'a NaslValue) -> Result<Self, FnError> {
let buf: &[u8] = <&[u8]>::from_nasl_value(val)?;
let ip = packet::ipv4::Ipv4Packet::new(&buf)
.ok_or_else(|| PacketForgeryError::CreatePacket)?;
Ok(ip)
}
}
impl<'a> FromNaslValue<'a> for UdpPacket<'a> {
fn from_nasl_value(val: &'a NaslValue) -> Result<Self, FnError> {
let ip: Ipv4Packet = Ipv4Packet::from_nasl_value(val)?;
let udp = packet::udp::UdpPacket::owned(ip.payload().to_vec())
.ok_or_else(|| PacketForgeryError::CreatePacket)?;
Ok(udp)
}
}
Doing this for the various packet types will allow us to simply write something like this in virtually all of the builtin functions:
#[nasl_function(named(udp, ...))]
fn get_udp_element(udp: UdpPacket, ...) -> Result<NaslValue, FnError> {
...
}
I think this will help separate argument handling from the code logic, parse as early as possible and allow reusing those packet types in various places.
Finally, I think a similar strategy could be used for the different elements of the packets which are currently only represented by the element
argument string. To clarify what I mean, here is the example of the get_udp_element
method:
pub enum UdpElement {
SourcePort,
DestinationPort,
Length,
Checksum,
}
// This is not very inspiring to write, and could even be done by a macro if we want to
impl FromStr for UdpElement {
type Err = ArgumentError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"uh_sport" => Ok(Self::SourcePort),
"uh_dport" => Ok(Self::DestinationPort),
"uh_len" => Ok(Self::Length),
"uh_sum" => Ok(Self::Checksum),
s => Err(ArgumentError::WrongArgument(format!("Invalid element for UDP packet: {}", s)).into()),
}
}
}
trait PacketElement {
type Packet<'a>: pnet::packet::Packet;
fn get_element<'a>(&self, packet: &Self::Packet<'a>) -> i64;
}
impl PacketElement for UdpElement {
type Packet<'a> = packet::udp::UdpPacket<'a>;
fn get_element<'a>(&self, packet: &Self::Packet<'a>) -> i64 {
match self {
UdpElement::SourcePort => packet.get_source() as i64,
UdpElement::DestinationPort => packet.get_destination() as i64,
UdpElement::Length => packet.get_length() as i64,
UdpElement::Checksum => packet.get_checksum() as i64,
}
}
}
/// Represents the fact that the `element` argument to
/// the nasl functions below can often be of value "data",
/// meaning that the function should simply return the
/// payload of the packet.
enum ElementOrData<T> {
Element(T),
Data,
}
impl<'a, T> FromNaslValue<'a> for ElementOrData<T>
where
T: FromStr<Err=ArgumentError>,
{
fn from_nasl_value(value: &NaslValue) -> Result<Self, FnError> {
let s = String::from_nasl_value(value)?;
Ok(match s.as_str() {
"data" => Self::Data,
s => Self::Element(
T::from_str(&s)?
),
})
}
}
impl<T: PacketElement> ElementOrData<T> {
fn get<'a>(self, packet: &T::Packet<'a>) -> NaslValue {
match self {
ElementOrData::Element(element) => element.get_element(packet).into(),
ElementOrData::Data => packet.payload().into(),
}
}
}
#[nasl_function(named(udp, element))]
fn get_udp_element(udp: UdpPacket, element: ElementOrData<UdpElement>) -> Result<NaslValue, FnError> {
Ok(element.get(&udp))
}
Using these element types again allows us to separate the parsing logic (which is often done twice for "setter" and "getter" builtins respectively) from the actual interesting logic, make future changes easier and avoid accidentally introducing typos or something similar that would break functionality.
956332b
to
b57941c
Compare
b57941c
to
4586f22
Compare
4586f22
to
d99bb22
Compare
…ween ipv4 and ipv6). Code Improvements and readability. Fix and add tests.
d99bb22
to
061617c
Compare
What:
Add: nasl function support for IPv6 packet forgery, TCP and UDP over IPv6 and ICMPv6
Jira: SC-1096
Why:
How:
Checklist: