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