suricata
detect-tls-version.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 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  * Implements the tls.version keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "threads.h"
28 #include "debug.h"
29 #include "decode.h"
30 
31 #include "detect.h"
32 #include "detect-parse.h"
33 
34 #include "detect-engine.h"
35 #include "detect-engine-mpm.h"
36 #include "detect-engine-state.h"
37 
38 #include "flow.h"
39 #include "flow-var.h"
40 #include "flow-util.h"
41 
42 #include "util-debug.h"
43 #include "util-unittest.h"
44 #include "util-unittest-helper.h"
45 
46 #include "app-layer.h"
47 #include "app-layer-parser.h"
48 
49 #include "app-layer-ssl.h"
50 #include "detect-tls-version.h"
51 
52 #include "stream-tcp.h"
53 
54 /**
55  * \brief Regex for parsing "id" option, matching number or "number"
56  */
57 #define PARSE_REGEX "^\\s*([A-z0-9\\.]+|\"[A-z0-9\\.]+\")\\s*$"
58 
59 static pcre *parse_regex;
60 static pcre_extra *parse_regex_study;
61 
62 static int DetectTlsVersionMatch (ThreadVars *, DetectEngineThreadCtx *,
63  Flow *, uint8_t, void *, void *,
64  const Signature *, const SigMatchCtx *);
65 static int DetectTlsVersionSetup (DetectEngineCtx *, Signature *, const char *);
66 #ifdef UNITTESTS
67 static void DetectTlsVersionRegisterTests(void);
68 #endif
69 static void DetectTlsVersionFree(void *);
70 static int g_tls_generic_list_id = 0;
71 
72 /**
73  * \brief Registration function for keyword: tls.version
74  */
76 {
77  sigmatch_table[DETECT_AL_TLS_VERSION].name = "tls.version";
78  sigmatch_table[DETECT_AL_TLS_VERSION].desc = "match on TLS/SSL version";
79  sigmatch_table[DETECT_AL_TLS_VERSION].url = DOC_URL DOC_VERSION "/rules/tls-keywords.html#tls-version";
80  sigmatch_table[DETECT_AL_TLS_VERSION].AppLayerTxMatch = DetectTlsVersionMatch;
81  sigmatch_table[DETECT_AL_TLS_VERSION].Setup = DetectTlsVersionSetup;
82  sigmatch_table[DETECT_AL_TLS_VERSION].Free = DetectTlsVersionFree;
83 #ifdef UNITTESTS
84  sigmatch_table[DETECT_AL_TLS_VERSION].RegisterTests = DetectTlsVersionRegisterTests;
85 #endif
86 
87  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex, &parse_regex_study);
88 
89  g_tls_generic_list_id = DetectBufferTypeRegister("tls_generic");
90 }
91 
92 /**
93  * \brief match the specified version on a tls session
94  *
95  * \param t pointer to thread vars
96  * \param det_ctx pointer to the pattern matcher thread
97  * \param p pointer to the current packet
98  * \param m pointer to the sigmatch that we will cast into DetectTlsVersionData
99  *
100  * \retval 0 no match
101  * \retval 1 match
102  */
103 static int DetectTlsVersionMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx,
104  Flow *f, uint8_t flags, void *state, void *txv,
105  const Signature *s, const SigMatchCtx *m)
106 {
107  SCEnter();
108 
109  const DetectTlsVersionData *tls_data = (const DetectTlsVersionData *)m;
110  SSLState *ssl_state = (SSLState *)state;
111  if (ssl_state == NULL) {
112  SCLogDebug("no tls state, no match");
113  SCReturnInt(0);
114  }
115 
116  int ret = 0;
117  uint16_t version = 0;
118  SCLogDebug("looking for tls_data->ver 0x%02X (flags 0x%02X)", tls_data->ver, flags);
119 
120  if (flags & STREAM_TOCLIENT) {
121  version = ssl_state->server_connp.version;
122  SCLogDebug("server (toclient) version is 0x%02X", version);
123  } else if (flags & STREAM_TOSERVER) {
124  version = ssl_state->client_connp.version;
125  SCLogDebug("client (toserver) version is 0x%02X", version);
126  }
127 
128  if ((tls_data->flags & DETECT_TLS_VERSION_FLAG_RAW) == 0) {
129  /* Match all TLSv1.3 drafts as TLSv1.3 */
130  if (((version >> 8) & 0xff) == 0x7f) {
131  version = TLS_VERSION_13;
132  }
133  }
134 
135  if (tls_data->ver == version) {
136  ret = 1;
137  }
138 
139  SCReturnInt(ret);
140 }
141 
142 /**
143  * \brief This function is used to parse IPV4 ip_id passed via keyword: "id"
144  *
145  * \param idstr Pointer to the user provided id option
146  *
147  * \retval id_d pointer to DetectTlsVersionData on success
148  * \retval NULL on failure
149  */
150 static DetectTlsVersionData *DetectTlsVersionParse (const char *str)
151 {
152  uint16_t temp;
153  DetectTlsVersionData *tls = NULL;
154  #define MAX_SUBSTRINGS 30
155  int ret = 0, res = 0;
156  int ov[MAX_SUBSTRINGS];
157 
158  ret = pcre_exec(parse_regex, parse_regex_study, str, strlen(str), 0, 0,
159  ov, MAX_SUBSTRINGS);
160 
161  if (ret < 1 || ret > 3) {
162  SCLogError(SC_ERR_PCRE_MATCH, "invalid tls.version option");
163  goto error;
164  }
165 
166  if (ret > 1) {
167  const char *str_ptr;
168  char *orig;
169  char *tmp_str;
170  res = pcre_get_substring((char *)str, ov, MAX_SUBSTRINGS, 1, &str_ptr);
171  if (res < 0) {
172  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
173  goto error;
174  }
175 
176  /* We have a correct id option */
177  tls = SCCalloc(1, sizeof(DetectTlsVersionData));
178  if (unlikely(tls == NULL))
179  goto error;
180 
181  orig = SCStrdup((char*)str_ptr);
182  if (unlikely(orig == NULL)) {
183  goto error;
184  }
185  tmp_str=orig;
186 
187  /* Let's see if we need to scape "'s */
188  if (tmp_str[0] == '"')
189  {
190  tmp_str[strlen(tmp_str) - 1] = '\0';
191  tmp_str += 1;
192  }
193 
194  if (strncmp("1.0", tmp_str, 3) == 0) {
195  temp = TLS_VERSION_10;
196  } else if (strncmp("1.1", tmp_str, 3) == 0) {
197  temp = TLS_VERSION_11;
198  } else if (strncmp("1.2", tmp_str, 3) == 0) {
199  temp = TLS_VERSION_12;
200  } else if (strncmp("1.3", tmp_str, 3) == 0) {
201  temp = TLS_VERSION_13;
202  } else if ((strncmp("0x", tmp_str, 2) == 0) && (strlen(str) == 6)) {
203  temp = (uint16_t)strtol(tmp_str, NULL, 0);
205  } else {
206  SCLogError(SC_ERR_INVALID_VALUE, "Invalid value");
207  SCFree(orig);
208  goto error;
209  }
210 
211  tls->ver = temp;
212 
213  SCFree(orig);
214 
215  SCLogDebug("will look for tls %"PRIu16"", tls->ver);
216  }
217 
218  return tls;
219 
220 error:
221  if (tls != NULL)
222  DetectTlsVersionFree(tls);
223  return NULL;
224 
225 }
226 
227 /**
228  * \brief this function is used to add the parsed "id" option
229  * \brief into the current signature
230  *
231  * \param de_ctx pointer to the Detection Engine Context
232  * \param s pointer to the Current Signature
233  * \param idstr pointer to the user provided "id" option
234  *
235  * \retval 0 on Success
236  * \retval -1 on Failure
237  */
238 static int DetectTlsVersionSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
239 {
240  DetectTlsVersionData *tls = NULL;
241  SigMatch *sm = NULL;
242 
244  return -1;
245 
246  tls = DetectTlsVersionParse(str);
247  if (tls == NULL)
248  goto error;
249 
250  /* Okay so far so good, lets get this into a SigMatch
251  * and put it in the Signature. */
252  sm = SigMatchAlloc();
253  if (sm == NULL)
254  goto error;
255 
257  sm->ctx = (void *)tls;
258 
259  SigMatchAppendSMToList(s, sm, g_tls_generic_list_id);
260 
261  return 0;
262 
263 error:
264  if (tls != NULL)
265  DetectTlsVersionFree(tls);
266  if (sm != NULL)
267  SCFree(sm);
268  return -1;
269 
270 }
271 
272 /**
273  * \brief this function will free memory associated with DetectTlsVersionData
274  *
275  * \param id_d pointer to DetectTlsVersionData
276  */
277 static void DetectTlsVersionFree(void *ptr)
278 {
280  SCFree(id_d);
281 }
282 
283 #ifdef UNITTESTS
285 #endif
#define MAX_SUBSTRINGS
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1409
uint16_t flags
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1150
int(* AppLayerTxMatch)(ThreadVars *, DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1136
#define SCLogDebug(...)
Definition: util-debug.h:335
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
uint16_t version
#define unlikely(expr)
Definition: util-optimize.h:35
SSLStateConnp server_connp
const char * name
Definition: detect.h:1164
Signature container.
Definition: detect.h:496
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:317
#define PARSE_REGEX
Regex for parsing "id" option, matching number or "number".
main detection engine ctx
Definition: detect.h:724
SSLv[2.0|3.[0|1|2|3]] state structure.
#define str(s)
#define SCCalloc(nm, a)
Definition: util-mem.h:197
Data structures and function prototypes for keeping state for the detection engine.
void(* Free)(void *)
Definition: detect.h:1155
#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)
#define SCEnter(...)
Definition: util-debug.h:337
#define STREAM_TOCLIENT
Definition: stream.h:32
uint8_t type
Definition: detect.h:323
#define SCReturnInt(x)
Definition: util-debug.h:341
const char * desc
Definition: detect.h:1166
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:288
int DetectBufferTypeRegister(const char *name)
SigMatchCtx * ctx
Definition: detect.h:325
uint8_t version
Definition: decode-gre.h:405
#define SCFree(a)
Definition: util-mem.h:228
PoolThreadReserved res
#define DETECT_TLS_VERSION_FLAG_RAW
const char * url
Definition: detect.h:1167
#define STREAM_TOSERVER
Definition: stream.h:31
SCMutex m
Definition: flow-hash.h:105
#define SCStrdup(a)
Definition: util-mem.h:212
#define DOC_URL
Definition: suricata.h:86
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:232
Per thread variable structure.
Definition: threadvars.h:57
#define DOC_VERSION
Definition: suricata.h:91
Flow data structure.
Definition: flow.h:325
void DetectTlsVersionRegister(void)
Registration function for keyword: tls.version.
SSLStateConnp client_connp
void(* RegisterTests)(void)
Definition: detect.h:1156
a single match condition for a signature
Definition: detect.h:322