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(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].desc = "match on specific decimal values of the IP header TOS field";
67  sigmatch_table[DETECT_TOS].Match = DetectTosMatch;
68  sigmatch_table[DETECT_TOS].Setup = DetectTosSetup;
69  sigmatch_table[DETECT_TOS].Free = DetectTosFree;
70  sigmatch_table[DETECT_TOS].RegisterTests = DetectTosRegisterTests;
74  DOC_URL DOC_VERSION "/rules/header-keywords.html#tos";
75 
76  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex, &parse_regex_study);
77 }
78 
79 /**
80  * \brief Match function for tos keyword.
81  *
82  * \param tv ThreadVars instance.
83  * \param det_ctx Pointer to the detection thread ctx.
84  * \param p Pointer to the packet.
85  * \param m Pointer to the SigMatch containing the tos data.
86  *
87  * \retval 0 no match
88  * \retval 1 match
89  */
90 static int DetectTosMatch(DetectEngineThreadCtx *det_ctx, Packet *p,
91  const Signature *s, const SigMatchCtx *ctx)
92 {
93  const DetectTosData *tosd = (const DetectTosData *)ctx;
94  int result = 0;
95 
96  if (!PKT_IS_IPV4(p) || PKT_IS_PSEUDOPKT(p)) {
97  return 0;
98  }
99 
100  if (tosd->tos == IPV4_GET_IPTOS(p)) {
101  SCLogDebug("tos match found for %d\n", tosd->tos);
102  result = 1;
103  }
104 
105  return (tosd->negated ^ result);
106 }
107 
108 static DetectTosData *DetectTosParse(const char *arg, bool negate)
109 {
110  DetectTosData *tosd = NULL;
111 #define MAX_SUBSTRINGS 30
112  int ret = 0, res = 0;
113  int ov[MAX_SUBSTRINGS];
114 
115  ret = pcre_exec(parse_regex, parse_regex_study, arg, strlen(arg), 0, 0,
116  ov, MAX_SUBSTRINGS);
117 
118  if (ret != 2) {
119  SCLogError(SC_ERR_PCRE_MATCH, "invalid tos option - %s. "
120  "The tos option value must be in the range "
121  "%u - %u", arg, DETECT_IPTOS_MIN, DETECT_IPTOS_MAX);
122  goto error;
123  }
124 
125  /* For TOS value */
126  char tosbytes_str[64] = "";
127  res = pcre_copy_substring((char *)arg, ov, MAX_SUBSTRINGS, 1,
128  tosbytes_str, sizeof(tosbytes_str));
129  if (res < 0) {
130  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
131  goto error;
132  }
133 
134  int64_t tos = 0;
135 
136  if (tosbytes_str[0] == 'x' || tosbytes_str[0] == 'X') {
137  int r = ByteExtractStringSigned(&tos, 16, 0, &tosbytes_str[1]);
138  if (r < 0) {
139  goto error;
140  }
141  } else {
142  int r = ByteExtractStringSigned(&tos, 10, 0, &tosbytes_str[0]);
143  if (r < 0) {
144  goto error;
145  }
146  }
147 
148  if (!(tos >= DETECT_IPTOS_MIN && tos <= DETECT_IPTOS_MAX)) {
149  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid tos argument - "
150  "%s. The tos option value must be in the range "
151  "%u - %u", tosbytes_str, DETECT_IPTOS_MIN, DETECT_IPTOS_MAX);
152  goto error;
153  }
154 
155  tosd = SCMalloc(sizeof(DetectTosData));
156  if (unlikely(tosd == NULL))
157  goto error;
158  tosd->tos = (uint8_t)tos;
159  tosd->negated = negate;
160 
161  return tosd;
162 
163 error:
164  return NULL;
165 }
166 
167 /**
168  * \brief Setup function for tos argument. Parse the argument and
169  * add it into the sig.
170  *
171  * \param de_ctx Detection Engine Context instance.
172  * \param s Pointer to the signature.
173  * \param arg Argument to be parsed.
174  *
175  * \retval 0 on Success.
176  * \retval -1 on Failure.
177  */
178 static int DetectTosSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
179 {
180  DetectTosData *tosd = DetectTosParse(arg, s->init_data->negated);
181  if (tosd == NULL)
182  return -1;
183 
184  SigMatch *sm = SigMatchAlloc();
185  if (sm == NULL) {
186  DetectTosFree(tosd);
187  return -1;
188  }
189 
190  sm->type = DETECT_TOS;
191  sm->ctx = (SigMatchCtx *)tosd;
192 
195  return 0;
196 }
197 
198 /**
199  * \brief Free data allocated by the tos keyword.
200  *
201  * \param tosd Data to be freed.
202  */
203 static void DetectTosFree(void *tosd)
204 {
205  SCFree(tosd);
206 }
207 
208 /********************************Unittests***********************************/
209 
210 #ifdef UNITTESTS
211 
212 static int DetectTosTest01(void)
213 {
214  DetectTosData *tosd = NULL;
215  tosd = DetectTosParse("12", false);
216  if (tosd != NULL && tosd->tos == 12 && !tosd->negated) {
217  DetectTosFree(tosd);
218  return 1;
219  }
220 
221  return 0;
222 }
223 
224 static int DetectTosTest02(void)
225 {
226  DetectTosData *tosd = NULL;
227  tosd = DetectTosParse("123", false);
228  if (tosd != NULL && tosd->tos == 123 && !tosd->negated) {
229  DetectTosFree(tosd);
230  return 1;
231  }
232 
233  return 0;
234 }
235 
236 static int DetectTosTest04(void)
237 {
238  DetectTosData *tosd = NULL;
239  tosd = DetectTosParse("256", false);
240  if (tosd != NULL) {
241  DetectTosFree(tosd);
242  return 0;
243  }
244 
245  return 1;
246 }
247 
248 static int DetectTosTest05(void)
249 {
250  DetectTosData *tosd = NULL;
251  tosd = DetectTosParse("boom", false);
252  if (tosd != NULL) {
253  DetectTosFree(tosd);
254  return 0;
255  }
256 
257  return 1;
258 }
259 
260 static int DetectTosTest06(void)
261 {
262  DetectTosData *tosd = NULL;
263  tosd = DetectTosParse("x12", false);
264  if (tosd != NULL && tosd->tos == 0x12 && !tosd->negated) {
265  DetectTosFree(tosd);
266  return 1;
267  }
268 
269  return 0;
270 }
271 
272 static int DetectTosTest07(void)
273 {
274  DetectTosData *tosd = NULL;
275  tosd = DetectTosParse("X12", false);
276  if (tosd != NULL && tosd->tos == 0x12 && !tosd->negated) {
277  DetectTosFree(tosd);
278  return 1;
279  }
280 
281  return 0;
282 }
283 
284 static int DetectTosTest08(void)
285 {
286  DetectTosData *tosd = NULL;
287  tosd = DetectTosParse("x121", false);
288  if (tosd != NULL) {
289  DetectTosFree(tosd);
290  return 0;
291  }
292 
293  return 1;
294 }
295 
296 static int DetectTosTest09(void)
297 {
298  DetectTosData *tosd = NULL;
299  tosd = DetectTosParse("12", true);
300  if (tosd != NULL && tosd->tos == 12 && tosd->negated) {
301  DetectTosFree(tosd);
302  return 1;
303  }
304 
305  return 0;
306 }
307 
308 static int DetectTosTest10(void)
309 {
310  DetectTosData *tosd = NULL;
311  tosd = DetectTosParse("x12", true);
312  if (tosd != NULL && tosd->tos == 0x12 && tosd->negated) {
313  DetectTosFree(tosd);
314  return 1;
315  }
316 
317  return 0;
318 }
319 
320 static int DetectTosTest12(void)
321 {
322  int result = 0;
323  uint8_t *buf = (uint8_t *)"Hi all!";
324  uint16_t buflen = strlen((char *)buf);
325  Packet *p;
326 
327  p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
328 
329  if (p == NULL)
330  goto end;
331 
332  IPV4_SET_RAW_IPTOS(p->ip4h, 10);
333 
334  const char *sigs[4];
335  sigs[0]= "alert ip any any -> any any (msg:\"Testing id 1\"; tos: 10 ; sid:1;)";
336  sigs[1]= "alert ip any any -> any any (msg:\"Testing id 2\"; tos: ! 10; sid:2;)";
337  sigs[2]= "alert ip any any -> any any (msg:\"Testing id 3\"; tos:20 ; sid:3;)";
338  sigs[3]= "alert ip any any -> any any (msg:\"Testing id 3\"; tos:! 20; sid:4;)";
339 
340  uint32_t sid[4] = {1, 2, 3, 4};
341 
342  uint32_t results[1][4] =
343  {
344  {1, 0, 0, 1},
345  };
346 
347  result = UTHGenericTest(&p, 1, sigs, sid, (uint32_t *) results, 4);
348 
349  UTHFreePackets(&p, 1);
350 
351 end:
352  return result;
353 }
354 
355 #endif
356 
357 void DetectTosRegisterTests(void)
358 {
359 #ifdef UNITTESTS
360  UtRegisterTest("DetectTosTest01", DetectTosTest01);
361  UtRegisterTest("DetectTosTest02", DetectTosTest02);
362  UtRegisterTest("DetectTosTest04", DetectTosTest04);
363  UtRegisterTest("DetectTosTest05", DetectTosTest05);
364  UtRegisterTest("DetectTosTest06", DetectTosTest06);
365  UtRegisterTest("DetectTosTest07", DetectTosTest07);
366  UtRegisterTest("DetectTosTest08", DetectTosTest08);
367  UtRegisterTest("DetectTosTest09", DetectTosTest09);
368  UtRegisterTest("DetectTosTest10", DetectTosTest10);
369  UtRegisterTest("DetectTosTest12", DetectTosTest12);
370 #endif
371  return;
372 }
uint8_t tos
Definition: detect-tos.h:29
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
SignatureInitData * init_data
Definition: detect.h:591
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
#define SCLogDebug(...)
Definition: util-debug.h:335
uint32_t flags
Definition: detect.h:523
#define MAX_SUBSTRINGS
#define unlikely(expr)
Definition: util-optimize.h:35
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223
const char * name
Definition: detect.h:1200
Signature container.
Definition: detect.h:522
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:313
#define PKT_IS_IPV4(p)
Definition: decode.h:251
main detection engine ctx
Definition: detect.h:761
#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:1191
#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:304
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:1381
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
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1170
uint8_t type
Definition: detect.h:319
const char * desc
Definition: detect.h:1202
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:346
IPV4Hdr * ip4h
Definition: decode.h:500
#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:321
#define SCMalloc(a)
Definition: util-mem.h:222
#define SCFree(a)
Definition: util-mem.h:322
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...
const char * url
Definition: detect.h:1203
#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:1132
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:232
#define SIGMATCH_HANDLE_NEGATION
Definition: detect.h:1389
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1194
#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:1192
a single match condition for a signature
Definition: detect.h:318