From: Victor Julien Date: Thu, 22 Nov 2018 11:57:32 +0000 (+0100) Subject: detect/transform: add to_sha1 keyword X-Git-Tag: suricata-5.0.0-beta1~295 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb73008ccfa1855cb802874effdb47ff340a71a8;p=thirdparty%2Fsuricata.git detect/transform: add to_sha1 keyword --- diff --git a/doc/userguide/rules/transforms.rst b/doc/userguide/rules/transforms.rst index 3f7ec328dc..79c5be9c5f 100644 --- a/doc/userguide/rules/transforms.rst +++ b/doc/userguide/rules/transforms.rst @@ -49,6 +49,19 @@ Example:: .. note:: depends on libnss being compiled into Suricata +to_sha1 +--------- + +Takes the buffer, calculates the SHA-1 hash and passes the raw hash value +on. + +Example:: + + alert http any any -> any any (http_request_line; to_sha1; \ + content:"|54A9 7A8A B09C 1B81 3725 2214 51D3 F997 F015 9DD7|"; sid:1;) + +.. note:: depends on libnss being compiled into Suricata + to_sha256 --------- diff --git a/src/Makefile.am b/src/Makefile.am index 008732595e..17cf791af3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -268,6 +268,7 @@ 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-sha1.c detect-transform-sha1.h \ detect-transform-sha256.c detect-transform-sha256.h \ detect-ttl.c detect-ttl.h \ detect-uricontent.c detect-uricontent.h \ diff --git a/src/detect-engine-register.c b/src/detect-engine-register.c index 01b87b0aea..926a8ba252 100644 --- a/src/detect-engine-register.c +++ b/src/detect-engine-register.c @@ -193,6 +193,7 @@ #include "detect-transform-compress-whitespace.h" #include "detect-transform-strip-whitespace.h" #include "detect-transform-md5.h" +#include "detect-transform-sha1.h" #include "detect-transform-sha256.h" #include "util-rule-vars.h" @@ -527,6 +528,7 @@ void SigTableSetup(void) DetectTransformCompressWhitespaceRegister(); DetectTransformStripWhitespaceRegister(); DetectTransformMd5Register(); + DetectTransformSha1Register(); DetectTransformSha256Register(); /* close keyword registration */ diff --git a/src/detect-engine-register.h b/src/detect-engine-register.h index 1927854a9e..e9585ef38b 100644 --- a/src/detect-engine-register.h +++ b/src/detect-engine-register.h @@ -216,6 +216,7 @@ enum { DETECT_TRANSFORM_COMPRESS_WHITESPACE, DETECT_TRANSFORM_STRIP_WHITESPACE, DETECT_TRANSFORM_MD5, + DETECT_TRANSFORM_SHA1, DETECT_TRANSFORM_SHA256, /* make sure this stays last */ diff --git a/src/detect-transform-sha1.c b/src/detect-transform-sha1.c new file mode 100644 index 0000000000..e7bbee81df --- /dev/null +++ b/src/detect-transform-sha1.c @@ -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 + * + * Implements the sha1 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-sha1.h" + +#include "util-unittest.h" +#include "util-print.h" + +static int DetectTransformToSha1Setup (DetectEngineCtx *, Signature *, const char *); +#ifdef HAVE_NSS +static void DetectTransformToSha1RegisterTests(void); +static void TransformToSha1(InspectionBuffer *buffer); +#endif + +void DetectTransformSha1Register(void) +{ + sigmatch_table[DETECT_TRANSFORM_SHA1].name = "to_sha1"; + sigmatch_table[DETECT_TRANSFORM_SHA1].desc = + "convert to sha1 hash of the buffer"; + sigmatch_table[DETECT_TRANSFORM_SHA1].url = + DOC_URL DOC_VERSION "/rules/transforms.html#to_sha1"; + sigmatch_table[DETECT_TRANSFORM_SHA1].Setup = + DetectTransformToSha1Setup; +#ifdef HAVE_NSS + sigmatch_table[DETECT_TRANSFORM_SHA1].Transform = + TransformToSha1; + sigmatch_table[DETECT_TRANSFORM_SHA1].RegisterTests = + DetectTransformToSha1RegisterTests; +#endif + sigmatch_table[DETECT_TRANSFORM_SHA1].flags |= SIGMATCH_NOOPT; +} + +#ifndef HAVE_NSS +static int DetectTransformToSha1Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr) +{ + SCLogError(SC_ERR_NO_SHA1_SUPPORT, "no SHA-1 calculation support built in, " + "needed for to_sha1 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 DetectTransformToSha1Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr) +{ + SCEnter(); + int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_SHA1); + SCReturnInt(r); +} + +static void TransformToSha1(InspectionBuffer *buffer) +{ + const uint8_t *input = buffer->inspect; + const uint32_t input_len = buffer->inspect_len; + uint8_t output[SHA1_LENGTH]; + + //PrintRawDataFp(stdout, input, input_len); + + HASHContext *sha1_ctx = HASH_Create(HASH_AlgSHA1); + if (sha1_ctx) { + HASH_Begin(sha1_ctx); + HASH_Update(sha1_ctx, input, input_len); + unsigned int len = 0; + HASH_End(sha1_ctx, output, &len, sizeof(output)); + HASH_Destroy(sha1_ctx); + + InspectionBufferCopy(buffer, output, sizeof(output)); + } +} + +#ifdef UNITTESTS +static int DetectTransformToSha1Test01(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); + TransformToSha1(&buffer); + PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len); + InspectionBufferFree(&buffer); + PASS; +} + +#endif + +static void DetectTransformToSha1RegisterTests(void) +{ +#ifdef UNITTESTS + UtRegisterTest("DetectTransformToSha1Test01", + DetectTransformToSha1Test01); +#endif +} +#endif diff --git a/src/detect-transform-sha1.h b/src/detect-transform-sha1.h new file mode 100644 index 0000000000..4d53caedf9 --- /dev/null +++ b/src/detect-transform-sha1.h @@ -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 + */ + +#ifndef __DETECT_TRANSFORM_SHA1_H__ +#define __DETECT_TRANSFORM_SHA1_H__ + +/* prototypes */ +void DetectTransformSha1Register (void); + +#endif /* __DETECT_TRANSFORM_SHA1_H__ */