r_lanlib/packet/
rst_packet.rs

1//! Provides helpers for creating RST packets
2
3use std::net;
4
5use derive_builder::Builder;
6use pnet::{
7    packet::{MutablePacket, ethernet, ip, ipv4, tcp},
8    util,
9};
10
11const PKT_ETH_SIZE: usize = ethernet::EthernetPacket::minimum_packet_size();
12const PKT_IP4_SIZE: usize = ipv4::Ipv4Packet::minimum_packet_size();
13const PKT_TCP_SIZE: usize = tcp::TcpPacket::minimum_packet_size();
14const PKT_TOTAL_SIZE: usize = PKT_ETH_SIZE + PKT_IP4_SIZE + PKT_TCP_SIZE;
15
16/// Represents a generator for raw RST packets
17#[derive(Debug, Builder)]
18#[builder(setter(into))]
19pub struct RstPacket {
20    /// IP address of the host machine performing scanning
21    source_ip: net::Ipv4Addr,
22    /// MAC address of host machine performing scanning
23    source_mac: util::MacAddr,
24    /// Source port on which host machine is listening for packets
25    source_port: u16,
26    /// Target destination IP for the packet
27    dest_ip: net::Ipv4Addr,
28    /// Target destination MAC address for the packet
29    dest_mac: util::MacAddr,
30    /// Target destination port for the packet
31    dest_port: u16,
32    /// The sequence number for this packet
33    sequence_number: u32,
34}
35
36impl RstPacket {
37    /// Builds a new RST request packet based on provided info.
38    /// RST packet is sent when a response is found for a SYN request. The RST
39    /// packet indicates that we want to reset the connection. This is called
40    /// "half open" scanning and helps to keep things stealthy
41    pub fn to_raw(&self) -> [u8; PKT_TOTAL_SIZE] {
42        let mut pkt_buf = [0u8; PKT_TOTAL_SIZE];
43
44        let mut eth_header = ethernet::MutableEthernetPacket::new(&mut pkt_buf)
45            .expect("failed to generate ethernet header");
46        eth_header.set_ethertype(ethernet::EtherTypes::Ipv4);
47        eth_header.set_source(self.source_mac);
48        eth_header.set_destination(self.dest_mac);
49
50        // set ip header
51        let mut ip_buffer = [0u8; PKT_IP4_SIZE + PKT_TCP_SIZE];
52
53        let mut ip_header =
54            ipv4::MutableIpv4Packet::new(&mut ip_buffer).expect("failed to generate ip header");
55
56        ip_header.set_next_level_protocol(ip::IpNextHeaderProtocols::Tcp);
57        ip_header.set_source(self.source_ip);
58        ip_header.set_destination(self.dest_ip);
59        ip_header.set_version(4);
60        ip_header.set_ttl(64);
61        ip_header.set_identification(0);
62        ip_header.set_header_length(5);
63        ip_header.set_total_length(40);
64        ip_header.set_checksum(ipv4::checksum(&ip_header.to_immutable()));
65
66        // set tcp header
67        let mut tcp_buffer = [0u8; PKT_TCP_SIZE];
68
69        let mut tcp_header =
70            tcp::MutableTcpPacket::new(&mut tcp_buffer).expect("failed to generate tcp header");
71
72        tcp_header.set_source(self.source_port);
73        tcp_header.set_destination(self.dest_port);
74        tcp_header.set_flags(tcp::TcpFlags::RST);
75        tcp_header.set_data_offset(5);
76        tcp_header.set_sequence(self.sequence_number);
77        tcp_header.set_checksum(tcp::ipv4_checksum(
78            &tcp_header.to_immutable(),
79            &self.source_ip,
80            &self.dest_ip,
81        ));
82
83        ip_header.set_payload(tcp_header.packet_mut());
84        eth_header.set_payload(ip_header.packet_mut());
85
86        pkt_buf
87    }
88}
89
90#[cfg(test)]
91#[path = "./rst_packet_tests.rs"]
92mod tests;