Skip to content

Commit

Permalink
Refactor. Reduce duplicated code (get tcp and udp elements shared bet…
Browse files Browse the repository at this point in the history
…ween ipv4 and ipv6). Code Improvements and readability. Fix and add tests.
  • Loading branch information
jjnicola committed Feb 27, 2025
1 parent 95429fe commit d99bb22
Show file tree
Hide file tree
Showing 12 changed files with 1,916 additions and 2,492 deletions.
2 changes: 1 addition & 1 deletion rust/examples/packet_forgery.nasl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ip_packet = forge_ip_packet(ip_v : 4,
ip_hl : 5,
ip_tos : 0,
ip_len : 20,
ip_id : rand(),
ip_id : 1234,
ip_p : IPPROTO_TCP, # 0x06
ip_ttl : 255,
ip_off : 0,
Expand Down
30 changes: 30 additions & 0 deletions rust/examples/packet_forgery_tcp_opts.nasl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-FileCopyrightText: 2025 Greenbone AG
#
# SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

ip_packet = forge_ip_packet(ip_v : 4,
ip_hl : 5,
ip_tos : 0,
ip_len : 20,
ip_id : 1234,
ip_p : 0x06,
ip_ttl : 255,
ip_off : 0,
ip_src : 192.168.1.6,
ip_dst : 192.168.1.1);
tcp_packet = forge_tcp_packet(ip: ip_packet,
data: 1234,
th_sport: 5080,
th_dport: 80,
th_seq: 1000,
th_ack: 0,
th_x2: 0,
th_off: 5,
th_flags: 33,
th_win: 0,
th_sum: 0,
th_urp: 0);
tcp_packet = insert_tcp_options(tcp: tcp_packet, 3, 10);
opt = get_tcp_option(tcp: tcp_packet, option: 3);
dump_tcp_packet(tcp_packet);
send_packet(tcp_packet);
34 changes: 34 additions & 0 deletions rust/examples/packet_forgery_tcp_v6.nasl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SPDX-FileCopyrightText: 2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

ip6_packet = forge_ip_v6_packet( ip6_v: 6,
ip6_p: 0x06,
ip6_hlim: 128,
ip6_src: "5858::1",
ip6_dst: "5858::1");

dump_ip_v6_packet(ip6_packet);

tcp6_packet = forge_tcp_v6_packet(ip6: ip6_packet,
th_sport: 5080,
th_dport: 80,
th_seq: 1000,
th_ack: 0,
th_x2: 0,
th_off: 5,
th_flags: 33,
th_win: 0,
th_sum: 0,
th_urp: 0);
dump_tcp_v6_packet(tcp6_packet);

send_v6packet(tcp6_packet);

tcp6_packet = set_tcp_v6_elements(tcp: tcp6_packet, th_sport: 33000);
get_tcp_v6_element(tcp:tcp6_packet, element:"th_sport");
##tcp_packet_opts = insert_tcp_v6_options(tcp: tcp6_packet, 3, 2);
##opt = get_tcp_v6_option(tcp: tcp_packet_opts, option: 3);

#dump_tcp_v6_packet(tcp_packet_opts);

20 changes: 14 additions & 6 deletions rust/examples/packet_forgery_udp_v6.nasl
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

### For testing local
# sudo ip addr add 5858::1/64 dev wlp6s0
# sudo ip -6 route add 5858::1 dev wlp6s0
# sudo target/debug/scannerctl execute script examples/packet_forgery_udp_v6.nasl -t "5858::1
###

IP6_HLIM = 128;

src = "5858::1";
dst = "5858::2";
dst = "5858::1";

ip6 = forge_ip_v6_packet( ip6_v: 6, # IP6_v,
ip6_p: IPPROTO_UDP, #0x11
ip6_plen:40,
ip6_hlim:IP6_HLIM,
ip6_src: src,
ip6_dst: dst);

dump_ip_v6_packet (ip6);

udp6_packet = forge_udp_v6_packet(ip: ip6,
udp6_packet = forge_udp_v6_packet(ip6: ip6,
uh_sport: 5080,
uh_dport: 80,
uh_len: 12,
th_sum: 0,
data: "1234");
uh_ulen: 12,
uh_sum: 0,
data: 1234);

display(get_udp_v6_element(udp:udp6_packet, element:"uh_sport"));

udp6_packet = set_udp_v6_elements(udp: udp6_packet, uh_sport: 33000);

display(get_udp_v6_element(udp:udp6_packet, element:"uh_sport"));

dump_ip_v6_packet (udp6_packet);
Expand Down
6 changes: 3 additions & 3 deletions rust/src/nasl/builtin/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use super::cryptographic::CryptographicError;
use super::host::HostError;
use super::http::HttpError;
use super::isotime::IsotimeError;
#[cfg(feature = "nasl-builtin-raw-ip")]
use super::raw_ip::RawIpError;
use super::regex::RegexError;
use super::sys::SysError;
use super::KBError;
use super::{misc::MiscError, network::socket::SocketError, ssh::SshError, string::StringError};
#[cfg(feature = "nasl-builtin-raw-ip")]
use super::raw_ip::RawIpError;


#[derive(Debug, Error)]
pub enum BuiltinError {
#[error("{0}")]
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use error::BuiltinError;
pub use host::HostError;
pub use knowledge_base::KBError;
#[cfg(feature = "nasl-builtin-raw-ip")]
pub use raw_ip::{RawIpError, RawIp, PacketForgeryError};
pub use raw_ip::{PacketForgeryError, RawIpError};

use crate::nasl::syntax::{Loader, NoOpLoader};
use crate::nasl::utils::{Context, Executor, NaslVarRegister, NaslVarRegisterBuilder, Register};
Expand Down
4 changes: 2 additions & 2 deletions rust/src/nasl/builtin/raw_ip/frame_forgery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ fn nasl_send_arp_request(register: &Register, context: &Context) -> Result<NaslV
"IPv6 does not support ARP protocol.",
));
}
let local_ip = get_source_ip(target_ip, 50000u16)?;
let local_ip = get_source_ip(target_ip)?;
let iface = get_interface_by_local_ip(local_ip)?;
let local_mac_address = get_local_mac_address(&iface.name)?;

