From d7c7d7eda35a74bda70d92d052d9648d7e36dc23 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Tue, 22 Mar 2022 09:46:45 -0600 Subject: [PATCH] detect-content: error on single char hex pairs Fix parsing of content like "|aa b cc|" which was parsed as "|aa bc|" without error or warning. This will now fail out, requiring all hex values to be 2 chars. Ticket #5201 --- src/detect-content.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/detect-content.c b/src/detect-content.c index 24c9475c4e98..f23c3c55586c 100644 --- a/src/detect-content.c +++ b/src/detect-content.c @@ -111,6 +111,12 @@ int DetectContentDataParse(const char *keyword, const char *contentstr, if (str[i] == '|') { bin_count++; if (bin) { + if (binpos > 0) { + SCLogError(SC_ERR_INVALID_SIGNATURE, + "Incomplete hex code in content - %s. Invalidating signature.", + contentstr); + goto error; + } bin = 0; } else { bin = 1; @@ -3050,6 +3056,25 @@ static int DetectLongContentTest3(void) return !DetectLongContentTestCommon(sig, 1); } +static int DetectBadBinContent(void) +{ + DetectEngineCtx *de_ctx = NULL; + de_ctx = DetectEngineCtxInit(); + FAIL_IF_NULL(de_ctx); + de_ctx->flags |= DE_QUIET; + FAIL_IF_NOT_NULL(DetectEngineAppendSig( + de_ctx, "alert tcp any any -> any any (msg:\"test\"; content:\"|a|\"; sid:1;)")); + FAIL_IF_NOT_NULL(DetectEngineAppendSig( + de_ctx, "alert tcp any any -> any any (msg:\"test\"; content:\"|aa b|\"; sid:1;)")); + FAIL_IF_NOT_NULL(DetectEngineAppendSig( + de_ctx, "alert tcp any any -> any any (msg:\"test\"; content:\"|aa bz|\"; sid:1;)")); + /* https://redmine.openinfosecfoundation.org/issues/5201 */ + FAIL_IF_NOT_NULL(DetectEngineAppendSig( + de_ctx, "alert tcp any any -> any any (msg:\"test\"; content:\"|22 2 22|\"; sid:1;)")); + DetectEngineCtxFree(de_ctx); + PASS; +} + /** * \brief this function registers unit tests for DetectContent */ @@ -3168,5 +3193,7 @@ static void DetectContentRegisterTests(void) UtRegisterTest("DetectLongContentTest1", DetectLongContentTest1); UtRegisterTest("DetectLongContentTest2", DetectLongContentTest2); UtRegisterTest("DetectLongContentTest3", DetectLongContentTest3); + + UtRegisterTest("DetectBadBinContent", DetectBadBinContent); } #endif /* UNITTESTS */