/src/suricata7/src/detect-transform-md5.c
Line | Count | Source |
1 | | /* Copyright (C) 2007-2020 Open Information Security Foundation |
2 | | * |
3 | | * You can copy, redistribute or modify this Program under the terms of |
4 | | * the GNU General Public License version 2 as published by the Free |
5 | | * Software Foundation. |
6 | | * |
7 | | * This program is distributed in the hope that it will be useful, |
8 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | | * GNU General Public License for more details. |
11 | | * |
12 | | * You should have received a copy of the GNU General Public License |
13 | | * version 2 along with this program; if not, write to the Free Software |
14 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
15 | | * 02110-1301, USA. |
16 | | */ |
17 | | |
18 | | /** |
19 | | * \file |
20 | | * |
21 | | * \author Victor Julien <victor@inliniac.net> |
22 | | * |
23 | | * Implements the to_md5 transformation keyword |
24 | | */ |
25 | | |
26 | | #include "suricata-common.h" |
27 | | |
28 | | #include "detect.h" |
29 | | #include "detect-engine.h" |
30 | | #include "detect-engine-prefilter.h" |
31 | | #include "detect-parse.h" |
32 | | #include "detect-transform-md5.h" |
33 | | |
34 | | #include "util-unittest.h" |
35 | | #include "util-print.h" |
36 | | #include "rust.h" |
37 | | |
38 | | static int DetectTransformToMd5Setup (DetectEngineCtx *, Signature *, const char *); |
39 | | #ifdef UNITTESTS |
40 | | static void DetectTransformToMd5RegisterTests(void); |
41 | | #endif |
42 | | static void TransformToMd5(InspectionBuffer *buffer, void *options); |
43 | | |
44 | | void DetectTransformMd5Register(void) |
45 | 34 | { |
46 | 34 | sigmatch_table[DETECT_TRANSFORM_MD5].name = "to_md5"; |
47 | 34 | sigmatch_table[DETECT_TRANSFORM_MD5].desc = |
48 | 34 | "convert to md5 hash of the buffer"; |
49 | 34 | sigmatch_table[DETECT_TRANSFORM_MD5].url = |
50 | 34 | "/rules/transforms.html#to-md5"; |
51 | 34 | sigmatch_table[DETECT_TRANSFORM_MD5].Setup = |
52 | 34 | DetectTransformToMd5Setup; |
53 | 34 | sigmatch_table[DETECT_TRANSFORM_MD5].Transform = |
54 | 34 | TransformToMd5; |
55 | | #ifdef UNITTESTS |
56 | | sigmatch_table[DETECT_TRANSFORM_MD5].RegisterTests = |
57 | | DetectTransformToMd5RegisterTests; |
58 | | #endif |
59 | 34 | sigmatch_table[DETECT_TRANSFORM_MD5].flags |= SIGMATCH_NOOPT; |
60 | 34 | } |
61 | | |
62 | | /** |
63 | | * \internal |
64 | | * \brief Apply the nocase keyword to the last pattern match, either content or uricontent |
65 | | * \param det_ctx detection engine ctx |
66 | | * \param s signature |
67 | | * \param nullstr should be null |
68 | | * \retval 0 ok |
69 | | * \retval -1 failure |
70 | | */ |
71 | | static int DetectTransformToMd5Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr) |
72 | 760 | { |
73 | 760 | SCEnter(); |
74 | 760 | if (g_disable_hashing) { |
75 | 0 | SCLogError("MD5 hashing has been disabled, " |
76 | 0 | "needed for to_md5 keyword"); |
77 | 0 | SCReturnInt(-1); |
78 | 0 | } |
79 | 760 | int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_MD5, NULL); |
80 | 760 | SCReturnInt(r); |
81 | 760 | } |
82 | | |
83 | | static void TransformToMd5(InspectionBuffer *buffer, void *options) |
84 | 17 | { |
85 | 17 | const uint8_t *input = buffer->inspect; |
86 | 17 | const uint32_t input_len = buffer->inspect_len; |
87 | 17 | uint8_t output[SC_MD5_LEN]; |
88 | | |
89 | | //PrintRawDataFp(stdout, input, input_len); |
90 | 17 | SCMd5HashBuffer(input, input_len, output, sizeof(output)); |
91 | 17 | InspectionBufferCopy(buffer, output, sizeof(output)); |
92 | 17 | } |
93 | | |
94 | | #ifdef UNITTESTS |
95 | | static int DetectTransformToMd5Test01(void) |
96 | | { |
97 | | const uint8_t *input = (const uint8_t *)" A B C D "; |
98 | | uint32_t input_len = strlen((char *)input); |
99 | | |
100 | | InspectionBuffer buffer; |
101 | | InspectionBufferInit(&buffer, 8); |
102 | | InspectionBufferSetup(NULL, -1, &buffer, input, input_len); |
103 | | PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len); |
104 | | TransformToMd5(&buffer, NULL); |
105 | | PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len); |
106 | | InspectionBufferFree(&buffer); |
107 | | PASS; |
108 | | } |
109 | | |
110 | | static void DetectTransformToMd5RegisterTests(void) |
111 | | { |
112 | | UtRegisterTest("DetectTransformToMd5Test01", |
113 | | DetectTransformToMd5Test01); |
114 | | } |
115 | | #endif |