huginn-net-http 2.0.0

HTTP fingerprinting (p0f-style) analysis for huginn-net
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use crate::error::HuginnNetHttpError;
use crate::http::common::HttpProcessor;
use crate::http::observable::{ObservableHttpRequest, ObservableHttpResponse};
use crate::http1::process as http1_process;
use crate::http2::process as http2_process;
use pnet::packet::ip::IpNextHeaderProtocols;
use pnet::packet::ipv4::Ipv4Packet;
use pnet::packet::ipv6::Ipv6Packet;
use pnet::packet::tcp::TcpPacket;
use pnet::packet::Packet;
use std::net::IpAddr;
#[cfg(any(feature = "p0f-request", feature = "p0f-response"))]
use std::time::Duration;
#[cfg(any(feature = "p0f-request", feature = "p0f-response"))]
use tracing::debug;
use ttl_cache::TtlCache;

/// FlowKey: (Client IP, Server IP, Client Port, Server Port)
pub type FlowKey = (IpAddr, IpAddr, u16, u16);

use crate::http::common::HttpParser;

/// Valid first bytes for an HTTP request payload.
///
/// Union of first letters of:
/// - HTTP/1.x methods (RFC 7231): `GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE, CONNECT, PATCH`
/// - WebDAV (RFC 4918): `PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK`
/// - WebDAV Versioning (RFC 3253): `REPORT`
/// - HTTP/2 connection preface (RFC 7540): `"PRI * HTTP/2.0..."` (starts with `P`)
///
/// Same fast-reject strategy used by nDPI's `http_fs = "CDGHLMOPRU"` table in
/// `src/lib/protocols/http.c` (we additionally include `T` for `TRACE`).
#[cfg(feature = "p0f-request")]
const HTTP_REQUEST_FIRST_BYTES: &[u8] = b"CDGHLMOPRTU";

/// Maximum legal frame length for an HTTP/2 frame using the default `SETTINGS_MAX_FRAME_SIZE`
/// (RFC 7540 ยง6.5.2). Used to discriminate HTTP/2 frames from random binary noise.
#[cfg(feature = "p0f-response")]
const HTTP2_MAX_FRAME_LEN: u32 = 16384;

/// Minimum payload size we attempt to parse. Below this we treat the data as
/// incomplete and wait for more TCP segments; avoids paying the trait-dispatch
/// cost on fragments that obviously can't contain an HTTP request or response.
#[cfg(feature = "p0f-request")]
const MIN_HTTP_PAYLOAD_LEN: usize = 4;

/// Cheap pre-filter: does this payload plausibly start an HTTP request (or HTTP/2
/// connection preface)? Rejects binary protocols (TLS, SSH, etc.) on flows tracked
/// by `huginn-net-http` before reaching the trait-dispatched parser path.
///
/// Also rejects fragments smaller than `MIN_HTTP_PAYLOAD_LEN`, so callers don't
/// need to do their own length guard; the function is self-validating.
#[cfg(feature = "p0f-request")]
fn looks_like_http_request(data: &[u8]) -> bool {
    if data.len() < MIN_HTTP_PAYLOAD_LEN {
        return false;
    }
    HTTP_REQUEST_FIRST_BYTES.contains(&data[0])
}

/// Cheap pre-filter: does this payload plausibly start an HTTP response?
/// - HTTP/1.x responses always start with the literal `"HTTP"` (then `/1.0` or `/1.1`).
/// - HTTP/2 responses are sequences of frames with no magic prefix, so we accept any
///   payload whose first 9 bytes look like a valid frame header (reasonable length and
///   a known frame type byte). Same structural test used by
///   `crate::http2::process::looks_like_http2_response`.
#[cfg(feature = "p0f-response")]
fn looks_like_http_response(data: &[u8]) -> bool {
    if data.starts_with(b"HTTP") {
        return true;
    }
    if data.len() < 9 {
        return false;
    }
    let frame_length = u32::from_be_bytes([0, data[0], data[1], data[2]]);
    if frame_length > HTTP2_MAX_FRAME_LEN {
        return false;
    }
    matches!(data[3], 0..=10)
}

/// HTTP parser that automatically detects and processes different HTTP versions
pub struct HttpProcessors {
    parsers: Vec<Box<dyn HttpParser>>,
}

impl HttpProcessors {
    pub fn new() -> Self {
        Self {
            parsers: vec![Box::new(Http1ParserAdapter::new()), Box::new(Http2ParserAdapter::new())],
        }
    }

