suricata
detect-tls-random.c
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 #include "suricata-common.h"
19 #include "threads.h"
20 #include "detect.h"
21 
22 #include "detect-parse.h"
23 #include "detect-engine.h"
24 #include "detect-engine-mpm.h"
25 #include "detect-content.h"
26 
27 #include "flow.h"
28 #include "stream-tcp.h"
29 
30 #include "app-layer.h"
31 #include "app-layer-ssl.h"
33 #include "detect-tls-random.h"
34 
35 #define DETECT_TLS_RANDOM_TIME_LEN 4
36 #define DETECT_TLS_RANDOM_BYTES_LEN 28
37 
38 static int DetectTlsRandomTimeSetup(DetectEngineCtx *, Signature *, const char *);
39 static int DetectTlsRandomBytesSetup(DetectEngineCtx *, Signature *, const char *);
40 static int DetectTlsRandomSetup(DetectEngineCtx *, Signature *, const char *);
41 static InspectionBuffer *GetRandomTimeData(DetectEngineThreadCtx *det_ctx,
42  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
43  const int list_id);
44 static InspectionBuffer *GetRandomBytesData(DetectEngineThreadCtx *det_ctx,
45  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
46  const int list_id);
47 static InspectionBuffer *GetRandomData(DetectEngineThreadCtx *det_ctx,
48  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
49  const int list_id);
50 
51 static int g_tls_random_time_buffer_id = 0;
52 static int g_tls_random_bytes_buffer_id = 0;
53 static int g_tls_random_buffer_id = 0;
54 
56 {
57  sigmatch_table[DETECT_AL_TLS_RANDOM_TIME].name = "tls.random_time";
58  sigmatch_table[DETECT_AL_TLS_RANDOM_TIME].desc = "sticky buffer to match specifically and only "
59  "on the first 4 bytes of a TLS random buffer";
60  sigmatch_table[DETECT_AL_TLS_RANDOM_TIME].url = "/rules/tls-keywords.html#tls-random-time";
61  sigmatch_table[DETECT_AL_TLS_RANDOM_TIME].Setup = DetectTlsRandomTimeSetup;
63 
64  /* Register engine for Server random */
66  DetectEngineInspectBufferGeneric, GetRandomTimeData);
68  GetRandomTimeData, ALPROTO_TLS, 0);
69 
70  /* Register engine for Client random */
72  DetectEngineInspectBufferGeneric, GetRandomTimeData);
74  GetRandomTimeData, ALPROTO_TLS, 0);
75 
76  DetectBufferTypeSetDescriptionByName("tls.random_time", "TLS Random Time");
77 
78  g_tls_random_time_buffer_id = DetectBufferTypeGetByName("tls.random_time");
79 }
80 
82 {
83  sigmatch_table[DETECT_AL_TLS_RANDOM_BYTES].name = "tls.random_bytes";
85  "sticky buffer to match specifically and only on the last 28 bytes of a TLS random "
86  "buffer";
87  sigmatch_table[DETECT_AL_TLS_RANDOM_BYTES].url = "/rules/tls-keywords.html#tls-random-bytes";
88  sigmatch_table[DETECT_AL_TLS_RANDOM_BYTES].Setup = DetectTlsRandomBytesSetup;
91 
92  /* Register engine for Server random */
94  DetectEngineInspectBufferGeneric, GetRandomBytesData);
96  GetRandomBytesData, ALPROTO_TLS, 0);
97 
98  /* Register engine for Client random */
100  DetectEngineInspectBufferGeneric, GetRandomBytesData);
102  GetRandomBytesData, ALPROTO_TLS, 0);
103 
104  DetectBufferTypeSetDescriptionByName("tls.random_bytes", "TLS Random Bytes");
105 
106  g_tls_random_bytes_buffer_id = DetectBufferTypeGetByName("tls.random_bytes");
107 }
108 
109 /**
110  * \brief Registration function for keyword: tls.random
111  */
113 {
116 
117  sigmatch_table[DETECT_AL_TLS_RANDOM].name = "tls.random";
119  "sticky buffer to match specifically and only on a TLS random buffer";
120  sigmatch_table[DETECT_AL_TLS_RANDOM].url = "/rules/tls-keywords.html#tls-random";
121  sigmatch_table[DETECT_AL_TLS_RANDOM].Setup = DetectTlsRandomSetup;
123 
124  /* Register engine for Server random */
126  DetectEngineInspectBufferGeneric, GetRandomData);
128  GetRandomData, ALPROTO_TLS, 0);
129 
130  /* Register engine for Client random */
132  DetectEngineInspectBufferGeneric, GetRandomData);
134  GetRandomData, ALPROTO_TLS, 0);
135 
136  DetectBufferTypeSetDescriptionByName("tls.random", "TLS Random");
137 
138  g_tls_random_buffer_id = DetectBufferTypeGetByName("tls.random");
139 }
140 
141 /**
142  * \brief this function setup the tls.random_time sticky buffer keyword used in the rule
143  *
144  * \param de_ctx Pointer to the Detection Engine Context
145  * \param s Pointer to the Signature to which the current keyword belongs
146  * \param str Should hold an empty string always
147  *
148  * \retval 0 On success
149  * \retval -1 On failure
150  */
151 static int DetectTlsRandomTimeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
152 {
153  if (DetectBufferSetActiveList(de_ctx, s, g_tls_random_time_buffer_id) < 0)
154  return -1;
155 
157  return -1;
158 
159  return 0;
160 }
161 
162 /**
163  * \brief this function setup the tls.random_bytes sticky buffer keyword used in the rule
164  *
165  * \param de_ctx Pointer to the Detection Engine Context
166  * \param s Pointer to the Signature to which the current keyword belongs
167  * \param str Should hold an empty string always
168  *
169  * \retval 0 On success
170  * \retval -1 On failure
171  */
172 static int DetectTlsRandomBytesSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
173 {
174  if (DetectBufferSetActiveList(de_ctx, s, g_tls_random_bytes_buffer_id) < 0)
175  return -1;
176 
178  return -1;
179 
180  return 0;
181 }
182 
183 /**
184  * \brief this function setup the tls.random sticky buffer keyword used in the rule
185  *
186  * \param de_ctx Pointer to the Detection Engine Context
187  * \param s Pointer to the Signature to which the current keyword belongs
188  * \param str Should hold an empty string always
189  *
190  * \retval 0 On success
191  * \retval -1 On failure
192  */
193 static int DetectTlsRandomSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
194 {
195  if (DetectBufferSetActiveList(de_ctx, s, g_tls_random_buffer_id) < 0)
196  return -1;
197 
199  return -1;
200 
201  return 0;
202 }
203 
204 static InspectionBuffer *GetRandomTimeData(DetectEngineThreadCtx *det_ctx,
205  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
206  const int list_id)
207 {
208  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
209  if (buffer->inspect == NULL) {
210  const SSLState *ssl_state = (SSLState *)f->alstate;
211  if (flow_flags & STREAM_TOSERVER) {
212  if (!(ssl_state->flags & TLS_TS_RANDOM_SET))
213  return NULL;
214  } else {
215  if (!(ssl_state->flags & TLS_TC_RANDOM_SET))
216  return NULL;
217  }
218  const uint32_t data_len = DETECT_TLS_RANDOM_TIME_LEN;
219  const uint8_t *data;
220  if (flow_flags & STREAM_TOSERVER) {
221  data = ssl_state->client_connp.random;
222  } else {
223  data = ssl_state->server_connp.random;
224  }
225  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
226  InspectionBufferApplyTransforms(buffer, transforms);
227  }
228  return buffer;
229 }
230 
231 static InspectionBuffer *GetRandomBytesData(DetectEngineThreadCtx *det_ctx,
232  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
233  const int list_id)
234 {
235  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
236  if (buffer->inspect == NULL) {
237  const SSLState *ssl_state = (SSLState *)f->alstate;
238  if (flow_flags & STREAM_TOSERVER) {
239  if (!(ssl_state->flags & TLS_TS_RANDOM_SET))
240  return NULL;
241  } else {
242  if (!(ssl_state->flags & TLS_TC_RANDOM_SET))
243  return NULL;
244  }
245  const uint32_t data_len = DETECT_TLS_RANDOM_BYTES_LEN;
246  const uint8_t *data;
247  if (flow_flags & STREAM_TOSERVER) {
248  data = ssl_state->client_connp.random + DETECT_TLS_RANDOM_TIME_LEN;
249  } else {
250  data = ssl_state->server_connp.random + DETECT_TLS_RANDOM_TIME_LEN;
251  }
252  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
253  InspectionBufferApplyTransforms(buffer, transforms);
254  }
255  return buffer;
256 }
257 
258 static InspectionBuffer *GetRandomData(DetectEngineThreadCtx *det_ctx,
259  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
260  const int list_id)
261 {
262  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
263  if (buffer->inspect == NULL) {
264  const SSLState *ssl_state = (SSLState *)f->alstate;
265  if (flow_flags & STREAM_TOSERVER) {
266  if (!(ssl_state->flags & TLS_TS_RANDOM_SET))
267  return NULL;
268  } else {
269  if (!(ssl_state->flags & TLS_TC_RANDOM_SET))
270  return NULL;
271  }
272  const uint32_t data_len = TLS_RANDOM_LEN;
273  const uint8_t *data;
274  if (flow_flags & STREAM_TOSERVER) {
275  data = ssl_state->client_connp.random;
276  } else {
277  data = ssl_state->server_connp.random;
278  }
279  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
280  InspectionBufferApplyTransforms(buffer, transforms);
281  }
282  return buffer;
283 }
SigTableElmt_::url
const char * url
Definition: detect.h:1312
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1738
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:296
detect-content.h
detect-engine.h
DetectTlsRandomTimeRegister
void DetectTlsRandomTimeRegister(void)
Definition: detect-tls-random.c:55
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1517
SigTableElmt_::desc
const char * desc
Definition: detect.h:1311
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:128
DetectEngineInspectBufferGeneric
uint8_t DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2155
SigTableElmt_::name
const char * name
Definition: detect.h:1309
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:408
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:314
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:33
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:315
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1357
InspectionBuffer
Definition: detect.h:373
DetectTlsRandomRegister
void DetectTlsRandomRegister(void)
Registration function for keyword: tls.random.
Definition: detect-tls-random.c:112
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1303
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:843
DETECT_AL_TLS_RANDOM
@ DETECT_AL_TLS_RANDOM
Definition: detect-engine-register.h:259
detect-tls-random.h
DetectTlsRandomBytesRegister
void DetectTlsRandomBytesRegister(void)
Definition: detect-tls-random.c:81
DETECT_AL_TLS_RANDOM_TIME
@ DETECT_AL_TLS_RANDOM_TIME
Definition: detect-engine-register.h:257
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1294
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1501
DETECT_TLS_RANDOM_BYTES_LEN
#define DETECT_TLS_RANDOM_BYTES_LEN
Definition: detect-tls-random.c:36
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1094
DETECT_AL_TLS_RANDOM_BYTES
@ DETECT_AL_TLS_RANDOM_BYTES
Definition: detect-engine-register.h:258
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
TLS_TC_RANDOM_SET
#define TLS_TC_RANDOM_SET
Definition: app-layer-ssl.h:133
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1098
DETECT_TLS_RANDOM_TIME_LEN
#define DETECT_TLS_RANDOM_TIME_LEN
Definition: detect-tls-random.c:35
detect-engine-mpm.h
detect.h
TLS_TS_RANDOM_SET
#define TLS_TS_RANDOM_SET
Definition: app-layer-ssl.h:130
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
TLS_RANDOM_LEN
#define TLS_RANDOM_LEN
Definition: app-layer-ssl.h:157
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
SSLStateConnp_::random
uint8_t random[TLS_RANDOM_LEN]
Definition: app-layer-ssl.h:254
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1712
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
str
#define str(s)
Definition: suricata-common.h:291
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1596
Flow_::alstate
void * alstate
Definition: flow.h:481
detect-parse.h
Signature_
Signature container.
Definition: detect.h:603
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1493
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:240
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1191
flow.h
app-layer-ssl.h
app-layer.h
SSLState_::flags
uint32_t flags
Definition: app-layer-ssl.h:303