]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: header_lowercase transform
authorPhilippe Antoine <pantoine@oisf.net>
Thu, 9 Nov 2023 08:57:58 +0000 (09:57 +0100)
committerVictor Julien <victor@inliniac.net>
Mon, 20 Nov 2023 11:44:07 +0000 (12:44 +0100)
Ticket: 6290

doc/userguide/rules/transforms.rst
src/Makefile.am
src/detect-engine-register.c
src/detect-engine-register.h
src/detect-transform-header-lowercase.c [new file with mode: 0644]
src/detect-transform-header-lowercase.h [new file with mode: 0644]

index f52bac7f3eea0157fae8dc04e04f033ec0bd13b9..0067ace1de8b6b5582923f5a15952aa9de0fd9bd 100644 (file)
@@ -159,3 +159,18 @@ Example::
     alert http any any -> any any (msg:"HTTP with xor"; http.uri; \
         xor:"0d0ac8ff"; content:"password="; sid:1;)
 
+header_lowercase
+----------------
+
+This transform is meant for HTTP/1 HTTP/2 header names normalization.
+It lowercases the header names, while keeping untouched the header values.
+
+The implementation uses a state machine :
+- it lowercases until it finds ``:```
+- it does not change until it finds a new line and switch back to first state
+
+This example alerts for both HTTP/1 and HTTP/2 with a authorization header
+Example::
+
+    alert http any any -> any any (msg:"HTTP authorization"; http.header_names; \
+        header_lowercase; content:"authorization:"; sid:1;)
index f8033de41b888f231659ca868687714362a2a78c..21e1dfe5fbeb5e11e630975d467e58f38670b0ee 100755 (executable)
@@ -342,6 +342,7 @@ noinst_HEADERS = \
        detect-transform-casechange.h \
        detect-transform-compress-whitespace.h \
        detect-transform-dotprefix.h \
+       detect-transform-header-lowercase.h \
        detect-transform-md5.h \
        detect-transform-pcrexform.h \
        detect-transform-sha1.h \
@@ -955,6 +956,7 @@ libsuricata_c_a_SOURCES = \
        detect-transform-casechange.c \
        detect-transform-compress-whitespace.c \
        detect-transform-dotprefix.c \
+       detect-transform-header-lowercase.c \
        detect-transform-md5.c \
        detect-transform-pcrexform.c \
        detect-transform-sha1.c \
index bd8f66519683758ebc4fb2e53d01382b41c3fad9..0f459eccb67b3a72a58ce52c43d6490aa5f3e231 100644 (file)
 #include "detect-transform-urldecode.h"
 #include "detect-transform-xor.h"
 #include "detect-transform-casechange.h"
+#include "detect-transform-header-lowercase.h"
 
 #include "util-rule-vars.h"
 
@@ -706,6 +707,7 @@ void SigTableSetup(void)
     DetectTransformXorRegister();
     DetectTransformToLowerRegister();
     DetectTransformToUpperRegister();
+    DetectTransformHeaderLowercaseRegister();
 
     DetectFileHandlerRegister();
 
index abc1a403dd09624b4693c7a65bd5dac7f910c22b..273aa10d7c9b3807930ddd65263202a22fe98c0c 100644 (file)
@@ -328,6 +328,7 @@ enum DetectKeywordId {
     DETECT_TRANSFORM_XOR,
     DETECT_TRANSFORM_TOLOWER,
     DETECT_TRANSFORM_TOUPPER,
+    DETECT_TRANSFORM_HEADER_LOWERCASE,
 
     DETECT_AL_IKE_EXCH_TYPE,
     DETECT_AL_IKE_SPI_INITIATOR,
diff --git a/src/detect-transform-header-lowercase.c b/src/detect-transform-header-lowercase.c
new file mode 100644 (file)
index 0000000..7c77620
--- /dev/null
@@ -0,0 +1,88 @@
+/* Copyright (C) 2023 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Philippe Antoine <contact@catenacyber.fr>
+ *
+ * Implements the header_lowercase transform keyword with option support
+ */
+
+#include "suricata-common.h"
+#include "detect.h"
+#include "detect-engine.h"
+#include "detect-parse.h"
+#include "detect-transform-header-lowercase.h"
+
+/**
+ *  \internal
+ *  \brief Apply the header_lowercase keyword to the last pattern match
+ *  \param det_ctx detection engine ctx
+ *  \param s signature
+ *  \param optstr options string
+ *  \retval 0 ok
+ *  \retval -1 failure
+ */
+static int DetectTransformHeaderLowercaseSetup(
+        DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
+{
+    SCEnter();
+    int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_HEADER_LOWERCASE, NULL);
+    SCReturnInt(r);
+}
+
+static void DetectTransformHeaderLowercase(InspectionBuffer *buffer, void *options)
+{
+    const uint8_t *input = buffer->inspect;
+    const uint32_t input_len = buffer->inspect_len;
+    if (input_len == 0) {
+        return;
+    }
+    uint8_t output[input_len];
+
+    // state 0 is header name, 1 is header value
+    int state = 0;
+    for (uint32_t i = 0; i < input_len; i++) {
+        if (state == 0) {
+            if (input[i] == ':') {
+                output[i] = input[i];
+                state = 1;
+            } else {
+                output[i] = u8_tolower(input[i]);
+            }
+        } else {
+            output[i] = input[i];
+            if (input[i] == '\n') {
+                state = 0;
+            }
+        }
+    }
+    InspectionBufferCopy(buffer, output, input_len);
+}
+
+void DetectTransformHeaderLowercaseRegister(void)
+{
+    sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].name = "header_lowercase";
+    sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].desc =
+            "modify buffer via lowercaseing header names";
+    sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].url =
+            "/rules/transforms.html#header_lowercase";
+    sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].Transform = DetectTransformHeaderLowercase;
+    sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].Setup = DetectTransformHeaderLowercaseSetup;
+    sigmatch_table[DETECT_TRANSFORM_HEADER_LOWERCASE].flags |= SIGMATCH_NOOPT;
+}
diff --git a/src/detect-transform-header-lowercase.h b/src/detect-transform-header-lowercase.h
new file mode 100644 (file)
index 0000000..aca7f87
--- /dev/null
@@ -0,0 +1,30 @@
+/* Copyright (C) 2023 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Philippe Antoine <contact@catenacyber.fr>
+ */
+
+#ifndef __DETECT_TRANSFORM_HEADER_LOWERCASE_H__
+#define __DETECT_TRANSFORM_HEADER_LOWERCASE_H__
+
+/* prototypes */
+void DetectTransformHeaderLowercaseRegister(void);
+
+#endif /* __DETECT_TRANSFORM_HEADER_LOWERCASE_H__ */