pingly 0.2.0

TLS and HTTP/1/2/3 fingerprint analysis server and Rust library
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
use std::{
    collections::{HashMap, VecDeque},
    error::Error as StdError,
    io,
    net::SocketAddr,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc, Mutex, MutexGuard,
    },
    thread::{self, JoinHandle},
    time::Duration,
};

use pcap::{Active, Capture, Device, Linktype, PacketHeader};
use pingly::tcp::{LinkLayer, TcpPacket};
use serde::{Deserialize, Serialize};

type CaptureError = Box<dyn StdError + Send + Sync>;

/// Direction of a captured packet relative to the Pingly listener.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PacketDirection {
    /// Packet sent by the remote client to Pingly.
    Inbound,

    /// Packet sent by Pingly to the remote client.
    Outbound,
}

/// One timestamped TCP packet captured for the configured listener.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CapturedPacket {
    /// Packet timestamp supplied by libpcap, in microseconds since the Unix epoch.
    pub timestamp_us: u64,

    /// Packet direction relative to the configured server port.
    pub direction: PacketDirection,

    /// Decoded IP and TCP wire fields.
    #[serde(flatten)]
    pub packet: TcpPacket,
}

struct CaptureInner {
    packets: Arc<Mutex<CaptureStore>>,
    shutdown: Arc<AtomicBool>,
    worker: Mutex<Option<JoinHandle<()>>>,
}

impl Drop for CaptureInner {
    fn drop(&mut self) {
        self.shutdown.store(true, Ordering::Release);
        if let Some(worker) = lock(&self.worker).take() {
            let _ = worker.join();
        }
    }
}

/// Shared handle to the background libpcap capture worker.
#[derive(Clone)]
pub struct TcpCapture {
    inner: Arc<CaptureInner>,
}

impl TcpCapture {
    /// Opens a capture device and starts a background worker with bounded storage.
    pub fn start(
        max_connections: usize,
        server_port: u16,
        interface: Option<&str>,
    ) -> Result<Self, CaptureError> {
        if max_connections == 0 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "TCP capture connection capacity must be greater than zero",
            )
            .into());
        }

        let devices = Device::list()?;
        let device = choose_device(&devices, interface)?;
        let device_name = device.name.clone();
        let mut capture = Capture::from_device(device)?
            .promisc(device_name != "any")
            .snaplen(65_535)
            .timeout(100)
            .immediate_mode(true)
            .open()?;
        let link_layer = link_layer(capture.get_datalink())?;
        let filter = format!("tcp port {server_port}");
        capture.filter(&filter, true)?;
        let capture = capture.setnonblock()?;

        let packets = Arc::new(Mutex::new(CaptureStore::new(max_connections)));
        let shutdown = Arc::new(AtomicBool::new(false));
        let worker_packets = packets.clone();
        let worker_shutdown = shutdown.clone();
        let worker = thread::Builder::new()
            .name("pingly-tcp-capture".to_owned())
            .spawn(move || {
                if let Err(error) = capture_loop(
                    capture,
                    link_layer,
                    server_port,
                    worker_packets,
                    worker_shutdown,
                ) {
                    tracing::error!(%error, "TCP packet capture stopped");
                }
            })?;

        tracing::info!(interface = %device_name, %filter, "TCP packet capture started");
        Ok(Self {
            inner: Arc::new(CaptureInner {
                packets,
                shutdown,
                worker: Mutex::new(Some(worker)),
            }),
        })
    }

    /// Returns a copy of one remote socket's packets in wire order.
    pub fn connection_packets(&self, remote: SocketAddr) -> Vec<CapturedPacket> {
        lock(&self.inner.packets).connection_packets(remote)
    }

    /// Stops the worker and waits for its capture loop to exit.
    pub fn shutdown(&self) {
        self.inner.shutdown.store(true, Ordering::Release);
        if let Some(worker) = lock(&self.inner.worker).take() {
            let _ = worker.join();
        }
    }
}

const MAX_PACKETS_PER_CONNECTION: usize = 64;

struct CaptureStore {
    /// Latest captured generation for each normalized remote socket.
    connections: HashMap<SocketAddr, VecDeque<CapturedPacket>>,

    /// Connection insertion order used for bounded FIFO eviction.
    insertion_order: VecDeque<SocketAddr>,

    /// Maximum number of remote connection generations retained at once.
    max_connections: usize,
}

