]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: strip_pseudo_headers transform
authorPhilippe Antoine <pantoine@oisf.net>
Tue, 12 Dec 2023 08:34:04 +0000 (09:34 +0100)
committerVictor Julien <victor@inliniac.net>
Mon, 15 Jan 2024 16:49:10 +0000 (17:49 +0100)
Ticket: 6546

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

index 0067ace1de8b6b5582923f5a15952aa9de0fd9bd..f730f0d2dc717bda627280a75f7a0458bb07b947 100644 (file)
@@ -174,3 +174,17 @@ Example::
 
     alert http any any -> any any (msg:"HTTP authorization"; http.header_names; \
         header_lowercase; content:"authorization:"; sid:1;)
+
+strip_pseudo_headers
+--------------------
+
+This transform is meant for HTTP/1 HTTP/2 header names normalization.
+It strips HTTP2 pseudo-headers (names and values).
+
+The implementation just strips every line beginning by ``:``.
+
+This example alerts for both HTTP/1 and HTTP/2 with only a user agent
+Example::
+
+    alert http any any -> any any (msg:"HTTP ua only"; http.header_names; \
+       bsize:16; content:"|0d 0a|User-Agent|0d 0a 0d 0a|"; nocase; sid:1;)
index 133ed47cd1e884504a61480cbc0e3d032946d63b..2af8b1d44cd8c782ac8859c49e9ce607162fd6b6 100755 (executable)
@@ -349,6 +349,7 @@ noinst_HEADERS = \
        detect-transform-pcrexform.h \
        detect-transform-sha1.h \
        detect-transform-sha256.h \
+       detect-transform-strip-pseudo-headers.h \
        detect-transform-strip-whitespace.h \
        detect-transform-urldecode.h \
        detect-transform-xor.h \
@@ -964,6 +965,7 @@ libsuricata_c_a_SOURCES = \
        detect-transform-pcrexform.c \
        detect-transform-sha1.c \
        detect-transform-sha256.c \
+       detect-transform-strip-pseudo-headers.c \
        detect-transform-strip-whitespace.c \
        detect-transform-urldecode.c \
        detect-transform-xor.c \
index 9f37e09455446bdaf5ca27a227d09dae643f1ec0..218b0d7f0cb19d8a76f5db6d9993cec3220ddd91 100644 (file)
 
 #include "detect-transform-compress-whitespace.h"
 #include "detect-transform-strip-whitespace.h"
+#include "detect-transform-strip-pseudo-headers.h"
 #include "detect-transform-md5.h"
 #include "detect-transform-sha1.h"
 #include "detect-transform-sha256.h"
@@ -706,6 +707,7 @@ void SigTableSetup(void)
 
     DetectTransformCompressWhitespaceRegister();
     DetectTransformStripWhitespaceRegister();
+    DetectTransformStripPseudoHeadersRegister();
     DetectTransformMd5Register();
     DetectTransformSha1Register();
     DetectTransformSha256Register();
index 9dd01f5fd487118a1fbd6442cd41c69a5b3f6e6c..8d4c7dfad3c1d72fea6812eae4d5ed69389af9bf 100644 (file)
@@ -323,6 +323,7 @@ enum DetectKeywordId {
 
     DETECT_TRANSFORM_COMPRESS_WHITESPACE,
     DETECT_TRANSFORM_STRIP_WHITESPACE,
+    DETECT_TRANSFORM_STRIP_PSEUDO_HEADERS,
     DETECT_TRANSFORM_MD5,
     DETECT_TRANSFORM_SHA1,
     DETECT_TRANSFORM_SHA256,
diff --git a/src/detect-transform-strip-pseudo-headers.c b/src/detect-transform-strip-pseudo-headers.c
new file mode 100644 (file)
index 0000000..450900d
--- /dev/null
@@ -0,0 +1,100 @@
+/* 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 strip_pseudo_headers transform keyword with option support
+ */
+
+#include "suricata-common.h"
+#include "detect.h"
+#include "detect-engine.h"
+#include "detect-parse.h"
+#include "detect-transform-strip-pseudo-headers.h"
+
+/**
+ *  \internal
+ *  \brief Apply the strip_pseudo_headers 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 DetectTransformStripPseudoHeadersSetup(
+        DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
+{
+    SCEnter();
+    int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_STRIP_PSEUDO_HEADERS, NULL);
+    SCReturnInt(r);
+}
+
+static void DetectTransformStripPseudoHeaders(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];
+
+    bool new_line = true;
+    bool pseudo = false;
+    uint32_t j = 0;
+    for (uint32_t i = 0; i < input_len; i++) {
+        if (new_line) {
+            if (input[i] == ':') {
+                pseudo = true;
+            }
+            if (input[i] != '\r' && input[i] != '\n') {
+                new_line = false;
+            }
+        } else {
+            if (input[i] == '\n') {
+                new_line = true;
+                if (!pseudo) {
+                    output[j] = input[i];
+                    j++;
+                }
+                pseudo = false;
+                continue;
+            }
+        }
+        if (!pseudo) {
+            output[j] = input[i];
+            j++;
+        }
+    }
+    InspectionBufferCopy(buffer, output, j);
+}
+
+void DetectTransformStripPseudoHeadersRegister(void)
+{
+    sigmatch_table[DETECT_TRANSFORM_STRIP_PSEUDO_HEADERS].name = "strip_pseudo_headers";
+    sigmatch_table[DETECT_TRANSFORM_STRIP_PSEUDO_HEADERS].desc =
+            "modify buffer via stripping pseudo headers";
+    sigmatch_table[DETECT_TRANSFORM_STRIP_PSEUDO_HEADERS].url =
+            "/rules/transforms.html#strip_pseudo_headers";
+    sigmatch_table[DETECT_TRANSFORM_STRIP_PSEUDO_HEADERS].Transform =
+            DetectTransformStripPseudoHeaders;
+    sigmatch_table[DETECT_TRANSFORM_STRIP_PSEUDO_HEADERS].Setup =
+            DetectTransformStripPseudoHeadersSetup;
+    sigmatch_table[DETECT_TRANSFORM_STRIP_PSEUDO_HEADERS].flags |= SIGMATCH_NOOPT;
+}
diff --git a/src/detect-transform-strip-pseudo-headers.h b/src/detect-transform-strip-pseudo-headers.h
new file mode 100644 (file)
index 0000000..c2016d4
--- /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_STRIP_PSEUDOHEADERS_H__
+#define __DETECT_TRANSFORM_STRIP_PSEUDOHEADERS_H__
+
+/* prototypes */
+void DetectTransformStripPseudoHeadersRegister(void);
+
+#endif /* __DETECT_TRANSFORM_STRIP_PSEUDOHEADERS_H__ */