bluebox 0.1.4

A fast DNS interceptor and cache for local networks
Documentation
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Packet capture abstraction.
//!
//! Provides a trait-based abstraction over packet capture to enable:
//! - Testing without real network interfaces
//! - Different capture backends

use std::net::IpAddr;

use pnet::datalink::{self, Channel, DataLinkReceiver, DataLinkSender, NetworkInterface};
use pnet::packet::Packet;
use pnet::packet::ethernet::{EtherTypes, EthernetPacket};
use pnet::packet::ipv4::Ipv4Packet;
use pnet::packet::ipv6::Ipv6Packet;
use pnet::packet::udp::UdpPacket;
use pnet::util::MacAddr;

use crate::error::{NetworkError, Result};

/// Information extracted from a captured packet.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PacketInfo {
    pub source_mac: MacAddr,
    pub dest_mac: MacAddr,
    pub source_ip: IpAddr,
    pub dest_ip: IpAddr,
    pub source_port: u16,
    pub dest_port: u16,
}

/// Trait for packet capture implementations.
pub trait PacketCapture: Send {
    /// Receive the next packet.
    /// Returns None if the capture has ended.
    fn next_packet(&mut self) -> Option<Vec<u8>>;
}

/// Trait for packet sending implementations.
pub trait PacketSender: Send {
    /// Send a packet.
    fn send(&mut self, packet: &[u8]) -> Result<()>;
}

/// Find a suitable network interface.
///
/// Returns the first interface that is:
/// - Up (active)
/// - Not a loopback interface
/// - Has at least one IP address
pub fn find_interface(name: Option<&str>) -> Result<NetworkInterface> {
    let interfaces = datalink::interfaces();

    if let Some(name) = name {
        interfaces
            .into_iter()
            .find(|iface| iface.name == name)
            .ok_or_else(|| NetworkError::NoInterface.into())
    } else {
        interfaces
            .into_iter()
            .find(|iface| iface.is_up() && !iface.is_loopback() && !iface.ips.is_empty())
            .ok_or_else(|| NetworkError::NoInterface.into())
    }
}

/// Production packet capture using pnet.
pub struct PnetCapture {
    rx: Box<dyn DataLinkReceiver>,
}

impl PnetCapture {
    /// Create a new capture for the given interface.
    pub fn new(interface: &NetworkInterface) -> Result<(Self, PnetSender)> {
        let (tx, rx) = match datalink::channel(interface, pnet::datalink::Config::default()) {
            Ok(Channel::Ethernet(tx, rx)) => (tx, rx),
            Ok(_) => return Err(NetworkError::UnsupportedChannel.into()),
            Err(e) => return Err(NetworkError::ChannelOpen(e.to_string()).into()),
        };

        Ok((Self { rx }, PnetSender { tx }))
    }
}

impl PacketCapture for PnetCapture {
    fn next_packet(&mut self) -> Option<Vec<u8>> {
        self.rx.next().ok().map(<[u8]>::to_vec)
    }
}

/// Production packet sender using pnet.
pub struct PnetSender {
    tx: Box<dyn DataLinkSender>,
}

impl PacketSender for PnetSender {
    fn send(&mut self, packet: &[u8]) -> Result<()> {
        self.tx
            .send_to(packet, None)
            .ok_or_else(|| NetworkError::SendFailed("send returned None".into()))?
            .map_err(|e| NetworkError::SendFailed(e.to_string()))?;
        Ok(())
    }
}

/// Extract DNS query information from an Ethernet packet.
///
/// Returns None if:
/// - The packet is not IPv4 or IPv6
/// - The packet is not UDP
/// - The destination port is not 53 (DNS)
pub fn extract_dns_query(packet: &[u8]) -> Option<(PacketInfo, Vec<u8>)> {
    let ethernet = EthernetPacket::new(packet)?;

    let (source_ip, dest_ip, udp_payload) = match ethernet.get_ethertype() {
        EtherTypes::Ipv4 => {
            let ipv4 = Ipv4Packet::new(ethernet.payload())?;
            (
                IpAddr::V4(ipv4.get_source()),
                IpAddr::V4(ipv4.get_destination()),
                ipv4.payload().to_vec(),
            )
        }
        EtherTypes::Ipv6 => {
            let ipv6 = Ipv6Packet::new(ethernet.payload())?;
            (
                IpAddr::V6(ipv6.get_source()),
                IpAddr::V6(ipv6.get_destination()),
                ipv6.payload().to_vec(),
            )
        }
        _ => return None,
    };

    let udp = UdpPacket::new(&udp_payload)?;

    // Only interested in DNS queries (port 53)
    if udp.get_destination() != 53 {
        return None;
    }

    let packet_info = PacketInfo {
        source_mac: ethernet.get_source(),
        dest_mac: ethernet.get_destination(),
        source_ip,
        dest_ip,
        source_port: udp.get_source(),
        dest_port: udp.get_destination(),
    };

    Some((packet_info, udp.payload().to_vec()))
}

