udp_packet/udp_packet.rs
1use ip_spoofing::{self, RawSocket, ReusablePacketWriter};
2
3/// This example shows how to generate fake UDP packet
4/// that delivers `b"hey"` bytes from "8.8.8.8:1234" to "127.0.0.1:5678".
5///
6/// I.e. the attacker changes its IPv4 address to 8.8.8.8 (Google Public DNS)
7fn main() -> ip_spoofing::Result<()> {
8 //wrapper around raw sockets, requires root privileges
9 let socket = RawSocket::new()?;
10
11 //wrapper for writing packets in pre-allocated memory
12 let mut writer = ReusablePacketWriter::new();
13
14 //sends fake UDP packet
15 socket.send_fake_udp_packet(
16 &mut writer,
17 [8, 8, 8, 8], //source IPv4 address
18 1234, //source port
19 [127, 0, 0, 1], //destination IPv4 address
20 5678, //destination port
21 b"hey", //data
22 64, //TTL on most Linux machines is 64
23 )?;
24
25 Ok(())
26}