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 DetectEngineInspectSNMPRequestGeneric(ThreadVars *tv,
54  const Signature *s, const SigMatchData *smd,
55  Flow *f, uint8_t flags, void *alstate,
56  void *txv, uint64_t tx_id);
57 
58 static int DetectSNMPPduTypeMatch (DetectEngineThreadCtx *, Flow *,
59  uint8_t, void *, void *, const Signature *,
60  const SigMatchCtx *);
61 
63 {
64  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].name = "snmp.pdu_type";
65  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].desc = "match SNMP PDU type";
66  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].url = "/rules/snmp-keywords.html#snmp-pdu-type";
68  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].AppLayerTxMatch = DetectSNMPPduTypeMatch;
69  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].Setup = DetectSNMPPduTypeSetup;
70  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].Free = DetectSNMPPduTypeFree;
71 #ifdef UNITTESTS
72  sigmatch_table[DETECT_AL_SNMP_PDU_TYPE].RegisterTests = DetectSNMPPduTypeRegisterTests;
73 #endif
74 
75  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
76 
79  DetectEngineInspectSNMPRequestGeneric);
80 
83  DetectEngineInspectSNMPRequestGeneric);
84 
85  g_snmp_pdu_type_buffer_id = DetectBufferTypeGetByName("snmp.pdu_type");
86 }
87 
88 static int DetectEngineInspectSNMPRequestGeneric(ThreadVars *tv,
90  const Signature *s, const SigMatchData *smd,
91  Flow *f, uint8_t flags, void *alstate,
92  void *txv, uint64_t tx_id)
93 {
94  return DetectEngineInspectGenericList(tv, de_ctx, det_ctx, s, smd,
95  f, flags, alstate, txv, tx_id);
96 }
97 
98 /**
99  * \internal
100  * \brief Function to match pdu_type of a TX
101  *
102  * \param t Pointer to thread vars.
103  * \param det_ctx Pointer to the pattern matcher thread.
104  * \param f Pointer to the current flow.
105  * \param flags Flags.
106  * \param state App layer state.
107  * \param s Pointer to the Signature.
108  * \param m Pointer to the sigmatch that we will cast into
109  * DetectSNMPPduTypeData.
110  *
111  * \retval 0 no match.
112  * \retval 1 match.
113  */
114 static int DetectSNMPPduTypeMatch (DetectEngineThreadCtx *det_ctx,
115  Flow *f, uint8_t flags, void *state,
116  void *txv, const Signature *s,
117  const SigMatchCtx *ctx)
118 {
119  SCEnter();
120 
121  const DetectSNMPPduTypeData *dd = (const DetectSNMPPduTypeData *)ctx;
122  uint32_t pdu_type;
123  rs_snmp_tx_get_pdu_type(txv, &pdu_type);
124  SCLogDebug("pdu_type %u ref_pdu_type %d",
125  pdu_type, dd->pdu_type);
126  if (pdu_type == dd->pdu_type)
127  SCReturnInt(1);
128  SCReturnInt(0);
129 }
130 
131 /**
132  * \internal
133  * \brief Function to parse options passed via snmp.pdu_type keywords.
134  *
135  * \param rawstr Pointer to the user provided options.
136  *
137  * \retval dd pointer to DetectSNMPPduTypeData on success.
138  * \retval NULL on failure.
139  */
140 static DetectSNMPPduTypeData *DetectSNMPPduTypeParse (const char *rawstr)
141 {
142  DetectSNMPPduTypeData *dd = NULL;
143  int ret = 0, res = 0;
144  int ov[MAX_SUBSTRINGS];
145  char value1[20] = "";
146  char *endptr = NULL;
147 
148  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, MAX_SUBSTRINGS);
149  if (ret != 2) {
150  SCLogError(SC_ERR_PCRE_MATCH, "Parse error %s", rawstr);
151  goto error;
152  }
153 
154  res = pcre_copy_substring((char *)rawstr, ov, MAX_SUBSTRINGS, 1, value1,
155  sizeof(value1));
156  if (res < 0) {
157  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
158  goto error;
159  }
160 
161  dd = SCCalloc(1, sizeof(DetectSNMPPduTypeData));
162  if (unlikely(dd == NULL))
163  goto error;
164 
165  /* set the value */
166  dd->pdu_type = strtoul(value1, &endptr, 10);
167  if (endptr == NULL || *endptr != '\0') {
168  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid character as arg "
169  "to snmp.pdu_type keyword");
170  goto error;
171  }
172 
173  return dd;
174 
175 error:
176  if (dd)
177  SCFree(dd);
178  return NULL;
179 }
180 
181 /**
182  * \brief Function to add the parsed snmp pdu_type field into the current signature.
183  *
184  * \param de_ctx Pointer to the Detection Engine Context.
185  * \param s Pointer to the Current Signature.
186  * \param rawstr Pointer to the user provided flags options.
187  * \param type Defines if this is notBefore or notAfter.
188  *
189  * \retval 0 on Success.
190  * \retval -1 on Failure.
191  */
192 static int DetectSNMPPduTypeSetup (DetectEngineCtx *de_ctx, Signature *s,
193  const char *rawstr)
194 {
195  DetectSNMPPduTypeData *dd = NULL;
196  SigMatch *sm = NULL;
197 
199  return -1;
200 
201  dd = DetectSNMPPduTypeParse(rawstr);
202  if (dd == NULL) {
203  SCLogError(SC_ERR_INVALID_ARGUMENT,"Parsing \'%s\' failed", rawstr);
204  goto error;
205  }
206 
207  /* okay so far so good, lets get this into a SigMatch
208  * and put it in the Signature. */
209  sm = SigMatchAlloc();
210  if (sm == NULL)
211  goto error;
212 
214  sm->ctx = (void *)dd;
215 
216  SCLogDebug("snmp.pdu_type %d", dd->pdu_type);
217  SigMatchAppendSMToList(s, sm, g_snmp_pdu_type_buffer_id);
218  return 0;
219 
220 error:
221  DetectSNMPPduTypeFree(de_ctx, dd);
222  return -1;
223 }
224 
225 /**
226  * \internal
227  * \brief Function to free memory associated with DetectSNMPPduTypeData.
228  *
229  * \param de_ptr Pointer to DetectSNMPPduTypeData.
230  */
231 static void DetectSNMPPduTypeFree(DetectEngineCtx *de_ctx, void *ptr)
232 {
233  SCFree(ptr);
234 }
235 
236 #ifdef UNITTESTS
238 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1212
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1471
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1211
detect-snmp-pdu_type.c
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1200
SigTableElmt_::name
const char * name
Definition: detect.h:1209
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
Flow_
Flow data structure.
Definition: flow.h:343
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
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:1181
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:237
SigMatchData_
Data needed for Match()
Definition: detect.h:328
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1195
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:1595
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:879
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
DetectSNMPPduTypeData
struct DetectSNMPPduTypeData_ DetectSNMPPduTypeData
DetectEngineThreadCtx_
Definition: detect.h:1009
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2472
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:322
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2402
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:1178
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
DETECT_AL_SNMP_PDU_TYPE
@ DETECT_AL_SNMP_PDU_TYPE
Definition: detect-engine-register.h:260
SigMatch_::type
uint8_t type
Definition: detect.h:320
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:314
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
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
str
#define str(s)
Definition: suricata-common.h:273
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:527
SigMatch_
a single match condition for a signature
Definition: detect.h:319
DetectSNMPPduTypeRegister
void DetectSNMPPduTypeRegister(void)
Definition: detect-snmp-pdu_type.c:62
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:170
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
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:349
detect-snmp-pdu_type.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1201