From: Giuseppe Longo Date: Mon, 30 Sep 2024 13:05:35 +0000 (+0200) Subject: sdp: add sdp.phone_number sticky buffer X-Git-Tag: suricata-8.0.0-beta1~103 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6fef5db5f7b7ab2905b5a717ec165709f7715ce4;p=thirdparty%2Fsuricata.git sdp: add sdp.phone_number sticky buffer This adds a sticky buffer to match the "Phone number" field in both requests and responses. Ticket #7291 --- diff --git a/rust/src/sdp/detect.rs b/rust/src/sdp/detect.rs index 150c88c303..af3554a780 100644 --- a/rust/src/sdp/detect.rs +++ b/rust/src/sdp/detect.rs @@ -31,6 +31,7 @@ static mut G_SDP_SESSION_NAME_BUFFER_ID: c_int = 0; static mut G_SDP_SESSION_INFO_BUFFER_ID: c_int = 0; static mut G_SDP_URI_BUFFER_ID: c_int = 0; static mut G_SDP_EMAIL_BUFFER_ID: c_int = 0; +static mut G_SDP_PHONE_NUMBER_BUFFER_ID: c_int = 0; unsafe extern "C" fn sdp_session_name_setup( de: *mut c_void, s: *mut c_void, _raw: *const std::os::raw::c_char, @@ -269,6 +270,53 @@ unsafe extern "C" fn sdp_email_get_data( false } +unsafe extern "C" fn sdp_phone_number_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_SDP_PHONE_NUMBER_BUFFER_ID) < 0 { + return -1; + } + return 0; +} + +unsafe extern "C" fn sdp_phone_number_get( + de: *mut c_void, transforms: *const c_void, flow: *const c_void, flow_flags: u8, + tx: *const c_void, list_id: c_int, +) -> *mut c_void { + return DetectHelperGetData( + de, + transforms, + flow, + flow_flags, + tx, + list_id, + sdp_phone_number_get_data, + ); +} + +unsafe extern "C" fn sdp_phone_number_get_data( + tx: *const c_void, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32, +) -> bool { + let tx = cast_pointer!(tx, SIPTransaction); + let sdp_option = match direction.into() { + Direction::ToServer => tx.request.as_ref().and_then(|req| req.body.as_ref()), + Direction::ToClient => tx.response.as_ref().and_then(|resp| resp.body.as_ref()), + }; + if let Some(sdp) = sdp_option { + if let Some(ref p) = sdp.phone_number { + *buffer = p.as_ptr(); + *buffer_len = p.len() as u32; + return true; + } + } + *buffer = ptr::null(); + *buffer_len = 0; + false +} + #[no_mangle] pub unsafe extern "C" fn ScDetectSdpRegister() { let kw = SCSigTableElmt { @@ -363,4 +411,23 @@ pub unsafe extern "C" fn ScDetectSdpRegister() { true, sdp_email_get, ); + let kw = SCSigTableElmt { + name: b"sdp.phone_number\0".as_ptr() as *const libc::c_char, + desc: b"sticky buffer to match on the SDP phone number field\0".as_ptr() + as *const libc::c_char, + url: b"/rules/sdp-keywords.html#sdp-phone-number\0".as_ptr() as *const libc::c_char, + Setup: sdp_phone_number_setup, + flags: SIGMATCH_NOOPT, + AppLayerTxMatch: None, + Free: None, + }; + let _ = DetectHelperKeywordRegister(&kw); + G_SDP_PHONE_NUMBER_BUFFER_ID = DetectHelperBufferMpmRegister( + b"sdp.phone_number\0".as_ptr() as *const libc::c_char, + b"sdp.phone_number\0".as_ptr() as *const libc::c_char, + ALPROTO_SIP, + true, + true, + sdp_phone_number_get, + ); }