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(DetectEngineCtx *de_ctx,
62  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
63  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
64 
65 static int DetectSNMPVersionMatch (DetectEngineThreadCtx *, Flow *,
66  uint8_t, void *, void *, const Signature *,
67  const SigMatchCtx *);
68 
69 /**
70  * \brief Registration function for snmp.procedure keyword.
71  */
73 {
74  sigmatch_table[DETECT_AL_SNMP_VERSION].name = "snmp.version";
75  sigmatch_table[DETECT_AL_SNMP_VERSION].desc = "match SNMP version";
76  sigmatch_table[DETECT_AL_SNMP_VERSION].url = "/rules/snmp-keywords.html#snmp-version";
78  sigmatch_table[DETECT_AL_SNMP_VERSION].AppLayerTxMatch = DetectSNMPVersionMatch;
79  sigmatch_table[DETECT_AL_SNMP_VERSION].Setup = DetectSNMPVersionSetup;
80  sigmatch_table[DETECT_AL_SNMP_VERSION].Free = DetectSNMPVersionFree;
81 #ifdef UNITTESTS
82  sigmatch_table[DETECT_AL_SNMP_VERSION].RegisterTests = DetectSNMPVersionRegisterTests;
83 #endif
84 
85  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
86 
88  DetectEngineInspectSNMPRequestGeneric, NULL);
89 
91  DetectEngineInspectSNMPRequestGeneric, NULL);
92 
93  g_snmp_version_buffer_id = DetectBufferTypeGetByName("snmp.version");
94 }
95 
96 static int DetectEngineInspectSNMPRequestGeneric(DetectEngineCtx *de_ctx,
97  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
98  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
99 {
101  de_ctx, det_ctx, s, engine->smd, f, flags, alstate, txv, tx_id);
102 }
103 
104 static inline int
105 VersionMatch(const uint32_t version,
106  enum DetectSNMPVersionMode mode, uint32_t ref_version)
107 {
108  switch (mode) {
109  case PROCEDURE_EQ:
110  if (version == ref_version)
111  SCReturnInt(1);
112  break;
113  case PROCEDURE_LT:
114  if (version < ref_version)
115  SCReturnInt(1);
116  break;
117  case PROCEDURE_LE:
118  if (version <= ref_version)
119  SCReturnInt(1);
120  break;
121  case PROCEDURE_GT:
122  if (version > ref_version)
123  SCReturnInt(1);
124  break;
125  case PROCEDURE_GE:
126  if (version >= ref_version)
127  SCReturnInt(1);
128  break;
129  }
130  SCReturnInt(0);
131 }
132 
133 /**
134  * \internal
135  * \brief Function to match version of a TX
136  *
137  * \param t Pointer to thread vars.
138  * \param det_ctx Pointer to the pattern matcher thread.
139  * \param f Pointer to the current flow.
140  * \param flags Flags.
141  * \param state App layer state.
142  * \param s Pointer to the Signature.
143  * \param m Pointer to the sigmatch that we will cast into
144  * DetectSNMPVersionData.
145  *
146  * \retval 0 no match.
147  * \retval 1 match.
148  */
149 static int DetectSNMPVersionMatch (DetectEngineThreadCtx *det_ctx,
150  Flow *f, uint8_t flags, void *state,
151  void *txv, const Signature *s,
152  const SigMatchCtx *ctx)
153 {
154  SCEnter();
155 
156  const DetectSNMPVersionData *dd = (const DetectSNMPVersionData *)ctx;
157  uint32_t version;
158  rs_snmp_tx_get_version(txv, &version);
159  SCLogDebug("version %u mode %u ref_version %d",
160  version, dd->mode, dd->version);
161  if (VersionMatch(version, dd->mode, dd->version))
162  SCReturnInt(1);
163  SCReturnInt(0);
164 }
165 
166 /**
167  * \internal
168  * \brief Function to parse options passed via snmp.version keywords.
169  *
170  * \param rawstr Pointer to the user provided options.
171  *
172  * \retval dd pointer to DetectSNMPVersionData on success.
173  * \retval NULL on failure.
174  */
175 static DetectSNMPVersionData *DetectSNMPVersionParse (const char *rawstr)
176 {
177  DetectSNMPVersionData *dd = NULL;
178  int ret = 0, res = 0;
179  size_t pcre2len;
180  char mode[2] = "";
181  char value1[20] = "";
182  char *endptr = NULL;
183 
184  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
185  if (ret < 3 || ret > 5) {
186  SCLogError(SC_ERR_PCRE_MATCH, "Parse error %s", rawstr);
187  goto error;
188  }
189 
190  pcre2len = sizeof(mode);
191  res = SC_Pcre2SubstringCopy(parse_regex.match, 1, (PCRE2_UCHAR8 *)mode, &pcre2len);
192  if (res < 0) {
193  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_copy_bynumber failed");
194  goto error;
195  }
196 
197  pcre2len = sizeof(value1);
198  res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)value1, &pcre2len);
199  if (res < 0) {
200  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_copy_bynumber failed");
201  goto error;
202  }
203 
204  dd = SCCalloc(1, sizeof(DetectSNMPVersionData));
205  if (unlikely(dd == NULL))
206  goto error;
207 
208  if (strlen(mode) == 1) {
209  if (mode[0] == '<')
210  dd->mode = PROCEDURE_LT;
211  else if (mode[0] == '>')
212  dd->mode = PROCEDURE_GT;
213  } else if (strlen(mode) == 2) {
214  if (strcmp(mode, "<=") == 0)
215  dd->mode = PROCEDURE_LE;
216  if (strcmp(mode, ">=") == 0)
217  dd->mode = PROCEDURE_GE;
218  }
219 
220  if (dd->mode == 0) {
221  dd->mode = PROCEDURE_EQ;
222  }
223 
224  /* set the first value */
225  dd->version = strtoul(value1, &endptr, 10);
226  if (endptr == NULL || *endptr != '\0') {
227  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid character as arg "
228  "to snmp.version keyword");
229  goto error;
230  }
231 
232  return dd;
233 
234 error:
235  if (dd)
236  SCFree(dd);
237  return NULL;
238 }
239 
240 
241 
242 /**
243  * \brief Function to add the parsed snmp version field into the current signature.
244  *
245  * \param de_ctx Pointer to the Detection Engine Context.
246  * \param s Pointer to the Current Signature.
247  * \param rawstr Pointer to the user provided flags options.
248  * \param type Defines if this is notBefore or notAfter.
249  *
250  * \retval 0 on Success.
251  * \retval -1 on Failure.
252  */
253 static int DetectSNMPVersionSetup (DetectEngineCtx *de_ctx, Signature *s,
254  const char *rawstr)
255 {
256  DetectSNMPVersionData *dd = NULL;
257  SigMatch *sm = NULL;
258 
260  return -1;
261 
262  dd = DetectSNMPVersionParse(rawstr);
263  if (dd == NULL) {
264  SCLogError(SC_ERR_INVALID_ARGUMENT,"Parsing \'%s\' failed", rawstr);
265  goto error;
266  }
267 
268  /* okay so far so good, lets get this into a SigMatch
269  * and put it in the Signature. */
270  sm = SigMatchAlloc();
271  if (sm == NULL)
272  goto error;
273 
275  sm->ctx = (void *)dd;
276 
277  SCLogDebug("snmp.version %d", dd->version);
278  SigMatchAppendSMToList(s, sm, g_snmp_version_buffer_id);
279  return 0;
280 
281 error:
282  DetectSNMPVersionFree(de_ctx, dd);
283  return -1;
284 }
285 
286 /**
287  * \internal
288  * \brief Function to free memory associated with DetectSNMPVersionData.
289  *
290  * \param de_ptr Pointer to DetectSNMPVersionData.
291  */
292 static void DetectSNMPVersionFree(DetectEngineCtx *de_ctx, void *ptr)
293 {
294  SCFree(ptr);
295 }
296 
297 
298 #ifdef UNITTESTS
300 #endif /* UNITTESTS */
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:45
DetectEngineAppInspectionEngine_
Definition: detect.h:398
SigTableElmt_::url
const char * url
Definition: detect.h:1270
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
DETECT_AL_SNMP_VERSION
@ DETECT_AL_SNMP_VERSION
Definition: detect-engine-register.h:270
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2474
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1257
DetectEngineInspectGenericList
int DetectEngineInspectGenericList(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:1941
DetectParseRegex
Definition: detect-parse.h:42
DetectSNMPVersionData_::mode
enum DetectSNMPVersionMode mode
Definition: detect-snmp-version.c:50
SigTableElmt_::name
const char * name
Definition: detect.h:1267
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:353
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
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:1238
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
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1077
ALPROTO_SNMP
@ ALPROTO_SNMP
Definition: app-layer-protos.h:53
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:1060
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:2597
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
SC_ERR_INVALID_ARGUMENT
@ SC_ERR_INVALID_ARGUMENT
Definition: util-error.h:43
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:324
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:225
conf.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1235
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
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:316
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:415
PROCEDURE_LE
@ PROCEDURE_LE
Definition: detect-snmp-version.c:43
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:322
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
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
detect-snmp-version.h
str
#define str(s)
Definition: suricata-common.h:272
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:72
Signature_
Signature container.
Definition: detect.h:548
SigMatch_
a single match condition for a signature
Definition: detect.h:321
detect-snmp-version.c
SC_Pcre2SubstringCopy
int SC_Pcre2SubstringCopy(pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
Definition: detect-parse.c:2573
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
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:1259