suricata
detect-entropy.c
Go to the documentation of this file.
1 /* Copyright (C) 2024 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 #include "suricata-common.h"
19 
20 #include "detect.h"
21 #include "detect-parse.h"
22 #include "detect-engine.h"
23 
24 #include "detect-entropy.h"
25 
26 #include "rust.h"
27 
28 static int DetectEntropySetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
29 {
30  DetectEntropyData *ded = SCDetectEntropyParse(arg);
31  if (ded == NULL) {
32  goto error;
33  }
34 
35  int sm_list = DETECT_SM_LIST_PMATCH;
37  if (DetectBufferGetActiveList(de_ctx, s) == -1)
38  goto error;
39 
40  sm_list = s->init_data->list;
41  }
42 
43  if (SigMatchAppendSMToList(de_ctx, s, DETECT_ENTROPY, (SigMatchCtx *)ded, sm_list) != NULL) {
44  SCReturnInt(0);
45  }
46 
47  /* fall through */
48 
49 error:
50  SCLogDebug("error during entropy setup");
51  if (ded != NULL) {
52  SCDetectEntropyFree(ded);
53  }
54  SCReturnInt(-1);
55 }
56 
57 static void DetectEntropyFree(DetectEngineCtx *de_ctx, void *ptr)
58 {
59  SCDetectEntropyFree(ptr);
60 }
61 
63  const SigMatchCtx *ctx, const uint8_t *buffer, const uint32_t buffer_len)
64 {
65  return SCDetectEntropyMatch(buffer, buffer_len, (const DetectEntropyData *)ctx);
66 }
67 
69 {
70  sigmatch_table[DETECT_ENTROPY].name = "entropy";
71  sigmatch_table[DETECT_ENTROPY].desc = "calculate entropy";
72  sigmatch_table[DETECT_BYTE_EXTRACT].url = "/rules/payload-keywords.html#entropy";
73  sigmatch_table[DETECT_ENTROPY].Free = DetectEntropyFree;
74  sigmatch_table[DETECT_ENTROPY].Setup = DetectEntropySetup;
75 }
SigTableElmt_::url
const char * url
Definition: detect.h:1405
detect-engine.h
DETECT_SM_LIST_PMATCH
@ DETECT_SM_LIST_PMATCH
Definition: detect.h:118
SigTableElmt_::desc
const char * desc
Definition: detect.h:1404
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
detect-entropy.h
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1392
SigTableElmt_::name
const char * name
Definition: detect.h:1402
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectEntropyDoMatch
bool DetectEntropyDoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchCtx *ctx, const uint8_t *buffer, const uint32_t buffer_len)
Definition: detect-entropy.c:62
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:920
DETECT_ENTROPY
@ DETECT_ENTROPY
Definition: detect-engine-register.h:96
rust.h
DetectBufferGetActiveList
int DetectBufferGetActiveList(DetectEngineCtx *de_ctx, Signature *s)
Definition: detect-engine.c:1502
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1387
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1197
SignatureInitData_::list
int list
Definition: detect.h:629
detect.h
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:748
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
DETECT_SM_LIST_NOTSET
#define DETECT_SM_LIST_NOTSET
Definition: detect.h:143
suricata-common.h
DetectEntropyRegister
void DetectEntropyRegister(void)
Definition: detect-entropy.c:68
DETECT_BYTE_EXTRACT
@ DETECT_BYTE_EXTRACT
Definition: detect-engine-register.h:85
detect-parse.h
Signature_
Signature container.
Definition: detect.h:669
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:464
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275