]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
sip: add sip.user_agent sticky buffer
authorGiuseppe Longo <giuseppe@glongo.it>
Sat, 10 Aug 2024 12:17:06 +0000 (14:17 +0200)
committerVictor Julien <victor@inliniac.net>
Sun, 22 Sep 2024 04:45:36 +0000 (06:45 +0200)
This adds a sticky (multi) buffer to match the "User-Agent" header field in
both requests and responses.

Ticket #6374

rust/src/sip/detect.rs

index 28669861fe48cae680442fab60ba734ae3c38e2d..92de8eca9d741e2caa3d80a2d5a24d3d0621f5a2 100644 (file)
@@ -36,6 +36,7 @@ static mut G_SIP_RESPONSE_LINE_BUFFER_ID: c_int = 0;
 static mut G_SIP_FROM_HDR_BUFFER_ID: c_int = 0;
 static mut G_SIP_TO_HDR_BUFFER_ID: c_int = 0;
 static mut G_SIP_VIA_HDR_BUFFER_ID: c_int = 0;
+static mut G_SIP_UA_HDR_BUFFER_ID: c_int = 0;
 
 #[no_mangle]
 pub unsafe extern "C" fn rs_sip_tx_get_method(
@@ -456,6 +457,47 @@ unsafe extern "C" fn sip_via_hdr_get_data(
     )
 }
 
+unsafe extern "C" fn sip_ua_hdr_setup(
+    de: *mut c_void, s: *mut c_void, _raw: *const std::os::raw::c_char,
+) -> c_int {
+    if DetectSignatureSetAppProto(s, ALPROTO_SIP) != 0 {
+        return -1;
+    }
+    if DetectBufferSetActiveList(de, s, G_SIP_UA_HDR_BUFFER_ID) < 0 {
+        return -1;
+    }
+    return 0;
+}
+
+unsafe extern "C" fn sip_ua_hdr_get(
+    de: *mut c_void, transforms: *const c_void, flow: *const c_void, flow_flags: u8,
+    tx: *const c_void, list_id: c_int, local_id: u32,
+) -> *mut c_void {
+    return DetectHelperGetMultiData(
+        de,
+        transforms,
+        flow,
+        flow_flags,
+        tx,
+        list_id,
+        local_id,
+        sip_ua_hdr_get_data,
+    );
+}
+
+unsafe extern "C" fn sip_ua_hdr_get_data(
+    tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32,
+) -> bool {
+    sip_get_header_value(
+        tx,
+        local_id,
+        flow_flags,
+        buffer,
+        buffer_len,
+        "User-Agent\0".as_ptr() as *const c_char,
+    )
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn ScDetectSipRegister() {
     let kw = SCSigTableElmt {
@@ -602,4 +644,23 @@ pub unsafe extern "C" fn ScDetectSipRegister() {
         true,
         sip_via_hdr_get,
     );
+    let kw = SCSigTableElmt {
+        name: b"sip.user_agent\0".as_ptr() as *const libc::c_char,
+        desc: b"sticky buffer to match on the SIP User-Agent header\0".as_ptr()
+            as *const libc::c_char,
+        url: b"/rules/sip-keywords.html#sip-user-agent\0".as_ptr() as *const libc::c_char,
+        Setup: sip_ua_hdr_setup,
+        flags: SIGMATCH_NOOPT,
+        AppLayerTxMatch: None,
+        Free: None,
+    };
+    let _g_sip_ua_hdr_kw_id = DetectHelperKeywordRegister(&kw);
+    G_SIP_UA_HDR_BUFFER_ID = DetectHelperMultiBufferMpmRegister(
+        b"sip.ua\0".as_ptr() as *const libc::c_char,
+        b"sip.ua\0".as_ptr() as *const libc::c_char,
+        ALPROTO_SIP,
+        true,
+        true,
+        sip_ua_hdr_get,
+    );
 }