]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/transform: initial to_sha256 implementation
authorVictor Julien <victor@inliniac.net>
Thu, 26 Oct 2017 06:14:14 +0000 (08:14 +0200)
committerVictor Julien <victor@inliniac.net>
Wed, 14 Feb 2018 13:25:46 +0000 (14:25 +0100)
Takes input buffer and replaces it with hash value for that buffer.
Hash value is in raw bytes.

src/Makefile.am
src/detect-engine-register.c
src/detect-engine-register.h
src/detect-transform-sha256.c [new file with mode: 0644]
src/detect-transform-sha256.h [new file with mode: 0644]

index ffbcdb9db65bf567432378add2bea198736eb818..010fe01ddb52b20c4918dff6626db0db4c8660cc 100644 (file)
@@ -252,6 +252,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-sha256.c detect-transform-sha256.h \
 detect-ttl.c detect-ttl.h \
 detect-uricontent.c detect-uricontent.h \
 detect-urilen.c detect-urilen.h \
index 972547022a2ea8774a6a2e628e2d1ea45c9d1827..54f421709a1d0ccd7a0ebc8e15a4f2df6eab9282 100644 (file)
 
 #include "detect-transform-compress-whitespace.h"
 #include "detect-transform-strip-whitespace.h"
+#include "detect-transform-sha256.h"
 
 #include "util-rule-vars.h"
 
@@ -484,6 +485,7 @@ void SigTableSetup(void)
 
     DetectTransformCompressWhitespaceRegister();
     DetectTransformStripWhitespaceRegister();
+    DetectTransformSha256Register();
 
     /* close keyword registration */
     DetectBufferTypeCloseRegistration();
index a16e36cdd5a63c0a4e70a9d78a2ae44136fcd185..17d03102fd38deadbf515c484f4c121b38b1faa7 100644 (file)
@@ -201,6 +201,7 @@ enum {
 
     DETECT_TRANSFORM_COMPRESS_WHITESPACE,
     DETECT_TRANSFORM_STRIP_WHITESPACE,
+    DETECT_TRANSFORM_SHA256,
 
     /* make sure this stays last */
     DETECT_TBLSIZE,
diff --git a/src/detect-transform-sha256.c b/src/detect-transform-sha256.c
new file mode 100644 (file)
index 0000000..430a7e1
--- /dev/null
@@ -0,0 +1,130 @@
+/* Copyright (C) 2007-2010 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 nocase keyword
+ */
+
+#include "suricata-common.h"
+
+#include "detect.h"
+#include "detect-engine.h"
+#include "detect-engine-prefilter.h"
+#include "detect-parse.h"
+#include "detect-transform-sha256.h"
+
+#include "util-unittest.h"
+#include "util-print.h"
+
+static int DetectTransformToSha256Setup (DetectEngineCtx *, Signature *, const char *);
+#ifdef HAVE_NSS
+static void DetectTransformToSha256RegisterTests(void);
+static void TransformToSha256(InspectionBuffer *buffer);
+#endif
+
+void DetectTransformSha256Register(void)
+{
+    sigmatch_table[DETECT_TRANSFORM_SHA256].name = "to_sha256";
+    sigmatch_table[DETECT_TRANSFORM_SHA256].desc =
+        "convert to sha256 hash of the buffer";
+    sigmatch_table[DETECT_TRANSFORM_SHA256].url =
+        DOC_URL DOC_VERSION "/rules/transformations.html#to_sha256";
+    sigmatch_table[DETECT_TRANSFORM_SHA256].Setup =
+        DetectTransformToSha256Setup;
+#ifdef HAVE_NSS
+    sigmatch_table[DETECT_TRANSFORM_SHA256].Transform =
+        TransformToSha256;
+    sigmatch_table[DETECT_TRANSFORM_SHA256].RegisterTests =
+        DetectTransformToSha256RegisterTests;
+#endif
+    sigmatch_table[DETECT_TRANSFORM_SHA256].flags |= SIGMATCH_NOOPT;
+}
+
+#ifndef HAVE_NSS
+static int DetectTransformToSha256Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
+{
+    SCLogError(SC_ERR_NO_SHA256_SUPPORT, "no SHA-256 calculation support built in, "
+            "needed for to_sha256 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 DetectTransformToSha256Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
+{
+    SCEnter();
+    int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_SHA256);
+    SCReturnInt(r);
+}
+
+static void TransformToSha256(InspectionBuffer *buffer)
+{
+    const uint8_t *input = buffer->inspect;
+    const uint32_t input_len = buffer->inspect_len;
+    uint8_t output[SHA256_LENGTH];
+
+    //PrintRawDataFp(stdout, input, input_len);
+
+    HASHContext *sha256_ctx = HASH_Create(HASH_AlgSHA256);
+    if (sha256_ctx) {
+        HASH_Begin(sha256_ctx);
+        HASH_Update(sha256_ctx, input, input_len);
+        unsigned int len = 0;
+        HASH_End(sha256_ctx, output, &len, sizeof(output));
+        HASH_Destroy(sha256_ctx);
+
+        InspectionBufferCopy(buffer, output, sizeof(output));
+    }
+}
+
+#ifdef UNITTESTS
+static int DetectTransformToSha256Test01(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);
+    TransformToSha256(&buffer);
+    PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
+    InspectionBufferFree(&buffer);
+    PASS;
+}
+
+#endif
+
+static void DetectTransformToSha256RegisterTests(void)
+{
+#ifdef UNITTESTS
+    UtRegisterTest("DetectTransformToSha256Test01",
+            DetectTransformToSha256Test01);
+#endif
+}
+#endif
diff --git a/src/detect-transform-sha256.h b/src/detect-transform-sha256.h
new file mode 100644 (file)
index 0000000..e8b4f0b
--- /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_SHA256_H__
+#define __DETECT_TRANSFORM_SHA256_H__
+
+/* prototypes */
+void DetectTransformSha256Register (void);
+
+#endif /* __DETECT_TRANSFORM_SHA256_H__ */