]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/transform: add to_md5 keyword
authorVictor Julien <victor@inliniac.net>
Thu, 22 Nov 2018 11:44:34 +0000 (12:44 +0100)
committerVictor Julien <victor@inliniac.net>
Tue, 29 Jan 2019 12:27:56 +0000 (13:27 +0100)
doc/userguide/rules/transforms.rst
src/Makefile.am
src/detect-engine-register.c
src/detect-engine-register.h
src/detect-transform-md5.c [new file with mode: 0644]
src/detect-transform-md5.h [new file with mode: 0644]

index 3b47bf19f77e70f41a72a788d76b8d89ab516a02..3f7ec328dc290072d75317fb548c7eb146d12a82 100644 (file)
@@ -36,6 +36,19 @@ compress_whitespace
 
 Compresses all consecutive whitespace into a single space.
 
+to_md5
+------
+
+Takes the buffer, calculates the MD5 hash and passes the raw hash value
+on.
+
+Example::
+
+    alert http any any -> any any (http_request_line; to_md5; \
+        content:"|54 A9 7A 8A B0 9C 1B 81 37 25 22 14 51 D3 F9 97|"; sid:1;)
+
+.. note:: depends on libnss being compiled into Suricata
+
 to_sha256
 ---------
 
index 9ffa85b5a65194c3d4245e4babd87e9740e9da7e..008732595e6ea9203f2996f8b8c0db1f36d9ade0 100644 (file)
@@ -267,6 +267,7 @@ detect-tls-version.c detect-tls-version.h \
 detect-tos.c detect-tos.h \
 detect-transform-compress-whitespace.c detect-transform-compress-whitespace.h \
 detect-transform-strip-whitespace.c detect-transform-strip-whitespace.h \
+detect-transform-md5.c detect-transform-md5.h \
 detect-transform-sha256.c detect-transform-sha256.h \
 detect-ttl.c detect-ttl.h \
 detect-uricontent.c detect-uricontent.h \
index 1315da9215496b7ae47dcddd1ffaa83858b4543c..01b87b0aeac2306b8173e73b51b69f6dd1fabf12 100644 (file)
 
 #include "detect-transform-compress-whitespace.h"
 #include "detect-transform-strip-whitespace.h"
+#include "detect-transform-md5.h"
 #include "detect-transform-sha256.h"
 
 #include "util-rule-vars.h"
@@ -525,6 +526,7 @@ void SigTableSetup(void)
 
     DetectTransformCompressWhitespaceRegister();
     DetectTransformStripWhitespaceRegister();
+    DetectTransformMd5Register();
     DetectTransformSha256Register();
 
     /* close keyword registration */