Expand Down Expand Up @@ -501,7 +501,7 @@ fn nasl_send_frame(register: &Register, context: &Context) -> Result<NaslValue,

let target_ip = get_host_ip(context)?;

let local_ip = get_source_ip(target_ip, 50000u16)?;
let local_ip = get_source_ip(target_ip)?;
let iface = get_interface_by_local_ip(local_ip)?;

// send the frame and get a response if pcap_active enabled
Expand Down
17 changes: 14 additions & 3 deletions rust/src/nasl/builtin/raw_ip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ mod packet_forgery;
pub mod raw_ip_utils;
use std::io;

use crate::nasl::{utils::{IntoFunctionSet, NaslVars, StoredFunctionSet}, FnError};
use crate::nasl::{
utils::{IntoFunctionSet, NaslVars, StoredFunctionSet},
FnError,
};
use frame_forgery::FrameForgery;
use packet_forgery::PacketForgery;
use thiserror::Error;
Expand Down Expand Up @@ -39,8 +42,16 @@ pub enum PacketForgeryError {
ParseSocketAddr(std::net::AddrParseError),
#[error("Failed to send packet. {0}")]
SendPacket(std::io::Error),
#[error("Failed to create packet from buffer.")]
CreatePacket,
#[error("Failed to create an Ipv4 packet from invalid buffer size.")]
CreateIpv4Packet,
#[error("Failed to create an Ipv6 packet from invalid buffer size.")]
CreateIpv6Packet,
#[error("Failed to create a TCP packet from invalid buffer size.")]
CreateTcpPacket,
#[error("Failed to create a UDP packet from invalid buffer size.")]
CreateUdpPacket,
#[error("Failed to create a ICMP packet from invalid buffer size.")]
CreateIcmpPacket,
}

impl From<PacketForgeryError> for FnError {
Expand Down
Loading

0 comments on commit d99bb22

Please sign in to comment.