impl CaptureStore {
    fn new(max_connections: usize) -> Self {
        Self {
            connections: HashMap::with_capacity(max_connections),
            insertion_order: VecDeque::with_capacity(max_connections),
            max_connections,
        }
    }

    fn push(&mut self, packet: CapturedPacket) {
        let remote = packet.remote_address();
        let starts_connection =
            packet.direction == PacketDirection::Inbound && packet.packet.is_initial_syn();

        if starts_connection {
            self.remove(remote);
        }

        if !self.connections.contains_key(&remote) {
            self.evict_oldest_if_full();
            self.insertion_order.push_back(remote);
            self.connections.insert(remote, VecDeque::new());
        }

        if let Some(packets) = self.connections.get_mut(&remote) {
            if packets.len() < MAX_PACKETS_PER_CONNECTION {
                packets.push_back(packet);
            }
        }
    }

    fn connection_packets(&self, remote: SocketAddr) -> Vec<CapturedPacket> {
        self.connections
            .get(&super::connection_key(remote))
            .map(|packets| packets.iter().cloned().collect())
            .unwrap_or_default()
    }

    fn remove(&mut self, remote: SocketAddr) {
        self.connections.remove(&remote);
        self.insertion_order
            .retain(|candidate| *candidate != remote);
    }

    fn evict_oldest_if_full(&mut self) {
        if self.connections.len() < self.max_connections {
            return;
        }

        if let Some(oldest) = self.insertion_order.pop_front() {
            self.connections.remove(&oldest);
        }
    }
}

fn choose_device(devices: &[Device], interface: Option<&str>) -> Result<Device, CaptureError> {
    if let Some(interface) = interface {
        return devices
            .iter()
            .find(|device| device.name == interface)
            .cloned()
            .ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::NotFound,
                    format!("packet capture interface {interface} was not found"),
                )
                .into()
            });
    }

    devices
        .iter()
        .find(|device| device.name == "any")
        .cloned()
        .or_else(|| Device::lookup().ok().flatten())
        .or_else(|| devices.first().cloned())
        .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "no pcap devices found").into())
}

fn link_layer(linktype: Linktype) -> Result<LinkLayer, CaptureError> {
    let link_layer = match linktype {
        Linktype::NULL | Linktype::LOOP => LinkLayer::Null,
        Linktype::ETHERNET => LinkLayer::Ethernet,
        Linktype::LINUX_SLL => LinkLayer::LinuxSll,
        Linktype::LINUX_SLL2 => LinkLayer::LinuxSll2,
        Linktype::RAW => LinkLayer::RawIp,
        Linktype::IPV4 => LinkLayer::Ipv4,
        Linktype::IPV6 => LinkLayer::Ipv6,
        other => {
            return Err(io::Error::new(
                io::ErrorKind::Unsupported,
                format!("unsupported pcap link type {}", other.0),
            )
            .into())
        }
    };
    Ok(link_layer)
}

fn capture_loop(
    mut capture: Capture<Active>,
    link_layer: LinkLayer,
    server_port: u16,
    packets: Arc<Mutex<CaptureStore>>,
    shutdown: Arc<AtomicBool>,
) -> Result<(), pcap::Error> {
    while !shutdown.load(Ordering::Acquire) {
        match capture.next_packet() {
            Ok(captured) => {
                let Ok(packet) = TcpPacket::parse(captured.data, link_layer) else {
                    continue;
                };
                let Some(direction) = CapturedPacket::direction_for(&packet, server_port) else {
                    continue;
                };
                let captured = CapturedPacket {
                    timestamp_us: timestamp_us(captured.header),
                    direction,
                    packet,
                };
                lock(&packets).push(captured);
            }
            Err(pcap::Error::TimeoutExpired | pcap::Error::NoMorePackets) => {
                thread::sleep(Duration::from_millis(5));
            }
            Err(error) => return Err(error),
        }
    }

    Ok(())
}

fn timestamp_us(header: &PacketHeader) -> u64 {
    let seconds = u64::try_from(header.ts.tv_sec).unwrap_or_default();
    let micros = u64::try_from(header.ts.tv_usec).unwrap_or_default();
    seconds.saturating_mul(1_000_000).saturating_add(micros)
}

fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

#[cfg(test)]
mod tests {
    use std::net::{Ipv4Addr, SocketAddr};

    use pingly::tcp::{LinkLayer, TcpPacket};

    use super::{CaptureStore, CapturedPacket, PacketDirection, MAX_PACKETS_PER_CONNECTION};

    const SERVER_PORT: u16 = 443;

