suricata
detect-ipopts.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2021 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 #ifdef UNITTESTS
49 static void IpOptsRegisterTests(void);
50 #endif
51 void DetectIpOptsFree(DetectEngineCtx *, void *);
52 
53 /**
54  * \brief Registration function for ipopts: keyword
55  */
57 {
58  sigmatch_table[DETECT_IPOPTS].name = "ipopts";
59  sigmatch_table[DETECT_IPOPTS].desc = "check if a specific IP option is set";
60  sigmatch_table[DETECT_IPOPTS].url = "/rules/header-keywords.html#ipopts";
61  sigmatch_table[DETECT_IPOPTS].Match = DetectIpOptsMatch;
62  sigmatch_table[DETECT_IPOPTS].Setup = DetectIpOptsSetup;
64 #ifdef UNITTESTS
65  sigmatch_table[DETECT_IPOPTS].RegisterTests = IpOptsRegisterTests;
66 #endif
67  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
68 }
69 
70 /**
71  * \struct DetectIpOptss_
72  * DetectIpOptss_ is used to store supported iptops values
73  */
74 
75 struct DetectIpOpts_ {
76  const char *ipopt_name; /**< ip option name */
77  uint16_t code; /**< ip option flag value */
78 } ipopts[] = {
79  {
80  "rr",
82  },
83  {
84  "lsrr",
86  },
87  {
88  "eol",
90  },
91  {
92  "nop",
94  },
95  {
96  "ts",
98  },
99  {
100  "sec",
102  },
103  {
104  "esec",
106  },
107  {
108  "ssrr",
110  },
111  {
112  "satid",
114  },
115  {
116  "any",
117  0xffff,
118  },
119  { NULL, 0 },
120 };
121 
122 /**
123  * \brief Return human readable value for ipopts flag
124  *
125  * \param flag uint16_t DetectIpOptsData ipopts flag value
126  */
127 const char *IpOptsFlagToString(uint16_t flag)
128 {
129  switch (flag) {
130  case IPV4_OPT_FLAG_RR:
131  return "rr";
132  case IPV4_OPT_FLAG_LSRR:
133  return "lsrr";
134  case IPV4_OPT_FLAG_EOL:
135  return "eol";
136  case IPV4_OPT_FLAG_NOP:
137  return "nop";
138  case IPV4_OPT_FLAG_TS:
139  return "ts";
140  case IPV4_OPT_FLAG_SEC:
141  return "sec";
142  case IPV4_OPT_FLAG_ESEC:
143  return "esec";
144  case IPV4_OPT_FLAG_SSRR:
145  return "ssrr";
146  case IPV4_OPT_FLAG_SID:
147  return "satid";
148  case 0xffff:
149  return "any";
150  default:
151  return NULL;
152  }
153 }
154 
155 /**
156  * \internal
157  * \brief This function is used to match ip option on a packet with those passed via ipopts:
158  *
159  * \param t pointer to thread vars
160  * \param det_ctx pointer to the pattern matcher thread
161  * \param p pointer to the current packet
162  * \param s pointer to the Signature
163  * \param m pointer to the sigmatch
164  *
165  * \retval 0 no match
166  * \retval 1 match
167  */
168 static int DetectIpOptsMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
169  const Signature *s, const SigMatchCtx *ctx)
170 {
171  const DetectIpOptsData *de = (const DetectIpOptsData *)ctx;
172 
173  if (!de || !PKT_IS_IPV4(p) || PKT_IS_PSEUDOPKT(p))
174  return 0;
175 
176  if (p->ip4vars.opts_set & de->ipopt) {
177  return 1;
178  }
179 
180  return 0;
181 }
182 
183 /**
184  * \internal
185  * \brief This function is used to parse ipopts options passed via ipopts: keyword
186  *
187  * \param rawstr Pointer to the user provided ipopts options
188  *
189  * \retval de pointer to DetectIpOptsData on success
190  * \retval NULL on failure
191  */
192 static DetectIpOptsData *DetectIpOptsParse (const char *rawstr)
193 {
194  int i;
195  DetectIpOptsData *de = NULL;
196  int found = 0;
197 
198  pcre2_match_data *match = NULL;
199  int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
200  if (ret < 1) {
201  SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
202  goto error;
203  }
204 
205  for(i = 0; ipopts[i].ipopt_name != NULL; i++) {
206  if((strcasecmp(ipopts[i].ipopt_name,rawstr)) == 0) {
207  found = 1;
208  break;
209  }
210  }
211 
212  if(found == 0)
213  goto error;
214 
215  de = SCMalloc(sizeof(DetectIpOptsData));
216  if (unlikely(de == NULL))
217  goto error;
218 
219  de->ipopt = ipopts[i].code;
220 
221  pcre2_match_data_free(match);
222  return de;
223 
224 error:
225  if (match) {
226  pcre2_match_data_free(match);
227  }
228  if (de) SCFree(de);
229  return NULL;
230 }
231 
232 /**
233  * \internal
234  * \brief this function is used to add the parsed ipopts into the current signature
235  *
236  * \param de_ctx pointer to the Detection Engine Context
237  * \param s pointer to the Current Signature
238  * \param rawstr pointer to the user provided ipopts options
239  *
240  * \retval 0 on Success
241  * \retval -1 on Failure
242  */
243 static int DetectIpOptsSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
244 {
245  DetectIpOptsData *de = NULL;
246 
247  de = DetectIpOptsParse(rawstr);
248  if (de == NULL)
249  goto error;
250 
252  NULL) {
253  goto error;
254  }
256 
257  return 0;
258 
259 error:
260  if (de)
261  SCFree(de);
262  return -1;
263 }
264 
265 /**
266  * \internal
267  * \brief this function will free memory associated with DetectIpOptsData
268  *
269  * \param de pointer to DetectIpOptsData
270  */
272 {
273  DetectIpOptsData *de = (DetectIpOptsData *)de_ptr;
274  if(de) SCFree(de);
275 }
276 
277 /*
278  * ONLY TESTS BELOW THIS COMMENT
279  */
280 
281 #ifdef UNITTESTS
282 /**
283  * \test IpOptsTestParse01 is a test for a valid ipopts value
284  */
285 static int IpOptsTestParse01 (void)
286 {
287  DetectIpOptsData *de = DetectIpOptsParse("lsrr");
288 
289  FAIL_IF_NULL(de);
290 
291  DetectIpOptsFree(NULL, de);
292 
293  PASS;
294 }
295 
296 /**
297  * \test IpOptsTestParse02 is a test for an invalid ipopts value
298  */
299 static int IpOptsTestParse02 (void)
300 {
301  DetectIpOptsData *de = DetectIpOptsParse("invalidopt");
302 
304 
305  DetectIpOptsFree(NULL, de);
306 
307  PASS;
308 }
309 
310 /**
311  * \test IpOptsTestParse03 test the match function on a packet that needs to match
312  */
313 static int IpOptsTestParse03 (void)
314 {
315  Packet *p = PacketGetFromAlloc();
316  FAIL_IF_NULL(p);
317  ThreadVars tv;
318  IPV4Hdr ip4h;
319 
320  memset(&tv, 0, sizeof(ThreadVars));
321  memset(&ip4h, 0, sizeof(IPV4Hdr));
322 
323  p->ip4h = &ip4h;
325 
326  DetectIpOptsData *de = DetectIpOptsParse("rr");
327  FAIL_IF_NULL(de);
328 
329  SigMatch *sm = SigMatchAlloc();
330  FAIL_IF_NULL(sm);
331 
332  sm->type = DETECT_IPOPTS;
333  sm->ctx = (SigMatchCtx *)de;
334 
335  FAIL_IF_NOT(DetectIpOptsMatch(NULL, p, NULL, sm->ctx));
336 
337  SCFree(de);
338  SCFree(sm);
339  SCFree(p);
340 
341  PASS;
342 }
343 
344 /**
345  * \test IpOptsTestParse04 test the match function on a packet that needs to not match
346  */
347 static int IpOptsTestParse04 (void)
348 {
349  Packet *p = PacketGetFromAlloc();
350  FAIL_IF_NULL(p);
351  ThreadVars tv;
352  IPV4Hdr ip4h;
353 
354  memset(&tv, 0, sizeof(ThreadVars));
355  memset(&ip4h, 0, sizeof(IPV4Hdr));
356 
357  p->ip4h = &ip4h;
359 
360  DetectIpOptsData *de = DetectIpOptsParse("lsrr");
361  FAIL_IF_NULL(de);
362 
363  SigMatch *sm = SigMatchAlloc();
364  FAIL_IF_NULL(sm);
365 
366  sm->type = DETECT_IPOPTS;
367  sm->ctx = (SigMatchCtx *)de;
368 
369  FAIL_IF(DetectIpOptsMatch(NULL, p, NULL, sm->ctx));
370 
371  SCFree(de);
372  SCFree(sm);
373  SCFree(p);
374 
375  PASS;
376 }
377 
378 /**
379  * \brief this function registers unit tests for IpOpts
380  */
381 void IpOptsRegisterTests(void)
382 {
383  UtRegisterTest("IpOptsTestParse01", IpOptsTestParse01);
384  UtRegisterTest("IpOptsTestParse02", IpOptsTestParse02);
385  UtRegisterTest("IpOptsTestParse03", IpOptsTestParse03);
386  UtRegisterTest("IpOptsTestParse04", IpOptsTestParse04);
387 }
388 #endif /* UNITTESTS */
DetectIpOpts_::ipopt_name
const char * ipopt_name
Definition: detect-ipopts.c:76
DetectIpOpts_
Definition: detect-ipopts.c:75
SigTableElmt_::url
const char * url
Definition: detect.h:1296
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1295
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1283
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1293
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1075
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:836
Packet_::ip4vars
IPV4Vars ip4vars
Definition: decode.h:551
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:56
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2674
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1278
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
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
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
IpOptsFlagToString
const char * IpOptsFlagToString(uint16_t flag)
Return human readable value for ipopts flag.
Definition: detect-ipopts.c:127
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
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:1092
de
uint8_t de
Definition: app-layer-htp.c:581
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2791
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
IPV4_OPT_FLAG_RR
@ IPV4_OPT_FLAG_RR
Definition: decode-ipv4.h:160
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:111
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:350
Signature_::flags
uint32_t flags
Definition: detect.h:594
Packet_
Definition: decode.h:437
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:545
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1261
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:333
DetectIpOptsFree
void DetectIpOptsFree(DetectEngineCtx *, void *)
Definition: detect-ipopts.c:271
decode-events.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:342
IPV4Hdr_
Definition: decode-ipv4.h:72
IPV4_OPT_FLAG_TS
@ IPV4_OPT_FLAG_TS
Definition: decode-ipv4.h:161
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:348
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
DetectIpOptsData_
Definition: detect-ipopts.h:37
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:229
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
IPV4_OPT_FLAG_ESEC
@ IPV4_OPT_FLAG_ESEC
Definition: decode-ipv4.h:169
Signature_
Signature container.
Definition: detect.h:593
SigMatch_
a single match condition for a signature
Definition: detect.h:347
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:175
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:447
DetectIpOpts_::code
uint16_t code
Definition: detect-ipopts.c:77
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:246
IPV4_OPT_FLAG_EOL
@ IPV4_OPT_FLAG_EOL
Definition: decode-ipv4.h:158
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1285
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:246