suricata
detect-ssl-version.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 detect-ssl-version.c
20  *
21  * \author Gurvinder Singh <gurvindersinghdahiya@gmail.com>
22  *
23  * Implements the ssl_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 "detect-ssl-version.h"
50 
51 #include "stream-tcp.h"
52 #include "app-layer-ssl.h"
53 
54 /**
55  * \brief Regex for parsing "id" option, matching number or "number"
56  */
57 #define PARSE_REGEX "^\\s*(!?[A-z0-9.]+)\\s*,?\\s*(!?[A-z0-9.]+)?\\s*\\,?\\s*" \
58  "(!?[A-z0-9.]+)?\\s*,?\\s*(!?[A-z0-9.]+)?\\s*,?\\s*(!?[A-z0-9.]+)?\\s*$"
59 
60 static DetectParseRegex parse_regex;
61 
62 static int DetectSslVersionMatch(DetectEngineThreadCtx *,
63  Flow *, uint8_t, void *, void *,
64  const Signature *, const SigMatchCtx *);
65 static int DetectSslVersionSetup(DetectEngineCtx *, Signature *, const char *);
66 #ifdef UNITTESTS
67 static void DetectSslVersionRegisterTests(void);
68 #endif
69 static void DetectSslVersionFree(void *);
70 static int g_tls_generic_list_id = 0;
71 
72 /**
73  * \brief Registration function for keyword: ssl_version
74  */
76 {
77  sigmatch_table[DETECT_AL_SSL_VERSION].name = "ssl_version";
78  sigmatch_table[DETECT_AL_SSL_VERSION].desc = "match version of SSL/TLS record";
79  sigmatch_table[DETECT_AL_SSL_VERSION].url = DOC_URL DOC_VERSION "/rules/tls-keywords.html#ssl-version";
80  sigmatch_table[DETECT_AL_SSL_VERSION].AppLayerTxMatch = DetectSslVersionMatch;
81  sigmatch_table[DETECT_AL_SSL_VERSION].Setup = DetectSslVersionSetup;
82  sigmatch_table[DETECT_AL_SSL_VERSION].Free = DetectSslVersionFree;
83 #ifdef UNITTESTS
84  sigmatch_table[DETECT_AL_SSL_VERSION].RegisterTests = DetectSslVersionRegisterTests;
85 #endif
86  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
87 
88  g_tls_generic_list_id = DetectBufferTypeRegister("tls_generic");
89 }
90 
91 /**
92  * \brief match the specified version on a ssl session
93  *
94  * \param t pointer to thread vars
95  * \param det_ctx pointer to the pattern matcher thread
96  * \param p pointer to the current packet
97  * \param m pointer to the sigmatch that we will cast into DetectSslVersionData
98  *
99  * \retval 0 no match
100  * \retval 1 match
101  */
102 static int DetectSslVersionMatch(DetectEngineThreadCtx *det_ctx,
103  Flow *f, uint8_t flags, void *state, void *txv,
104  const Signature *s, const SigMatchCtx *m)
105 {
106  SCEnter();
107 
108  int ret = 0;
109  uint16_t ver = 0;
110  uint8_t sig_ver = TLS_UNKNOWN;
111 
112  const DetectSslVersionData *ssl = (const DetectSslVersionData *)m;
113  SSLState *app_state = (SSLState *)state;
114  if (app_state == NULL) {
115  SCLogDebug("no app state, no match");
116  SCReturnInt(0);
117  }
118 
119  if (flags & STREAM_TOCLIENT) {
120  SCLogDebug("server (toclient) version is 0x%02X",
121  app_state->server_connp.version);
122  ver = app_state->server_connp.version;
123  } else if (flags & STREAM_TOSERVER) {
124  SCLogDebug("client (toserver) version is 0x%02X",
125  app_state->client_connp.version);
126  ver = app_state->client_connp.version;
127  }
128 
129  switch (ver) {
130  case SSL_VERSION_2:
131  if (ver == ssl->data[SSLv2].ver)
132  ret = 1;
133  sig_ver = SSLv2;
134  break;
135  case SSL_VERSION_3:
136  if (ver == ssl->data[SSLv3].ver)
137  ret = 1;
138  sig_ver = SSLv3;
139  break;
140  case TLS_VERSION_10:
141  if (ver == ssl->data[TLS10].ver)
142  ret = 1;
143  sig_ver = TLS10;
144  break;
145  case TLS_VERSION_11:
146  if (ver == ssl->data[TLS11].ver)
147  ret = 1;
148  sig_ver = TLS11;
149  break;
150  case TLS_VERSION_12:
151  if (ver == ssl->data[TLS12].ver)
152  ret = 1;
153  sig_ver = TLS12;
154  break;
169  if (((ver >> 8) & 0xff) == 0x7f)
170  ver = TLS_VERSION_13;
171  /* fall through */
172  case TLS_VERSION_13:
173  if (ver == ssl->data[TLS13].ver)
174  ret = 1;
175  sig_ver = TLS13;
176  break;
177  }
178 
179  if (sig_ver == TLS_UNKNOWN)
180  SCReturnInt(0);
181 
182  SCReturnInt(ret ^ ((ssl->data[sig_ver].flags & DETECT_SSL_VERSION_NEGATED) ? 1 : 0));
183 }
184 
185 /**
186  * \brief This function is used to parse ssl_version data passed via
187  * keyword: "ssl_version"
188  *
189  * \param str Pointer to the user provided options
190  *
191  * \retval ssl pointer to DetectSslVersionData on success
192  * \retval NULL on failure
193  */
194 static DetectSslVersionData *DetectSslVersionParse(const char *str)
195 {
196  DetectSslVersionData *ssl = NULL;
197  int ret = 0, res = 0;
198  int ov[MAX_SUBSTRINGS];
199 
200  ret = DetectParsePcreExec(&parse_regex, str, 0, 0, ov, MAX_SUBSTRINGS);
201  if (ret < 1 || ret > 5) {
202  SCLogError(SC_ERR_PCRE_MATCH, "invalid ssl_version option");
203  goto error;
204  }
205 
206  if (ret > 1) {
207  uint8_t found = 0, neg = 0;
208  char *tmp_str;
209 
210  /* We have a correct ssl_version options */
211  ssl = SCCalloc(1, sizeof (DetectSslVersionData));
212  if (unlikely(ssl == NULL))
213  goto error;
214 
215  int i;
216  for (i = 1; i < ret; i++) {
217  char ver_ptr[64];
218  res = pcre_copy_substring((char *) str, ov, MAX_SUBSTRINGS, i, ver_ptr, sizeof(ver_ptr));
219  if (res < 0) {
220  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
221  if (found == 0)
222  goto error;
223  break;
224  }
225 
226  tmp_str = ver_ptr;
227 
228  /* Let's see if we need to scape "'s */
229  if (tmp_str[0] == '"') {
230  tmp_str[strlen(tmp_str) - 1] = '\0';
231  tmp_str += 1;
232  }
233 
234 
235  if (tmp_str[0] == '!') {
236  neg = 1;
237  tmp_str++;
238  }
239 
240  if (strcasecmp("sslv2", tmp_str) == 0) {
241  ssl->data[SSLv2].ver = SSL_VERSION_2;
242  if (neg == 1)
244  } else if (strcasecmp("sslv3", tmp_str) == 0) {
245  ssl->data[SSLv3].ver = SSL_VERSION_3;
246  if (neg == 1)
248  } else if (strcasecmp("tls1.0", tmp_str) == 0) {
249  ssl->data[TLS10].ver = TLS_VERSION_10;
250  if (neg == 1)
252  } else if (strcasecmp("tls1.1", tmp_str) == 0) {
253  ssl->data[TLS11].ver = TLS_VERSION_11;
254  if (neg == 1)
256  } else if (strcasecmp("tls1.2", tmp_str) == 0) {
257  ssl->data[TLS12].ver = TLS_VERSION_12;
258  if (neg == 1)
260  } else if (strcasecmp("tls1.3", tmp_str) == 0) {
261  ssl->data[TLS13].ver = TLS_VERSION_13;
262  if (neg == 1)
264  } else if (strcmp(tmp_str, "") == 0) {
265  if (found == 0)
266  goto error;
267  break;
268  } else {
269  SCLogError(SC_ERR_INVALID_VALUE, "Invalid value");
270  goto error;
271  }
272 
273  found = 1;
274  neg = 0;
275  }
276  }
277 
278  return ssl;
279 
280 error:
281  if (ssl != NULL)
282  DetectSslVersionFree(ssl);
283  return NULL;
284 
285 }
286 
287 /**
288  * \brief this function is used to add the parsed "id" option
289  * \brief into the current signature
290  *
291  * \param de_ctx pointer to the Detection Engine Context
292  * \param s pointer to the Current Signature
293  * \param idstr pointer to the user provided "id" option
294  *
295  * \retval 0 on Success
296  * \retval -1 on Failure
297  */
298 static int DetectSslVersionSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
299 {
300  DetectSslVersionData *ssl = NULL;
301  SigMatch *sm = NULL;
302 
304  return -1;
305 
306  ssl = DetectSslVersionParse(str);
307  if (ssl == NULL)
308  goto error;
309 
310  /* Okay so far so good, lets get this into a SigMatch
311  * and put it in the Signature. */
312  sm = SigMatchAlloc();
313  if (sm == NULL)
314  goto error;
315 
317  sm->ctx = (void *)ssl;
318 
319  SigMatchAppendSMToList(s, sm, g_tls_generic_list_id);
320  return 0;
321 
322 error:
323  if (ssl != NULL)
324  DetectSslVersionFree(ssl);
325  if (sm != NULL)
326  SCFree(sm);
327  return -1;
328 }
329 
330 /**
331  * \brief this function will free memory associated with DetectSslVersionData
332  *
333  * \param id_d pointer to DetectSslVersionData
334  */
335 void DetectSslVersionFree(void *ptr)
336 {
337  if (ptr != NULL)
338  SCFree(ptr);
339 }
340 
341 #ifdef UNITTESTS
343 #endif
TLS_VERSION_13_DRAFT26
@ TLS_VERSION_13_DRAFT26
Definition: app-layer-ssl.h:153
TLS_VERSION_13_DRAFT21
@ TLS_VERSION_13_DRAFT21
Definition: app-layer-ssl.h:158
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1459
TLS_VERSION_13_DRAFT27
@ TLS_VERSION_13_DRAFT27
Definition: app-layer-ssl.h:152
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:233
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
TLS_VERSION_13_DRAFT17
@ TLS_VERSION_13_DRAFT17
Definition: app-layer-ssl.h:162
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
TLS_VERSION_13_DRAFT16
@ TLS_VERSION_13_DRAFT16
Definition: app-layer-ssl.h:163
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1201
stream-tcp.h
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
SCFree
#define SCFree(a)
Definition: util-mem.h:322
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:255
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:33
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:335
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:256
TLS_VERSION_11
@ TLS_VERSION_11
Definition: app-layer-ssl.h:148
TLS12
@ TLS12
Definition: detect-ssl-version.h:35
threads.h
Flow_
Flow data structure.
Definition: flow.h:342
TLS11
@ TLS11
Definition: detect-ssl-version.h:34
TLS_VERSION_10
@ TLS_VERSION_10
Definition: app-layer-ssl.h:147
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:761
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1174
SSLv3
@ SSLv3
Definition: detect-ssl-version.h:32
TLS_VERSION_13_PRE_DRAFT16
@ TLS_VERSION_13_PRE_DRAFT16
Definition: app-layer-ssl.h:164
TLS13
@ TLS13
Definition: detect-ssl-version.h:36
m
SCMutex m
Definition: flow-hash.h:5
SigTableElmt_::Free
void(* Free)(void *)
Definition: detect.h:1192
detect-ssl-version.c
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
TLS_VERSION_13_DRAFT25
@ TLS_VERSION_13_DRAFT25
Definition: app-layer-ssl.h:154
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1187
SSLVersionData_::flags
uint8_t flags
Definition: detect-ssl-version.h:44
TLS_VERSION_13_DRAFT20
@ TLS_VERSION_13_DRAFT20
Definition: app-layer-ssl.h:159
util-unittest.h
util-unittest-helper.h
decode.h
util-debug.h
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
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
TLS_VERSION_12
@ TLS_VERSION_12
Definition: app-layer-ssl.h:149
res
PoolThreadReserved res
Definition: stream-tcp-private.h:60
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing "id" option, matching number or "number".
Definition: detect-ssl-version.c:57
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2440
SCEnter
#define SCEnter(...)
Definition: util-debug.h:337
detect-engine-mpm.h
detect.h
DetectSslVersionData_
Definition: detect-ssl-version.h:47
app-layer-parser.h
TLS_VERSION_13_DRAFT28
@ TLS_VERSION_13_DRAFT28
Definition: app-layer-ssl.h:151
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:321
TLS_VERSION_13_DRAFT22
@ TLS_VERSION_13_DRAFT22
Definition: app-layer-ssl.h:157
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2372
SCCalloc
#define SCCalloc(nm, a)
Definition: util-mem.h:253
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
detect-ssl-version.h
SSL_VERSION_3
@ SSL_VERSION_3
Definition: app-layer-ssl.h:146
SigMatch_::type
uint8_t type
Definition: detect.h:319
SSLv2
@ SSLv2
Definition: detect-ssl-version.h:31
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:313
SSL_VERSION_2
@ SSL_VERSION_2
Definition: app-layer-ssl.h:145
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:816
flags
uint8_t flags
Definition: decode-gre.h:2
TLS_VERSION_13_DRAFT18
@ TLS_VERSION_13_DRAFT18
Definition: app-layer-ssl.h:161
suricata-common.h
DetectSslVersionData_::data
SSLVersionData data[TLS_SIZE]
Definition: detect-ssl-version.h:48
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectParseRegex_
Definition: detect-parse.h:42
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
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
TLS_VERSION_13_DRAFT19
@ TLS_VERSION_13_DRAFT19
Definition: app-layer-ssl.h:160
TLS_VERSION_13_DRAFT24
@ TLS_VERSION_13_DRAFT24
Definition: app-layer-ssl.h:155
DETECT_AL_SSL_VERSION
@ DETECT_AL_SSL_VERSION
Definition: detect-engine-register.h:163
str
#define str(s)
Definition: suricata-common.h:256
TLS_VERSION_13
@ TLS_VERSION_13
Definition: app-layer-ssl.h:150
detect-parse.h
Signature_
Signature container.
Definition: detect.h:522
SigMatch_
a single match condition for a signature
Definition: detect.h:318
TLS_UNKNOWN
@ TLS_UNKNOWN
Definition: detect-ssl-version.h:39
DetectSslVersionRegister
void DetectSslVersionRegister(void)
Registration function for keyword: ssl_version.
Definition: detect-ssl-version.c:75
TLS_VERSION_13_DRAFT23
@ TLS_VERSION_13_DRAFT23
Definition: app-layer-ssl.h:156
SSLVersionData_::ver
uint16_t ver
Definition: detect-ssl-version.h:43
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:341
DETECT_SSL_VERSION_NEGATED
#define DETECT_SSL_VERSION_NEGATED
Definition: detect-ssl-version.h:28
flow-var.h
app-layer-ssl.h
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
DOC_VERSION
#define DOC_VERSION
Definition: suricata.h:91
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1193
TLS10
@ TLS10
Definition: detect-ssl-version.h:33
app-layer.h
SSLStateConnp_::version
uint16_t version
Definition: app-layer-ssl.h:189