    #[test]
    fn new_syn_replaces_a_reused_remote_socket_generation() {
        let remote = SocketAddr::from((Ipv4Addr::new(192, 0, 2, 10), 50_000));
        let mut store = CaptureStore::new(2);
        store.push(packet(remote, 1, PacketDirection::Inbound, 10, 0, 0x02));
        store.push(packet(remote, 2, PacketDirection::Outbound, 20, 11, 0x12));
        store.push(packet(remote, 3, PacketDirection::Inbound, 11, 21, 0x10));
        store.push(packet(remote, 4, PacketDirection::Inbound, 30, 0, 0x02));

        let packets = store.connection_packets(remote);
        assert_eq!(packets.len(), 1);
        assert_eq!(packets[0].packet.tcp.sequence_number, 30);
    }

    #[test]
    fn evicts_the_oldest_connection_when_capacity_is_reached() {
        let first = SocketAddr::from((Ipv4Addr::new(192, 0, 2, 1), 50_001));
        let second = SocketAddr::from((Ipv4Addr::new(192, 0, 2, 2), 50_002));
        let third = SocketAddr::from((Ipv4Addr::new(192, 0, 2, 3), 50_003));
        let mut store = CaptureStore::new(2);

        for (timestamp, remote) in [(1, first), (2, second), (3, third)] {
            store.push(packet(
                remote,
                timestamp,
                PacketDirection::Inbound,
                10,
                0,
                0x02,
            ));
        }

        assert!(store.connection_packets(first).is_empty());
        assert_eq!(store.connection_packets(second).len(), 1);
        assert_eq!(store.connection_packets(third).len(), 1);
    }

    #[test]
    fn per_connection_limit_preserves_the_initial_handshake() {
        let remote = SocketAddr::from((Ipv4Addr::new(192, 0, 2, 10), 50_000));
        let mut store = CaptureStore::new(1);
        store.push(packet(remote, 1, PacketDirection::Inbound, 10, 0, 0x02));
        store.push(packet(remote, 2, PacketDirection::Outbound, 20, 11, 0x12));
        store.push(packet(remote, 3, PacketDirection::Inbound, 11, 21, 0x10));

        for timestamp in 4..=(MAX_PACKETS_PER_CONNECTION as u64 + 10) {
            store.push(packet(
                remote,
                timestamp,
                PacketDirection::Inbound,
                11,
                21,
                0x10,
            ));
        }

        let packets = store.connection_packets(remote);
        assert_eq!(packets.len(), MAX_PACKETS_PER_CONNECTION);
        assert!(packets[0].packet.is_initial_syn());
        assert!(packets[1].packet.is_syn_ack());
        assert!(packets[2].packet.is_handshake_ack());
    }

    fn packet(
        remote: SocketAddr,
        timestamp_us: u64,
        direction: PacketDirection,
        sequence_number: u32,
        acknowledgment_number: u32,
        flags: u8,
    ) -> CapturedPacket {
        let server = SocketAddr::from((Ipv4Addr::new(198, 51, 100, 20), SERVER_PORT));
        let (source, destination) = match direction {
            PacketDirection::Inbound => (remote, server),
            PacketDirection::Outbound => (server, remote),
        };
        let mut bytes = vec![0_u8; 40];
        bytes[0] = 0x45;
        bytes[2..4].copy_from_slice(&40_u16.to_be_bytes());
        bytes[8] = 64;
        bytes[9] = 6;
        let (source_ip, destination_ip) = match (source.ip(), destination.ip()) {
            (std::net::IpAddr::V4(source), std::net::IpAddr::V4(destination)) => {
                (source, destination)
            }
            _ => unreachable!("test fixture uses IPv4 endpoints"),
        };
        bytes[12..16].copy_from_slice(&source_ip.octets());
        bytes[16..20].copy_from_slice(&destination_ip.octets());
        let tcp = &mut bytes[20..];
        tcp[0..2].copy_from_slice(&source.port().to_be_bytes());
        tcp[2..4].copy_from_slice(&destination.port().to_be_bytes());
        tcp[4..8].copy_from_slice(&sequence_number.to_be_bytes());
        tcp[8..12].copy_from_slice(&acknowledgment_number.to_be_bytes());
        tcp[12] = 5 << 4;
        tcp[13] = flags;

        CapturedPacket {
            timestamp_us,
            direction,
            packet: TcpPacket::parse(&bytes, LinkLayer::Ipv4).unwrap(),
        }
    }
}