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  int ov[MAX_SUBSTRINGS];
132 
133  ret = DetectParsePcreExec(&parse_regex, tcpmssstr, 0, 0, ov, MAX_SUBSTRINGS);
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 = pcre_get_substring((char *) tcpmssstr, ov, MAX_SUBSTRINGS, 1, &str_ptr);
141  if (res < 0) {
142  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
143  goto error;
144  }
145  arg1 = (char *) str_ptr;
146  SCLogDebug("Arg1 \"%s\"", arg1);
147 
148  if (ret >= 3) {
149  res = pcre_get_substring((char *) tcpmssstr, ov, MAX_SUBSTRINGS, 2, &str_ptr);
150  if (res < 0) {
151  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
152  goto error;
153  }
154  arg2 = (char *) str_ptr;
155  SCLogDebug("Arg2 \"%s\"", arg2);
156 
157  if (ret >= 4) {
158  res = pcre_get_substring((char *) tcpmssstr, ov, MAX_SUBSTRINGS, 3, &str_ptr);
159  if (res < 0) {
160  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
161  goto error;
162  }
163  arg3 = (char *) str_ptr;
164  SCLogDebug("Arg3 \"%s\"", arg3);
165  }
166  }
167 
168  tcpmssd = SCMalloc(sizeof (DetectTcpmssData));
169  if (unlikely(tcpmssd == NULL))
170  goto error;
171  tcpmssd->arg1 = 0;
172  tcpmssd->arg2 = 0;
173 
174  if (arg2 != NULL) {
175  /*set the values*/
176  switch(arg2[0]) {
177  case '<':
178  if (arg3 == NULL)
179  goto error;
180 
181  tcpmssd->mode = DETECT_TCPMSS_LT;
182  if (StringParseUint16(&tcpmssd->arg1, 10, 0, (const char *)arg3) < 0) {
183  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg: %s", arg3);
184  goto error;
185  }
186  SCLogDebug("tcpmss is %"PRIu16"",tcpmssd->arg1);
187  if (strlen(arg1) > 0)
188  goto error;
189 
190  break;
191  case '>':
192  if (arg3 == NULL)
193  goto error;
194 
195  tcpmssd->mode = DETECT_TCPMSS_GT;
196  if (StringParseUint16(&tcpmssd->arg1, 10, 0, (const char *)arg3) < 0) {
197  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg: %s", arg3);
198  goto error;
199  }
200  SCLogDebug("tcpmss is %"PRIu16"",tcpmssd->arg1);
201  if (strlen(arg1) > 0)
202  goto error;
203 
204  break;
205  case '-':
206  if (arg1 == NULL || strlen(arg1)== 0)
207  goto error;
208  if (arg3 == NULL || strlen(arg3)== 0)
209  goto error;
210 
211  tcpmssd->mode = DETECT_TCPMSS_RA;
212  if (StringParseUint16(&tcpmssd->arg1, 10, 0, (const char *)arg1) < 0) {
213  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg: %s", arg1);
214  goto error;
215  }
216  if (StringParseUint16(&tcpmssd->arg2, 10, 0, (const char *)arg3) < 0) {
217  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid second arg: %s", arg3);
218  goto error;
219  }
220  SCLogDebug("tcpmss is %"PRIu16" to %"PRIu16"",tcpmssd->arg1, tcpmssd->arg2);
221  if (tcpmssd->arg1 >= tcpmssd->arg2) {
222  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid tcpmss range. ");
223  goto error;
224  }
225  break;
226  default:
227  tcpmssd->mode = DETECT_TCPMSS_EQ;
228 
229  if ((arg2 != NULL && strlen(arg2) > 0) ||
230  (arg3 != NULL && strlen(arg3) > 0) ||
231  (arg1 == NULL ||strlen(arg1) == 0))
232  goto error;
233 
234  if (StringParseUint16(&tcpmssd->arg1, 10, 0, (const char *)arg1) < 0) {
235  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg: %s", arg1);
236  goto error;
237  }
238  break;
239  }
240  } else {
241  tcpmssd->mode = DETECT_TCPMSS_EQ;
242 
243  if ((arg3 != NULL && strlen(arg3) > 0) ||
244  (arg1 == NULL ||strlen(arg1) == 0))
245  goto error;
246 
247  if (StringParseUint16(&tcpmssd->arg1, 10, 0, (const char *)arg1) < 0) {
248  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg: %s", arg1);
249  goto error;
250  }
251  }
252 
253  SCFree(arg1);
254  SCFree(arg2);
255  SCFree(arg3);
256  return tcpmssd;
257 
258 error:
259  if (tcpmssd)
260  SCFree(tcpmssd);
261  if (arg1)
262  SCFree(arg1);
263  if (arg2)
264  SCFree(arg2);
265  if (arg3)
266  SCFree(arg3);
267  return NULL;
268 }
269 
270 /**
271  * \brief this function is used to attach the parsed tcpmss data into the current signature
272  *
273  * \param de_ctx pointer to the Detection Engine Context
274  * \param s pointer to the Current Signature
275  * \param tcpmssstr pointer to the user provided tcpmss options
276  *
277  * \retval 0 on Success
278  * \retval -1 on Failure
279  */
280 static int DetectTcpmssSetup (DetectEngineCtx *de_ctx, Signature *s, const char *tcpmssstr)
281 {
282  DetectTcpmssData *tcpmssd = DetectTcpmssParse(tcpmssstr);
283  if (tcpmssd == NULL)
284  return -1;
285 
286  SigMatch *sm = SigMatchAlloc();
287  if (sm == NULL) {
288  DetectTcpmssFree(de_ctx, tcpmssd);
289  return -1;
290  }
291 
292  sm->type = DETECT_TCPMSS;
293  sm->ctx = (SigMatchCtx *)tcpmssd;
294 
297 
298  return 0;
299 }
300 
301 /**
302  * \brief this function will free memory associated with DetectTcpmssData
303  *
304  * \param ptr pointer to DetectTcpmssData
305  */
307 {
308  DetectTcpmssData *tcpmssd = (DetectTcpmssData *)ptr;
309  SCFree(tcpmssd);
310 }
311 
312 /* prefilter code */
313 
314 static void
315 PrefilterPacketTcpmssMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
316 {
317  if (!(PKT_IS_TCP(p)) || PKT_IS_PSEUDOPKT(p))
318  return;
319 
320  if (!(TCP_HAS_MSS(p)))
321  return;
322 
323  uint16_t ptcpmss = TCP_GET_MSS(p);
324 
325  /* during setup Suricata will automatically see if there is another
326  * check that can be added: alproto, sport or dport */
327  const PrefilterPacketHeaderCtx *ctx = pectx;
328  if (PrefilterPacketHeaderExtraMatch(ctx, p) == FALSE)
329  return;
330 
331  /* if we match, add all the sigs that use this prefilter. This means
332  * that these will be inspected further */
333  if (TcpmssMatch(ptcpmss, ctx->v1.u8[0], ctx->v1.u16[1], ctx->v1.u16[2]))
334  {
335  SCLogDebug("packet matches tcpmss/hl %u", ptcpmss);
336  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
337  }
338 }
339 
340 static void
341 PrefilterPacketTcpmssSet(PrefilterPacketHeaderValue *v, void *smctx)
342 {
343  const DetectTcpmssData *a = smctx;
344  v->u8[0] = a->mode;
345  v->u16[1] = a->arg1;
346  v->u16[2] = a->arg2;
347 }
348 
349 static bool
350 PrefilterPacketTcpmssCompare(PrefilterPacketHeaderValue v, void *smctx)
351 {
352  const DetectTcpmssData *a = smctx;
353  if (v.u8[0] == a->mode &&
354  v.u16[1] == a->arg1 &&
355  v.u16[2] == a->arg2)
356  return true;
357  return false;
358 }
359 
360 static int PrefilterSetupTcpmss(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
361 {
363  PrefilterPacketTcpmssSet,
364  PrefilterPacketTcpmssCompare,
365  PrefilterPacketTcpmssMatch);
366 }
367 
368 static bool PrefilterTcpmssIsPrefilterable(const Signature *s)
369 {
370  const SigMatch *sm;
371  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
372  switch (sm->type) {
373  case DETECT_TCPMSS:
374  return true;
375  }
376  }
377  return false;
378 }
379 
380 #ifdef UNITTESTS
381 #include "tests/detect-tcpmss.c"
382 #endif
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1214
StringParseUint16
int StringParseUint16(uint16_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:336
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our options.
Definition: detect-tcpmss.c:37
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
DetectTcpmssRegister
void DetectTcpmssRegister(void)
Registration function for tcpmss: keyword.
Definition: detect-tcpmss.c:56
SigTableElmt_::name
const char * name
Definition: detect.h:1211
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1147
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1347
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
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:1111
detect-tcpmss.h
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
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:94
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:1196
DetectTcpmssFree
void DetectTcpmssFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectTcpmssData
Definition: detect-tcpmss.c:306
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:1199
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition: decode.h:259
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:1010
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2493
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:324
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:89
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
FALSE
#define FALSE
Definition: suricata-common.h:34
Signature_::flags
uint32_t flags
Definition: detect.h:529
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2423
Packet_
Definition: decode.h:414
TCP_GET_MSS
#define TCP_GET_MSS(p)
Definition: decode-tcp.h:105
DETECT_TCPMSS_EQ
#define DETECT_TCPMSS_EQ
Definition: detect-tcpmss.h:28
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:597
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1179
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:522
DetectTcpmssData_
Definition: detect-tcpmss.h:32
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:321
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:315
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_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: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
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:1198
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
SigMatch_
a single match condition for a signature
Definition: detect.h:320
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:263
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203
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