    /// Parse HTTP request data using the appropriate parser
    #[cfg(feature = "p0f-request")]
    #[inline]
    pub fn parse_request(&self, data: &[u8]) -> Option<ObservableHttpRequest> {
        // Fast-reject: skip the trait-dispatched parser loop for payloads that
        // obviously can't start an HTTP request or HTTP/2 preface.
        if !looks_like_http_request(data) {
            return None;
        }
        for parser in &self.parsers {
            if parser.can_parse(data) {
                if let Some(result) = parser.parse_request(data) {
                    return Some(result);
                }
            }
        }
        None
    }

    /// Parse HTTP response data using the appropriate parser
    #[cfg(feature = "p0f-response")]
    #[inline]
    pub fn parse_response(&self, data: &[u8]) -> Option<ObservableHttpResponse> {
        // Fast-reject: skip the parser loop for payloads that don't look like an
        // HTTP/1.x response line or a valid HTTP/2 frame header.
        if !looks_like_http_response(data) {
            return None;
        }
        for parser in &self.parsers {
            if parser.can_parse(data) {
                if let Some(result) = parser.parse_response(data) {
                    return Some(result);
                }
            }
        }
        None
    }

    /// Get all supported HTTP versions
    pub fn supported_versions(&self) -> Vec<crate::http::Version> {
        self.parsers.iter().map(|p| p.supported_version()).collect()
    }
}

/// Adapter that bridges HTTP/1.x processor to the unified HttpParser interface
struct Http1ParserAdapter {
    processor: http1_process::Http1Processor,
}

impl Http1ParserAdapter {
    fn new() -> Self {
        Self { processor: http1_process::Http1Processor::new() }
    }
}

impl HttpParser for Http1ParserAdapter {
    fn supported_version(&self) -> crate::http::Version {
        crate::http::Version::V11
    }

    fn can_parse(&self, data: &[u8]) -> bool {
        self.processor.can_process_request(data) || self.processor.can_process_response(data)
    }

    fn name(&self) -> &'static str {
        "HTTP/1.x"
    }

    fn parse_request(&self, data: &[u8]) -> Option<ObservableHttpRequest> {
        self.processor.process_request(data).ok().flatten()
    }

    fn parse_response(&self, data: &[u8]) -> Option<ObservableHttpResponse> {
        self.processor.process_response(data).ok().flatten()
    }
}

/// Adapter that bridges HTTP/2 processor to the unified HttpParser interface
struct Http2ParserAdapter {
    processor: http2_process::Http2Processor,
}

impl Http2ParserAdapter {
    fn new() -> Self {
        Self { processor: http2_process::Http2Processor::new() }
    }
}

impl HttpParser for Http2ParserAdapter {
    fn supported_version(&self) -> crate::http::Version {
        crate::http::Version::V20
    }

    fn can_parse(&self, data: &[u8]) -> bool {
        self.processor.can_process_request(data) || self.processor.can_process_response(data)
    }

    fn name(&self) -> &'static str {
        "HTTP/2"
    }

    fn parse_request(&self, data: &[u8]) -> Option<ObservableHttpRequest> {
        self.processor.process_request(data).ok().flatten()
    }

    fn parse_response(&self, data: &[u8]) -> Option<ObservableHttpResponse> {
        self.processor.process_response(data).ok().flatten()
    }
}

impl Default for HttpProcessors {
    fn default() -> Self {
        Self::new()
    }
}

pub struct ObservableHttpPackage {
    #[cfg(feature = "p0f-request")]
    pub http_request: Option<ObservableHttpRequest>,
    #[cfg(feature = "p0f-response")]
    pub http_response: Option<ObservableHttpResponse>,
}

impl ObservableHttpPackage {
    #[must_use]
    pub const fn empty() -> Self {
        Self {
            #[cfg(feature = "p0f-request")]
            http_request: None,
            #[cfg(feature = "p0f-response")]
            http_response: None,
        }
    }
}

#[cfg(any(feature = "p0f-request", feature = "p0f-response"))]
#[derive(Clone)]
struct TcpData {
    sequence: u32,
    data: Vec<u8>,
}

