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 res = 0;
154  size_t pcre2len;
155 
156  pcre2_match_data *match = NULL;
157  int ret = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
158  if (ret < 1 || ret > 3) {
159  SCLogError("invalid tls.version option");
160  goto error;
161  }
162 
163  if (ret > 1) {
164  char ver_ptr[64];
165  char *tmp_str;
166  pcre2len = sizeof(ver_ptr);
167  res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)ver_ptr, &pcre2len);
168  if (res < 0) {
169  SCLogError("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("Invalid value");
200  goto error;
201  }
202 
203  tls->ver = temp;
204 
205  SCLogDebug("will look for tls %"PRIu16"", tls->ver);
206  }
207 
208  pcre2_match_data_free(match);
209  return tls;
210 
211 error:
212  if (match) {
213  pcre2_match_data_free(match);
214  }
215  if (tls != NULL)
216  DetectTlsVersionFree(de_ctx, tls);
217  return NULL;
218 
219 }
220 
221 /**
222  * \brief this function is used to add the parsed "id" option
223  * \brief into the current signature
224  *
225  * \param de_ctx pointer to the Detection Engine Context
226  * \param s pointer to the Current Signature
227  * \param idstr pointer to the user provided "id" option
228  *
229  * \retval 0 on Success
230  * \retval -1 on Failure
231  */
232 static int DetectTlsVersionSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
233 {
234  DetectTlsVersionData *tls = NULL;
235 
237  return -1;
238 
239  tls = DetectTlsVersionParse(de_ctx, str);
240  if (tls == NULL)
241  goto error;
242 
243  /* Okay so far so good, lets get this into a SigMatch
244  * and put it in the Signature. */
245 
247  g_tls_generic_list_id) == NULL) {
248  goto error;
249  }
250 
251  return 0;
252 
253 error:
254  if (tls != NULL)
255  DetectTlsVersionFree(de_ctx, tls);
256  return -1;
257 
258 }
259 
260 /**
261  * \brief this function will free memory associated with DetectTlsVersionData
262  *
263  * \param id_d pointer to DetectTlsVersionData
264  */
265 static void DetectTlsVersionFree(DetectEngineCtx *de_ctx, void *ptr)
266 {
268  SCFree(id_d);
269 }
270 
271 #ifdef UNITTESTS
273 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:296
detect-engine.h
detect-tls-version.c
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1294
flow-util.h
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1304
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:314
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:33
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:315
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1275
TLS_VERSION_12
@ TLS_VERSION_12
Definition: app-layer-ssl.h:167
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2641
m
SCMutex m
Definition: flow-hash.h:6
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
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
TLS_VERSION_13
@ TLS_VERSION_13
Definition: app-layer-ssl.h:168
util-debug.h
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
DetectEngineThreadCtx_
Definition: detect.h:1093
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2767
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
app-layer-parser.h
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
TLS_VERSION_11
@ TLS_VERSION_11
Definition: app-layer-ssl.h:166
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:344
TLS_VERSION_10
@ TLS_VERSION_10
Definition: app-layer-ssl.h:165
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1029
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
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:134
str
#define str(s)
Definition: suricata-common.h:291
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:436
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:275
flow-var.h
app-layer-ssl.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1296
app-layer.h
SSLStateConnp_::version
uint16_t version
Definition: app-layer-ssl.h:244