suricata
detect-transform-sha1.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 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 #include "rust.h"
38 
39 static int DetectTransformToSha1Setup (DetectEngineCtx *, Signature *, const char *);
40 #ifdef UNITTESTS
41 static void DetectTransformToSha1RegisterTests(void);
42 #endif
43 static void TransformToSha1(InspectionBuffer *buffer, void *options);
44 
46 {
49  "convert to sha1 hash of the buffer";
51  "/rules/transforms.html#to-sha1";
53  DetectTransformToSha1Setup;
55  TransformToSha1;
56 #ifdef UNITTESTS
58  DetectTransformToSha1RegisterTests;
59 #endif
61 }
62 
63 /**
64  * \internal
65  * \brief Apply the nocase keyword to the last pattern match, either content or uricontent
66  * \param det_ctx detection engine ctx
67  * \param s signature
68  * \param nullstr should be null
69  * \retval 0 ok
70  * \retval -1 failure
71  */
72 static int DetectTransformToSha1Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
73 {
74  SCEnter();
75  if (g_disable_hashing) {
76  SCLogError(SC_ERR_HASHING_DISABLED, "SHA1 hashing has been disabled, "
77  "needed for to_sha1 keyword");
78  SCReturnInt(-1);
79  }
81  SCReturnInt(r);
82 }
83 
84 static void TransformToSha1(InspectionBuffer *buffer, void *options)
85 {
86  const uint8_t *input = buffer->inspect;
87  const uint32_t input_len = buffer->inspect_len;
88  uint8_t output[SC_SHA1_LEN];
89 
90  //PrintRawDataFp(stdout, input, input_len);
91  SCSha1HashBuffer(input, input_len, output, sizeof(output));
92  InspectionBufferCopy(buffer, output, sizeof(output));
93 }
94 
95 #ifdef UNITTESTS
96 static int DetectTransformToSha1Test01(void)
97 {
98  const uint8_t *input = (const uint8_t *)" A B C D ";
99  uint32_t input_len = strlen((char *)input);
100 
101  InspectionBuffer buffer;
102  InspectionBufferInit(&buffer, 8);
103  InspectionBufferSetup(NULL, -1, &buffer, input, input_len);
104  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
105  TransformToSha1(&buffer, NULL);
106  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
107  InspectionBufferFree(&buffer);
108  PASS;
109 }
110 
111 static void DetectTransformToSha1RegisterTests(void)
112 {
113  UtRegisterTest("DetectTransformToSha1Test01",
114  DetectTransformToSha1Test01);
115 }
116 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1238
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1237
SigTableElmt_::name
const char * name
Definition: detect.h:1235
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
InspectionBuffer
Definition: detect.h:337
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1229
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:784
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1220
detect-engine-prefilter.h
util-unittest.h
InspectionBufferInit
void InspectionBufferInit(InspectionBuffer *buffer, uint32_t initial_size)
Definition: detect-engine.c:1426
DETECT_TRANSFORM_SHA1
@ DETECT_TRANSFORM_SHA1
Definition: detect-engine-register.h:318
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:298
detect.h
SC_SHA1_LEN
#define SC_SHA1_LEN
Definition: rust.h:32
PrintRawDataFp
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:143
SC_ERR_HASHING_DISABLED
@ SC_ERR_HASHING_DISABLED
Definition: util-error.h:370
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine.c:1497
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
SigTableElmt_::Transform
void(* Transform)(InspectionBuffer *, void *context)
Definition: detect.h:1216
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255
DetectTransformSha1Register
void DetectTransformSha1Register(void)
Definition: detect-transform-sha1.c:45
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:340
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:338
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1450
detect-parse.h
Signature_
Signature container.
Definition: detect.h:539
DetectSignatureAddTransform
int DetectSignatureAddTransform(Signature *s, int transform, void *options)
Definition: detect-parse.c:1468
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1421
detect-transform-sha1.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:302
InspectionBufferFree
void InspectionBufferFree(InspectionBuffer *buffer)
Definition: detect-engine.c:1468
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1227
g_disable_hashing
bool g_disable_hashing
Definition: suricata.c:201