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 ret = 0, res = 0;
126  size_t pcre2len;
127  char value1[20] = "";
128  char *endptr = NULL;
129 
130  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
131  if (ret != 2) {
132  SCLogError(SC_ERR_PCRE_MATCH, "Parse error %s", rawstr);
133  goto error;
134  }
135 
136  pcre2len = sizeof(value1);
137  res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)value1, &pcre2len);
138  if (res < 0) {
139  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_copy_bynumber failed");
140  goto error;
141  }
142 
143  dd = SCCalloc(1, sizeof(DetectSNMPPduTypeData));
144  if (unlikely(dd == NULL))
145  goto error;
146 
147  /* set the value */
148  dd->pdu_type = strtoul(value1, &endptr, 10);
149  if (endptr == NULL || *endptr != '\0') {
150  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid character as arg "
151  "to snmp.pdu_type keyword");
152  goto error;
153  }
154 
155  return dd;
156 
157 error:
158  if (dd)
159  SCFree(dd);
160  return NULL;
161 }
162 
163 /**
164  * \brief Function to add the parsed snmp pdu_type field into the current signature.
165  *
166  * \param de_ctx Pointer to the Detection Engine Context.
167  * \param s Pointer to the Current Signature.
168  * \param rawstr Pointer to the user provided flags options.
169  * \param type Defines if this is notBefore or notAfter.
170  *
171  * \retval 0 on Success.
172  * \retval -1 on Failure.
173  */
174 static int DetectSNMPPduTypeSetup (DetectEngineCtx *de_ctx, Signature *s,
175  const char *rawstr)
176 {
177  DetectSNMPPduTypeData *dd = NULL;
178  SigMatch *sm = NULL;
179 
181  return -1;
182 
183  dd = DetectSNMPPduTypeParse(rawstr);
184  if (dd == NULL) {
185  SCLogError(SC_ERR_INVALID_ARGUMENT,"Parsing \'%s\' failed", rawstr);
186  goto error;
187  }
188 
189  /* okay so far so good, lets get this into a SigMatch
190  * and put it in the Signature. */
191  sm = SigMatchAlloc();
192  if (sm == NULL)
193  goto error;
194 
196  sm->ctx = (void *)dd;
197 
198  SCLogDebug("snmp.pdu_type %d", dd->pdu_type);
199  SigMatchAppendSMToList(s, sm, g_snmp_pdu_type_buffer_id);
200  return 0;
201 
202 error:
203  DetectSNMPPduTypeFree(de_ctx, dd);
204  return -1;
205 }
206 
207 /**
208  * \internal
209  * \brief Function to free memory associated with DetectSNMPPduTypeData.
210  *
211  * \param de_ptr Pointer to DetectSNMPPduTypeData.
212  */
213 static void DetectSNMPPduTypeFree(DetectEngineCtx *de_ctx, void *ptr)
214 {
215  SCFree(ptr);
216 }
217 
218 #ifdef UNITTESTS
220 #endif /* UNITTESTS */
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:47
SigTableElmt_::url
const char * url
Definition: detect.h:1238
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1493
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1237
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2488
detect-snmp-pdu_type.c
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1225
DetectParseRegex
Definition: detect-parse.h:44
SigTableElmt_::name
const char * name
Definition: detect.h:1235
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:784
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:1206
rust.h
DetectSNMPPduTypeData_::pdu_type
uint32_t pdu_type
Definition: detect-snmp-pdu_type.c:41
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:229
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1220
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1086
ALPROTO_SNMP
@ ALPROTO_SNMP
Definition: app-layer-protos.h:53
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:228
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectSNMPPduTypeData
struct DetectSNMPPduTypeData_ DetectSNMPPduTypeData
DetectEngineThreadCtx_
Definition: detect.h:1024
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2611
SCEnter
#define SCEnter(...)
Definition: util-debug.h:298
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:316
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-snmp-pdu_type.c:37
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:228
conf.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1203
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:238
DETECT_AL_SNMP_PDU_TYPE
@ DETECT_AL_SNMP_PDU_TYPE
Definition: detect-engine-register.h:287
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:308
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
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255
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:1951
str
#define str(s)
Definition: suricata-common.h:280
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:539
SigMatch_
a single match condition for a signature
Definition: detect.h:313
DetectSNMPPduTypeRegister
void DetectSNMPPduTypeRegister(void)
Definition: detect-snmp-pdu_type.c:56
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:302
DetectSNMPPduTypeData_
Definition: detect-snmp-pdu_type.c:40
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:352
detect-snmp-pdu_type.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1227