suricata
detect-ttl.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 Gurvinder Singh <gurvindersighdahiya@gmail.com>
22  * \author Victor Julien <victor@inliniac.net>
23  *
24  * Implements the ttl keyword including prefilter support.
25  */
26 
27 #include "suricata-common.h"
28 #include "stream-tcp.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
33 
34 #include "detect-ttl.h"
35 #include "util-debug.h"
36 
37 /**
38  * \brief Regex for parsing our ttl options
39  */
40 #define PARSE_REGEX "^\\s*([0-9]*)?\\s*([<>=-]+)?\\s*([0-9]+)?\\s*$"
41 
42 static DetectParseRegex parse_regex;
43 
44 /* prototypes */
45 static int DetectTtlMatch (DetectEngineThreadCtx *, Packet *,
46  const Signature *, const SigMatchCtx *);
47 static int DetectTtlSetup (DetectEngineCtx *, Signature *, const char *);
48 void DetectTtlFree (DetectEngineCtx *, void *);
49 #ifdef UNITTESTS
50 void DetectTtlRegisterTests (void);
51 #endif
52 static int PrefilterSetupTtl(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
53 static bool PrefilterTtlIsPrefilterable(const Signature *s);
54 
55 /**
56  * \brief Registration function for ttl: keyword
57  */
58 
60 {
62  sigmatch_table[DETECT_TTL].desc = "check for a specific IP time-to-live value";
63  sigmatch_table[DETECT_TTL].url = "/rules/header-keywords.html#ttl";
64  sigmatch_table[DETECT_TTL].Match = DetectTtlMatch;
65  sigmatch_table[DETECT_TTL].Setup = DetectTtlSetup;
67 #ifdef UNITTESTS
69 #endif
70  sigmatch_table[DETECT_TTL].SupportsPrefilter = PrefilterTtlIsPrefilterable;
71  sigmatch_table[DETECT_TTL].SetupPrefilter = PrefilterSetupTtl;
72 
73  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
74  return;
75 }
76 
77 static inline int TtlMatch(const uint8_t pttl, const uint8_t mode,
78  const uint8_t dttl1, const uint8_t dttl2)
79 {
80  if (mode == DETECT_TTL_EQ && pttl == dttl1)
81  return 1;
82  else if (mode == DETECT_TTL_LT && pttl < dttl1)
83  return 1;
84  else if (mode == DETECT_TTL_GT && pttl > dttl1)
85  return 1;
86  else if (mode == DETECT_TTL_RA && (pttl > dttl1 && pttl < dttl2))
87  return 1;
88 
89  return 0;
90 }
91 
92 /**
93  * \brief This function is used to match TTL rule option on a packet with
94  * those passed via ttl
95  *
96  * \param t pointer to thread vars
97  * \param det_ctx pointer to the pattern matcher thread
98  * \param p pointer to the current packet
99  * \param m pointer to the sigmatch that we will cast into DetectTtlData
100  *
101  * \retval 0 no match
102  * \retval 1 match
103  */
104 static int DetectTtlMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
105  const Signature *s, const SigMatchCtx *ctx)
106 {
107  if (PKT_IS_PSEUDOPKT(p))
108  return 0;
109 
110  uint8_t pttl;
111  if (PKT_IS_IPV4(p)) {
112  pttl = IPV4_GET_IPTTL(p);
113  } else if (PKT_IS_IPV6(p)) {
114  pttl = IPV6_GET_HLIM(p);
115  } else {
116  SCLogDebug("Packet is of not IPv4 or IPv6");
117  return 0;
118  }
119 
120  const DetectTtlData *ttld = (const DetectTtlData *)ctx;
121  return TtlMatch(pttl, ttld->mode, ttld->ttl1, ttld->ttl2);
122 }
123 
124 /**
125  * \brief This function is used to parse ttl options passed via ttl: keyword
126  *
127  * \param ttlstr Pointer to the user provided ttl options
128  *
129  * \retval ttld pointer to DetectTtlData on success
130  * \retval NULL on failure
131  */
132 
133 static DetectTtlData *DetectTtlParse (const char *ttlstr)
134 {
135  int ov[MAX_SUBSTRINGS];
136  char arg1[6] = "";
137  char arg2[6] = "";
138  char arg3[6] = "";
139 
140  int ret = DetectParsePcreExec(&parse_regex, ttlstr, 0, 0, ov, MAX_SUBSTRINGS);
141  if (ret < 2 || ret > 4) {
142  SCLogError(SC_ERR_PCRE_MATCH, "parse error, ret %" PRId32 "", ret);
143  return NULL;
144  }
145 
146  int res = pcre_copy_substring((char *) ttlstr, ov, MAX_SUBSTRINGS, 1, arg1, sizeof(arg1));
147  if (res < 0) {
148  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
149  return NULL;
150  }
151  SCLogDebug("arg1 \"%s\"", arg1);
152 
153  if (ret >= 3) {
154  res = pcre_copy_substring((char *) ttlstr, ov, MAX_SUBSTRINGS, 2, arg2, sizeof(arg2));
155  if (res < 0) {
156  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
157  return NULL;
158  }
159  SCLogDebug("arg2 \"%s\"", arg2);
160 
161  if (ret >= 4) {
162  res = pcre_copy_substring((char *) ttlstr, ov, MAX_SUBSTRINGS, 3, arg3, sizeof(arg3));
163  if (res < 0) {
164  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
165  return NULL;
166  }
167  SCLogDebug("arg3 \"%s\"", arg3);
168  }
169  }
170 
171  int ttl1 = 0;
172  int ttl2 = 0;
173  int mode = 0;
174 
175  if (strlen(arg2) > 0) {
176  switch (arg2[0]) {
177  case '<':
178  if (strlen(arg3) == 0)
179  return NULL;
180 
181  mode = DETECT_TTL_LT;
182  ttl1 = atoi(arg3);
183 
184  SCLogDebug("ttl is %d",ttl1);
185  if (strlen(arg1) > 0)
186  return NULL;
187 
188  break;
189  case '>':
190  if (strlen(arg3) == 0)
191  return NULL;
192 
193  mode = DETECT_TTL_GT;
194  ttl1 = atoi(arg3);
195 
196  SCLogDebug("ttl is %d",ttl1);
197  if (strlen(arg1) > 0)
198  return NULL;
199 
200  break;
201  case '-':
202  if (strlen(arg1) == 0 || strlen(arg3) == 0)
203  return NULL;
204 
205  mode = DETECT_TTL_RA;
206  ttl1 = atoi(arg1);
207  ttl2 = atoi(arg3);
208 
209  SCLogDebug("ttl is %d to %d",ttl1, ttl2);
210  if (ttl1 >= ttl2) {
211  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid ttl range");
212  return NULL;
213  }
214  break;
215  default:
216  mode = DETECT_TTL_EQ;
217 
218  if ((strlen(arg2) > 0) ||
219  (strlen(arg3) > 0) ||
220  (strlen(arg1) == 0))
221  return NULL;
222 
223  ttl1 = atoi(arg1);
224  break;
225  }
226  } else {
227  mode = DETECT_TTL_EQ;
228 
229  if ((strlen(arg3) > 0) ||
230  (strlen(arg1) == 0))
231  return NULL;
232 
233  ttl1 = atoi(arg1);
234  }
235 
236  if (ttl1 < 0 || ttl1 > UCHAR_MAX ||
237  ttl2 < 0 || ttl2 > UCHAR_MAX) {
238  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid ttl value(s)");
239  return NULL;
240  }
241 
242  DetectTtlData *ttld = SCMalloc(sizeof(DetectTtlData));
243  if (unlikely(ttld == NULL))
244  return NULL;
245  ttld->ttl1 = (uint8_t)ttl1;
246  ttld->ttl2 = (uint8_t)ttl2;
247  ttld->mode = mode;
248 
249  return ttld;
250 }
251 
252 /**
253  * \brief this function is used to attld the parsed ttl data into the current signature
254  *
255  * \param de_ctx pointer to the Detection Engine Context
256  * \param s pointer to the Current Signature
257  * \param ttlstr pointer to the user provided ttl options
258  *
259  * \retval 0 on Success
260  * \retval -1 on Failure
261  */
262 static int DetectTtlSetup (DetectEngineCtx *de_ctx, Signature *s, const char *ttlstr)
263 {
264  DetectTtlData *ttld = DetectTtlParse(ttlstr);
265  if (ttld == NULL)
266  return -1;
267 
268  SigMatch *sm = SigMatchAlloc();
269  if (sm == NULL) {
270  DetectTtlFree(de_ctx, ttld);
271  return -1;
272  }
273 
274  sm->type = DETECT_TTL;
275  sm->ctx = (SigMatchCtx *)ttld;
276 
279  return 0;
280 }
281 
282 /**
283  * \brief this function will free memory associated with DetectTtlData
284  *
285  * \param ptr pointer to DetectTtlData
286  */
288 {
289  DetectTtlData *ttld = (DetectTtlData *)ptr;
290  SCFree(ttld);
291 }
292 
293 /* prefilter code */
294 
295 static void
296 PrefilterPacketTtlMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
297 {
298  if (PKT_IS_PSEUDOPKT(p)) {
299  SCReturn;
300  }
301 
302  uint8_t pttl;
303  if (PKT_IS_IPV4(p)) {
304  pttl = IPV4_GET_IPTTL(p);
305  } else if (PKT_IS_IPV6(p)) {
306  pttl = IPV6_GET_HLIM(p);
307  } else {
308  SCLogDebug("Packet is of not IPv4 or IPv6");
309  return;
310  }
311 
312  const PrefilterPacketHeaderCtx *ctx = pectx;
313  if (PrefilterPacketHeaderExtraMatch(ctx, p) == FALSE)
314  return;
315 
316  if (TtlMatch(pttl, ctx->v1.u8[0], ctx->v1.u8[1], ctx->v1.u8[2]))
317  {
318  SCLogDebug("packet matches ttl/hl %u", pttl);
319  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
320  }
321 }
322 
323 static void
324 PrefilterPacketTtlSet(PrefilterPacketHeaderValue *v, void *smctx)
325 {
326  const DetectTtlData *a = smctx;
327  v->u8[0] = a->mode;
328  v->u8[1] = a->ttl1;
329  v->u8[2] = a->ttl2;
330 }
331 
332 static bool
333 PrefilterPacketTtlCompare(PrefilterPacketHeaderValue v, void *smctx)
334 {
335  const DetectTtlData *a = smctx;
336  if (v.u8[0] == a->mode &&
337  v.u8[1] == a->ttl1 &&
338  v.u8[2] == a->ttl2)
339  return TRUE;
340  return FALSE;
341 }
342 
343 static int PrefilterSetupTtl(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
344 {
346  PrefilterPacketTtlSet,
347  PrefilterPacketTtlCompare,
348  PrefilterPacketTtlMatch);
349 }
350 
351 static bool PrefilterTtlIsPrefilterable(const Signature *s)
352 {
353  const SigMatch *sm;
354  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
355  switch (sm->type) {
356  case DETECT_TTL:
357  return TRUE;
358  }
359  }
360  return FALSE;
361 }
362 
363 #ifdef UNITTESTS
364 #include "tests/detect-ttl.c"
365 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DETECT_TTL
@ DETECT_TTL
Definition: detect-engine-register.h:43
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1192
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:1125
PKT_IS_IPV6
#define PKT_IS_IPV6(p)
Definition: decode.h:255
stream-tcp.h
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1337
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1103
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:761
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
PrefilterPacketHeaderCtx_::sigs_array
SigIntId * sigs_array
Definition: detect-engine-prefilter-common.h:41
PrefilterPacketHeaderValue::u8
uint8_t u8[16]
Definition: detect-engine-prefilter-common.h:22
DetectTtlData_
Definition: detect-ttl.h:32
PrefilterPacketHeaderCtx_::sigs_cnt
uint32_t sigs_cnt
Definition: detect-engine-prefilter-common.h:40
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
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1190
DetectTtlData_::ttl2
uint8_t ttl2
Definition: detect-ttl.h:34
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:33
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
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our ttl options.
Definition: detect-ttl.c:40
DetectEngineThreadCtx_
Definition: detect.h:1004
DetectTtlData_::ttl1
uint8_t ttl1
Definition: detect-ttl.h:33
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2446
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:322
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:89
TRUE
#define TRUE
Definition: suricata-common.h:33
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:321
FALSE
#define FALSE
Definition: suricata-common.h:34
SCReturn
#define SCReturn
Definition: util-debug.h:302
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:2378
Packet_
Definition: decode.h:411
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:591
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1171
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:516
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
detect-ttl.c
detect-ttl.h
DetectTtlData_::mode
uint8_t mode
Definition: detect-ttl.h:36
SigMatch_::type
uint8_t type
Definition: detect.h:319
PrefilterPacketHeaderCtx_::v1
PrefilterPacketHeaderValue v1
Definition: detect-engine-prefilter-common.h:34
DETECT_TTL_EQ
#define DETECT_TTL_EQ
Definition: detect-ttl.h:28
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:313
DETECT_TTL_RA
#define DETECT_TTL_RA
Definition: detect-ttl.h:30
DetectTtlRegisterTests
void DetectTtlRegisterTests(void)
this function registers unit tests for DetectTtl
Definition: detect-ttl.c:209
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectParseRegex_
Definition: detect-parse.h:42
DETECT_TTL_GT
#define DETECT_TTL_GT
Definition: detect-ttl.h:29
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
PrefilterSetupPacketHeader
int PrefilterSetupPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, void(*Set)(PrefilterPacketHeaderValue *v, void *), bool(*Compare)(PrefilterPacketHeaderValue v, void *), void(*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
Definition: detect-engine-prefilter-common.c:407
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1189
DetectTtlFree
void DetectTtlFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectTtlData
Definition: detect-ttl.c:287
detect-parse.h
Signature_
Signature container.
Definition: detect.h:522
SigMatch_
a single match condition for a signature
Definition: detect.h:318
IPV6_GET_HLIM
#define IPV6_GET_HLIM(p)
Definition: decode-ipv6.h:90
PrefilterPacketHeaderValue
Definition: detect-engine-prefilter-common.h:21
detect-engine-prefilter-common.h
DETECT_TTL_LT
#define DETECT_TTL_LT
Definition: detect-ttl.h:27
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:254
DetectTtlRegister
void DetectTtlRegister(void)
Registration function for ttl: keyword.
Definition: detect-ttl.c:59
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
IPV4_GET_IPTTL
#define IPV4_GET_IPTTL(p)
Definition: decode-ipv4.h:146
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1193
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223