From 017e39d8fd59028d07efa281c8fb9250b33a056f Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Mon, 26 Apr 2021 15:01:53 +0200 Subject: [PATCH] http2: makes all HTTP1 header keywords work --- rust/src/http2/detect.rs | 21 +++++++++++++ src/detect-http-headers-stub.h | 56 +++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/rust/src/http2/detect.rs b/rust/src/http2/detect.rs index f46d946286..efa68d886e 100644 --- a/rust/src/http2/detect.rs +++ b/rust/src/http2/detect.rs @@ -539,6 +539,27 @@ pub unsafe extern "C" fn rs_http2_tx_get_status( return 0; } +#[no_mangle] +pub unsafe extern "C" fn rs_http2_tx_get_header_value( + tx: &mut HTTP2Transaction, direction: u8, strname: *const std::os::raw::c_char, + buffer: *mut *const u8, buffer_len: *mut u32, +) -> u8 { + let hname: &CStr = CStr::from_ptr(strname); //unsafe + if let Ok(s) = hname.to_str() { + let frames = if direction == STREAM_TOSERVER { + &tx.frames_ts + } else { + &tx.frames_tc + }; + if let Ok(value) = http2_frames_get_header_value(frames, &s.to_lowercase()) { + *buffer = value.as_ptr(); //unsafe + *buffer_len = value.len() as u32; + return 1; + } + } + return 0; +} + fn http2_escape_header(hd: &parser::HTTP2FrameHeaders, i: u32) -> Vec { //minimum size + 2 for escapes let normalsize = hd.blocks[i as usize].value.len() + 2 + hd.blocks[i as usize].name.len() + 2; diff --git a/src/detect-http-headers-stub.h b/src/detect-http-headers-stub.h index adebbc5714..9cae4433e7 100644 --- a/src/detect-http-headers-stub.h +++ b/src/detect-http-headers-stub.h @@ -81,6 +81,29 @@ static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx, return buffer; } +static InspectionBuffer *GetRequestData2(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_header_value(txv, STREAM_TOSERVER, HEADER_NAME, &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; +} + #endif #ifdef KEYWORD_TOCLIENT static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx, @@ -113,6 +136,29 @@ static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx, return buffer; } + +static InspectionBuffer *GetResponseData2(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_header_value(txv, STREAM_TOCLIENT, HEADER_NAME, &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; +} #endif /** @@ -129,7 +175,7 @@ static int DetectHttpHeadersSetupSticky(DetectEngineCtx *de_ctx, Signature *s, c if (DetectBufferSetActiveList(s, g_buffer_id) < 0) return -1; - if (DetectSignatureSetAppProto(s, ALPROTO_HTTP1) < 0) + if (DetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0) return -1; return 0; @@ -149,18 +195,26 @@ static void DetectHttpHeadersRegisterStub(void) #ifdef KEYWORD_TOSERVER DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, GetRequestData, ALPROTO_HTTP1, HTP_REQUEST_HEADERS); + DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, + GetRequestData2, ALPROTO_HTTP2, HTTP2StateDataClient); #endif #ifdef KEYWORD_TOCLIENT DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister, GetResponseData, ALPROTO_HTTP1, HTP_RESPONSE_HEADERS); + DetectAppLayerMpmRegister2(BUFFER_NAME, SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister, + GetResponseData2, ALPROTO_HTTP2, HTTP2StateDataServer); #endif #ifdef KEYWORD_TOSERVER DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_HTTP1, SIG_FLAG_TOSERVER, HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetRequestData); + DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_HTTP2, SIG_FLAG_TOSERVER, + HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetRequestData2); #endif #ifdef KEYWORD_TOCLIENT DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_HTTP1, SIG_FLAG_TOCLIENT, HTP_RESPONSE_HEADERS, DetectEngineInspectBufferGeneric, GetResponseData); + DetectAppLayerInspectEngineRegister2(BUFFER_NAME, ALPROTO_HTTP2, SIG_FLAG_TOCLIENT, + HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetResponseData2); #endif DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC); -- 2.47.2