diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:39:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:39:49 +0000 |
commit | a0aa2307322cd47bbf416810ac0292925e03be87 (patch) | |
tree | 37076262a026c4b48c8a0e84f44ff9187556ca35 /src/detect-transform-sha1.c | |
parent | Initial commit. (diff) | |
download | suricata-a0aa2307322cd47bbf416810ac0292925e03be87.tar.xz suricata-a0aa2307322cd47bbf416810ac0292925e03be87.zip |
Adding upstream version 1:7.0.3.upstream/1%7.0.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/detect-transform-sha1.c')
-rw-r--r-- | src/detect-transform-sha1.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/detect-transform-sha1.c b/src/detect-transform-sha1.c new file mode 100644 index 0000000..927b25e --- /dev/null +++ b/src/detect-transform-sha1.c @@ -0,0 +1,116 @@ +/* Copyright (C) 2007-2020 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 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" + +#include "rust.h" + +static int DetectTransformToSha1Setup (DetectEngineCtx *, Signature *, const char *); +#ifdef UNITTESTS +static void DetectTransformToSha1RegisterTests(void); +#endif +static void TransformToSha1(InspectionBuffer *buffer, void *options); + +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 = + "/rules/transforms.html#to-sha1"; + sigmatch_table[DETECT_TRANSFORM_SHA1].Setup = + DetectTransformToSha1Setup; + sigmatch_table[DETECT_TRANSFORM_SHA1].Transform = + TransformToSha1; +#ifdef UNITTESTS + sigmatch_table[DETECT_TRANSFORM_SHA1].RegisterTests = + DetectTransformToSha1RegisterTests; +#endif + sigmatch_table[DETECT_TRANSFORM_SHA1].flags |= SIGMATCH_NOOPT; +} + +/** + * \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(); + if (g_disable_hashing) { + SCLogError("SHA1 hashing has been disabled, " + "needed for to_sha1 keyword"); + SCReturnInt(-1); + } + int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_SHA1, NULL); + SCReturnInt(r); +} + +static void TransformToSha1(InspectionBuffer *buffer, void *options) +{ + const uint8_t *input = buffer->inspect; + const uint32_t input_len = buffer->inspect_len; + uint8_t output[SC_SHA1_LEN]; + + //PrintRawDataFp(stdout, input, input_len); + SCSha1HashBuffer(input, input_len, output, sizeof(output)); + 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(NULL, -1, &buffer, input, input_len); + PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len); + TransformToSha1(&buffer, NULL); + PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len); + InspectionBufferFree(&buffer); + PASS; +} + +static void DetectTransformToSha1RegisterTests(void) +{ + UtRegisterTest("DetectTransformToSha1Test01", + DetectTransformToSha1Test01); +} +#endif |