suricata
detect-dhcp-rebinding-time.c
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 #include "rust.h"
21 #include "detect-engine.h"
22 #include "detect-engine-mpm.h"
24 #include "detect-engine-uint.h"
25 #include "detect-parse.h"
26 
27 static int g_buffer_id = 0;
28 
29 /**
30  * \internal
31  * \brief Function to match rebinding time of a TX
32  *
33  * \param t Pointer to thread vars.
34  * \param det_ctx Pointer to the pattern matcher thread.
35  * \param f Pointer to the current flow.
36  * \param flags Flags.
37  * \param state App layer state.
38  * \param s Pointer to the Signature.
39  * \param m Pointer to the sigmatch that we will cast into
40  * DetectU64Data.
41  *
42  * \retval 0 no match.
43  * \retval 1 match.
44  */
45 static int DetectDHCPRebindingTimeMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
46  void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
47 {
48  SCEnter();
49 
50  uint64_t res;
51  if (rs_dhcp_tx_get_rebinding_time(txv, &res)) {
52  const DetectU64Data *dd = (const DetectU64Data *)ctx;
53  if (DetectU64Match(res, dd)) {
54  SCReturnInt(1);
55  }
56  }
57  SCReturnInt(0);
58 }
59 
60 /**
61  * \internal
62  * \brief Function to free memory associated with DetectU64Data.
63  *
64  * \param de_ptr Pointer to DetectU64Data.
65  */
66 static void DetectDHCPRebindingTimeFree(DetectEngineCtx *de_ctx, void *ptr)
67 {
68  rs_detect_u64_free(ptr);
69 }
70 
71 /**
72  * \brief Function to add the parsed dhcp rebinding time field into the current signature.
73  *
74  * \param de_ctx Pointer to the Detection Engine Context.
75  * \param s Pointer to the Current Signature.
76  * \param rawstr Pointer to the user provided flags options.
77  * \param type Defines if this is notBefore or notAfter.
78  *
79  * \retval 0 on Success.
80  * \retval -1 on Failure.
81  */
82 static int DetectDHCPRebindingTimeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
83 {
85  return -1;
86 
87  DetectU64Data *dd = DetectU64Parse(rawstr);
88  if (dd == NULL) {
89  SCLogError("Parsing \'%s\' failed for %s", rawstr,
91  return -1;
92  }
93 
94  /* okay so far so good, lets get this into a SigMatch
95  * and put it in the Signature. */
96 
98  de_ctx, s, DETECT_AL_DHCP_REBINDING_TIME, (SigMatchCtx *)dd, g_buffer_id) == NULL) {
99  goto error;
100  }
101  return 0;
102 
103 error:
104  DetectDHCPRebindingTimeFree(de_ctx, dd);
105  return -1;
106 }
107 
108 /**
109  * \brief Registration function for dhcp.procedure keyword.
110  */
112 {
113  sigmatch_table[DETECT_AL_DHCP_REBINDING_TIME].name = "dhcp.rebinding_time";
114  sigmatch_table[DETECT_AL_DHCP_REBINDING_TIME].desc = "match DHCP rebinding time";
116  "/rules/dhcp-keywords.html#dhcp-rebinding-time";
117  sigmatch_table[DETECT_AL_DHCP_REBINDING_TIME].AppLayerTxMatch = DetectDHCPRebindingTimeMatch;
118  sigmatch_table[DETECT_AL_DHCP_REBINDING_TIME].Setup = DetectDHCPRebindingTimeSetup;
119  sigmatch_table[DETECT_AL_DHCP_REBINDING_TIME].Free = DetectDHCPRebindingTimeFree;
120 
123 
126 
127  g_buffer_id = DetectBufferTypeGetByName("dhcp.rebinding-time");
128 }
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1296
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1295
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1283
SigTableElmt_::name
const char * name
Definition: detect.h:1293
detect-dhcp-rebinding-time.h
Flow_
Flow data structure.
Definition: flow.h:350
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:836
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1264
rust.h
ALPROTO_DHCP
@ ALPROTO_DHCP
Definition: app-layer-protos.h:52
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:264
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1278
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1119
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:263
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1092
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
DetectDHCPRebindingTimeRegister
void DetectDHCPRebindingTimeRegister(void)
Registration function for dhcp.procedure keyword.
Definition: detect-dhcp-rebinding-time.c:111
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:342
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2126
DetectU64Parse
DetectUintData_u64 * DetectU64Parse(const char *u64str)
Definition: detect-engine-uint.c:147
DetectU64Data
DetectUintData_u64 DetectU64Data
Definition: detect-engine-uint.h:40
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
detect-parse.h
Signature_
Signature container.
Definition: detect.h:593
DetectU64Match
int DetectU64Match(const uint64_t parg, const DetectUintData_u64 *du64)
Definition: detect-engine-uint.c:142
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:216
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:447
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
DETECT_AL_DHCP_REBINDING_TIME
@ DETECT_AL_DHCP_REBINDING_TIME
Definition: detect-engine-register.h:294