/// Test utilities for packet capture (only available in tests).
#[allow(clippy::cast_possible_truncation)] // Test packet sizes are always small
#[cfg(test)]
pub mod tests {
    use super::*;
    use std::collections::VecDeque;
    use std::sync::{Arc, Mutex};

    /// Mock packet capture for testing.
    pub struct MockCapture {
        packets: VecDeque<Vec<u8>>,
    }

    impl MockCapture {
        pub fn new(packets: Vec<Vec<u8>>) -> Self {
            Self {
                packets: packets.into(),
            }
        }
    }

    impl PacketCapture for MockCapture {
        fn next_packet(&mut self) -> Option<Vec<u8>> {
            self.packets.pop_front()
        }
    }

    /// Mock packet sender for testing.
    #[derive(Clone, Default)]
    pub struct MockSender {
        pub sent_packets: Arc<Mutex<Vec<Vec<u8>>>>,
    }

    impl MockSender {
        pub fn new() -> Self {
            Self::default()
        }

        pub fn sent_count(&self) -> usize {
            self.sent_packets.lock().unwrap().len()
        }

        pub fn last_sent(&self) -> Option<Vec<u8>> {
            self.sent_packets.lock().unwrap().last().cloned()
        }
    }

    impl PacketSender for MockSender {
        fn send(&mut self, packet: &[u8]) -> Result<()> {
            self.sent_packets.lock().unwrap().push(packet.to_vec());
            Ok(())
        }
    }

    #[test]
    fn should_return_packets_in_order_from_mock_capture() {
        let packets = vec![vec![1, 2, 3], vec![4, 5, 6]];
        let mut capture = MockCapture::new(packets);

        assert_eq!(capture.next_packet(), Some(vec![1, 2, 3]));
        assert_eq!(capture.next_packet(), Some(vec![4, 5, 6]));
        assert_eq!(capture.next_packet(), None);
    }

    #[test]
    fn should_track_sent_packets_in_mock_sender() {
        let mut sender = MockSender::new();

        sender.send(&[1, 2, 3]).unwrap();
        assert_eq!(sender.sent_count(), 1);

        sender.send(&[4, 5, 6]).unwrap();
        assert_eq!(sender.sent_count(), 2);
        assert_eq!(sender.last_sent(), Some(vec![4, 5, 6]));
    }

