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 #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  { "rr", IPV4_OPT_FLAG_RR, },
80  { "lsrr", IPV4_OPT_FLAG_LSRR, },
81  { "eol", IPV4_OPT_FLAG_EOL, },
82  { "nop", IPV4_OPT_FLAG_NOP, },
83  { "ts", IPV4_OPT_FLAG_TS, },
84  { "sec", IPV4_OPT_FLAG_SEC, },
85  { "ssrr", IPV4_OPT_FLAG_SSRR, },
86  { "satid", IPV4_OPT_FLAG_SID, },
87  { "any", 0xffff, },
88  { NULL, 0 },
89 };
90 
91 /**
92  * \internal
93  * \brief This function is used to match ip option on a packet with those passed via ipopts:
94  *
95  * \param t pointer to thread vars
96  * \param det_ctx pointer to the pattern matcher thread
97  * \param p pointer to the current packet
98  * \param s pointer to the Signature
99  * \param m pointer to the sigmatch
100  *
101  * \retval 0 no match
102  * \retval 1 match
103  */
104 static int DetectIpOptsMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
105  const Signature *s, const SigMatchCtx *ctx)
106 {
107  const DetectIpOptsData *de = (const DetectIpOptsData *)ctx;
108 
109  if (!de || !PKT_IS_IPV4(p) || PKT_IS_PSEUDOPKT(p))
110  return 0;
111 
112  if (p->ip4vars.opts_set & de->ipopt) {
113  return 1;
114  }
115 
116  return 0;
117 }
118 
119 /**
120  * \internal
121  * \brief This function is used to parse ipopts options passed via ipopts: keyword
122  *
123  * \param rawstr Pointer to the user provided ipopts options
124  *
125  * \retval de pointer to DetectIpOptsData on success
126  * \retval NULL on failure
127  */
128 static DetectIpOptsData *DetectIpOptsParse (const char *rawstr)
129 {
130  int i;
131  DetectIpOptsData *de = NULL;
132  int ret = 0, found = 0;
133  int ov[MAX_SUBSTRINGS];
134 
135  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, MAX_SUBSTRINGS);
136  if (ret < 1) {
137  SCLogError(SC_ERR_PCRE_MATCH, "pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
138  goto error;
139  }
140 
141  for(i = 0; ipopts[i].ipopt_name != NULL; i++) {
142  if((strcasecmp(ipopts[i].ipopt_name,rawstr)) == 0) {
143  found = 1;
144  break;
145  }
146  }
147 
148  if(found == 0)
149  goto error;
150 
151  de = SCMalloc(sizeof(DetectIpOptsData));
152  if (unlikely(de == NULL))
153  goto error;
154 
155  de->ipopt = ipopts[i].code;
156 
157  return de;
158 
159 error:
160  if (de) SCFree(de);
161  return NULL;
162 }
163 
164 /**
165  * \internal
166  * \brief this function is used to add the parsed ipopts into the current signature
167  *
168  * \param de_ctx pointer to the Detection Engine Context
169  * \param s pointer to the Current Signature
170  * \param rawstr pointer to the user provided ipopts options
171  *
172  * \retval 0 on Success
173  * \retval -1 on Failure
174  */
175 static int DetectIpOptsSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
176 {
177  DetectIpOptsData *de = NULL;
178  SigMatch *sm = NULL;
179 
180  de = DetectIpOptsParse(rawstr);
181  if (de == NULL)
182  goto error;
183 
184  sm = SigMatchAlloc();
185  if (sm == NULL)
186  goto error;
187 
188  sm->type = DETECT_IPOPTS;
189  sm->ctx = (SigMatchCtx *)de;
190 
193 
194  return 0;
195 
196 error:
197  if (de) SCFree(de);
198  if (sm) SCFree(sm);
199  return -1;
200 }
201 
202 /**
203  * \internal
204  * \brief this function will free memory associated with DetectIpOptsData
205  *
206  * \param de pointer to DetectIpOptsData
207  */
209 {
210  DetectIpOptsData *de = (DetectIpOptsData *)de_ptr;
211  if(de) SCFree(de);
212 }
213 
214 /*
215  * ONLY TESTS BELOW THIS COMMENT
216  */
217 
218 #ifdef UNITTESTS
219 /**
220  * \test IpOptsTestParse01 is a test for a valid ipopts value
221  *
222  * \retval 1 on succces
223  * \retval 0 on failure
224  */
225 static int IpOptsTestParse01 (void)
226 {
227  DetectIpOptsData *de = NULL;
228  de = DetectIpOptsParse("lsrr");
229  if (de) {
230  DetectIpOptsFree(NULL, de);
231  return 1;
232  }
233 
234  return 0;
235 }
236 
237 /**
238  * \test IpOptsTestParse02 is a test for an invalid ipopts value
239  *
240  * \retval 1 on succces
241  * \retval 0 on failure
242  */
243 static int IpOptsTestParse02 (void)
244 {
245  DetectIpOptsData *de = NULL;
246  de = DetectIpOptsParse("invalidopt");
247  if (de) {
248  DetectIpOptsFree(NULL, de);
249  return 0;
250  }
251 
252  return 1;
253 }
254 
255 /**
256  * \test IpOptsTestParse03 test the match function on a packet that needs to match
257  *
258  * \retval 1 on succces
259  * \retval 0 on failure
260  */
261 static int IpOptsTestParse03 (void)
262 {
264  if (unlikely(p == NULL))
265  return 0;
266  ThreadVars tv;
267  int ret = 0;
268  DetectIpOptsData *de = NULL;
269  SigMatch *sm = NULL;
270  IPV4Hdr ip4h;
271 
272  memset(&tv, 0, sizeof(ThreadVars));
273  memset(p, 0, SIZE_OF_PACKET);
274  memset(&ip4h, 0, sizeof(IPV4Hdr));
275 
276  p->ip4h = &ip4h;
278 
279  de = DetectIpOptsParse("rr");
280 
281  if (de == NULL)
282  goto error;
283 
284  sm = SigMatchAlloc();
285  if (sm == NULL)
286  goto error;
287 
288  sm->type = DETECT_IPOPTS;
289  sm->ctx = (SigMatchCtx *)de;
290 
291  ret = DetectIpOptsMatch(NULL, p, NULL, sm->ctx);
292 
293  if(ret) {
294  SCFree(p);
295  return 1;
296  }
297 
298 error:
299  if (de) SCFree(de);
300  if (sm) SCFree(sm);
301  SCFree(p);
302  return 0;
303 }
304 
305 /**
306  * \test IpOptsTestParse04 test the match function on a packet that needs to not match
307  *
308  * \retval 1 on succces
309  * \retval 0 on failure
310  */
311 static int IpOptsTestParse04 (void)
312 {
314  if (unlikely(p == NULL))
315  return 0;
316  ThreadVars tv;
317  int ret = 0;
318  DetectIpOptsData *de = NULL;
319  SigMatch *sm = NULL;
320  IPV4Hdr ip4h;
321 
322  memset(&tv, 0, sizeof(ThreadVars));
323  memset(p, 0, SIZE_OF_PACKET);
324  memset(&ip4h, 0, sizeof(IPV4Hdr));
325 
326  p->ip4h = &ip4h;
328 
329  de = DetectIpOptsParse("lsrr");
330 
331  if (de == NULL)
332  goto error;
333 
334  sm = SigMatchAlloc();
335  if (sm == NULL)
336  goto error;
337 
338  sm->type = DETECT_IPOPTS;
339  sm->ctx = (SigMatchCtx *)de;
340 
341  ret = DetectIpOptsMatch(NULL, p, NULL, sm->ctx);
342 
343  if(ret) {
344  SCFree(p);
345  return 0;
346  }
347 
348  /* Error expected. */
349 error:
350  if (de) SCFree(de);
351  if (sm) SCFree(sm);
352  SCFree(p);
353  return 1;
354 }
355 
356 /**
357  * \brief this function registers unit tests for IpOpts
358  */
359 void IpOptsRegisterTests(void)
360 {
361  UtRegisterTest("IpOptsTestParse01", IpOptsTestParse01);
362  UtRegisterTest("IpOptsTestParse02", IpOptsTestParse02);
363  UtRegisterTest("IpOptsTestParse03", IpOptsTestParse03);
364  UtRegisterTest("IpOptsTestParse04", IpOptsTestParse04);
365 }
366 #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:1214
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
SigTableElmt_::name
const char * name
Definition: detect.h:1211
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1147
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:767
Packet_::ip4vars
IPV4Vars ip4vars
Definition: decode.h:515
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:1196
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:1010
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2493
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
de
int de
Definition: app-layer-htp.c:575
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:627
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
Signature_::flags
uint32_t flags
Definition: detect.h:529
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2423
Packet_
Definition: decode.h:414
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:509
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1179
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
DetectIpOptsFree
void DetectIpOptsFree(DetectEngineCtx *, void *)
Definition: detect-ipopts.c:208
decode-events.h
SigMatch_::type
uint8_t type
Definition: detect.h:321
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:315
IPV4Hdr_
Definition: decode-ipv4.h:71
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:528
SigMatch_
a single match condition for a signature
Definition: detect.h:320
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: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:257
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:1203
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223