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_TLS_RANDOM_TIME].name = "tls.random_time";
58  sigmatch_table[DETECT_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_TLS_RANDOM_TIME].url = "/rules/tls-keywords.html#tls-random-time";
61  sigmatch_table[DETECT_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_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_TLS_RANDOM_BYTES].url = "/rules/tls-keywords.html#tls-random-bytes";
88  sigmatch_table[DETECT_TLS_RANDOM_BYTES].Setup = DetectTlsRandomBytesSetup;
90 
91  /* Register engine for Server random */
93  DetectEngineInspectBufferGeneric, GetRandomBytesData);
95  GetRandomBytesData, ALPROTO_TLS, 0);
96 
97  /* Register engine for Client random */
99  DetectEngineInspectBufferGeneric, GetRandomBytesData);
101  GetRandomBytesData, ALPROTO_TLS, 0);
102 
103  DetectBufferTypeSetDescriptionByName("tls.random_bytes", "TLS Random Bytes");
104 
105  g_tls_random_bytes_buffer_id = DetectBufferTypeGetByName("tls.random_bytes");
106 }
107 
108 /**
109  * \brief Registration function for keyword: tls.random
110  */
112 {
115 
116  sigmatch_table[DETECT_TLS_RANDOM].name = "tls.random";
118  "sticky buffer to match specifically and only on a TLS random buffer";
119  sigmatch_table[DETECT_TLS_RANDOM].url = "/rules/tls-keywords.html#tls-random";
120  sigmatch_table[DETECT_TLS_RANDOM].Setup = DetectTlsRandomSetup;
122 
123  /* Register engine for Server random */
125  DetectEngineInspectBufferGeneric, GetRandomData);
127  GetRandomData, ALPROTO_TLS, 0);
128 
129  /* Register engine for Client random */
131  DetectEngineInspectBufferGeneric, GetRandomData);
133  GetRandomData, ALPROTO_TLS, 0);
134 
135  DetectBufferTypeSetDescriptionByName("tls.random", "TLS Random");
136 
137  g_tls_random_buffer_id = DetectBufferTypeGetByName("tls.random");
138 }
139 
140 /**
141  * \brief this function setup the tls.random_time sticky buffer keyword used in the rule
142  *
143  * \param de_ctx Pointer to the Detection Engine Context
144  * \param s Pointer to the Signature to which the current keyword belongs
145  * \param str Should hold an empty string always
146  *
147  * \retval 0 On success
148  * \retval -1 On failure
149  */
150 static int DetectTlsRandomTimeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
151 {
152  if (DetectBufferSetActiveList(de_ctx, s, g_tls_random_time_buffer_id) < 0)
153  return -1;
154 
156  return -1;
157 
158  return 0;
159 }
160 
161 /**
162  * \brief this function setup the tls.random_bytes sticky buffer keyword used in the rule
163  *
164  * \param de_ctx Pointer to the Detection Engine Context
165  * \param s Pointer to the Signature to which the current keyword belongs
166  * \param str Should hold an empty string always
167  *
168  * \retval 0 On success
169  * \retval -1 On failure
170  */
171 static int DetectTlsRandomBytesSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
172 {
173  if (DetectBufferSetActiveList(de_ctx, s, g_tls_random_bytes_buffer_id) < 0)
174  return -1;
175 
177  return -1;
178 
179  return 0;
180 }
181 
182 /**
183  * \brief this function setup the tls.random sticky buffer keyword used in the rule
184  *
185  * \param de_ctx Pointer to the Detection Engine Context
186  * \param s Pointer to the Signature to which the current keyword belongs
187  * \param str Should hold an empty string always
188  *
189  * \retval 0 On success
190  * \retval -1 On failure
191  */
192 static int DetectTlsRandomSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
193 {
194  if (DetectBufferSetActiveList(de_ctx, s, g_tls_random_buffer_id) < 0)
195  return -1;
196 
198  return -1;
199 
200  return 0;
201 }
202 
203 static InspectionBuffer *GetRandomTimeData(DetectEngineThreadCtx *det_ctx,
204  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
205  const int list_id)
206 {
207  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
208  if (buffer->inspect == NULL) {
209  const SSLState *ssl_state = (SSLState *)f->alstate;
210  if (flow_flags & STREAM_TOSERVER) {
211  if (!(ssl_state->flags & TLS_TS_RANDOM_SET))
212  return NULL;
213  } else {
214  if (!(ssl_state->flags & TLS_TC_RANDOM_SET))
215  return NULL;
216  }
217  const uint32_t data_len = DETECT_TLS_RANDOM_TIME_LEN;
218  const uint8_t *data;
219  if (flow_flags & STREAM_TOSERVER) {
220  data = ssl_state->client_connp.random;
221  } else {
222  data = ssl_state->server_connp.random;
223  }
224  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
225  InspectionBufferApplyTransforms(buffer, transforms);
226  }
227  return buffer;
228 }
229 
230 static InspectionBuffer *GetRandomBytesData(DetectEngineThreadCtx *det_ctx,
231  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
232  const int list_id)
233 {
234  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
235  if (buffer->inspect == NULL) {
236  const SSLState *ssl_state = (SSLState *)f->alstate;
237  if (flow_flags & STREAM_TOSERVER) {
238  if (!(ssl_state->flags & TLS_TS_RANDOM_SET))
239  return NULL;
240  } else {
241  if (!(ssl_state->flags & TLS_TC_RANDOM_SET))
242  return NULL;
243  }
244  const uint32_t data_len = DETECT_TLS_RANDOM_BYTES_LEN;
245  const uint8_t *data;
246  if (flow_flags & STREAM_TOSERVER) {
247  data = ssl_state->client_connp.random + DETECT_TLS_RANDOM_TIME_LEN;
248  } else {
249  data = ssl_state->server_connp.random + DETECT_TLS_RANDOM_TIME_LEN;
250  }
251  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
252  InspectionBufferApplyTransforms(buffer, transforms);
253  }
254  return buffer;
255 }
256 
257 static InspectionBuffer *GetRandomData(DetectEngineThreadCtx *det_ctx,
258  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
259  const int list_id)
260 {
261  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
262  if (buffer->inspect == NULL) {
263  const SSLState *ssl_state = (SSLState *)f->alstate;
264  if (flow_flags & STREAM_TOSERVER) {
265  if (!(ssl_state->flags & TLS_TS_RANDOM_SET))
266  return NULL;
267  } else {
268  if (!(ssl_state->flags & TLS_TC_RANDOM_SET))
269  return NULL;
270  }
271  const uint32_t data_len = TLS_RANDOM_LEN;
272  const uint8_t *data;
273  if (flow_flags & STREAM_TOSERVER) {
274  data = ssl_state->client_connp.random;
275  } else {
276  data = ssl_state->server_connp.random;
277  }
278  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
279  InspectionBufferApplyTransforms(buffer, transforms);
280  }
281  return buffer;
282 }
SigTableElmt_::url
const char * url
Definition: detect.h:1322
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1770
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:1527
SigTableElmt_::desc
const char * desc
Definition: detect.h:1321
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
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:2157
SigTableElmt_::name
const char * name
Definition: detect.h:1319
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:410
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:314
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:39
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:1359
InspectionBuffer
Definition: detect.h:375
DetectTlsRandomRegister
void DetectTlsRandomRegister(void)
Registration function for keyword: tls.random.
Definition: detect-tls-random.c:111
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1313
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:854
detect-tls-random.h
DetectTlsRandomBytesRegister
void DetectTlsRandomBytesRegister(void)
Definition: detect-tls-random.c:81
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:270
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1304
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1503
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:1096
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:269
TLS_TC_RANDOM_SET
#define TLS_TC_RANDOM_SET
Definition: app-layer-ssl.h:133
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1109
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
DETECT_TLS_RANDOM_BYTES
@ DETECT_TLS_RANDOM_BYTES
Definition: detect-engine-register.h:260
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:1714
DETECT_TLS_RANDOM_TIME
@ DETECT_TLS_RANDOM_TIME
Definition: detect-engine-register.h:259
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:376
str
#define str(s)
Definition: suricata-common.h:300
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:1598
Flow_::alstate
void * alstate
Definition: flow.h:474
detect-parse.h
Signature_
Signature container.
Definition: detect.h:614
DETECT_TLS_RANDOM
@ DETECT_TLS_RANDOM
Definition: detect-engine-register.h:261
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1503
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:242
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1193
flow.h
app-layer-ssl.h
app-layer.h
SSLState_::flags
uint32_t flags
Definition: app-layer-ssl.h:303