suricata
detect-entropy.c
Go to the documentation of this file.
1/* Copyright (C) 2025 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"
24
25#include "detect-entropy.h"
26#include "util-var-name.h"
27#include "flow-var.h"
28
29#include "rust.h"
30
31static int DetectEntropySetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
32{
33 DetectEntropyData *ded = SCDetectEntropyParse(arg);
34 if (ded == NULL) {
35 goto error;
36 }
37
38 int sm_list = DETECT_SM_LIST_PMATCH;
41 goto error;
42
43 sm_list = s->init_data->list;
44 ded->fv_idx = VarNameStoreRegister(
46 }
47
48 if (SCSigMatchAppendSMToList(de_ctx, s, DETECT_ENTROPY, (SigMatchCtx *)ded, sm_list) != NULL) {
49 SCReturnInt(0);
50 }
51
52 /* fall through */
53
54error:
55 SCLogDebug("error during entropy setup");
56 if (ded != NULL) {
57 SCDetectEntropyFree(ded);
58 }
59 SCReturnInt(-1);
60}
61
62static void DetectEntropyFree(DetectEngineCtx *de_ctx, void *ptr)
63{
64 if (ptr) {
65 DetectEntropyData *ded = (DetectEntropyData *)ptr;
67 SCDetectEntropyFree(ptr);
68 }
69}
70
72 const SigMatchCtx *ctx, const uint8_t *buffer, const uint32_t buffer_len)
73{
74 double entropy = -1.0;
75 bool rc = SCDetectEntropyMatch(buffer, buffer_len, (const DetectEntropyData *)ctx, &entropy);
76
77 if (entropy != -1.0) {
78 DetectEntropyData *ded = (DetectEntropyData *)ctx;
79 FlowVarAddFloat(det_ctx->p->flow, ded->fv_idx, entropy);
80 }
81
82 return rc;
83}
84
86{
88 sigmatch_table[DETECT_ENTROPY].desc = "calculate entropy";
89 sigmatch_table[DETECT_ENTROPY].url = "/rules/payload-keywords.html#entropy";
90 sigmatch_table[DETECT_ENTROPY].Free = DetectEntropyFree;
91 sigmatch_table[DETECT_ENTROPY].Setup = DetectEntropySetup;
92}
int DetectBufferGetActiveList(DetectEngineCtx *de_ctx, Signature *s)
const char * DetectEngineBufferTypeGetNameById(const DetectEngineCtx *de_ctx, const int id)
void DetectEntropyRegister(void)
bool DetectEntropyDoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchCtx *ctx, const uint8_t *buffer, const uint32_t buffer_len)
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
#define DETECT_SM_LIST_NOTSET
Definition detect.h:144
@ DETECT_SM_LIST_PMATCH
Definition detect.h:119
void FlowVarAddFloat(Flow *f, uint32_t idx, double value)
Definition flow-var.c:142
DetectEngineCtx * de_ctx
struct Thresholds ctx
main detection engine ctx
Definition detect.h:932
struct Flow_ * flow
Definition decode.h:546
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
SignatureInitData * init_data
Definition detect.h:747
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCReturnInt(x)
Definition util-debug.h:281
void VarNameStoreUnregister(const uint32_t id, const enum VarTypes type)
uint32_t VarNameStoreRegister(const char *name, const enum VarTypes type)
@ VAR_TYPE_FLOW_FLOAT
Definition util-var.h:38