suricata
detect-ipopts.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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 Breno Silva <breno.silva@gmail.com>
22  *
23  * Implements the ipopts keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "suricata.h"
28 #include "decode.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
32 
33 #include "flow-var.h"
34 #include "decode-events.h"
35 
36 #include "util-debug.h"
37 
38 #include "detect-ipopts.h"
39 #include "util-unittest.h"
40 
41 #define PARSE_REGEX "\\S[A-z]"
42 
43 static DetectParseRegex parse_regex;
44 
45 static int DetectIpOptsMatch (DetectEngineThreadCtx *, Packet *,
46  const Signature *, const SigMatchCtx *);
47 static int DetectIpOptsSetup (DetectEngineCtx *, Signature *, const char *);
48 void IpOptsRegisterTests(void);
49 void DetectIpOptsFree(DetectEngineCtx *, void *);
50 
51 /**
52  * \brief Registration function for ipopts: keyword
53  */
55 {
56  sigmatch_table[DETECT_IPOPTS].name = "ipopts";
57  sigmatch_table[DETECT_IPOPTS].desc = "check if a specific IP option is set";
58  sigmatch_table[DETECT_IPOPTS].url = "/rules/header-keywords.html#ipopts";
59  sigmatch_table[DETECT_IPOPTS].Match = DetectIpOptsMatch;
60  sigmatch_table[DETECT_IPOPTS].Setup = DetectIpOptsSetup;
63 
64  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
65 }
66 
67 /**
68  * \struct DetectIpOptss_
69  * DetectIpOptss_ is used to store supported iptops values
70  */
71 
72 struct DetectIpOpts_ {
73  const char *ipopt_name; /**< ip option name */
74  uint16_t code; /**< ip option flag value */
75 } ipopts[] = {
76  { "rr", IPV4_OPT_FLAG_RR, },
77  { "lsrr", IPV4_OPT_FLAG_LSRR, },
78  { "eol", IPV4_OPT_FLAG_EOL, },
79  { "nop", IPV4_OPT_FLAG_NOP, },
80  { "ts", IPV4_OPT_FLAG_TS, },
81  { "sec", IPV4_OPT_FLAG_SEC, },
82  { "ssrr", IPV4_OPT_FLAG_SSRR, },
83  { "satid", IPV4_OPT_FLAG_SID, },
84  { "any", 0xffff, },
85  { NULL, 0 },
86 };
87 
88 /**
89  * \internal
90  * \brief This function is used to match ip option on a packet with those passed via ipopts:
91  *
92  * \param t pointer to thread vars
93  * \param det_ctx pointer to the pattern matcher thread
94  * \param p pointer to the current packet
95  * \param s pointer to the Signature
96  * \param m pointer to the sigmatch
97  *
98  * \retval 0 no match
99  * \retval 1 match
100  */
101 static int DetectIpOptsMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
102  const Signature *s, const SigMatchCtx *ctx)
103 {
104  const DetectIpOptsData *de = (const DetectIpOptsData *)ctx;
105 
106  if (!de || !PKT_IS_IPV4(p) || PKT_IS_PSEUDOPKT(p))
107  return 0;
108 
109  if (p->ip4vars.opts_set & de->ipopt) {
110  return 1;
111  }
112 
113  return 0;
114 }
115 
116 /**
117  * \internal
118  * \brief This function is used to parse ipopts options passed via ipopts: keyword
119  *
120  * \param rawstr Pointer to the user provided ipopts options
121  *
122  * \retval de pointer to DetectIpOptsData on success
123  * \retval NULL on failure
124  */
125 static DetectIpOptsData *DetectIpOptsParse (const char *rawstr)
126 {
127  int i;
128  DetectIpOptsData *de = NULL;
129  int ret = 0, found = 0;
130  int ov[MAX_SUBSTRINGS];
131 
132  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, MAX_SUBSTRINGS);
133  if (ret < 1) {
134  SCLogError(SC_ERR_PCRE_MATCH, "pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
135  goto error;
136  }
137 
138  for(i = 0; ipopts[i].ipopt_name != NULL; i++) {
139  if((strcasecmp(ipopts[i].ipopt_name,rawstr)) == 0) {
140  found = 1;
141  break;
142  }
143  }
144 
145  if(found == 0)
146  goto error;
147 
148  de = SCMalloc(sizeof(DetectIpOptsData));
149  if (unlikely(de == NULL))
150  goto error;
151 
152  de->ipopt = ipopts[i].code;
153 
154  return de;
155 
156 error:
157  if (de) SCFree(de);
158  return NULL;
159 }
160 
161 /**
162  * \internal
163  * \brief this function is used to add the parsed ipopts into the current signature
164  *
165  * \param de_ctx pointer to the Detection Engine Context
166  * \param s pointer to the Current Signature
167  * \param rawstr pointer to the user provided ipopts options
168  *
169  * \retval 0 on Success
170  * \retval -1 on Failure
171  */
172 static int DetectIpOptsSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
173 {
174  DetectIpOptsData *de = NULL;
175  SigMatch *sm = NULL;
176 
177  de = DetectIpOptsParse(rawstr);
178  if (de == NULL)
179  goto error;
180 
181  sm = SigMatchAlloc();
182  if (sm == NULL)
183  goto error;
184 
185  sm->type = DETECT_IPOPTS;
186  sm->ctx = (SigMatchCtx *)de;
187 
190 
191  return 0;
192 
193 error:
194  if (de) SCFree(de);
195  if (sm) SCFree(sm);
196  return -1;
197 }
198 
199 /**
200  * \internal
201  * \brief this function will free memory associated with DetectIpOptsData
202  *
203  * \param de pointer to DetectIpOptsData
204  */
206 {
207  DetectIpOptsData *de = (DetectIpOptsData *)de_ptr;
208  if(de) SCFree(de);
209 }
210 
211 /*
212  * ONLY TESTS BELOW THIS COMMENT
213  */
214 
215 #ifdef UNITTESTS
216 /**
217  * \test IpOptsTestParse01 is a test for a valid ipopts value
218  *
219  * \retval 1 on succces
220  * \retval 0 on failure
221  */
222 static int IpOptsTestParse01 (void)
223 {
224  DetectIpOptsData *de = NULL;
225  de = DetectIpOptsParse("lsrr");
226  if (de) {
227  DetectIpOptsFree(NULL, de);
228  return 1;
229  }
230 
231  return 0;
232 }
233 
234 /**
235  * \test IpOptsTestParse02 is a test for an invalid ipopts value
236  *
237  * \retval 1 on succces
238  * \retval 0 on failure
239  */
240 static int IpOptsTestParse02 (void)
241 {
242  DetectIpOptsData *de = NULL;
243  de = DetectIpOptsParse("invalidopt");
244  if (de) {
245  DetectIpOptsFree(NULL, de);
246  return 0;
247  }
248 
249  return 1;
250 }
251 
252 /**
253  * \test IpOptsTestParse03 test the match function on a packet that needs to match
254  *
255  * \retval 1 on succces
256  * \retval 0 on failure
257  */
258 static int IpOptsTestParse03 (void)
259 {
261  if (unlikely(p == NULL))
262  return 0;
263  ThreadVars tv;
264  int ret = 0;
265  DetectIpOptsData *de = NULL;
266  SigMatch *sm = NULL;
267  IPV4Hdr ip4h;
268 
269  memset(&tv, 0, sizeof(ThreadVars));
270  memset(p, 0, SIZE_OF_PACKET);
271  memset(&ip4h, 0, sizeof(IPV4Hdr));
272 
273  p->ip4h = &ip4h;
275 
276  de = DetectIpOptsParse("rr");
277 
278  if (de == NULL)
279  goto error;
280 
281  sm = SigMatchAlloc();
282  if (sm == NULL)
283  goto error;
284 
285  sm->type = DETECT_IPOPTS;
286  sm->ctx = (SigMatchCtx *)de;
287 
288  ret = DetectIpOptsMatch(NULL, p, NULL, sm->ctx);
289 
290  if(ret) {
291  SCFree(p);
292  return 1;
293  }
294 
295 error:
296  if (de) SCFree(de);
297  if (sm) SCFree(sm);
298  SCFree(p);
299  return 0;
300 }
301 
302 /**
303  * \test IpOptsTestParse04 test the match function on a packet that needs to not match
304  *
305  * \retval 1 on succces
306  * \retval 0 on failure
307  */
308 static int IpOptsTestParse04 (void)
309 {
311  if (unlikely(p == NULL))
312  return 0;
313  ThreadVars tv;
314  int ret = 0;
315  DetectIpOptsData *de = NULL;
316  SigMatch *sm = NULL;
317  IPV4Hdr ip4h;
318 
319  memset(&tv, 0, sizeof(ThreadVars));
320  memset(p, 0, SIZE_OF_PACKET);
321  memset(&ip4h, 0, sizeof(IPV4Hdr));
322 
323  p->ip4h = &ip4h;
325 
326  de = DetectIpOptsParse("lsrr");
327 
328  if (de == NULL)
329  goto error;
330 
331  sm = SigMatchAlloc();
332  if (sm == NULL)
333  goto error;
334 
335  sm->type = DETECT_IPOPTS;
336  sm->ctx = (SigMatchCtx *)de;
337 
338  ret = DetectIpOptsMatch(NULL, p, NULL, sm->ctx);
339 
340  if(ret) {
341  SCFree(p);
342  return 0;
343  }
344 
345  /* Error expected. */
346 error:
347  if (de) SCFree(de);
348  if (sm) SCFree(sm);
349  SCFree(p);
350  return 1;
351 }
352 #endif /* UNITTESTS */
353 
354 /**
355  * \brief this function registers unit tests for IpOpts
356  */
358 {
359 #ifdef UNITTESTS
360  UtRegisterTest("IpOptsTestParse01", IpOptsTestParse01);
361  UtRegisterTest("IpOptsTestParse02", IpOptsTestParse02);
362  UtRegisterTest("IpOptsTestParse03", IpOptsTestParse03);
363  UtRegisterTest("IpOptsTestParse04", IpOptsTestParse04);
364 #endif /* UNITTESTS */
365 }
DetectIpOpts_::ipopt_name
const char * ipopt_name
Definition: detect-ipopts.c:73
DetectIpOpts_
Definition: detect-ipopts.c:72
SigTableElmt_::url
const char * url
Definition: detect.h:1212
SigTableElmt_::desc
const char * desc
Definition: detect.h:1211
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1200
SigTableElmt_::name
const char * name
Definition: detect.h:1209
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1125
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DETECT_IPOPTS
@ DETECT_IPOPTS
Definition: detect-engine-register.h:39
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
Packet_::ip4vars
IPV4Vars ip4vars
Definition: decode.h:509
IPV4_OPT_FLAG_SEC
@ IPV4_OPT_FLAG_SEC
Definition: decode-ipv4.h:166
DetectIpOptsRegister
void DetectIpOptsRegister(void)
Registration function for ipopts: keyword.
Definition: detect-ipopts.c:54
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1195
util-unittest.h
ipopts
struct DetectIpOpts_ ipopts[]
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-ipopts.c:41
IPV4_OPT_FLAG_NOP
@ IPV4_OPT_FLAG_NOP
Definition: decode-ipv4.h:159
decode.h
util-debug.h
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
IPV4_OPT_FLAG_SID
@ IPV4_OPT_FLAG_SID
Definition: decode-ipv4.h:165
DetectEngineThreadCtx_
Definition: detect.h:1009
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2456
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
de
int de
Definition: app-layer-htp.c:572
IPV4_OPT_FLAG_RR
@ IPV4_OPT_FLAG_RR
Definition: decode-ipv4.h:160
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:89
SIZE_OF_PACKET
#define SIZE_OF_PACKET
Definition: decode.h:621
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:322
Signature_::flags
uint32_t flags
Definition: detect.h:528
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2388
Packet_
Definition: decode.h:411
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:503
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1178
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
DetectIpOptsFree
void DetectIpOptsFree(DetectEngineCtx *, void *)
Definition: detect-ipopts.c:205
decode-events.h
SigMatch_::type
uint8_t type
Definition: detect.h:320
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:314
IPV4Hdr_
Definition: decode-ipv4.h:71
IpOptsRegisterTests
void IpOptsRegisterTests(void)
this function registers unit tests for IpOpts
Definition: detect-ipopts.c:357
IPV4_OPT_FLAG_TS
@ IPV4_OPT_FLAG_TS
Definition: decode-ipv4.h:161
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
DetectIpOptsData_
Definition: detect-ipopts.h:40
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
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
IPV4_OPT_FLAG_SSRR
@ IPV4_OPT_FLAG_SSRR
Definition: decode-ipv4.h:164
detect-ipopts.h
suricata.h
IPV4Vars_::opts_set
uint16_t opts_set
Definition: decode-ipv4.h:177
DetectIpOpts_::code
uint16_t code
Definition: detect-ipopts.c:74
IPV4_OPT_FLAG_LSRR
@ IPV4_OPT_FLAG_LSRR
Definition: decode-ipv4.h:163
flow-var.h
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:254
IPV4_OPT_FLAG_EOL
@ IPV4_OPT_FLAG_EOL
Definition: decode-ipv4.h:158
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:1201
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223