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  * \internal
124  * \brief This function is used to match ip option on a packet with those passed via ipopts:
125  *
126  * \param t pointer to thread vars
127  * \param det_ctx pointer to the pattern matcher thread
128  * \param p pointer to the current packet
129  * \param s pointer to the Signature
130  * \param m pointer to the sigmatch
131  *
132  * \retval 0 no match
133  * \retval 1 match
134  */
135 static int DetectIpOptsMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
136  const Signature *s, const SigMatchCtx *ctx)
137 {
138  const DetectIpOptsData *de = (const DetectIpOptsData *)ctx;
139 
140  if (!de || !PKT_IS_IPV4(p) || PKT_IS_PSEUDOPKT(p))
141  return 0;
142 
143  if (p->ip4vars.opts_set & de->ipopt) {
144  return 1;
145  }
146 
147  return 0;
148 }
149 
150 /**
151  * \internal
152  * \brief This function is used to parse ipopts options passed via ipopts: keyword
153  *
154  * \param rawstr Pointer to the user provided ipopts options
155  *
156  * \retval de pointer to DetectIpOptsData on success
157  * \retval NULL on failure
158  */
159 static DetectIpOptsData *DetectIpOptsParse (const char *rawstr)
160 {
161  int i;
162  DetectIpOptsData *de = NULL;
163  int ret = 0, found = 0;
164 
165  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
166  if (ret < 1) {
167  SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
168  goto error;
169  }
170 
171  for(i = 0; ipopts[i].ipopt_name != NULL; i++) {
172  if((strcasecmp(ipopts[i].ipopt_name,rawstr)) == 0) {
173  found = 1;
174  break;
175  }
176  }
177 
178  if(found == 0)
179  goto error;
180 
181  de = SCMalloc(sizeof(DetectIpOptsData));
182  if (unlikely(de == NULL))
183  goto error;
184 
185  de->ipopt = ipopts[i].code;
186 
187  return de;
188 
189 error:
190  if (de) SCFree(de);
191  return NULL;
192 }
193 
194 /**
195  * \internal
196  * \brief this function is used to add the parsed ipopts into the current signature
197  *
198  * \param de_ctx pointer to the Detection Engine Context
199  * \param s pointer to the Current Signature
200  * \param rawstr pointer to the user provided ipopts options
201  *
202  * \retval 0 on Success
203  * \retval -1 on Failure
204  */
205 static int DetectIpOptsSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
206 {
207  DetectIpOptsData *de = NULL;
208  SigMatch *sm = NULL;
209 
210  de = DetectIpOptsParse(rawstr);
211  if (de == NULL)
212  goto error;
213 
214  sm = SigMatchAlloc();
215  if (sm == NULL)
216  goto error;
217 
218  sm->type = DETECT_IPOPTS;
219  sm->ctx = (SigMatchCtx *)de;
220 
223 
224  return 0;
225 
226 error:
227  if (de) SCFree(de);
228  if (sm) SCFree(sm);
229  return -1;
230 }
231 
232 /**
233  * \internal
234  * \brief this function will free memory associated with DetectIpOptsData
235  *
236  * \param de pointer to DetectIpOptsData
237  */
239 {
240  DetectIpOptsData *de = (DetectIpOptsData *)de_ptr;
241  if(de) SCFree(de);
242 }
243 
244 /*
245  * ONLY TESTS BELOW THIS COMMENT
246  */
247 
248 #ifdef UNITTESTS
249 /**
250  * \test IpOptsTestParse01 is a test for a valid ipopts value
251  */
252 static int IpOptsTestParse01 (void)
253 {
254  DetectIpOptsData *de = DetectIpOptsParse("lsrr");
255 
256  FAIL_IF_NULL(de);
257 
258  DetectIpOptsFree(NULL, de);
259 
260  PASS;
261 }
262 
263 /**
264  * \test IpOptsTestParse02 is a test for an invalid ipopts value
265  */
266 static int IpOptsTestParse02 (void)
267 {
268  DetectIpOptsData *de = DetectIpOptsParse("invalidopt");
269 
271 
272  DetectIpOptsFree(NULL, de);
273 
274  PASS;
275 }
276 
277 /**
278  * \test IpOptsTestParse03 test the match function on a packet that needs to match
279  */
280 static int IpOptsTestParse03 (void)
281 {
282  Packet *p = PacketGetFromAlloc();
283  FAIL_IF_NULL(p);
284  ThreadVars tv;
285  IPV4Hdr ip4h;
286 
287  memset(&tv, 0, sizeof(ThreadVars));
288  memset(&ip4h, 0, sizeof(IPV4Hdr));
289 
290  p->ip4h = &ip4h;
292 
293  DetectIpOptsData *de = DetectIpOptsParse("rr");
294  FAIL_IF_NULL(de);
295 
296  SigMatch *sm = SigMatchAlloc();
297  FAIL_IF_NULL(sm);
298 
299  sm->type = DETECT_IPOPTS;
300  sm->ctx = (SigMatchCtx *)de;
301 
302  FAIL_IF_NOT(DetectIpOptsMatch(NULL, p, NULL, sm->ctx));
303 
304  SCFree(de);
305  SCFree(sm);
306  SCFree(p);
307 
308  PASS;
309 }
310 
311 /**
312  * \test IpOptsTestParse04 test the match function on a packet that needs to not match
313  */
314 static int IpOptsTestParse04 (void)
315 {
316  Packet *p = PacketGetFromAlloc();
317  FAIL_IF_NULL(p);
318  ThreadVars tv;
319  IPV4Hdr ip4h;
320 
321  memset(&tv, 0, sizeof(ThreadVars));
322  memset(&ip4h, 0, sizeof(IPV4Hdr));
323 
324  p->ip4h = &ip4h;
326 
327  DetectIpOptsData *de = DetectIpOptsParse("lsrr");
328  FAIL_IF_NULL(de);
329 
330  SigMatch *sm = SigMatchAlloc();
331  FAIL_IF_NULL(sm);
332 
333  sm->type = DETECT_IPOPTS;
334  sm->ctx = (SigMatchCtx *)de;
335 
336  FAIL_IF(DetectIpOptsMatch(NULL, p, NULL, sm->ctx));
337 
338  SCFree(de);
339  SCFree(sm);
340  SCFree(p);
341 
342  PASS;
343 }
344 
345 /**
346  * \brief this function registers unit tests for IpOpts
347  */
348 void IpOptsRegisterTests(void)
349 {
350  UtRegisterTest("IpOptsTestParse01", IpOptsTestParse01);
351  UtRegisterTest("IpOptsTestParse02", IpOptsTestParse02);
352  UtRegisterTest("IpOptsTestParse03", IpOptsTestParse03);
353  UtRegisterTest("IpOptsTestParse04", IpOptsTestParse04);
354 }
355 #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:1241
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:1240
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2488
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1228
DetectParseRegex
Definition: detect-parse.h:44
SigTableElmt_::name
const char * name
Definition: detect.h:1238
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1059
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:785
Packet_::ip4vars
IPV4Vars ip4vars
Definition: decode.h:537
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
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1223
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
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:1025
de
uint8_t de
Definition: app-layer-htp.c:576
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2609
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:78
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:316
Signature_::flags
uint32_t flags
Definition: detect.h:541
Packet_
Definition: decode.h:428
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:531
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1206
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:239
DetectIpOptsFree
void DetectIpOptsFree(DetectEngineCtx *, void *)
Definition: detect-ipopts.c:238
decode-events.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:308
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:314
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
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:540
SigMatch_
a single match condition for a signature
Definition: detect.h:313
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
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:354
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1230
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:215