suricata
detect-snmp-pdu_type.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-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 Pierre Chifflier <chifflier@wzdftpd.net>
22  */
23 
24 #include "suricata-common.h"
25 #include "conf.h"
26 #include "detect.h"
27 #include "detect-parse.h"
28 #include "detect-engine.h"
30 #include "detect-snmp-pdu_type.h"
31 #include "app-layer-parser.h"
32 #include "rust.h"
33 
34 /**
35  * [snmp.pdu_type]:<type>;
36  */
37 #define PARSE_REGEX "^\\s*([0-9]+)\\s*$"
38 static DetectParseRegex parse_regex;
39 
40 typedef struct DetectSNMPPduTypeData_ {
41  uint32_t pdu_type;
43 
44 static DetectSNMPPduTypeData *DetectSNMPPduTypeParse (const char *);
45 static int DetectSNMPPduTypeSetup (DetectEngineCtx *, Signature *s, const char *str);
46 static void DetectSNMPPduTypeFree(DetectEngineCtx *, void *);
47 #ifdef UNITTESTS
48 static void DetectSNMPPduTypeRegisterTests(void);
49 #endif
50 static int g_snmp_pdu_type_buffer_id = 0;
51 
52 static int DetectSNMPPduTypeMatch (DetectEngineThreadCtx *, Flow *,
53  uint8_t, void *, void *, const Signature *,
54  const SigMatchCtx *);
55 
57 {
58  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].name = "snmp.pdu_type";
59  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].desc = "match SNMP PDU type";
60  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].url = "/rules/snmp-keywords.html#snmp-pdu-type";
62  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].AppLayerTxMatch = DetectSNMPPduTypeMatch;
63  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].Setup = DetectSNMPPduTypeSetup;
64  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].Free = DetectSNMPPduTypeFree;
65 #ifdef UNITTESTS
66  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].RegisterTests = DetectSNMPPduTypeRegisterTests;
67 #endif
68 
69  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
70 
73 
76 
77  g_snmp_pdu_type_buffer_id = DetectBufferTypeGetByName("snmp.pdu_type");
78 }
79 
80 /**
81  * \internal
82  * \brief Function to match pdu_type of a TX
83  *
84  * \param t Pointer to thread vars.
85  * \param det_ctx Pointer to the pattern matcher thread.
86  * \param f Pointer to the current flow.
87  * \param flags Flags.
88  * \param state App layer state.
89  * \param s Pointer to the Signature.
90  * \param m Pointer to the sigmatch that we will cast into
91  * DetectSNMPPduTypeData.
92  *
93  * \retval 0 no match.
94  * \retval 1 match.
95  */
96 static int DetectSNMPPduTypeMatch (DetectEngineThreadCtx *det_ctx,
97  Flow *f, uint8_t flags, void *state,
98  void *txv, const Signature *s,
99  const SigMatchCtx *ctx)
100 {
101  SCEnter();
102 
103  const DetectSNMPPduTypeData *dd = (const DetectSNMPPduTypeData *)ctx;
104  uint32_t pdu_type;
105  rs_snmp_tx_get_pdu_type(txv, &pdu_type);
106  SCLogDebug("pdu_type %u ref_pdu_type %d",
107  pdu_type, dd->pdu_type);
108  if (pdu_type == dd->pdu_type)
109  SCReturnInt(1);
110  SCReturnInt(0);
111 }
112 
113 /**
114  * \internal
115  * \brief Function to parse options passed via snmp.pdu_type keywords.
116  *
117  * \param rawstr Pointer to the user provided options.
118  *
119  * \retval dd pointer to DetectSNMPPduTypeData on success.
120  * \retval NULL on failure.
121  */
122 static DetectSNMPPduTypeData *DetectSNMPPduTypeParse (const char *rawstr)
123 {
124  DetectSNMPPduTypeData *dd = NULL;
125  int res = 0;
126  size_t pcre2len;
127  char value1[20] = "";
128  char *endptr = NULL;
129 
130  pcre2_match_data *match = NULL;
131  int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
132  if (ret != 2) {
133  SCLogError("Parse error %s", rawstr);
134  goto error;
135  }
136 
137  pcre2len = sizeof(value1);
138  res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)value1, &pcre2len);
139  if (res < 0) {
140  SCLogError("pcre2_substring_copy_bynumber failed");
141  goto error;
142  }
143 
144  dd = SCCalloc(1, sizeof(DetectSNMPPduTypeData));
145  if (unlikely(dd == NULL))
146  goto error;
147 
148  /* set the value */
149  dd->pdu_type = strtoul(value1, &endptr, 10);
150  if (endptr == NULL || *endptr != '\0') {
151  SCLogError("invalid character as arg "
152  "to snmp.pdu_type keyword");
153  goto error;
154  }
155 
156  pcre2_match_data_free(match);
157  return dd;
158 
159 error:
160  if (match) {
161  pcre2_match_data_free(match);
162  }
163  if (dd)
164  SCFree(dd);
165  return NULL;
166 }
167 
168 /**
169  * \brief Function to add the parsed snmp pdu_type field into the current signature.
170  *
171  * \param de_ctx Pointer to the Detection Engine Context.
172  * \param s Pointer to the Current Signature.
173  * \param rawstr Pointer to the user provided flags options.
174  * \param type Defines if this is notBefore or notAfter.
175  *
176  * \retval 0 on Success.
177  * \retval -1 on Failure.
178  */
179 static int DetectSNMPPduTypeSetup (DetectEngineCtx *de_ctx, Signature *s,
180  const char *rawstr)
181 {
182  DetectSNMPPduTypeData *dd = NULL;
183 
185  return -1;
186 
187  dd = DetectSNMPPduTypeParse(rawstr);
188  if (dd == NULL) {
189  SCLogError("Parsing \'%s\' failed", rawstr);
190  goto error;
191  }
192 
193  /* okay so far so good, lets get this into a SigMatch
194  * and put it in the Signature. */
195 
196  SCLogDebug("snmp.pdu_type %d", dd->pdu_type);
198  g_snmp_pdu_type_buffer_id) == NULL) {
199  goto error;
200  }
201  return 0;
202 
203 error:
204  DetectSNMPPduTypeFree(de_ctx, dd);
205  return -1;
206 }
207 
208 /**
209  * \internal
210  * \brief Function to free memory associated with DetectSNMPPduTypeData.
211  *
212  * \param de_ptr Pointer to DetectSNMPPduTypeData.
213  */
214 static void DetectSNMPPduTypeFree(DetectEngineCtx *de_ctx, void *ptr)
215 {
216  SCFree(ptr);
217 }
218 
219 #ifdef UNITTESTS
221 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1311
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1742
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1310
detect-snmp-pdu_type.c
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1298
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1308
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:848
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1279
rust.h
DetectSNMPPduTypeData_::pdu_type
uint32_t pdu_type
Definition: detect-snmp-pdu_type.c:41
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2663
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:267
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1293
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1087
ALPROTO_SNMP
@ ALPROTO_SNMP
Definition: app-layer-protos.h:53
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectSNMPPduTypeData
struct DetectSNMPPduTypeData_ DetectSNMPPduTypeData
DetectEngineThreadCtx_
Definition: detect.h:1104
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2789
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect.h
app-layer-parser.h
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-snmp-pdu_type.c:37
conf.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1276
DETECT_AL_SNMP_PDU_TYPE
@ DETECT_AL_SNMP_PDU_TYPE
Definition: detect-engine-register.h:300
detect-engine-content-inspection.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:345
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:2094
str
#define str(s)
Definition: suricata-common.h:291
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:602
DetectSNMPPduTypeRegister
void DetectSNMPPduTypeRegister(void)
Definition: detect-snmp-pdu_type.c:56
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:233
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:436
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
DetectSNMPPduTypeData_
Definition: detect-snmp-pdu_type.c:40
detect-snmp-pdu_type.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1300