Struct ip_spoofing::RawSocket
source · pub struct RawSocket { /* private fields */ }Expand description
Wrapper around low-level socket(AF_INET, SOCK_RAW, IPPROTO_RAW)
Implementations§
source§impl RawSocket
impl RawSocket
sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Trying to create raw socket
Examples found in repository?
examples/udp_packet.rs (line 9)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() -> ip_spoofing::Result<()> {
//wrapper around raw sockets, requires root privileges
let socket = RawSocket::new()?;
//wrapper for writing packets in pre-allocated memory
let mut writer = ReusablePacketWriter::new();
//sends fake UDP packet
socket.send_fake_udp_packet(
&mut writer,
[8, 8, 8, 8], //source IPv4 address
1234, //source port
[127, 0, 0, 1], //destination IPv4 address
5678, //destination port
b"hey", //data
64, //TTL on most Linux machines is 64
)?;
Ok(())
}More examples
examples/udp_flood.rs (line 9)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() -> ip_spoofing::Result<()> {
//wrapper around raw sockets, requires root privileges
let socket = RawSocket::new()?;
//wrapper for writing packets in pre-allocated memory
let mut writer = ReusablePacketWriter::new();
//thread-local pseudorandom number generator
let mut rng = thread_rng();
//endless spam with a randomly generated UDP packet
loop {
let ret = socket.send_fake_udp_packet(
&mut writer,
rng.gen(), //random source IPv4 address
rng.gen(), //random source port
[127, 0, 0, 1], //destination IPv4 address
5678, //destination port
b"hey", //data
64, //TTL on most Linux machines is 64
);
if let Err(err) = ret {
println!("{err:?}");
}
}
}sourcepub fn sendto(&self, buf: &[u8], addr: [u8; 4]) -> Result<usize>
pub fn sendto(&self, buf: &[u8], addr: [u8; 4]) -> Result<usize>
Wraps sendto call with nix crate and other extra security checks
sourcepub fn send_fake_udp_packet(
&self,
writer: &mut ReusablePacketWriter,
source: [u8; 4],
source_port: u16,
destination: [u8; 4],
destination_port: u16,
payload: &[u8],
time_to_live: u8
) -> Result<usize>
pub fn send_fake_udp_packet(
&self,
writer: &mut ReusablePacketWriter,
source: [u8; 4],
source_port: u16,
destination: [u8; 4],
destination_port: u16,
payload: &[u8],
time_to_live: u8
) -> Result<usize>
Sends fake UDP packet with given parameters
Examples found in repository?
examples/udp_packet.rs (lines 15-23)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() -> ip_spoofing::Result<()> {
//wrapper around raw sockets, requires root privileges
let socket = RawSocket::new()?;
//wrapper for writing packets in pre-allocated memory
let mut writer = ReusablePacketWriter::new();
//sends fake UDP packet
socket.send_fake_udp_packet(
&mut writer,
[8, 8, 8, 8], //source IPv4 address
1234, //source port
[127, 0, 0, 1], //destination IPv4 address
5678, //destination port
b"hey", //data
64, //TTL on most Linux machines is 64
)?;
Ok(())
}More examples
examples/udp_flood.rs (lines 18-26)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() -> ip_spoofing::Result<()> {
//wrapper around raw sockets, requires root privileges
let socket = RawSocket::new()?;
//wrapper for writing packets in pre-allocated memory
let mut writer = ReusablePacketWriter::new();
//thread-local pseudorandom number generator
let mut rng = thread_rng();
//endless spam with a randomly generated UDP packet
loop {
let ret = socket.send_fake_udp_packet(
&mut writer,
rng.gen(), //random source IPv4 address
rng.gen(), //random source port
[127, 0, 0, 1], //destination IPv4 address
5678, //destination port
b"hey", //data
64, //TTL on most Linux machines is 64
);
if let Err(err) = ret {
println!("{err:?}");
}
}
}