suricata
detect-tcpmss.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 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 pcre *parse_regex;
39 static pcre_extra *parse_regex_study;
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 (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 = DOC_URL DOC_VERSION "/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, &parse_regex_study);
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 t pointer to thread vars
93  * \param det_ctx pointer to the pattern matcher thread
94  * \param p pointer to the current packet
95  * \param m pointer to the sigmatch that we will cast into DetectTcpmssData
96  *
97  * \retval 0 no match
98  * \retval 1 match
99  */
100 static int DetectTcpmssMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
101  const Signature *s, const SigMatchCtx *ctx)
102 {
103 
104  if (!(PKT_IS_TCP(p)) || PKT_IS_PSEUDOPKT(p))
105  return 0;
106 
107  if (!(TCP_HAS_MSS(p)))
108  return 0;
109 
110  uint16_t ptcpmss = TCP_GET_MSS(p);
111 
112  const DetectTcpmssData *tcpmssd = (const DetectTcpmssData *)ctx;
113  return TcpmssMatch(ptcpmss, tcpmssd->mode, tcpmssd->arg1, tcpmssd->arg2);
114 }
115 
116 /**
117  * \brief This function is used to parse tcpmss options passed via tcpmss: keyword
118  *
119  * \param tcpmssstr Pointer to the user provided tcpmss options
120  *
121  * \retval tcpmssd pointer to DetectTcpmssData on success
122  * \retval NULL on failure
123  */
124 
125 static DetectTcpmssData *DetectTcpmssParse (const char *tcpmssstr)
126 {
127  DetectTcpmssData *tcpmssd = NULL;
128  char *arg1 = NULL;
129  char *arg2 = NULL;
130  char *arg3 = NULL;
131 #define MAX_SUBSTRINGS 30
132  int ret = 0, res = 0;
133  int ov[MAX_SUBSTRINGS];
134 
135  ret = pcre_exec(parse_regex, parse_regex_study, tcpmssstr, strlen(tcpmssstr), 0, 0, ov, MAX_SUBSTRINGS);
136  if (ret < 2 || ret > 4) {
137  SCLogError(SC_ERR_PCRE_MATCH, "parse error, ret %" PRId32 "", ret);
138  goto error;
139  }
140  const char *str_ptr;
141 
142  res = pcre_get_substring((char *) tcpmssstr, ov, MAX_SUBSTRINGS, 1, &str_ptr);
143  if (res < 0) {
144  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
145  goto error;
146  }
147  arg1 = (char *) str_ptr;
148  SCLogDebug("Arg1 \"%s\"", arg1);
149 
150  if (ret >= 3) {
151  res = pcre_get_substring((char *) tcpmssstr, ov, MAX_SUBSTRINGS, 2, &str_ptr);
152  if (res < 0) {
153  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
154  goto error;
155  }
156  arg2 = (char *) str_ptr;
157  SCLogDebug("Arg2 \"%s\"", arg2);
158 
159  if (ret >= 4) {
160  res = pcre_get_substring((char *) tcpmssstr, ov, MAX_SUBSTRINGS, 3, &str_ptr);
161  if (res < 0) {
162  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring 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  tcpmssd->arg1 = (uint16_t) atoi(arg3);
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  tcpmssd->arg1 = (uint16_t) atoi(arg3);
197 
198  SCLogDebug("tcpmss is %"PRIu16"",tcpmssd->arg1);
199  if (strlen(arg1) > 0)
200  goto error;
201 
202  break;
203  case '-':
204  if (arg1 == NULL || strlen(arg1)== 0)
205  goto error;
206  if (arg3 == NULL || strlen(arg3)== 0)
207  goto error;
208 
209  tcpmssd->mode = DETECT_TCPMSS_RA;
210  tcpmssd->arg1 = (uint16_t) atoi(arg1);
211 
212  tcpmssd->arg2 = (uint16_t) atoi(arg3);
213  SCLogDebug("tcpmss is %"PRIu16" to %"PRIu16"",tcpmssd->arg1, tcpmssd->arg2);
214  if (tcpmssd->arg1 >= tcpmssd->arg2) {
215  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid tcpmss range. ");
216  goto error;
217  }
218  break;
219  default:
220  tcpmssd->mode = DETECT_TCPMSS_EQ;
221 
222  if ((arg2 != NULL && strlen(arg2) > 0) ||
223  (arg3 != NULL && strlen(arg3) > 0) ||
224  (arg1 == NULL ||strlen(arg1) == 0))
225  goto error;
226 
227  tcpmssd->arg1 = (uint16_t) atoi(arg1);
228  break;
229  }
230  } else {
231  tcpmssd->mode = DETECT_TCPMSS_EQ;
232 
233  if ((arg3 != NULL && strlen(arg3) > 0) ||
234  (arg1 == NULL ||strlen(arg1) == 0))
235  goto error;
236 
237  tcpmssd->arg1 = (uint16_t) atoi(arg1);
238  }
239 
240  SCFree(arg1);
241  SCFree(arg2);
242  SCFree(arg3);
243  return tcpmssd;
244 
245 error:
246  if (tcpmssd)
247  SCFree(tcpmssd);
248  if (arg1)
249  SCFree(arg1);
250  if (arg2)
251  SCFree(arg2);
252  if (arg3)
253  SCFree(arg3);
254  return NULL;
255 }
256 
257 /**
258  * \brief this function is used to atcpmssd the parsed tcpmss data into the current signature
259  *
260  * \param de_ctx pointer to the Detection Engine Context
261  * \param s pointer to the Current Signature
262  * \param tcpmssstr pointer to the user provided tcpmss options
263  *
264  * \retval 0 on Success
265  * \retval -1 on Failure
266  */
267 static int DetectTcpmssSetup (DetectEngineCtx *de_ctx, Signature *s, const char *tcpmssstr)
268 {
269  DetectTcpmssData *tcpmssd = DetectTcpmssParse(tcpmssstr);
270  if (tcpmssd == NULL)
271  return -1;
272 
273  SigMatch *sm = SigMatchAlloc();
274  if (sm == NULL) {
275  DetectTcpmssFree(tcpmssd);
276  return -1;
277  }
278 
279  sm->type = DETECT_TCPMSS;
280  sm->ctx = (SigMatchCtx *)tcpmssd;
281 
284 
285  return 0;
286 }
287 
288 /**
289  * \brief this function will free memory associated with DetectTcpmssData
290  *
291  * \param ptr pointer to DetectTcpmssData
292  */
293 void DetectTcpmssFree(void *ptr)
294 {
295  DetectTcpmssData *tcpmssd = (DetectTcpmssData *)ptr;
296  SCFree(tcpmssd);
297 }
298 
299 /* prefilter code */
300 
301 static void
302 PrefilterPacketTcpmssMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
303 {
304  if (!(PKT_IS_TCP(p)) || PKT_IS_PSEUDOPKT(p))
305  return;
306 
307  if (!(TCP_HAS_MSS(p)))
308  return;
309 
310  uint16_t ptcpmss = TCP_GET_MSS(p);
311 
312  /* during setup Suricata will automatically see if there is another
313  * check that can be added: alproto, sport or dport */
314  const PrefilterPacketHeaderCtx *ctx = pectx;
315  if (PrefilterPacketHeaderExtraMatch(ctx, p) == FALSE)
316  return;
317 
318  /* if we match, add all the sigs that use this prefilter. This means
319  * that these will be inspected further */
320  if (TcpmssMatch(ptcpmss, ctx->v1.u8[0], ctx->v1.u16[1], ctx->v1.u16[2]))
321  {
322  SCLogDebug("packet matches tcpmss/hl %u", ptcpmss);
323  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
324  }
325 }
326 
327 static void
328 PrefilterPacketTcpmssSet(PrefilterPacketHeaderValue *v, void *smctx)
329 {
330  const DetectTcpmssData *a = smctx;
331  v->u8[0] = a->mode;
332  v->u16[1] = a->arg1;
333  v->u16[2] = a->arg2;
334 }
335 
336 static _Bool
337 PrefilterPacketTcpmssCompare(PrefilterPacketHeaderValue v, void *smctx)
338 {
339  const DetectTcpmssData *a = smctx;
340  if (v.u8[0] == a->mode &&
341  v.u16[1] == a->arg1 &&
342  v.u16[2] == a->arg2)
343  return TRUE;
344  return FALSE;
345 }
346 
347 static int PrefilterSetupTcpmss(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
348 {
349  return PrefilterSetupPacketHeader(de_ctx, sgh, DETECT_TCPMSS,
350  PrefilterPacketTcpmssSet,
351  PrefilterPacketTcpmssCompare,
352  PrefilterPacketTcpmssMatch);
353 }
354 
355 static _Bool PrefilterTcpmssIsPrefilterable(const Signature *s)
356 {
357  const SigMatch *sm;
358  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
359  switch (sm->type) {
360  case DETECT_TCPMSS:
361  return TRUE;
362  }
363  }
364  return FALSE;
365 }
366 
367 #ifdef UNITTESTS
368 #include "tests/detect-tcpmss.c"
369 #endif
void DetectTcpmssRegister(void)
Registration function for tcpmss: keyword.
Definition: detect-tcpmss.c:56
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1439
SignatureInitData * init_data
Definition: detect.h:586
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1179
#define DETECT_TCPMSS_LT
Definition: detect-tcpmss.h:27
#define SCLogDebug(...)
Definition: util-debug.h:335
#define DETECT_TCPMSS_GT
Definition: detect-tcpmss.h:29
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1182
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))
uint32_t flags
Definition: detect.h:518
_Bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1181
#define PARSE_REGEX
Regex for parsing our options.
Definition: detect-tcpmss.c:36
#define FALSE
#define unlikely(expr)
Definition: util-optimize.h:35
#define TCP_HAS_MSS(p)
Definition: decode-tcp.h:95
Container for matching data for a signature group.
Definition: detect.h:1329
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:219
#define DETECT_TCPMSS_RA
Definition: detect-tcpmss.h:30
const char * name
Definition: detect.h:1193
Signature container.
Definition: detect.h:517
#define TRUE
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:308
struct SigMatch_ * next
Definition: detect.h:317
main detection engine ctx
Definition: detect.h:756
#define TCP_GET_MSS(p)
Definition: decode-tcp.h:106
void(* Free)(void *)
Definition: detect.h:1184
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void DetectSetupParseRegexes(const char *parse_str, pcre **parse_regex, pcre_extra **parse_regex_study)
void DetectTcpmssFree(void *)
this function will free memory associated with DetectTcpmssData
PrefilterRuleStore pmq
Definition: detect.h:1095
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1163
uint8_t type
Definition: detect.h:314
void DetectTcpmssRegisterTests(void)
this function registers unit tests for DetectTcpmss
const char * desc
Definition: detect.h:1195
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:288
struct SigMatch_ ** smlists
Definition: detect.h:511
SigMatchCtx * ctx
Definition: detect.h:316
#define SCMalloc(a)
Definition: util-mem.h:222
#define SCFree(a)
Definition: util-mem.h:322
PoolThreadReserved res
#define PKT_IS_TCP(p)
Definition: decode.h:253
#define DETECT_TCPMSS_EQ
Definition: detect-tcpmss.h:28
const char * url
Definition: detect.h:1196
#define MAX_SUBSTRINGS
#define DOC_URL
Definition: suricata.h:86
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1132
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:232
#define DOC_VERSION
Definition: suricata.h:91
void(* RegisterTests)(void)
Definition: detect.h:1185
a single match condition for a signature
Definition: detect.h:313