suricata
detect-tcpmss.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 Victor Julien <victor@inliniac.net>
22  *
23  */
24 
25 #include "suricata-common.h"
26 
27 #include "detect.h"
28 #include "detect-parse.h"
30 #include "util-byte.h"
31 
32 #include "detect-tcpmss.h"
33 
34 /**
35  * \brief Regex for parsing our options
36  */
37 #define PARSE_REGEX "^\\s*([0-9]*)?\\s*([<>=-]+)?\\s*([0-9]+)?\\s*$"
38 
39 static DetectParseRegex parse_regex;
40 
41 /* prototypes */
42 static int DetectTcpmssMatch (DetectEngineThreadCtx *, Packet *,
43  const Signature *, const SigMatchCtx *);
44 static int DetectTcpmssSetup (DetectEngineCtx *, Signature *, const char *);
45 void DetectTcpmssFree (DetectEngineCtx *, void *);
46 #ifdef UNITTESTS
47 void DetectTcpmssRegisterTests (void);
48 #endif
49 static int PrefilterSetupTcpmss(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
50 static bool PrefilterTcpmssIsPrefilterable(const Signature *s);
51 
52 /**
53  * \brief Registration function for tcpmss: keyword
54  */
55 
57 {
58  sigmatch_table[DETECT_TCPMSS].name = "tcp.mss";
59  sigmatch_table[DETECT_TCPMSS].desc = "match on TCP MSS option field";
60  sigmatch_table[DETECT_TCPMSS].url = "/rules/header-keywords.html#tcpmss";
61  sigmatch_table[DETECT_TCPMSS].Match = DetectTcpmssMatch;
62  sigmatch_table[DETECT_TCPMSS].Setup = DetectTcpmssSetup;
64 #ifdef UNITTESTS
66 #endif
67  sigmatch_table[DETECT_TCPMSS].SupportsPrefilter = PrefilterTcpmssIsPrefilterable;
68  sigmatch_table[DETECT_TCPMSS].SetupPrefilter = PrefilterSetupTcpmss;
69 
70  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
71  return;
72 }
73 
74 static inline int TcpmssMatch(const uint16_t parg, const uint8_t mode,
75  const uint16_t darg1, const uint16_t darg2)
76 {
77  if (mode == DETECT_TCPMSS_EQ && parg == darg1)
78  return 1;
79  else if (mode == DETECT_TCPMSS_LT && parg < darg1)
80  return 1;
81  else if (mode == DETECT_TCPMSS_GT && parg > darg1)
82  return 1;
83  else if (mode == DETECT_TCPMSS_RA && (parg > darg1 && parg < darg2))
84  return 1;
85 
86  return 0;
87 }
88 
89 /**
90  * \brief This function is used to match TCPMSS rule option on a packet with those passed via tcpmss:
91  *
92  * \param det_ctx pointer to the pattern matcher thread
93  * \param p pointer to the current packet
94  * \param ctx pointer to the sigmatch that we will cast into DetectTcpmssData
95  *
96  * \retval 0 no match
97  * \retval 1 match
98  */
99 static int DetectTcpmssMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
100  const Signature *s, const SigMatchCtx *ctx)
101 {
102 
103  if (!(PKT_IS_TCP(p)) || PKT_IS_PSEUDOPKT(p))
104  return 0;
105 
106  if (!(TCP_HAS_MSS(p)))
107  return 0;
108 
109  uint16_t ptcpmss = TCP_GET_MSS(p);
110 
111  const DetectTcpmssData *tcpmssd = (const DetectTcpmssData *)ctx;
112  return TcpmssMatch(ptcpmss, tcpmssd->mode, tcpmssd->arg1, tcpmssd->arg2);
113 }
114 
115 /**
116  * \brief This function is used to parse tcpmss options passed via tcpmss: keyword
117  *
118  * \param tcpmssstr Pointer to the user provided tcpmss options
119  *
120  * \retval tcpmssd pointer to DetectTcpmssData on success
121  * \retval NULL on failure
122  */
123 
124 static DetectTcpmssData *DetectTcpmssParse (const char *tcpmssstr)
125 {
126  DetectTcpmssData *tcpmssd = NULL;
127  char *arg1 = NULL;
128  char *arg2 = NULL;
129  char *arg3 = NULL;
130  int ret = 0, res = 0;
131  size_t pcre2_len;
132 
133  ret = DetectParsePcreExec(&parse_regex, tcpmssstr, 0, 0);
134  if (ret < 2 || ret > 4) {
135  SCLogError(SC_ERR_PCRE_MATCH, "parse error, ret %" PRId32 "", ret);
136  goto error;
137  }
138  const char *str_ptr;
139 
140  res = pcre2_substring_get_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
141  if (res < 0) {
142  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_get_bynumber failed");
143  goto error;
144  }
145  arg1 = (char *) str_ptr;
146  SCLogDebug("Arg1 \"%s\"", arg1);
147 
148  if (ret >= 3) {
149  res = pcre2_substring_get_bynumber(
150  parse_regex.match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
151  if (res < 0) {
152  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_get_bynumber failed");
153  goto error;
154  }
155  arg2 = (char *) str_ptr;
156  SCLogDebug("Arg2 \"%s\"", arg2);
157 
158  if (ret >= 4) {
159  res = pcre2_substring_get_bynumber(
160  parse_regex.match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
161  if (res < 0) {
162  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_get_bynumber failed");
163  goto error;
164  }
165  arg3 = (char *) str_ptr;
166  SCLogDebug("Arg3 \"%s\"", arg3);
167  }
168  }
169 
170  tcpmssd = SCMalloc(sizeof (DetectTcpmssData));
171  if (unlikely(tcpmssd == NULL))
172  goto error;
173  tcpmssd->arg1 = 0;
174  tcpmssd->arg2 = 0;
175 
176  if (arg2 != NULL) {
177  /*set the values*/
178  switch(arg2[0]) {
179  case '<':
180  if (arg3 == NULL)
181  goto error;
182 
183  tcpmssd->mode = DETECT_TCPMSS_LT;
184  if (StringParseUint16(&tcpmssd->arg1, 10, 0, (const char *)arg3) < 0) {
185  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg: %s", arg3);
186  goto error;
187  }
188  SCLogDebug("tcpmss is %"PRIu16"",tcpmssd->arg1);
189  if (strlen(arg1) > 0)
190  goto error;
191 
192  break;
193  case '>':
194  if (arg3 == NULL)
195  goto error;
196 
197  tcpmssd->mode = DETECT_TCPMSS_GT;
198  if (StringParseUint16(&tcpmssd->arg1, 10, 0, (const char *)arg3) < 0) {
199  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg: %s", arg3);
200  goto error;
201  }
202  SCLogDebug("tcpmss is %"PRIu16"",tcpmssd->arg1);
203  if (strlen(arg1) > 0)
204  goto error;
205 
206  break;
207  case '-':
208  if (arg1 == NULL || strlen(arg1)== 0)
209  goto error;
210  if (arg3 == NULL || strlen(arg3)== 0)
211  goto error;
212 
213  tcpmssd->mode = DETECT_TCPMSS_RA;
214  if (StringParseUint16(&tcpmssd->arg1, 10, 0, (const char *)arg1) < 0) {
215  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg: %s", arg1);
216  goto error;
217  }
218  if (StringParseUint16(&tcpmssd->arg2, 10, 0, (const char *)arg3) < 0) {
219  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid second arg: %s", arg3);
220  goto error;
221  }
222  SCLogDebug("tcpmss is %"PRIu16" to %"PRIu16"",tcpmssd->arg1, tcpmssd->arg2);
223  if (tcpmssd->arg1 >= tcpmssd->arg2) {
224  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid tcpmss range. ");
225  goto error;
226  }
227  break;
228  default:
229  tcpmssd->mode = DETECT_TCPMSS_EQ;
230 
231  if ((arg2 != NULL && strlen(arg2) > 0) ||
232  (arg3 != NULL && strlen(arg3) > 0) ||
233  (arg1 == NULL ||strlen(arg1) == 0))
234  goto error;
235 
236  if (StringParseUint16(&tcpmssd->arg1, 10, 0, (const char *)arg1) < 0) {
237  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg: %s", arg1);
238  goto error;
239  }
240  break;
241  }
242  } else {
243  tcpmssd->mode = DETECT_TCPMSS_EQ;
244 
245  if ((arg3 != NULL && strlen(arg3) > 0) ||
246  (arg1 == NULL ||strlen(arg1) == 0))
247  goto error;
248 
249  if (StringParseUint16(&tcpmssd->arg1, 10, 0, (const char *)arg1) < 0) {
250  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg: %s", arg1);
251  goto error;
252  }
253  }
254 
255  pcre2_substring_free((PCRE2_UCHAR8 *)arg1);
256  pcre2_substring_free((PCRE2_UCHAR8 *)arg2);
257  pcre2_substring_free((PCRE2_UCHAR8 *)arg3);
258  return tcpmssd;
259 
260 error:
261  if (tcpmssd)
262  SCFree(tcpmssd);
263  if (arg1)
264  pcre2_substring_free((PCRE2_UCHAR8 *)arg1);
265  if (arg2)
266  pcre2_substring_free((PCRE2_UCHAR8 *)arg2);
267  if (arg3)
268  pcre2_substring_free((PCRE2_UCHAR8 *)arg3);
269  return NULL;
270 }
271 
272 /**
273  * \brief this function is used to attach the parsed tcpmss data into the current signature
274  *
275  * \param de_ctx pointer to the Detection Engine Context
276  * \param s pointer to the Current Signature
277  * \param tcpmssstr pointer to the user provided tcpmss options
278  *
279  * \retval 0 on Success
280  * \retval -1 on Failure
281  */
282 static int DetectTcpmssSetup (DetectEngineCtx *de_ctx, Signature *s, const char *tcpmssstr)
283 {
284  DetectTcpmssData *tcpmssd = DetectTcpmssParse(tcpmssstr);
285  if (tcpmssd == NULL)
286  return -1;
287 
288  SigMatch *sm = SigMatchAlloc();
289  if (sm == NULL) {
290  DetectTcpmssFree(de_ctx, tcpmssd);
291  return -1;
292  }
293 
294  sm->type = DETECT_TCPMSS;
295  sm->ctx = (SigMatchCtx *)tcpmssd;
296 
299 
300  return 0;
301 }
302 
303 /**
304  * \brief this function will free memory associated with DetectTcpmssData
305  *
306  * \param ptr pointer to DetectTcpmssData
307  */
309 {
310  DetectTcpmssData *tcpmssd = (DetectTcpmssData *)ptr;
311  SCFree(tcpmssd);
312 }
313 
314 /* prefilter code */
315 
316 static void
317 PrefilterPacketTcpmssMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
318 {
319  if (!(PKT_IS_TCP(p)) || PKT_IS_PSEUDOPKT(p))
320  return;
321 
322  if (!(TCP_HAS_MSS(p)))
323  return;
324 
325  uint16_t ptcpmss = TCP_GET_MSS(p);
326 
327  /* during setup Suricata will automatically see if there is another
328  * check that can be added: alproto, sport or dport */
329  const PrefilterPacketHeaderCtx *ctx = pectx;
330  if (!PrefilterPacketHeaderExtraMatch(ctx, p))
331  return;
332 
333  /* if we match, add all the sigs that use this prefilter. This means
334  * that these will be inspected further */
335  if (TcpmssMatch(ptcpmss, ctx->v1.u8[0], ctx->v1.u16[1], ctx->v1.u16[2]))
336  {
337  SCLogDebug("packet matches tcpmss/hl %u", ptcpmss);
338  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
339  }
340 }
341 
342 static void
343 PrefilterPacketTcpmssSet(PrefilterPacketHeaderValue *v, void *smctx)
344 {
345  const DetectTcpmssData *a = smctx;
346  v->u8[0] = a->mode;
347  v->u16[1] = a->arg1;
348  v->u16[2] = a->arg2;
349 }
350 
351 static bool
352 PrefilterPacketTcpmssCompare(PrefilterPacketHeaderValue v, void *smctx)
353 {
354  const DetectTcpmssData *a = smctx;
355  if (v.u8[0] == a->mode &&
356  v.u16[1] == a->arg1 &&
357  v.u16[2] == a->arg2)
358  return true;
359  return false;
360 }
361 
362 static int PrefilterSetupTcpmss(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
363 {
365  PrefilterPacketTcpmssSet,
366  PrefilterPacketTcpmssCompare,
367  PrefilterPacketTcpmssMatch);
368 }
369 
370 static bool PrefilterTcpmssIsPrefilterable(const Signature *s)
371 {
372  const SigMatch *sm;
373  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
374  switch (sm->type) {
375  case DETECT_TCPMSS:
376  return true;
377  }
378  }
379  return false;
380 }
381 
382 #ifdef UNITTESTS
383 #include "tests/detect-tcpmss.c"
384 #endif
util-byte.h
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:45
SigTableElmt_::url
const char * url
Definition: detect.h:1270
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our options.
Definition: detect-tcpmss.c:37
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2474
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1257
DetectTcpmssRegister
void DetectTcpmssRegister(void)
Registration function for tcpmss: keyword.
Definition: detect-tcpmss.c:56
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1267
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1224
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1425
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DETECT_TCPMSS_GT
#define DETECT_TCPMSS_GT
Definition: detect-tcpmss.h:29
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1167
detect-tcpmss.h
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:336
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
DETECT_TCPMSS_RA
#define DETECT_TCPMSS_RA
Definition: detect-tcpmss.h:30
PrefilterPacketHeaderValue::u8
uint8_t u8[16]
Definition: detect-engine-prefilter-common.h:22
TCP_HAS_MSS
#define TCP_HAS_MSS(p)
Definition: decode-tcp.h:96
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:1252
DetectTcpmssFree
void DetectTcpmssFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectTcpmssData
Definition: detect-tcpmss.c:308
PrefilterPacketHeaderValue::u16
uint16_t u16[8]
Definition: detect-engine-prefilter-common.h:23
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1255
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition: decode.h:264
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:33
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1060
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2597
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:325
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:89
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:324
Signature_::flags
uint32_t flags
Definition: detect.h:549
Packet_
Definition: decode.h:427
TCP_GET_MSS
#define TCP_GET_MSS(p)
Definition: decode-tcp.h:107
DETECT_TCPMSS_EQ
#define DETECT_TCPMSS_EQ
Definition: detect-tcpmss.h:28
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:619
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1235
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:542
DetectTcpmssData_
Definition: detect-tcpmss.h:32
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
PrefilterPacketHeaderCtx_::v1
PrefilterPacketHeaderValue v1
Definition: detect-engine-prefilter-common.h:34
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:316
DetectTcpmssRegisterTests
void DetectTcpmssRegisterTests(void)
this function registers unit tests for DetectTcpmss
Definition: detect-tcpmss.c:143
DetectTcpmssData_::arg2
uint16_t arg2
Definition: detect-tcpmss.h:34
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:322
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
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:417
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
DetectTcpmssData_::arg1
uint16_t arg1
Definition: detect-tcpmss.h:33
detect-tcpmss.c
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1254
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
SigMatch_
a single match condition for a signature
Definition: detect.h:321
PrefilterPacketHeaderValue
Definition: detect-engine-prefilter-common.h:21
DetectTcpmssData_::mode
uint8_t mode
Definition: detect-tcpmss.h:36
detect-engine-prefilter-common.h
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
DETECT_TCPMSS
@ DETECT_TCPMSS
Definition: detect-engine-register.h:266
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259
DETECT_TCPMSS_LT
#define DETECT_TCPMSS_LT
Definition: detect-tcpmss.h:27
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223