suricata
detect-tos.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 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 DetectParseRegex parse_regex;
49 
50 static int DetectTosSetup(DetectEngineCtx *, Signature *, const char *);
51 static int DetectTosMatch(DetectEngineThreadCtx *, Packet *,
52  const Signature *, const SigMatchCtx *);
53 static void DetectTosRegisterTests(void);
54 static void DetectTosFree(void *);
55 
56 #define DETECT_IPTOS_MIN 0
57 #define DETECT_IPTOS_MAX 255
58 
59 /**
60  * \brief Register Tos keyword.
61  */
63 {
65  sigmatch_table[DETECT_TOS].desc = "match on specific decimal values of the IP header TOS field";
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);
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(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  int ret = 0, res = 0;
111  int ov[MAX_SUBSTRINGS];
112 
113  ret = DetectParsePcreExec(&parse_regex, arg, 0, 0, ov, MAX_SUBSTRINGS);
114  if (ret != 2) {
115  SCLogError(SC_ERR_PCRE_MATCH, "invalid tos option - %s. "
116  "The tos option value must be in the range "
117  "%u - %u", arg, DETECT_IPTOS_MIN, DETECT_IPTOS_MAX);
118  goto error;
119  }
120 
121  /* For TOS value */
122  char tosbytes_str[64] = "";
123  res = pcre_copy_substring((char *)arg, ov, MAX_SUBSTRINGS, 1,
124  tosbytes_str, sizeof(tosbytes_str));
125  if (res < 0) {
126  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
127  goto error;
128  }
129 
130  int64_t tos = 0;
131 
132  if (tosbytes_str[0] == 'x' || tosbytes_str[0] == 'X') {
133  if (StringParseInt64(&tos, 16, 0, &tosbytes_str[1]) < 0) {
134  goto error;
135  }
136  } else {
137  if (StringParseInt64(&tos, 10, 0, &tosbytes_str[0]) < 0) {
138  goto error;
139  }
140  }
141 
142  if (!(tos >= DETECT_IPTOS_MIN && tos <= DETECT_IPTOS_MAX)) {
143  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid tos argument - "
144  "%s. The tos option value must be in the range "
145  "%u - %u", tosbytes_str, DETECT_IPTOS_MIN, DETECT_IPTOS_MAX);
146  goto error;
147  }
148 
149  tosd = SCMalloc(sizeof(DetectTosData));
150  if (unlikely(tosd == NULL))
151  goto error;
152  tosd->tos = (uint8_t)tos;
153  tosd->negated = negate;
154 
155  return tosd;
156 
157 error:
158  return NULL;
159 }
160 
161 /**
162  * \brief Setup function for tos argument. Parse the argument and
163  * add it into the sig.
164  *
165  * \param de_ctx Detection Engine Context instance.
166  * \param s Pointer to the signature.
167  * \param arg Argument to be parsed.
168  *
169  * \retval 0 on Success.
170  * \retval -1 on Failure.
171  */
172 static int DetectTosSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
173 {
174  DetectTosData *tosd = DetectTosParse(arg, s->init_data->negated);
175  if (tosd == NULL)
176  return -1;
177 
178  SigMatch *sm = SigMatchAlloc();
179  if (sm == NULL) {
180  DetectTosFree(tosd);
181  return -1;
182  }
183 
184  sm->type = DETECT_TOS;
185  sm->ctx = (SigMatchCtx *)tosd;
186 
189  return 0;
190 }
191 
192 /**
193  * \brief Free data allocated by the tos keyword.
194  *
195  * \param tosd Data to be freed.
196  */
197 static void DetectTosFree(void *tosd)
198 {
199  SCFree(tosd);
200 }
201 
202 /********************************Unittests***********************************/
203 
204 #ifdef UNITTESTS
205 
206 static int DetectTosTest01(void)
207 {
208  DetectTosData *tosd = NULL;
209  tosd = DetectTosParse("12", false);
210  if (tosd != NULL && tosd->tos == 12 && !tosd->negated) {
211  DetectTosFree(tosd);
212  return 1;
213  }
214 
215  return 0;
216 }
217 
218 static int DetectTosTest02(void)
219 {
220  DetectTosData *tosd = NULL;
221  tosd = DetectTosParse("123", false);
222  if (tosd != NULL && tosd->tos == 123 && !tosd->negated) {
223  DetectTosFree(tosd);
224  return 1;
225  }
226 
227  return 0;
228 }
229 
230 static int DetectTosTest04(void)
231 {
232  DetectTosData *tosd = NULL;
233  tosd = DetectTosParse("256", false);
234  if (tosd != NULL) {
235  DetectTosFree(tosd);
236  return 0;
237  }
238 
239  return 1;
240 }
241 
242 static int DetectTosTest05(void)
243 {
244  DetectTosData *tosd = NULL;
245  tosd = DetectTosParse("boom", false);
246  if (tosd != NULL) {
247  DetectTosFree(tosd);
248  return 0;
249  }
250 
251  return 1;
252 }
253 
254 static int DetectTosTest06(void)
255 {
256  DetectTosData *tosd = NULL;
257  tosd = DetectTosParse("x12", false);
258  if (tosd != NULL && tosd->tos == 0x12 && !tosd->negated) {
259  DetectTosFree(tosd);
260  return 1;
261  }
262 
263  return 0;
264 }
265 
266 static int DetectTosTest07(void)
267 {
268  DetectTosData *tosd = NULL;
269  tosd = DetectTosParse("X12", false);
270  if (tosd != NULL && tosd->tos == 0x12 && !tosd->negated) {
271  DetectTosFree(tosd);
272  return 1;
273  }
274 
275  return 0;
276 }
277 
278 static int DetectTosTest08(void)
279 {
280  DetectTosData *tosd = NULL;
281  tosd = DetectTosParse("x121", false);
282  if (tosd != NULL) {
283  DetectTosFree(tosd);
284  return 0;
285  }
286 
287  return 1;
288 }
289 
290 static int DetectTosTest09(void)
291 {
292  DetectTosData *tosd = NULL;
293  tosd = DetectTosParse("12", true);
294  if (tosd != NULL && tosd->tos == 12 && tosd->negated) {
295  DetectTosFree(tosd);
296  return 1;
297  }
298 
299  return 0;
300 }
301 
302 static int DetectTosTest10(void)
303 {
304  DetectTosData *tosd = NULL;
305  tosd = DetectTosParse("x12", true);
306  if (tosd != NULL && tosd->tos == 0x12 && tosd->negated) {
307  DetectTosFree(tosd);
308  return 1;
309  }
310 
311  return 0;
312 }
313 
314 static int DetectTosTest12(void)
315 {
316  int result = 0;
317  uint8_t *buf = (uint8_t *)"Hi all!";
318  uint16_t buflen = strlen((char *)buf);
319  Packet *p;
320 
321  p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
322 
323  if (p == NULL)
324  goto end;
325 
326  IPV4_SET_RAW_IPTOS(p->ip4h, 10);
327 
328  const char *sigs[4];
329  sigs[0]= "alert ip any any -> any any (msg:\"Testing id 1\"; tos: 10 ; sid:1;)";
330  sigs[1]= "alert ip any any -> any any (msg:\"Testing id 2\"; tos: ! 10; sid:2;)";
331  sigs[2]= "alert ip any any -> any any (msg:\"Testing id 3\"; tos:20 ; sid:3;)";
332  sigs[3]= "alert ip any any -> any any (msg:\"Testing id 3\"; tos:! 20; sid:4;)";
333 
334  uint32_t sid[4] = {1, 2, 3, 4};
335 
336  uint32_t results[1][4] =
337  {
338  {1, 0, 0, 1},
339  };
340 
341  result = UTHGenericTest(&p, 1, sigs, sid, (uint32_t *) results, 4);
342 
343  UTHFreePackets(&p, 1);
344 
345 end:
346  return result;
347 }
348 
349 #endif
350 
351 void DetectTosRegisterTests(void)
352 {
353 #ifdef UNITTESTS
354  UtRegisterTest("DetectTosTest01", DetectTosTest01);
355  UtRegisterTest("DetectTosTest02", DetectTosTest02);
356  UtRegisterTest("DetectTosTest04", DetectTosTest04);
357  UtRegisterTest("DetectTosTest05", DetectTosTest05);
358  UtRegisterTest("DetectTosTest06", DetectTosTest06);
359  UtRegisterTest("DetectTosTest07", DetectTosTest07);
360  UtRegisterTest("DetectTosTest08", DetectTosTest08);
361  UtRegisterTest("DetectTosTest09", DetectTosTest09);
362  UtRegisterTest("DetectTosTest10", DetectTosTest10);
363  UtRegisterTest("DetectTosTest12", DetectTosTest12);
364 #endif
365  return;
366 }
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1204
detect-engine.h
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-tos.c:46
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1201
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1115
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
SCFree
#define SCFree(a)
Definition: util-mem.h:322
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
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:335
SIGMATCH_QUOTES_OPTIONAL
#define SIGMATCH_QUOTES_OPTIONAL
Definition: detect.h:1382
threads.h
results
struct DetectRfbSecresult_ results[]
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1195
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:761
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
DETECT_IPTOS_MAX
#define DETECT_IPTOS_MAX
Definition: detect-tos.c:57
UTHBuildPacket
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.
Definition: util-unittest-helper.c:262
SigTableElmt_::Free
void(* Free)(void *)
Definition: detect.h:1192
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1187
util-unittest.h
util-unittest-helper.h
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:16
DetectEngineThreadCtx_
Definition: detect.h:1004
res
PoolThreadReserved res
Definition: stream-tcp-private.h:60
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2440
detect-tos.h
detect-engine-mpm.h
detect.h
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:89
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:321
SCMalloc
#define SCMalloc(a)
Definition: util-mem.h:222
Signature_::flags
uint32_t flags
Definition: detect.h:523
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2372
Packet_
Definition: decode.h:408
DETECT_IPTOS_MIN
#define DETECT_IPTOS_MIN
Definition: detect-tos.c:56
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:501
SIGMATCH_HANDLE_NEGATION
#define SIGMATCH_HANDLE_NEGATION
Definition: detect.h:1390
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:591
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SignatureInitData_::negated
bool negated
Definition: detect.h:478
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1171
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
IPV4_SET_RAW_IPTOS
#define IPV4_SET_RAW_IPTOS(ip4h, value)
Definition: decode-ipv4.h:113
SigMatch_::type
uint8_t type
Definition: detect.h:319
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:313
DetectTosData_::negated
uint8_t negated
Definition: detect-tos.h:28
StringParseInt64
int StringParseInt64(int64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:477
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:294
DOC_URL
#define DOC_URL
Definition: suricata.h:86
UTHGenericTest
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...
Definition: util-unittest-helper.c:575
DetectTosRegister
void DetectTosRegister(void)
Register Tos keyword.
Definition: detect-tos.c:62
IPV4_GET_IPTOS
#define IPV4_GET_IPTOS(p)
Definition: decode-ipv4.h:125
detect-parse.h
Signature_
Signature container.
Definition: detect.h:522
SigMatch_
a single match condition for a signature
Definition: detect.h:318
DETECT_TOS
@ DETECT_TOS
Definition: detect-engine-register.h:44
app-layer-protos.h
flow.h
flow-var.h
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:252
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
DOC_VERSION
#define DOC_VERSION
Definition: suricata.h:91
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1193
DetectTosData_::tos
uint8_t tos
Definition: detect-tos.h:29
DetectTosData_
Definition: detect-tos.h:27
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223
UTHFreePackets
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:393