suricata
detect-snmp-version.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-version.h"
31 #include "app-layer-parser.h"
32 #include "rust.h"
33 
34 /**
35  * [snmp.version]:[<|>|<=|>=]<version>;
36  */
37 #define PARSE_REGEX "^\\s*(<=|>=|<|>)?\\s*([0-9]+)\\s*$"
38 static DetectParseRegex parse_regex;
39 
41  PROCEDURE_EQ = 1, /* equal */
42  PROCEDURE_LT, /* less than */
43  PROCEDURE_LE, /* less than or equal */
44  PROCEDURE_GT, /* greater than */
45  PROCEDURE_GE, /* greater than or equal */
46 };
47 
48 typedef struct DetectSNMPVersionData_ {
49  uint32_t version;
52 
53 static DetectSNMPVersionData *DetectSNMPVersionParse (const char *);
54 static int DetectSNMPVersionSetup (DetectEngineCtx *, Signature *s, const char *str);
55 static void DetectSNMPVersionFree(DetectEngineCtx *, void *);
56 #ifdef UNITTESTS
57 static void DetectSNMPVersionRegisterTests(void);
58 #endif
59 static int g_snmp_version_buffer_id = 0;
60 
61 static int DetectEngineInspectSNMPRequestGeneric(ThreadVars *tv,
63  const Signature *s, const SigMatchData *smd,
64  Flow *f, uint8_t flags, void *alstate,
65  void *txv, uint64_t tx_id);
66 
67 static int DetectSNMPVersionMatch (DetectEngineThreadCtx *, Flow *,
68  uint8_t, void *, void *, const Signature *,
69  const SigMatchCtx *);
70 
71 /**
72  * \brief Registration function for snmp.procedure keyword.
73  */
75 {
76  sigmatch_table[DETECT_AL_SNMP_VERSION].name = "snmp.version";
77  sigmatch_table[DETECT_AL_SNMP_VERSION].desc = "match SNMP version";
78  sigmatch_table[DETECT_AL_SNMP_VERSION].url = "/rules/snmp-keywords.html#snmp-version";
80  sigmatch_table[DETECT_AL_SNMP_VERSION].AppLayerTxMatch = DetectSNMPVersionMatch;
81  sigmatch_table[DETECT_AL_SNMP_VERSION].Setup = DetectSNMPVersionSetup;
82  sigmatch_table[DETECT_AL_SNMP_VERSION].Free = DetectSNMPVersionFree;
83 #ifdef UNITTESTS
84  sigmatch_table[DETECT_AL_SNMP_VERSION].RegisterTests = DetectSNMPVersionRegisterTests;
85 #endif
86 
87  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
88 
91  DetectEngineInspectSNMPRequestGeneric);
92 
95  DetectEngineInspectSNMPRequestGeneric);
96 
97  g_snmp_version_buffer_id = DetectBufferTypeGetByName("snmp.version");
98 }
99 
100 static int DetectEngineInspectSNMPRequestGeneric(ThreadVars *tv,
102  const Signature *s, const SigMatchData *smd,
103  Flow *f, uint8_t flags, void *alstate,
104  void *txv, uint64_t tx_id)
105 {
106  return DetectEngineInspectGenericList(tv, de_ctx, det_ctx, s, smd,
107  f, flags, alstate, txv, tx_id);
108 }
109 
110 static inline int
111 VersionMatch(const uint32_t version,
112  enum DetectSNMPVersionMode mode, uint32_t ref_version)
113 {
114  switch (mode) {
115  case PROCEDURE_EQ:
116  if (version == ref_version)
117  SCReturnInt(1);
118  break;
119  case PROCEDURE_LT:
120  if (version < ref_version)
121  SCReturnInt(1);
122  break;
123  case PROCEDURE_LE:
124  if (version <= ref_version)
125  SCReturnInt(1);
126  break;
127  case PROCEDURE_GT:
128  if (version > ref_version)
129  SCReturnInt(1);
130  break;
131  case PROCEDURE_GE:
132  if (version >= ref_version)
133  SCReturnInt(1);
134  break;
135  }
136  SCReturnInt(0);
137 }
138 
139 /**
140  * \internal
141  * \brief Function to match version of a TX
142  *
143  * \param t Pointer to thread vars.
144  * \param det_ctx Pointer to the pattern matcher thread.
145  * \param f Pointer to the current flow.
146  * \param flags Flags.
147  * \param state App layer state.
148  * \param s Pointer to the Signature.
149  * \param m Pointer to the sigmatch that we will cast into
150  * DetectSNMPVersionData.
151  *
152  * \retval 0 no match.
153  * \retval 1 match.
154  */
155 static int DetectSNMPVersionMatch (DetectEngineThreadCtx *det_ctx,
156  Flow *f, uint8_t flags, void *state,
157  void *txv, const Signature *s,
158  const SigMatchCtx *ctx)
159 {
160  SCEnter();
161 
162  const DetectSNMPVersionData *dd = (const DetectSNMPVersionData *)ctx;
163  uint32_t version;
164  rs_snmp_tx_get_version(txv, &version);
165  SCLogDebug("version %u mode %u ref_version %d",
166  version, dd->mode, dd->version);
167  if (VersionMatch(version, dd->mode, dd->version))
168  SCReturnInt(1);
169  SCReturnInt(0);
170 }
171 
172 /**
173  * \internal
174  * \brief Function to parse options passed via snmp.version keywords.
175  *
176  * \param rawstr Pointer to the user provided options.
177  *
178  * \retval dd pointer to DetectSNMPVersionData on success.
179  * \retval NULL on failure.
180  */
181 static DetectSNMPVersionData *DetectSNMPVersionParse (const char *rawstr)
182 {
183  DetectSNMPVersionData *dd = NULL;
184  int ret = 0, res = 0;
185  int ov[MAX_SUBSTRINGS];
186  char mode[2] = "";
187  char value1[20] = "";
188  char *endptr = NULL;
189 
190  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, MAX_SUBSTRINGS);
191  if (ret < 3 || ret > 5) {
192  SCLogError(SC_ERR_PCRE_MATCH, "Parse error %s", rawstr);
193  goto error;
194  }
195 
196  res = pcre_copy_substring((char *)rawstr, ov, MAX_SUBSTRINGS, 1, mode,
197  sizeof(mode));
198  if (res < 0) {
199  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
200  goto error;
201  }
202 
203  res = pcre_copy_substring((char *)rawstr, ov, MAX_SUBSTRINGS, 2, value1,
204  sizeof(value1));
205  if (res < 0) {
206  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
207  goto error;
208  }
209 
210  dd = SCCalloc(1, sizeof(DetectSNMPVersionData));
211  if (unlikely(dd == NULL))
212  goto error;
213 
214  if (strlen(mode) == 1) {
215  if (mode[0] == '<')
216  dd->mode = PROCEDURE_LT;
217  else if (mode[0] == '>')
218  dd->mode = PROCEDURE_GT;
219  } else if (strlen(mode) == 2) {
220  if (strcmp(mode, "<=") == 0)
221  dd->mode = PROCEDURE_LE;
222  if (strcmp(mode, ">=") == 0)
223  dd->mode = PROCEDURE_GE;
224  }
225 
226  if (dd->mode == 0) {
227  dd->mode = PROCEDURE_EQ;
228  }
229 
230  /* set the first value */
231  dd->version = strtoul(value1, &endptr, 10);
232  if (endptr == NULL || *endptr != '\0') {
233  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid character as arg "
234  "to snmp.version keyword");
235  goto error;
236  }
237 
238  return dd;
239 
240 error:
241  if (dd)
242  SCFree(dd);
243  return NULL;
244 }
245 
246 
247 
248 /**
249  * \brief Function to add the parsed snmp version field into the current signature.
250  *
251  * \param de_ctx Pointer to the Detection Engine Context.
252  * \param s Pointer to the Current Signature.
253  * \param rawstr Pointer to the user provided flags options.
254  * \param type Defines if this is notBefore or notAfter.
255  *
256  * \retval 0 on Success.
257  * \retval -1 on Failure.
258  */
259 static int DetectSNMPVersionSetup (DetectEngineCtx *de_ctx, Signature *s,
260  const char *rawstr)
261 {
262  DetectSNMPVersionData *dd = NULL;
263  SigMatch *sm = NULL;
264 
266  return -1;
267 
268  dd = DetectSNMPVersionParse(rawstr);
269  if (dd == NULL) {
270  SCLogError(SC_ERR_INVALID_ARGUMENT,"Parsing \'%s\' failed", rawstr);
271  goto error;
272  }
273 
274  /* okay so far so good, lets get this into a SigMatch
275  * and put it in the Signature. */
276  sm = SigMatchAlloc();
277  if (sm == NULL)
278  goto error;
279 
281  sm->ctx = (void *)dd;
282 
283  SCLogDebug("snmp.version %d", dd->version);
284  SigMatchAppendSMToList(s, sm, g_snmp_version_buffer_id);
285  return 0;
286 
287 error:
288  DetectSNMPVersionFree(de_ctx, dd);
289  return -1;
290 }
291 
292 /**
293  * \internal
294  * \brief Function to free memory associated with DetectSNMPVersionData.
295  *
296  * \param de_ptr Pointer to DetectSNMPVersionData.
297  */
298 static void DetectSNMPVersionFree(DetectEngineCtx *de_ctx, void *ptr)
299 {
300  SCFree(ptr);
301 }
302 
303 
304 #ifdef UNITTESTS
306 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1214
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1480
DETECT_AL_SNMP_VERSION
@ DETECT_AL_SNMP_VERSION
Definition: detect-engine-register.h:267
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
DetectSNMPVersionData_::mode
enum DetectSNMPVersionMode mode
Definition: detect-snmp-version.c:50
SigTableElmt_::name
const char * name
Definition: detect.h:1211
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectSNMPVersionMode
DetectSNMPVersionMode
Definition: detect-snmp-version.c:40
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
DetectSNMPVersionData
struct DetectSNMPVersionData_ DetectSNMPVersionData
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1182
rust.h
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigMatchData_
Data needed for Match()
Definition: detect.h:329
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
DetectEngineInspectGenericList
int DetectEngineInspectGenericList(ThreadVars *tv, const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, const uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1596
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:880
ALPROTO_SNMP
@ ALPROTO_SNMP
Definition: app-layer-protos.h:52
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1010
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-snmp-version.c:37
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2493
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SC_ERR_INVALID_ARGUMENT
@ SC_ERR_INVALID_ARGUMENT
Definition: util-error.h:43
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2423
conf.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1179
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:321
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:315
PROCEDURE_LE
@ PROCEDURE_LE
Definition: detect-snmp-version.c:43
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectParseRegex_
Definition: detect-parse.h:42
DetectSNMPVersionData_::version
uint32_t version
Definition: detect-snmp-version.c:49
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
version
uint8_t version
Definition: decode-gre.h:1
PROCEDURE_EQ
@ PROCEDURE_EQ
Definition: detect-snmp-version.c:41
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
detect-snmp-version.h
str
#define str(s)
Definition: suricata-common.h:273
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
DetectSNMPVersionRegister
void DetectSNMPVersionRegister(void)
Registration function for snmp.procedure keyword.
Definition: detect-snmp-version.c:74
Signature_
Signature container.
Definition: detect.h:528
SigMatch_
a single match condition for a signature
Definition: detect.h:320
detect-snmp-version.c
DetectSNMPVersionData_
Definition: detect-snmp-version.c:48
PROCEDURE_GE
@ PROCEDURE_GE
Definition: detect-snmp-version.c:45
PROCEDURE_LT
@ PROCEDURE_LT
Definition: detect-snmp-version.c:42
PROCEDURE_GT
@ PROCEDURE_GT
Definition: detect-snmp-version.c:44
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback)
register inspect engine at start up time
Definition: detect-engine.c:171
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203