]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: http_response_line for HTTP2
authorPhilippe Antoine <pantoine@oisf.net>
Mon, 3 Apr 2023 08:36:25 +0000 (10:36 +0200)
committerVictor Julien <vjulien@oisf.net>
Fri, 12 May 2023 17:52:15 +0000 (19:52 +0200)
Ticket: #4067

Synthetized as HTTP/2 <STAT>\r\n

rust/src/http2/detect.rs
rust/src/http2/http2.rs
src/detect-http-response-line.c

index 1634880904fa77d5b6b4cc926d87e8fe4f46f91b..642b0c8220907d6ac195f21af5f66e4b328fae1e 100644 (file)
@@ -532,6 +532,35 @@ pub unsafe extern "C" fn rs_http2_tx_get_request_line(
     return 1;
 }
 
+fn http2_tx_get_resp_line(tx: &mut HTTP2Transaction) {
+    if !tx.resp_line.is_empty() {
+        return;
+    }
+    let empty = Vec::new();
+    let mut resp_line : Vec<u8> = Vec::new();
+
+    let status =
+        if let Ok(value) = http2_frames_get_header_firstvalue(tx, Direction::ToClient, ":status") {
+            value
+        } else {
+            &empty
+        };
+    resp_line.extend(b" HTTP/2 ");
+    resp_line.extend(status);
+    resp_line.extend(b"\r\n");
+    tx.resp_line.extend(resp_line)
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn rs_http2_tx_get_response_line(
+    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32,
+) -> u8 {
+    http2_tx_get_resp_line(tx);
+    *buffer = tx.resp_line.as_ptr(); //unsafe
+    *buffer_len = tx.resp_line.len() as u32;
+    return 1;
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn rs_http2_tx_get_uri(
     tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32,
index cf1433f55d20f58c0b673d22f8546f0680219240..011000b5432b92b3a4f0912fe66d9a54650bc8d5 100644 (file)
@@ -143,6 +143,7 @@ pub struct HTTP2Transaction {
     //must be attached to transaction for memory management (be freed at the right time)
     pub escaped: Vec<Vec<u8>>,
     pub req_line: Vec<u8>,
+    pub resp_line: Vec<u8>,
 }
 
 impl Transaction for HTTP2Transaction {
@@ -173,6 +174,7 @@ impl HTTP2Transaction {
             ft_ts: FileTransferTracker::new(),
             escaped: Vec::with_capacity(16),
             req_line: Vec::new(),
+            resp_line: Vec::new(),
         }
     }
 
index 2ce97eefe2db360ff2fa7e7aaab68ba2cf14fbf1..8758644681c7831e409a5ddb3fdaa40ff8be184c 100644 (file)
@@ -70,6 +70,29 @@ static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
         void *txv, const int list_id);
 static int g_http_response_line_id = 0;
 
+static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
+        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
+        const int list_id)
+{
+    SCEnter();
+
+    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
+    if (buffer->inspect == NULL) {
+        uint32_t b_len = 0;
+        const uint8_t *b = NULL;
+
+        if (rs_http2_tx_get_response_line(txv, &b, &b_len) != 1)
+            return NULL;
+        if (b == NULL || b_len == 0)
+            return NULL;
+
+        InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
+        InspectionBufferApplyTransforms(buffer, transforms);
+    }
+
+    return buffer;
+}
+
 /**
  * \brief Registers the keyword handlers for the "http_response_line" keyword.
  */
@@ -91,6 +114,11 @@ void DetectHttpResponseLineRegister(void)
     DetectAppLayerMpmRegister2("http_response_line", SIG_FLAG_TOCLIENT, 2,
             PrefilterGenericMpmRegister, GetData, ALPROTO_HTTP1, HTP_RESPONSE_LINE);
 
+    DetectAppLayerInspectEngineRegister2("http_response_line", ALPROTO_HTTP2, SIG_FLAG_TOCLIENT,
+            HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetData2);
+    DetectAppLayerMpmRegister2("http_response_line", SIG_FLAG_TOCLIENT, 2,
+            PrefilterGenericMpmRegister, GetData2, ALPROTO_HTTP2, HTTP2StateDataServer);
+
     DetectBufferTypeSetDescriptionByName("http_response_line",
             "http response line");
 
@@ -115,7 +143,7 @@ static int DetectHttpResponseLineSetup(DetectEngineCtx *de_ctx, Signature *s, co
     if (DetectBufferSetActiveList(de_ctx, s, g_http_response_line_id) < 0)
         return -1;
 
-    if (DetectSignatureSetAppProto(s, ALPROTO_HTTP1) < 0)
+    if (DetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0)
         return -1;
 
     return 0;