index f2b862c2d547487b28a5b284e4b1e1a53634cadf..1927854a9e280d569aec0eb6967e83a99b825fdf 100644 (file)
@@ -215,6 +215,7 @@ enum {
 
     DETECT_TRANSFORM_COMPRESS_WHITESPACE,
     DETECT_TRANSFORM_STRIP_WHITESPACE,
+    DETECT_TRANSFORM_MD5,
     DETECT_TRANSFORM_SHA256,
 
     /* make sure this stays last */
diff --git a/src/detect-transform-md5.c b/src/detect-transform-md5.c
new file mode 100644 (file)
index 0000000..984cf44
--- /dev/null
@@ -0,0 +1,130 @@
+/* Copyright (C) 2007-2018 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 Victor Julien <victor@inliniac.net>
+ *
+ * Implements the to_md5 transformation keyword
+ */
+
+#include "suricata-common.h"
+
+#include "detect.h"
+#include "detect-engine.h"
+#include "detect-engine-prefilter.h"
+#include "detect-parse.h"
+#include "detect-transform-md5.h"
+
+#include "util-unittest.h"
+#include "util-print.h"
+
+static int DetectTransformToMd5Setup (DetectEngineCtx *, Signature *, const char *);
+#ifdef HAVE_NSS
+static void DetectTransformToMd5RegisterTests(void);
+static void TransformToMd5(InspectionBuffer *buffer);
+#endif
+
+void DetectTransformMd5Register(void)
+{
+    sigmatch_table[DETECT_TRANSFORM_MD5].name = "to_md5";
+    sigmatch_table[DETECT_TRANSFORM_MD5].desc =
+        "convert to md5 hash of the buffer";
+    sigmatch_table[DETECT_TRANSFORM_MD5].url =
+        DOC_URL DOC_VERSION "/rules/transforms.html#to_sha256";
+    sigmatch_table[DETECT_TRANSFORM_MD5].Setup =
+        DetectTransformToMd5Setup;
+#ifdef HAVE_NSS
+    sigmatch_table[DETECT_TRANSFORM_MD5].Transform =
+        TransformToMd5;
+    sigmatch_table[DETECT_TRANSFORM_MD5].RegisterTests =
+        DetectTransformToMd5RegisterTests;
+#endif
+    sigmatch_table[DETECT_TRANSFORM_MD5].flags |= SIGMATCH_NOOPT;
+}
+
+#ifndef HAVE_NSS
+static int DetectTransformToMd5Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
+{
+    SCLogError(SC_ERR_NO_MD5_SUPPORT, "no MD5 calculation support built in, "
+            "needed for to_md5 keyword");
+    return -1;
+}
+#else
+/**
+ *  \internal
+ *  \brief Apply the nocase keyword to the last pattern match, either content or uricontent
+ *  \param det_ctx detection engine ctx
+ *  \param s signature
+ *  \param nullstr should be null
+ *  \retval 0 ok
+ *  \retval -1 failure
+ */
+static int DetectTransformToMd5Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
+{
+    SCEnter();
+    int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_MD5);
+    SCReturnInt(r);
+}
+
+static void TransformToMd5(InspectionBuffer *buffer)
+{
+    const uint8_t *input = buffer->inspect;
+    const uint32_t input_len = buffer->inspect_len;
+    uint8_t output[MD5_LENGTH];
+
+    //PrintRawDataFp(stdout, input, input_len);
+
+    HASHContext *ctx = HASH_Create(HASH_AlgMD5);
+    if (ctx) {
+        HASH_Begin(ctx);
+        HASH_Update(ctx, input, input_len);
+        unsigned int len = 0;
+        HASH_End(ctx, output, &len, sizeof(output));
+        HASH_Destroy(ctx);
+
+        InspectionBufferCopy(buffer, output, sizeof(output));
+    }
+}
+
+#ifdef UNITTESTS
+static int DetectTransformToMd5Test01(void)
+{
+    const uint8_t *input = (const uint8_t *)" A B C D ";
+    uint32_t input_len = strlen((char *)input);
+
+    InspectionBuffer buffer;
+    InspectionBufferInit(&buffer, 8);
+    InspectionBufferSetup(&buffer, input, input_len);
+    PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
+    TransformToMd5(&buffer);
+    PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
+    InspectionBufferFree(&buffer);
+    PASS;
+}
+
+#endif
+
+static void DetectTransformToMd5RegisterTests(void)
+{
+#ifdef UNITTESTS
+    UtRegisterTest("DetectTransformToMd5Test01",
+            DetectTransformToMd5Test01);
+#endif
+}
+#endif
diff --git a/src/detect-transform-md5.h b/src/detect-transform-md5.h
new file mode 100644 (file)
index 0000000..a65bb38
--- /dev/null
@@ -0,0 +1,30 @@
+/* Copyright (C) 2017 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 Victor Julien <victor@inliniac.net>
+ */
+
+#ifndef __DETECT_TRANSFORM_MD5_H__
+#define __DETECT_TRANSFORM_MD5_H__
+
+/* prototypes */
+void DetectTransformMd5Register (void);
+
+#endif /* __DETECT_TRANSFORM_MD5_H__ */