suricata
detect-tos.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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 Anoop Saldanha <anoopsaldanha@gmail.com>
22  */
23 
24 #include "suricata-common.h"
25 #include "threads.h"
26 #include "decode.h"
27 
28 #include "detect.h"
29 #include "detect-parse.h"
30 #include "detect-engine.h"
31 #include "detect-engine-mpm.h"
32 #include "detect-engine-state.h"
33 #include "detect-tos.h"
34 
35 #include "app-layer-protos.h"
36 
37 #include "flow.h"
38 #include "flow-var.h"
39 #include "flow-util.h"
40 
41 #include "util-byte.h"
42 #include "util-debug.h"
43 #include "util-unittest.h"
44 #include "util-unittest-helper.h"
45 
46 #define PARSE_REGEX "^\\s*(!?\\s*[0-9]{1,3}|!?\\s*[xX][0-9a-fA-F]{1,2})\\s*$"
47 
48 static pcre *parse_regex;
49 static pcre_extra *parse_regex_study;
50 
51 static int DetectTosSetup(DetectEngineCtx *, Signature *, const char *);
52 static int DetectTosMatch(ThreadVars *, DetectEngineThreadCtx *, Packet *,
53  const Signature *, const SigMatchCtx *);
54 static void DetectTosRegisterTests(void);
55 static void DetectTosFree(void *);
56 
57 #define DETECT_IPTOS_MIN 0
58 #define DETECT_IPTOS_MAX 255
59 
60 /**
61  * \brief Register Tos keyword.
62  */
64 {
66  sigmatch_table[DETECT_TOS].Match = DetectTosMatch;
67  sigmatch_table[DETECT_TOS].Setup = DetectTosSetup;
68  sigmatch_table[DETECT_TOS].Free = DetectTosFree;
69  sigmatch_table[DETECT_TOS].RegisterTests = DetectTosRegisterTests;
73  DOC_URL DOC_VERSION "/rules/header-keywords.html#tos";
74 
75  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex, &parse_regex_study);
76 }
77 
78 /**
79  * \brief Match function for tos keyword.
80  *
81  * \param tv ThreadVars instance.
82  * \param det_ctx Pointer to the detection thread ctx.
83  * \param p Pointer to the packet.
84  * \param m Pointer to the SigMatch containing the tos data.
85  *
86  * \retval 0 no match
87  * \retval 1 match
88  */
89 static int DetectTosMatch(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, Packet *p,
90  const Signature *s, const SigMatchCtx *ctx)
91 {
92  const DetectTosData *tosd = (const DetectTosData *)ctx;
93  int result = 0;
94 
95  if (!PKT_IS_IPV4(p) || PKT_IS_PSEUDOPKT(p)) {
96  return 0;
97  }
98 
99  if (tosd->tos == IPV4_GET_IPTOS(p)) {
100  SCLogDebug("tos match found for %d\n", tosd->tos);
101  result = 1;
102  }
103 
104  return (tosd->negated ^ result);
105 }
106 
107 static DetectTosData *DetectTosParse(const char *arg, bool negate)
108 {
109  DetectTosData *tosd = NULL;
110 #define MAX_SUBSTRINGS 30
111  int ret = 0, res = 0;
112  int ov[MAX_SUBSTRINGS];
113 
114  ret = pcre_exec(parse_regex, parse_regex_study, arg, strlen(arg), 0, 0,
115  ov, MAX_SUBSTRINGS);
116 
117  if (ret != 2) {
118  SCLogError(SC_ERR_PCRE_MATCH, "invalid tos option - %s. "
119  "The tos option value must be in the range "
120  "%u - %u", arg, DETECT_IPTOS_MIN, DETECT_IPTOS_MAX);
121  goto error;
122  }
123 
124  const char *str_ptr;
125  res = pcre_get_substring((char *)arg, ov, MAX_SUBSTRINGS, 1,
126  &str_ptr);
127  if (res < 0) {
128  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
129  goto error;
130  }
131 
132  int64_t tos = 0;
133 
134  if (*str_ptr == 'x' || *str_ptr == 'X') {
135  int r = ByteExtractStringSigned(&tos, 16, 0, str_ptr + 1);
136  if (r < 0) {
137  goto error;
138  }
139  } else {
140  int r = ByteExtractStringSigned(&tos, 10, 0, str_ptr);
141  if (r < 0) {
142  goto error;
143  }
144  }
145  if (!(tos >= DETECT_IPTOS_MIN && tos <= DETECT_IPTOS_MAX)) {
146  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid tos argument - "
147  "%s. The tos option value must be in the range "
148  "%u - %u", str_ptr, DETECT_IPTOS_MIN, DETECT_IPTOS_MAX);
149  goto error;
150  }
151 
152  tosd = SCMalloc(sizeof(DetectTosData));
153  if (unlikely(tosd == NULL))
154  goto error;
155  tosd->tos = (uint8_t)tos;
156  tosd->negated = negate;
157 
158  return tosd;
159 
160 error:
161  return NULL;
162 }
163 
164 /**
165  * \brief Setup function for tos argument. Parse the argument and
166  * add it into the sig.
167  *
168  * \param de_ctx Detection Engine Context instance.
169  * \param s Pointer to the signature.
170  * \param arg Argument to be parsed.
171  *
172  * \retval 0 on Success.
173  * \retval -1 on Failure.
174  */
175 static int DetectTosSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
176 {
177  DetectTosData *tosd = DetectTosParse(arg, s->init_data->negated);
178  if (tosd == NULL)
179  return -1;
180 
181  SigMatch *sm = SigMatchAlloc();
182  if (sm == NULL) {
183  DetectTosFree(tosd);
184  return -1;
185  }
186 
187  sm->type = DETECT_TOS;
188  sm->ctx = (SigMatchCtx *)tosd;
189 
192  return 0;
193 }
194 
195 /**
196  * \brief Free data allocated by the tos keyword.
197  *
198  * \param tosd Data to be freed.
199  */
200 static void DetectTosFree(void *tosd)
201 {
202  SCFree(tosd);
203 }
204 
205 /********************************Unittests***********************************/
206 
207 #ifdef UNITTESTS
208 
209 static int DetectTosTest01(void)
210 {
211  DetectTosData *tosd = NULL;
212  tosd = DetectTosParse("12", false);
213  if (tosd != NULL && tosd->tos == 12 && !tosd->negated) {
214  DetectTosFree(tosd);
215  return 1;
216  }
217 
218  return 0;
219 }
220 
221 static int DetectTosTest02(void)
222 {
223  DetectTosData *tosd = NULL;
224  tosd = DetectTosParse("123", false);
225  if (tosd != NULL && tosd->tos == 123 && !tosd->negated) {
226  DetectTosFree(tosd);
227  return 1;
228  }
229 
230  return 0;
231 }
232 
233 static int DetectTosTest04(void)
234 {
235  DetectTosData *tosd = NULL;
236  tosd = DetectTosParse("256", false);
237  if (tosd != NULL) {
238  DetectTosFree(tosd);
239  return 0;
240  }
241 
242  return 1;
243 }
244 
245 static int DetectTosTest05(void)
246 {
247  DetectTosData *tosd = NULL;
248  tosd = DetectTosParse("boom", false);
249  if (tosd != NULL) {
250  DetectTosFree(tosd);
251  return 0;
252  }
253 
254  return 1;
255 }
256 
257 static int DetectTosTest06(void)
258 {
259  DetectTosData *tosd = NULL;
260  tosd = DetectTosParse("x12", false);
261  if (tosd != NULL && tosd->tos == 0x12 && !tosd->negated) {
262  DetectTosFree(tosd);
263  return 1;
264  }
265 
266  return 0;
267 }
268 
269 static int DetectTosTest07(void)
270 {
271  DetectTosData *tosd = NULL;
272  tosd = DetectTosParse("X12", false);
273  if (tosd != NULL && tosd->tos == 0x12 && !tosd->negated) {
274  DetectTosFree(tosd);
275  return 1;
276  }
277 
278  return 0;
279 }
280 
281 static int DetectTosTest08(void)
282 {
283  DetectTosData *tosd = NULL;
284  tosd = DetectTosParse("x121", false);
285  if (tosd != NULL) {
286  DetectTosFree(tosd);
287  return 0;
288  }
289 
290  return 1;
291 }
292 
293 static int DetectTosTest09(void)
294 {
295  DetectTosData *tosd = NULL;
296  tosd = DetectTosParse("12", true);
297  if (tosd != NULL && tosd->tos == 12 && tosd->negated) {
298  DetectTosFree(tosd);
299  return 1;
300  }
301 
302  return 0;
303 }
304 
305 static int DetectTosTest10(void)
306 {
307  DetectTosData *tosd = NULL;
308  tosd = DetectTosParse("x12", true);
309  if (tosd != NULL && tosd->tos == 0x12 && tosd->negated) {
310  DetectTosFree(tosd);
311  return 1;
312  }
313 
314  return 0;
315 }
316 
317 static int DetectTosTest12(void)
318 {
319  int result = 0;
320  uint8_t *buf = (uint8_t *)"Hi all!";
321  uint16_t buflen = strlen((char *)buf);
322  Packet *p;
323 
324  p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
325 
326  if (p == NULL)
327  goto end;
328 
329  IPV4_SET_RAW_IPTOS(p->ip4h, 10);
330 
331  const char *sigs[4];
332  sigs[0]= "alert ip any any -> any any (msg:\"Testing id 1\"; tos: 10 ; sid:1;)";
333  sigs[1]= "alert ip any any -> any any (msg:\"Testing id 2\"; tos: ! 10; sid:2;)";
334  sigs[2]= "alert ip any any -> any any (msg:\"Testing id 3\"; tos:20 ; sid:3;)";
335  sigs[3]= "alert ip any any -> any any (msg:\"Testing id 3\"; tos:! 20; sid:4;)";
336 
337  uint32_t sid[4] = {1, 2, 3, 4};
338 
339  uint32_t results[1][4] =
340  {
341  {1, 0, 0, 1},
342  };
343 
344  result = UTHGenericTest(&p, 1, sigs, sid, (uint32_t *) results, 4);
345 
346  UTHFreePackets(&p, 1);
347 
348 end:
349  return result;
350 }
351 
352 #endif
353 
354 void DetectTosRegisterTests(void)
355 {
356 #ifdef UNITTESTS
357  UtRegisterTest("DetectTosTest01", DetectTosTest01);
358  UtRegisterTest("DetectTosTest02", DetectTosTest02);
359  UtRegisterTest("DetectTosTest04", DetectTosTest04);
360  UtRegisterTest("DetectTosTest05", DetectTosTest05);
361  UtRegisterTest("DetectTosTest06", DetectTosTest06);
362  UtRegisterTest("DetectTosTest07", DetectTosTest07);
363  UtRegisterTest("DetectTosTest08", DetectTosTest08);
364  UtRegisterTest("DetectTosTest09", DetectTosTest09);
365  UtRegisterTest("DetectTosTest10", DetectTosTest10);
366  UtRegisterTest("DetectTosTest12", DetectTosTest12);
367 #endif
368  return;
369 }
uint8_t tos
Definition: detect-tos.h:29
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
SignatureInitData * init_data
Definition: detect.h:560
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
#define SCLogDebug(...)
Definition: util-debug.h:335
uint32_t flags
Definition: detect.h:493
#define MAX_SUBSTRINGS
#define unlikely(expr)
Definition: util-optimize.h:35
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:230
const char * name
Definition: detect.h:1160
Signature container.
Definition: detect.h:492
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:319
#define PKT_IS_IPV4(p)
Definition: decode.h:250
main detection engine ctx
Definition: detect.h:720
#define DETECT_IPTOS_MAX
Definition: detect-tos.c:58
Data structures and function prototypes for keeping state for the detection engine.
void(* Free)(void *)
Definition: detect.h:1151
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
int ByteExtractStringSigned(int64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:259
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void DetectSetupParseRegexes(const char *parse_str, pcre **parse_regex, pcre_extra **parse_regex_study)
#define SIGMATCH_QUOTES_OPTIONAL
Definition: detect.h:1340
uint8_t negated
Definition: detect-tos.h:28
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
#define DETECT_IPTOS_MIN
Definition: detect-tos.c:57
uint8_t type
Definition: detect.h:325
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:282
IPV4Hdr * ip4h
Definition: decode.h:503
#define IPV4_SET_RAW_IPTOS(ip4h, value)
Definition: decode-ipv4.h:113
void DetectTosRegister(void)
Register Tos keyword.
Definition: detect-tos.c:63
SigMatchCtx * ctx
Definition: detect.h:327
#define SCMalloc(a)
Definition: util-mem.h:174
#define SCFree(a)
Definition: util-mem.h:236
PoolThreadReserved res
int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
UTHGenericTest: function that perfom a generic check taking care of as maximum common unittest elemen...
int(* Match)(ThreadVars *, DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1129
const char * url
Definition: detect.h:1163
#define IPV4_GET_IPTOS(p)
Definition: decode-ipv4.h:125
#define DOC_URL
Definition: suricata.h:86
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1140
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:226
Per thread variable structure.
Definition: threadvars.h:57
#define SIGMATCH_HANDLE_NEGATION
Definition: detect.h:1348
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1154
#define PARSE_REGEX
Definition: detect-tos.c:46
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself...
void(* RegisterTests)(void)
Definition: detect.h:1152
a single match condition for a signature
Definition: detect.h:324