#[cfg_attr(
    not(any(feature = "p0f-request", feature = "p0f-response")),
    allow(dead_code)
)]
pub struct TcpFlow {
    client_ip: IpAddr,
    server_ip: IpAddr,
    client_port: u16,
    server_port: u16,
    #[cfg(feature = "p0f-request")]
    client_data: Vec<TcpData>,
    #[cfg(feature = "p0f-response")]
    server_data: Vec<TcpData>,
    #[cfg(feature = "p0f-request")]
    client_http_parsed: bool,
    #[cfg(feature = "p0f-response")]
    server_http_parsed: bool,
}

#[cfg(any(feature = "p0f-request", feature = "p0f-response"))]
impl TcpFlow {
    #[cfg_attr(not(feature = "p0f-request"), allow(unused_variables))]
    fn init(
        src_ip: IpAddr,
        src_port: u16,
        dst_ip: IpAddr,
        dst_port: u16,
        tcp_data: TcpData,
    ) -> TcpFlow {
        TcpFlow {
            client_ip: src_ip,
            server_ip: dst_ip,
            client_port: src_port,
            server_port: dst_port,
            #[cfg(feature = "p0f-request")]
            client_data: vec![tcp_data],
            #[cfg(feature = "p0f-response")]
            server_data: Vec::new(),
            #[cfg(feature = "p0f-request")]
            client_http_parsed: false,
            #[cfg(feature = "p0f-response")]
            server_http_parsed: false,
        }
    }

    fn is_fully_parsed(&self) -> bool {
        #[cfg(all(feature = "p0f-request", feature = "p0f-response"))]
        {
            self.client_http_parsed && self.server_http_parsed
        }
        #[cfg(all(feature = "p0f-request", not(feature = "p0f-response")))]
        {
            self.client_http_parsed
        }
        #[cfg(all(not(feature = "p0f-request"), feature = "p0f-response"))]
        {
            self.server_http_parsed
        }
    }
}

#[cfg(any(feature = "p0f-request", feature = "p0f-response"))]
fn ordered_payload(data: &[TcpData]) -> Vec<u8> {
    let mut sorted_data = data.to_vec();
    sorted_data.sort_by_key(|tcp_data| tcp_data.sequence);
    let mut full_data = Vec::new();
    for tcp_data in sorted_data {
        full_data.extend_from_slice(&tcp_data.data);
    }
    full_data
}

#[inline]
pub fn process_http_ipv4(
    packet: &Ipv4Packet,
    http_flows: &mut TtlCache<FlowKey, TcpFlow>,
    processors: &HttpProcessors,
) -> Result<ObservableHttpPackage, HuginnNetHttpError> {
    if packet.get_next_level_protocol() != IpNextHeaderProtocols::Tcp {
        return Err(HuginnNetHttpError::UnsupportedProtocol("IPv4".to_string()));
    }
    if let Some(tcp) = TcpPacket::new(packet.payload()) {
        process_tcp_packet(
            http_flows,
            tcp,
            IpAddr::V4(packet.get_source()),
            IpAddr::V4(packet.get_destination()),
            processors,
        )
    } else {
        Ok(ObservableHttpPackage::empty())
    }
}

#[inline]
pub fn process_http_ipv6(
    packet: &Ipv6Packet,
    http_flows: &mut TtlCache<FlowKey, TcpFlow>,
    processors: &HttpProcessors,
) -> Result<ObservableHttpPackage, HuginnNetHttpError> {
    if packet.get_next_header() != IpNextHeaderProtocols::Tcp {
        return Err(HuginnNetHttpError::UnsupportedProtocol("IPv6".to_string()));
    }
    if let Some(tcp) = TcpPacket::new(packet.payload()) {
        process_tcp_packet(
            http_flows,
            tcp,
            IpAddr::V6(packet.get_source()),
            IpAddr::V6(packet.get_destination()),
            processors,
        )
    } else {
        Ok(ObservableHttpPackage::empty())
    }
}

