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