suricata
detect-dhcp-renewal-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 renewal 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 DetectDHCPRenewalTimeMatch(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_renewal_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 DetectDHCPRenewalTimeFree(DetectEngineCtx *de_ctx, void *ptr)
67 {
68  rs_detect_u64_free(ptr);
69 }
70 
71 /**
72  * \brief Function to add the parsed dhcp renewal 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 DetectDHCPRenewalTimeSetup(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  SigMatch *sm = SigMatchAlloc();
97  if (sm == NULL)
98  goto error;
99 
101  sm->ctx = (void *)dd;
102 
103  SigMatchAppendSMToList(s, sm, g_buffer_id);
104  return 0;
105 
106 error:
107  DetectDHCPRenewalTimeFree(de_ctx, dd);
108  return -1;
109 }
110 
111 /**
112  * \brief Registration function for dhcp.procedure keyword.
113  */
115 {
116  sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].name = "dhcp.renewal_time";
117  sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].desc = "match DHCP renewal time";
118  sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].url = "/rules/dhcp-keywords.html#dhcp-renewal-time";
119  sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].AppLayerTxMatch = DetectDHCPRenewalTimeMatch;
120  sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].Setup = DetectDHCPRenewalTimeSetup;
121  sigmatch_table[DETECT_AL_DHCP_RENEWAL_TIME].Free = DetectDHCPRenewalTimeFree;
122 
125 
128 
129  g_buffer_id = DetectBufferTypeGetByName("dhcp.renewal-time");
130 }
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1241
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1498
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1240
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1228
SigTableElmt_::name
const char * name
Definition: detect.h:1238
Flow_
Flow data structure.
Definition: flow.h:357
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1209
rust.h
ALPROTO_DHCP
@ ALPROTO_DHCP
Definition: app-layer-protos.h:52
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:229
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1223
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1079
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:228
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1025
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:316
DetectDHCPRenewalTimeRegister
void DetectDHCPRenewalTimeRegister(void)
Registration function for dhcp.procedure keyword.
Definition: detect-dhcp-renewal-time.c:114
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:224
detect-dhcp-renewal-time.h
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:239
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:308
DETECT_AL_DHCP_RENEWAL_TIME
@ DETECT_AL_DHCP_RENEWAL_TIME
Definition: detect-engine-register.h:283
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:314
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
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:1948
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:540
SigMatch_
a single match condition for a signature
Definition: detect.h:313
DetectU64Match
int DetectU64Match(const uint64_t parg, const DetectUintData_u64 *du64)
Definition: detect-engine-uint.c:142
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:354