#[cfg_attr(
    not(any(feature = "p0f-request", feature = "p0f-response")),
    allow(unused_variables)
)]
fn process_tcp_packet(
    http_flows: &mut TtlCache<FlowKey, TcpFlow>,
    tcp: TcpPacket,
    src_ip: IpAddr,
    dst_ip: IpAddr,
    processors: &HttpProcessors,
) -> Result<ObservableHttpPackage, HuginnNetHttpError> {
    #[cfg(not(any(feature = "p0f-request", feature = "p0f-response")))]
    {
        Ok(ObservableHttpPackage::empty())
    }

    #[cfg(any(feature = "p0f-request", feature = "p0f-response"))]
    {
        let src_port: u16 = tcp.get_source();
        let dst_port: u16 = tcp.get_destination();
        let mut observable_http_package = ObservableHttpPackage::empty();

        let flow_key: FlowKey = (src_ip, dst_ip, src_port, dst_port);
        let (tcp_flow, is_client) = {
            if let Some(flow) = http_flows.get_mut(&flow_key) {
                (Some(flow), true)
            } else {
                let reversed_key: FlowKey = (dst_ip, src_ip, dst_port, src_port);
                if let Some(flow) = http_flows.get_mut(&reversed_key) {
                    (Some(flow), false)
                } else {
                    (None, false)
                }
            }
        };

        if let Some(flow) = tcp_flow {
            if !tcp.payload().is_empty() {
                let tcp_data =
                    TcpData { sequence: tcp.get_sequence(), data: Vec::from(tcp.payload()) };

                if is_client && src_ip == flow.client_ip && src_port == flow.client_port {
                    #[cfg(feature = "p0f-request")]
                    {
                        if !flow.client_http_parsed {
                            flow.client_data.push(tcp_data);
                            let full_data = ordered_payload(&flow.client_data);
                            match parse_http_request(&full_data, processors) {
                                Ok(Some(http_request_parsed)) => {
                                    observable_http_package.http_request =
                                        Some(http_request_parsed);
                                    flow.client_http_parsed = true;
                                }
                                Ok(None) => {}
                                Err(_e) => {}
                            }
                        } else {
                            debug!("CLIENT: HTTP already parsed, discarding additional data");
                        }
                    }
                    #[cfg(not(feature = "p0f-request"))]
                    let _ = (&tcp_data, processors);
                } else if src_ip == flow.server_ip && src_port == flow.server_port {
                    #[cfg(feature = "p0f-response")]
                    {
                        // Only add data and parse if not already parsed.
                        if !flow.server_http_parsed {
                            flow.server_data.push(tcp_data);
                            let full_data = ordered_payload(&flow.server_data);
                            match parse_http_response(&full_data, processors) {
                                Ok(Some(http_response_parsed)) => {
                                    observable_http_package.http_response =
                                        Some(http_response_parsed);
                                    flow.server_http_parsed = true;
                                }
                                Ok(None) => {
                                    debug!("SERVER: Data not complete yet, waiting for more");
                                }
                                Err(_e) => {}
                            }
                        } else {
                            debug!("SERVER: HTTP already parsed, discarding additional data");
                        }
                    }
                    #[cfg(not(feature = "p0f-response"))]
                    let _ = (&tcp_data, processors);
                }

                if flow.is_fully_parsed() {
                    debug!("All enabled HTTP sides parsed, removing flow from http_flows early");
                    http_flows.remove(&flow_key);
                    return Ok(observable_http_package);
                }

                // Clean up on connection close
                if tcp.get_flags()
                    & (pnet::packet::tcp::TcpFlags::FIN | pnet::packet::tcp::TcpFlags::RST)
                    != 0
                {
                    debug!("Connection closed or reset");
                    http_flows.remove(&flow_key);
                }
            }
        } else if tcp.get_flags() & pnet::packet::tcp::TcpFlags::SYN != 0 {
            let tcp_data: TcpData =
                TcpData { sequence: tcp.get_sequence(), data: Vec::from(tcp.payload()) };
            let flow: TcpFlow = TcpFlow::init(src_ip, src_port, dst_ip, dst_port, tcp_data);
            http_flows.insert(flow_key, flow, Duration::new(60, 0));
        }

        Ok(observable_http_package)
    }
}

#[cfg(feature = "p0f-request")]
fn parse_http_request(
    data: &[u8],
    processors: &HttpProcessors,
) -> Result<Option<ObservableHttpRequest>, HuginnNetHttpError> {
    match processors.parse_request(data) {
        Some(request) => {
            debug!("Successfully parsed HTTP request using polymorphic parser");
            Ok(Some(request))
        }
        None => {
            debug!("No HTTP parser could handle request data");
            Ok(None)
        }
    }
}

#[cfg(feature = "p0f-response")]
fn parse_http_response(
    data: &[u8],
    processors: &HttpProcessors,
) -> Result<Option<ObservableHttpResponse>, HuginnNetHttpError> {
    match processors.parse_response(data) {
        Some(response) => {
            debug!("Successfully parsed HTTP response using polymorphic parser");
            Ok(Some(response))
        }
        None => {
            debug!("No HTTP parser could handle response data");
            Ok(None)
        }
    }
}