    #[test]
    fn should_compare_packet_info_by_value() {
        use std::net::{Ipv4Addr, Ipv6Addr};

        let info1 = PacketInfo {
            source_mac: MacAddr::new(1, 2, 3, 4, 5, 6),
            dest_mac: MacAddr::new(6, 5, 4, 3, 2, 1),
            source_ip: IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)),
            dest_ip: IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2)),
            source_port: 12345,
            dest_port: 53,
        };

        let info2 = info1.clone();
        assert_eq!(info1, info2);

        let info3 = PacketInfo {
            source_ip: IpAddr::V6(Ipv6Addr::LOCALHOST),
            ..info1
        };
        assert_ne!(info1, info3);
    }

    #[test]
    fn should_extract_dns_query_from_valid_ipv4_packet() {
        use pnet::packet::ethernet::MutableEthernetPacket;
        use pnet::packet::ip::IpNextHeaderProtocols;
        use pnet::packet::ipv4::MutableIpv4Packet;
        use pnet::packet::udp::MutableUdpPacket;
        use std::net::Ipv4Addr;

        let dns_payload = b"DNS query data";
        let udp_len = 8 + dns_payload.len();
        let ipv4_len = 20 + udp_len;
        let total_len = 14 + ipv4_len;

        let mut buffer = vec![0u8; total_len];

        // Build Ethernet header
        {
            let mut eth = MutableEthernetPacket::new(&mut buffer[..]).unwrap();
            eth.set_source(MacAddr::new(0x11, 0x22, 0x33, 0x44, 0x55, 0x66));
            eth.set_destination(MacAddr::new(0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff));
            eth.set_ethertype(EtherTypes::Ipv4);
        }

        // Build IPv4 header
        {
            let mut ipv4 = MutableIpv4Packet::new(&mut buffer[14..]).unwrap();
            ipv4.set_version(4);
            ipv4.set_header_length(5);
            ipv4.set_total_length(ipv4_len as u16);
            ipv4.set_next_level_protocol(IpNextHeaderProtocols::Udp);
            ipv4.set_source(Ipv4Addr::new(192, 168, 1, 100));
            ipv4.set_destination(Ipv4Addr::new(192, 168, 1, 1));
        }

        // Build UDP header
        {
            let mut udp = MutableUdpPacket::new(&mut buffer[34..]).unwrap();
            udp.set_source(12345);
            udp.set_destination(53); // DNS port
            udp.set_length(udp_len as u16);
            udp.set_payload(dns_payload);
        }

        let result = extract_dns_query(&buffer);
        assert!(result.is_some());

        let (info, payload) = result.unwrap();
        assert_eq!(
            info.source_mac,
            MacAddr::new(0x11, 0x22, 0x33, 0x44, 0x55, 0x66)
        );
        assert_eq!(
            info.dest_mac,
            MacAddr::new(0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff)
        );
        assert_eq!(info.source_ip, IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100)));
        assert_eq!(info.dest_ip, IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)));
        assert_eq!(info.source_port, 12345);
        assert_eq!(info.dest_port, 53);
        assert_eq!(payload, dns_payload);
    }

    #[test]
    fn should_extract_dns_query_from_valid_ipv6_packet() {
        use pnet::packet::ethernet::MutableEthernetPacket;
        use pnet::packet::ip::IpNextHeaderProtocols;
        use pnet::packet::ipv6::MutableIpv6Packet;
        use pnet::packet::udp::MutableUdpPacket;
        use std::net::Ipv6Addr;

        let dns_payload = b"DNS query data";
        let udp_len = 8 + dns_payload.len();
        let total_len = 14 + 40 + udp_len; // Ethernet + IPv6 + UDP

        let mut buffer = vec![0u8; total_len];

        // Build Ethernet header
        {
            let mut eth = MutableEthernetPacket::new(&mut buffer[..]).unwrap();
            eth.set_source(MacAddr::new(0x11, 0x22, 0x33, 0x44, 0x55, 0x66));
            eth.set_destination(MacAddr::new(0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff));
            eth.set_ethertype(EtherTypes::Ipv6);
        }

        // Build IPv6 header
        {
            let mut ipv6 = MutableIpv6Packet::new(&mut buffer[14..]).unwrap();
            ipv6.set_version(6);
            ipv6.set_payload_length(udp_len as u16);
            ipv6.set_next_header(IpNextHeaderProtocols::Udp);
            ipv6.set_source(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1));
            ipv6.set_destination(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 2));
        }

        // Build UDP header
        {
            let mut udp = MutableUdpPacket::new(&mut buffer[54..]).unwrap();
            udp.set_source(54321);
            udp.set_destination(53); // DNS port
            udp.set_length(udp_len as u16);
            udp.set_payload(dns_payload);
        }

        let result = extract_dns_query(&buffer);
        assert!(result.is_some());

        let (info, payload) = result.unwrap();
        assert_eq!(
            info.source_ip,
            IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1))
        );
        assert_eq!(
            info.dest_ip,
            IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 2))
        );
        assert_eq!(info.source_port, 54321);
        assert_eq!(info.dest_port, 53);
        assert_eq!(payload, dns_payload);
    }

    #[test]
    fn should_return_none_for_non_dns_port() {
        use pnet::packet::ethernet::MutableEthernetPacket;
        use pnet::packet::ip::IpNextHeaderProtocols;
        use pnet::packet::ipv4::MutableIpv4Packet;
        use pnet::packet::udp::MutableUdpPacket;
        use std::net::Ipv4Addr;

        let dns_payload = b"Not DNS";
        let udp_len = 8 + dns_payload.len();
        let ipv4_len = 20 + udp_len;
        let total_len = 14 + ipv4_len;

        let mut buffer = vec![0u8; total_len];

        {
            let mut eth = MutableEthernetPacket::new(&mut buffer[..]).unwrap();
            eth.set_ethertype(EtherTypes::Ipv4);
        }

        {
            let mut ipv4 = MutableIpv4Packet::new(&mut buffer[14..]).unwrap();
            ipv4.set_version(4);
            ipv4.set_header_length(5);
            ipv4.set_total_length(ipv4_len as u16);
            ipv4.set_next_level_protocol(IpNextHeaderProtocols::Udp);
            ipv4.set_source(Ipv4Addr::new(192, 168, 1, 100));
            ipv4.set_destination(Ipv4Addr::new(192, 168, 1, 1));
        }

        {
            let mut udp = MutableUdpPacket::new(&mut buffer[34..]).unwrap();
            udp.set_source(12345);
            udp.set_destination(80); // HTTP port, not DNS
            udp.set_length(udp_len as u16);
            udp.set_payload(dns_payload);
        }

        let result = extract_dns_query(&buffer);
        assert!(result.is_none());
    }

    #[test]
    fn should_return_none_for_non_ip_packet() {
        use pnet::packet::ethernet::MutableEthernetPacket;

        let mut buffer = vec![0u8; 64];
        {
            let mut eth = MutableEthernetPacket::new(&mut buffer[..]).unwrap();
            eth.set_ethertype(EtherTypes::Arp); // Not IPv4 or IPv6
        }

        let result = extract_dns_query(&buffer);
        assert!(result.is_none());
    }

    #[test]
    fn should_return_none_for_too_short_packet() {
        let buffer = vec![0u8; 10]; // Too short for Ethernet header
        let result = extract_dns_query(&buffer);
        assert!(result.is_none());
    }
}