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  SigMatch *sm = NULL;
247 
248  de = DetectIpOptsParse(rawstr);
249  if (de == NULL)
250  goto error;
251 
252  sm = SigMatchAlloc();
253  if (sm == NULL)
254  goto error;
255 
256  sm->type = DETECT_IPOPTS;
257  sm->ctx = (SigMatchCtx *)de;
258 
261 
262  return 0;
263 
264 error:
265  if (de) SCFree(de);
266  if (sm) SCFree(sm);
267  return -1;
268 }
269 
270 /**
271  * \internal
272  * \brief this function will free memory associated with DetectIpOptsData
273  *
274  * \param de pointer to DetectIpOptsData
275  */
277 {
278  DetectIpOptsData *de = (DetectIpOptsData *)de_ptr;
279  if(de) SCFree(de);
280 }
281 
282 /*
283  * ONLY TESTS BELOW THIS COMMENT
284  */
285 
286 #ifdef UNITTESTS
287 /**
288  * \test IpOptsTestParse01 is a test for a valid ipopts value
289  */
290 static int IpOptsTestParse01 (void)
291 {
292  DetectIpOptsData *de = DetectIpOptsParse("lsrr");
293 
294  FAIL_IF_NULL(de);
295 
296  DetectIpOptsFree(NULL, de);
297 
298  PASS;
299 }
300 
301 /**
302  * \test IpOptsTestParse02 is a test for an invalid ipopts value
303  */
304 static int IpOptsTestParse02 (void)
305 {
306  DetectIpOptsData *de = DetectIpOptsParse("invalidopt");
307 
309 
310  DetectIpOptsFree(NULL, de);
311 
312  PASS;
313 }
314 
315 /**
316  * \test IpOptsTestParse03 test the match function on a packet that needs to match
317  */
318 static int IpOptsTestParse03 (void)
319 {
320  Packet *p = PacketGetFromAlloc();
321  FAIL_IF_NULL(p);
322  ThreadVars tv;
323  IPV4Hdr ip4h;
324 
325  memset(&tv, 0, sizeof(ThreadVars));
326  memset(&ip4h, 0, sizeof(IPV4Hdr));
327 
328  p->ip4h = &ip4h;
330 
331  DetectIpOptsData *de = DetectIpOptsParse("rr");
332  FAIL_IF_NULL(de);
333 
334  SigMatch *sm = SigMatchAlloc();
335  FAIL_IF_NULL(sm);
336 
337  sm->type = DETECT_IPOPTS;
338  sm->ctx = (SigMatchCtx *)de;
339 
340  FAIL_IF_NOT(DetectIpOptsMatch(NULL, p, NULL, sm->ctx));
341 
342  SCFree(de);
343  SCFree(sm);
344  SCFree(p);
345 
346  PASS;
347 }
348 
349 /**
350  * \test IpOptsTestParse04 test the match function on a packet that needs to not match
351  */
352 static int IpOptsTestParse04 (void)
353 {
354  Packet *p = PacketGetFromAlloc();
355  FAIL_IF_NULL(p);
356  ThreadVars tv;
357  IPV4Hdr ip4h;
358 
359  memset(&tv, 0, sizeof(ThreadVars));
360  memset(&ip4h, 0, sizeof(IPV4Hdr));
361 
362  p->ip4h = &ip4h;
364 
365  DetectIpOptsData *de = DetectIpOptsParse("lsrr");
366  FAIL_IF_NULL(de);
367 
368  SigMatch *sm = SigMatchAlloc();
369  FAIL_IF_NULL(sm);
370 
371  sm->type = DETECT_IPOPTS;
372  sm->ctx = (SigMatchCtx *)de;
373 
374  FAIL_IF(DetectIpOptsMatch(NULL, p, NULL, sm->ctx));
375 
376  SCFree(de);
377  SCFree(sm);
378  SCFree(p);
379 
380  PASS;
381 }
382 
383 /**
384  * \brief this function registers unit tests for IpOpts
385  */
386 void IpOptsRegisterTests(void)
387 {
388  UtRegisterTest("IpOptsTestParse01", IpOptsTestParse01);
389  UtRegisterTest("IpOptsTestParse02", IpOptsTestParse02);
390  UtRegisterTest("IpOptsTestParse03", IpOptsTestParse03);
391  UtRegisterTest("IpOptsTestParse04", IpOptsTestParse04);
392 }
393 #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:1288
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:437
SigTableElmt_::desc
const char * desc
Definition: detect.h:1287
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1275
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1285
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1053
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:827
Packet_::ip4vars
IPV4Vars ip4vars
Definition: decode.h:541
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:2629
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1270
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:1075
de
uint8_t de
Definition: app-layer-htp.c:577
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2753
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:108
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:344
Signature_::flags
uint32_t flags
Definition: detect.h:583
Packet_
Definition: decode.h:430
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:535
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1253
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:322
DetectIpOptsFree
void DetectIpOptsFree(DetectEngineCtx *, void *)
Definition: detect-ipopts.c:276
decode-events.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:336
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:342
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
DetectIpOptsData_
Definition: detect-ipopts.h:38
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:173
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:582
SigMatch_
a single match condition for a signature
Definition: detect.h:341
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:178
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:245
IPV4_OPT_FLAG_EOL
@ IPV4_OPT_FLAG_EOL
Definition: decode-ipv4.h:158
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1277
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:242