suricata
detect-transform-md5.c
Go to the documentation of this file.
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"
31 #include "detect-parse.h"
32 #include "detect-transform-md5.h"
33 
34 #include "util-unittest.h"
35 #include "util-print.h"
36 
37 static int DetectTransformToMd5Setup (DetectEngineCtx *, Signature *, const char *);
38 #ifdef HAVE_NSS
39 #ifdef UNITTESTS
40 static void DetectTransformToMd5RegisterTests(void);
41 #endif
42 static void TransformToMd5(InspectionBuffer *buffer, void *options);
43 #endif
44 
46 {
49  "convert to md5 hash of the buffer";
51  "/rules/transforms.html#to-md5";
53  DetectTransformToMd5Setup;
54 #ifdef HAVE_NSS
56  TransformToMd5;
57 #ifdef UNITTESTS
59  DetectTransformToMd5RegisterTests;
60 #endif
61 #endif
63 }
64 
65 #ifndef HAVE_NSS
66 static int DetectTransformToMd5Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
67 {
68  SCLogError(SC_ERR_NO_MD5_SUPPORT, "no MD5 calculation support built in, "
69  "needed for to_md5 keyword");
70  return -1;
71 }
72 #else
73 /**
74  * \internal
75  * \brief Apply the nocase keyword to the last pattern match, either content or uricontent
76  * \param det_ctx detection engine ctx
77  * \param s signature
78  * \param nullstr should be null
79  * \retval 0 ok
80  * \retval -1 failure
81  */
82 static int DetectTransformToMd5Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
83 {
84  SCEnter();
86  SCReturnInt(r);
87 }
88 
89 static void TransformToMd5(InspectionBuffer *buffer, void *options)
90 {
91  const uint8_t *input = buffer->inspect;
92  const uint32_t input_len = buffer->inspect_len;
93  uint8_t output[MD5_LENGTH];
94 
95  //PrintRawDataFp(stdout, input, input_len);
96 
97  HASHContext *ctx = HASH_Create(HASH_AlgMD5);
98  if (ctx) {
99  HASH_Begin(ctx);
100  HASH_Update(ctx, input, input_len);
101  unsigned int len = 0;
102  HASH_End(ctx, output, &len, sizeof(output));
103  HASH_Destroy(ctx);
104 
105  InspectionBufferCopy(buffer, output, sizeof(output));
106  }
107 }
108 
109 #ifdef UNITTESTS
110 static int DetectTransformToMd5Test01(void)
111 {
112  const uint8_t *input = (const uint8_t *)" A B C D ";
113  uint32_t input_len = strlen((char *)input);
114 
115  InspectionBuffer buffer;
116  InspectionBufferInit(&buffer, 8);
117  InspectionBufferSetup(&buffer, input, input_len);
118  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
119  TransformToMd5(&buffer, NULL);
120  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
121  InspectionBufferFree(&buffer);
122  PASS;
123 }
124 
125 static void DetectTransformToMd5RegisterTests(void)
126 {
127  UtRegisterTest("DetectTransformToMd5Test01",
128  DetectTransformToMd5Test01);
129 }
130 #endif
131 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1214
len
uint8_t len
Definition: app-layer-dnp3.h:2
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
SigTableElmt_::name
const char * name
Definition: detect.h:1211
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
InspectionBuffer
Definition: detect.h:344
SC_ERR_NO_MD5_SUPPORT
@ SC_ERR_NO_MD5_SUPPORT
Definition: util-error.h:241
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1205
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
detect-engine-prefilter.h
util-unittest.h
InspectionBufferInit
void InspectionBufferInit(InspectionBuffer *buffer, uint32_t initial_size)
Definition: detect-engine.c:1110
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
util-print.h
detect-transform-md5.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
PrintRawDataFp
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:141
DETECT_TRANSFORM_MD5
@ DETECT_TRANSFORM_MD5
Definition: detect-engine-register.h:294
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine.c:1156
suricata-common.h
InspectionBufferSetup
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1120
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SigTableElmt_::Transform
void(* Transform)(InspectionBuffer *, void *context)
Definition: detect.h:1192
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:347
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
DetectSignatureAddTransform
int DetectSignatureAddTransform(Signature *s, int transform, void *options)
Definition: detect-parse.c:1455
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1380
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
DetectTransformMd5Register
void DetectTransformMd5Register(void)
Definition: detect-transform-md5.c:45
InspectionBufferFree
void InspectionBufferFree(InspectionBuffer *buffer)
Definition: detect-engine.c:1127
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203