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-helper.h"
44 
45 #define PARSE_REGEX "^\\s*(!?\\s*[0-9]{1,3}|!?\\s*[xX][0-9a-fA-F]{1,2})\\s*$"
46 
47 static DetectParseRegex parse_regex;
48 
49 static int DetectTosSetup(DetectEngineCtx *, Signature *, const char *);
50 static int DetectTosMatch(DetectEngineThreadCtx *, Packet *,
51  const Signature *, const SigMatchCtx *);
52 #ifdef UNITTESTS
53 static void DetectTosRegisterTests(void);
54 #endif
55 static void DetectTosFree(DetectEngineCtx *, 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 #ifdef UNITTESTS
71  sigmatch_table[DETECT_TOS].RegisterTests = DetectTosRegisterTests;
72 #endif
76  "/rules/header-keywords.html#tos";
77 
78  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
79 }
80 
81 /**
82  * \brief Match function for tos keyword.
83  *
84  * \param tv ThreadVars instance.
85  * \param det_ctx Pointer to the detection thread ctx.
86  * \param p Pointer to the packet.
87  * \param m Pointer to the SigMatch containing the tos data.
88  *
89  * \retval 0 no match
90  * \retval 1 match
91  */
92 static int DetectTosMatch(DetectEngineThreadCtx *det_ctx, Packet *p,
93  const Signature *s, const SigMatchCtx *ctx)
94 {
95  const DetectTosData *tosd = (const DetectTosData *)ctx;
96  int result = 0;
97 
99  if (!PacketIsIPv4(p)) {
100  return 0;
101  }
102 
103  const IPV4Hdr *ip4h = PacketGetIPv4(p);
104  if (tosd->tos == IPV4_GET_RAW_IPTOS(ip4h)) {
105  SCLogDebug("tos match found for %d", tosd->tos);
106  result = 1;
107  }
108 
109  return (tosd->negated ^ result);
110 }
111 
112 static DetectTosData *DetectTosParse(const char *arg, bool negate)
113 {
114  DetectTosData *tosd = NULL;
115  size_t pcre2len;
116 
117  pcre2_match_data *match = NULL;
118  int ret = DetectParsePcreExec(&parse_regex, &match, arg, 0, 0);
119  if (ret != 2) {
120  SCLogError("invalid tos option - %s. "
121  "The tos option value must be in the range "
122  "%u - %u",
124  goto error;
125  }
126 
127  /* For TOS value */
128  char tosbytes_str[64] = "";
129  pcre2len = sizeof(tosbytes_str);
130  int res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)tosbytes_str, &pcre2len);
131  if (res < 0) {
132  SCLogError("pcre2_substring_copy_bynumber failed");
133  goto error;
134  }
135 
136  int64_t tos = 0;
137 
138  if (tosbytes_str[0] == 'x' || tosbytes_str[0] == 'X') {
139  if (StringParseInt64(&tos, 16, 0, &tosbytes_str[1]) < 0) {
140  goto error;
141  }
142  } else {
143  if (StringParseInt64(&tos, 10, 0, &tosbytes_str[0]) < 0) {
144  goto error;
145  }
146  }
147 
148  if (!(tos >= DETECT_IPTOS_MIN && tos <= DETECT_IPTOS_MAX)) {
149  SCLogError("Invalid tos argument - "
150  "%s. The tos option value must be in the range "
151  "%u - %u",
152  tosbytes_str, DETECT_IPTOS_MIN, DETECT_IPTOS_MAX);
153  goto error;
154  }
155 
156  tosd = SCMalloc(sizeof(DetectTosData));
157  if (unlikely(tosd == NULL))
158  goto error;
159  tosd->tos = (uint8_t)tos;
160  tosd->negated = negate;
161 
162  pcre2_match_data_free(match);
163  return tosd;
164 
165 error:
166  if (match) {
167  pcre2_match_data_free(match);
168  }
169  return NULL;
170 }
171 
172 /**
173  * \brief Setup function for tos argument. Parse the argument and
174  * add it into the sig.
175  *
176  * \param de_ctx Detection Engine Context instance.
177  * \param s Pointer to the signature.
178  * \param arg Argument to be parsed.
179  *
180  * \retval 0 on Success.
181  * \retval -1 on Failure.
182  */
183 static int DetectTosSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
184 {
185  DetectTosData *tosd = DetectTosParse(arg, s->init_data->negated);
186  if (tosd == NULL)
187  return -1;
188 
190  de_ctx, s, DETECT_TOS, (SigMatchCtx *)tosd, DETECT_SM_LIST_MATCH) == NULL) {
191  DetectTosFree(de_ctx, tosd);
192  return -1;
193  }
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(DetectEngineCtx *de_ctx, 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(NULL, 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(NULL, 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(NULL, 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(NULL, 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(NULL, 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(NULL, 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(NULL, 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(NULL, 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(NULL, 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  p->l3.hdrs.ip4h->ip_tos = 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 void DetectTosRegisterTests(void)
356 {
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 }
368 #endif
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
detect-engine.h
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-tos.c:45
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1529
flow-util.h
DetectParseRegex
Definition: detect-parse.h:94
SigTableElmt_::name
const char * name
Definition: detect.h:1542
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1364
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:101
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
threads.h
SIGMATCH_QUOTES_OPTIONAL
#define SIGMATCH_QUOTES_OPTIONAL
Definition: detect-engine-register.h:318
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
PacketL3::hdrs
union PacketL3::Hdrs hdrs
IPV4Hdr_::ip_tos
uint8_t ip_tos
Definition: decode-ipv4.h:74
DETECT_IPTOS_MAX
#define DETECT_IPTOS_MAX
Definition: detect-tos.c:58
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:241
p
Packet * p
Definition: fuzz_dataset.c:30
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:4025
PacketL3::Hdrs::ip4h
IPV4Hdr * ip4h
Definition: decode.h:450
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
util-unittest-helper.h
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
IPV4_GET_RAW_IPTOS
#define IPV4_GET_RAW_IPTOS(ip4h)
Definition: decode-ipv4.h:97
DetectEngineThreadCtx_
Definition: detect.h:1316
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:4151
detect-tos.h
detect-engine-mpm.h
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:424
detect.h
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:118
Signature_::flags
uint32_t flags
Definition: detect.h:693
Packet_
Definition: decode.h:516
DETECT_IPTOS_MIN
#define DETECT_IPTOS_MIN
Definition: detect-tos.c:57
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:775
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
StringParseInt64
int StringParseInt64(int64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:581
SignatureInitData_::negated
bool negated
Definition: detect.h:611
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1504
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:355
IPV4Hdr_
Definition: decode-ipv4.h:72
DetectTosData_::negated
uint8_t negated
Definition: detect-tos.h:28
suricata-common.h
UTHGenericTest
int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
UTHGenericTest: function that perform a generic check taking care of as maximum common unittest eleme...
Definition: util-unittest-helper.c:578
DetectTosRegister
void DetectTosRegister(void)
Register Tos keyword.
Definition: detect-tos.c:63
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
Packet_::l3
struct PacketL3 l3
Definition: decode.h:615
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
DETECT_TOS
@ DETECT_TOS
Definition: detect-engine-register.h:46
app-layer-protos.h
flow.h
flow-var.h
SIGMATCH_HANDLE_NEGATION
#define SIGMATCH_HANDLE_NEGATION
Definition: detect-engine-register.h:326
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531
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:257
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:453