suricata
detect-transform-sha1.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2018 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 sha1 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-sha1.h"
33 
34 #include "util-unittest.h"
35 #include "util-print.h"
36 
37 static int DetectTransformToSha1Setup (DetectEngineCtx *, Signature *, const char *);
38 #ifdef HAVE_NSS
39 static void DetectTransformToSha1RegisterTests(void);
40 static void TransformToSha1(InspectionBuffer *buffer);
41 #endif
42 
44 {
47  "convert to sha1 hash of the buffer";
49  DOC_URL DOC_VERSION "/rules/transforms.html#to_sha1";
51  DetectTransformToSha1Setup;
52 #ifdef HAVE_NSS
54  TransformToSha1;
56  DetectTransformToSha1RegisterTests;
57 #endif
59 }
60 
61 #ifndef HAVE_NSS
62 static int DetectTransformToSha1Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
63 {
64  SCLogError(SC_ERR_NO_SHA1_SUPPORT, "no SHA-1 calculation support built in, "
65  "needed for to_sha1 keyword");
66  return -1;
67 }
68 #else
69 /**
70  * \internal
71  * \brief Apply the nocase keyword to the last pattern match, either content or uricontent
72  * \param det_ctx detection engine ctx
73  * \param s signature
74  * \param nullstr should be null
75  * \retval 0 ok
76  * \retval -1 failure
77  */
78 static int DetectTransformToSha1Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
79 {
80  SCEnter();
82  SCReturnInt(r);
83 }
84 
85 static void TransformToSha1(InspectionBuffer *buffer)
86 {
87  const uint8_t *input = buffer->inspect;
88  const uint32_t input_len = buffer->inspect_len;
89  uint8_t output[SHA1_LENGTH];
90 
91  //PrintRawDataFp(stdout, input, input_len);
92 
93  HASHContext *sha1_ctx = HASH_Create(HASH_AlgSHA1);
94  if (sha1_ctx) {
95  HASH_Begin(sha1_ctx);
96  HASH_Update(sha1_ctx, input, input_len);
97  unsigned int len = 0;
98  HASH_End(sha1_ctx, output, &len, sizeof(output));
99  HASH_Destroy(sha1_ctx);
100 
101  InspectionBufferCopy(buffer, output, sizeof(output));
102  }
103 }
104 
105 #ifdef UNITTESTS
106 static int DetectTransformToSha1Test01(void)
107 {
108  const uint8_t *input = (const uint8_t *)" A B C D ";
109  uint32_t input_len = strlen((char *)input);
110 
111  InspectionBuffer buffer;
112  InspectionBufferInit(&buffer, 8);
113  InspectionBufferSetup(&buffer, input, input_len);
114  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
115  TransformToSha1(&buffer);
116  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
117  InspectionBufferFree(&buffer);
118  PASS;
119 }
120 
121 #endif
122 
123 static void DetectTransformToSha1RegisterTests(void)
124 {
125 #ifdef UNITTESTS
126  UtRegisterTest("DetectTransformToSha1Test01",
127  DetectTransformToSha1Test01);
128 #endif
129 }
130 #endif
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1439
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1179
#define PASS
Pass the test.
const char * name
Definition: detect.h:1193
Signature container.
Definition: detect.h:517
int DetectSignatureAddTransform(Signature *s, int transform)
void InspectionBufferInit(InspectionBuffer *buffer, uint32_t initial_size)
void(* Transform)(InspectionBuffer *)
Definition: detect.h:1176
main detection engine ctx
Definition: detect.h:756
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define SCEnter(...)
Definition: util-debug.h:337
#define SCReturnInt(x)
Definition: util-debug.h:341
void DetectTransformSha1Register(void)
const char * desc
Definition: detect.h:1195
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:141
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
#define SIGMATCH_NOOPT
Definition: detect.h:1362
const char * url
Definition: detect.h:1196
uint32_t inspect_len
Definition: detect.h:340
const uint8_t * inspect
Definition: detect.h:338
#define DOC_URL
Definition: suricata.h:86
uint8_t len
void InspectionBufferFree(InspectionBuffer *buffer)
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1187
#define SHA1_LENGTH
Definition: util-crypt.h:45
void(* RegisterTests)(void)
Definition: detect.h:1185