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