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

source

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
Hide additional 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:?}");
        }
    }
}
source

pub fn sendto(&self, buf: &[u8], addr: [u8; 4]) -> Result<usize>

Wraps sendto call with nix crate and other extra security checks

source

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
Hide additional 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:?}");
        }
    }
}
source

pub fn send_fake_tcp_syn_packet(
    &self,
    writer: &mut ReusablePacketWriter,
    source: [u8; 4],
    source_port: u16,
    destination: [u8; 4],
    destination_port: u16,
    payload: &[u8],
    time_to_live: u8,
    sequence_number: u32,
    window_size: u16
) -> Result<usize>

Sends fake TCP-SYN packet with given parameters

Trait Implementations§

source§

impl AsRawFd for RawSocket

Implementation that converts RawSocket into fd (file descriptor)

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Debug for RawSocket

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for RawSocket

Destructor implementation for RawSocket

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere
    V: MultiLane<T>,

§

fn vzip(self) -> V