suricata
util-ja3.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2017 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 Mats Klepsland <mats.klepsland@gmail.com>
22  *
23  * Functions used to generate JA3 fingerprint.
24  */
25 
26 #include "suricata-common.h"
27 #include "app-layer-ssl.h"
28 #include "util-validate.h"
29 #include "util-ja3.h"
30 
31 #include "detect-engine.h"
32 
33 /**
34  * \brief Allocate new buffer.
35  *
36  * \return pointer to buffer on success.
37  * \return NULL on failure.
38  */
40 {
41  JA3Buffer *buffer = SCCalloc(1, sizeof(JA3Buffer));
42  if (buffer == NULL) {
43  return NULL;
44  }
45 
46  return buffer;
47 }
48 
49 /**
50  * \brief Free allocated buffer.
51  *
52  * \param buffer The buffer to free.
53  */
54 void Ja3BufferFree(JA3Buffer **buffer)
55 {
56  DEBUG_VALIDATE_BUG_ON(*buffer == NULL);
57 
58  if ((*buffer)->data != NULL) {
59  SCFree((*buffer)->data);
60  (*buffer)->data = NULL;
61  }
62 
63  SCFree(*buffer);
64  *buffer = NULL;
65 }
66 
67 /**
68  * \internal
69  * \brief Resize buffer if it is full.
70  *
71  * \param buffer The buffer.
72  * \param len The length of the data that should fit into the buffer.
73  *
74  * \retval 0 on success.
75  * \retval -1 on failure.
76  */
77 static int Ja3BufferResizeIfFull(JA3Buffer *buffer, uint32_t len)
78 {
79  DEBUG_VALIDATE_BUG_ON(buffer == NULL);
80 
81  while (buffer->used + len + 2 > buffer->size)
82  {
83  buffer->size *= 2;
84  char *tmp = SCRealloc(buffer->data, buffer->size);
85  if (tmp == NULL) {
86  SCLogError("Error resizing JA3 buffer");
87  return -1;
88  }
89  buffer->data = tmp;
90  }
91 
92  return 0;
93 }
94 
95 /**
96  * \brief Append buffer to buffer.
97  *
98  * Append the second buffer to the first and then free it.
99  *
100  * \param buffer1 The first buffer.
101  * \param buffer2 The second buffer.
102  *
103  * \retval 0 on success.
104  * \retval -1 on failure.
105  */
106 int Ja3BufferAppendBuffer(JA3Buffer **buffer1, JA3Buffer **buffer2)
107 {
108  if (*buffer1 == NULL || *buffer2 == NULL) {
109  SCLogError("Buffers should not be NULL");
110  return -1;
111  }
112 
113  /* If buffer1 contains no data, then we just copy the second buffer
114  instead of appending its data. */
115  if ((*buffer1)->data == NULL) {
116  (*buffer1)->data = (*buffer2)->data;
117  (*buffer1)->used = (*buffer2)->used;
118  (*buffer1)->size = (*buffer2)->size;
119  SCFree(*buffer2);
120  return 0;
121  }
122 
123  int rc = Ja3BufferResizeIfFull(*buffer1, (*buffer2)->used);
124  if (rc != 0) {
125  Ja3BufferFree(buffer1);
126  Ja3BufferFree(buffer2);
127  return -1;
128  }
129 
130  if ((*buffer2)->used == 0) {
131  (*buffer1)->used += snprintf((*buffer1)->data + (*buffer1)->used,
132  (*buffer1)->size - (*buffer1)->used, ",");
133  } else {
134  (*buffer1)->used += snprintf((*buffer1)->data + (*buffer1)->used,
135  (*buffer1)->size - (*buffer1)->used, ",%s",
136  (*buffer2)->data);
137  }
138 
139  Ja3BufferFree(buffer2);
140 
141  return 0;
142 }
143 
144 /**
145  * \internal
146  * \brief Return number of digits in number.
147  *
148  * \param num The number.
149  *
150  * \return digits Number of digits.
151  */
152 static uint32_t NumberOfDigits(uint32_t num)
153 {
154  if (num < 10) {
155  return 1;
156  }
157 
158  return 1 + NumberOfDigits(num / 10);
159 }
160 
161 /**
162  * \brief Add value to buffer.
163  *
164  * \param buffer The buffer.
165  * \param value The value.
166  *
167  * \retval 0 on success.
168  * \retval -1 on failure.
169  */
170 int Ja3BufferAddValue(JA3Buffer **buffer, uint32_t value)
171 {
172  if (*buffer == NULL) {
173  SCLogError("Buffer should not be NULL");
174  return -1;
175  }
176 
177  if ((*buffer)->data == NULL) {
178  (*buffer)->data = SCMalloc(JA3_BUFFER_INITIAL_SIZE);
179  if ((*buffer)->data == NULL) {
180  SCLogError("Error allocating memory for JA3 data");
181  Ja3BufferFree(buffer);
182  return -1;
183  }
184  (*buffer)->size = JA3_BUFFER_INITIAL_SIZE;
185  }
186 
187  uint32_t value_len = NumberOfDigits(value);
188 
189  int rc = Ja3BufferResizeIfFull(*buffer, value_len);
190  if (rc != 0) {
191  Ja3BufferFree(buffer);
192  return -1;
193  }
194 
195  if ((*buffer)->used == 0) {
196  (*buffer)->used += snprintf((*buffer)->data, (*buffer)->size, "%u", value);
197  }
198  else {
199  (*buffer)->used += snprintf(
200  (*buffer)->data + (*buffer)->used, (*buffer)->size - (*buffer)->used, "-%u", value);
201  }
202 
203  return 0;
204 }
205 
206 /**
207  * \brief Generate Ja3 hash string.
208  *
209  * \param buffer The Ja3 buffer.
210  *
211  * \retval pointer to hash string on success.
212  * \retval NULL on failure.
213  */
215 {
216  if (buffer == NULL) {
217  SCLogError("Buffer should not be NULL");
218  return NULL;
219  }
220 
221  if (buffer->data == NULL) {
222  SCLogError("Buffer data should not be NULL");
223  return NULL;
224  }
225 
226  char *ja3_hash = SCMalloc(SC_MD5_HEX_LEN + 1);
227  if (ja3_hash == NULL) {
228  SCLogError("Error allocating memory for JA3 hash");
229  return NULL;
230  }
231 
232  SCMd5HashBufferToHex((unsigned char *)buffer->data, buffer->used, ja3_hash, SC_MD5_HEX_LEN + 1);
233  return ja3_hash;
234 }
235 
236 /**
237  * \brief Check if JA3 is disabled.
238  *
239  * Issue warning if JA3 is disabled or if we are lacking support for JA3.
240  *
241  * \param type Type to add to warning.
242  *
243  * \retval 1 if disabled.
244  * \retval 0 otherwise.
245  */
246 int Ja3IsDisabled(const char *type)
247 {
248  bool is_enabled = SSLJA3IsEnabled();
249  if (is_enabled == 0) {
250  if (strcmp(type, "rule") != 0) {
251  SCLogWarning("JA3 is disabled, skipping %s", type);
252  }
253  return 1;
254  }
255 
256  return 0;
257 }
258 
260  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
261  const int list_id)
262 {
263  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
264  if (buffer->inspect == NULL) {
265  uint32_t b_len = 0;
266  const uint8_t *b = NULL;
267 
268  if (rs_quic_tx_get_ja3(txv, &b, &b_len) != 1)
269  return NULL;
270  if (b == NULL || b_len == 0)
271  return NULL;
272 
273  uint8_t ja3_hash[SC_MD5_HEX_LEN + 1];
274  // this adds a final zero
275  SCMd5HashBufferToHex(b, b_len, (char *)ja3_hash, SC_MD5_HEX_LEN + 1);
276 
277  InspectionBufferSetup(det_ctx, list_id, buffer, NULL, 0);
278  InspectionBufferCopy(buffer, ja3_hash, SC_MD5_HEX_LEN);
279  InspectionBufferApplyTransforms(buffer, transforms);
280  }
281  return buffer;
282 }
283 
285  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
286  const int list_id)
287 {
288  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
289  if (buffer->inspect == NULL) {
290  uint32_t b_len = 0;
291  const uint8_t *b = NULL;
292 
293  if (rs_quic_tx_get_ja3(txv, &b, &b_len) != 1)
294  return NULL;
295  if (b == NULL || b_len == 0)
296  return NULL;
297 
298  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
299  InspectionBufferApplyTransforms(buffer, transforms);
300  }
301  return buffer;
302 }
Ja3DetectGetHash
InspectionBuffer * Ja3DetectGetHash(DetectEngineThreadCtx *det_ctx, const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv, const int list_id)
Definition: util-ja3.c:259
len
uint8_t len
Definition: app-layer-dnp3.h:2
detect-engine.h
JA3Buffer_
Definition: util-ja3.h:31
DetectEngineTransforms
Definition: detect.h:372
JA3Buffer_::size
size_t size
Definition: util-ja3.h:33
InspectionBuffer
Definition: detect.h:337
Flow_
Flow data structure.
Definition: flow.h:357
Ja3BufferAddValue
int Ja3BufferAddValue(JA3Buffer **buffer, uint32_t value)
Add value to buffer.
Definition: util-ja3.c:170
JA3Buffer_::data
char * data
Definition: util-ja3.h:32
util-ja3.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1364
JA3_BUFFER_INITIAL_SIZE
#define JA3_BUFFER_INITIAL_SIZE
Definition: util-ja3.h:27
Ja3IsDisabled
int Ja3IsDisabled(const char *type)
Check if JA3 is disabled.
Definition: util-ja3.c:246
JA3Buffer_::used
size_t used
Definition: util-ja3.h:34
Ja3BufferInit
JA3Buffer * Ja3BufferInit(void)
Allocate new buffer.
Definition: util-ja3.c:39
Ja3BufferFree
void Ja3BufferFree(JA3Buffer **buffer)
Free allocated buffer.
Definition: util-ja3.c:54
type
uint8_t type
Definition: decode-icmpv4.h:0
SSLJA3IsEnabled
bool SSLJA3IsEnabled(void)
Definition: app-layer-ssl.c:3102
DetectEngineThreadCtx_
Definition: detect.h:1025
Ja3BufferAppendBuffer
int Ja3BufferAppendBuffer(JA3Buffer **buffer1, JA3Buffer **buffer2)
Append buffer to buffer.
Definition: util-ja3.c:106
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine.c:1494
suricata-common.h
Ja3GenerateHash
char * Ja3GenerateHash(JA3Buffer *buffer)
Generate Ja3 hash string.
Definition: util-ja3.c:214
SC_MD5_HEX_LEN
#define SC_MD5_HEX_LEN
Definition: rust.h:36
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1550
util-validate.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:338
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
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:1446
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:111
app-layer-ssl.h
Ja3DetectGetString
InspectionBuffer * Ja3DetectGetString(DetectEngineThreadCtx *det_ctx, const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv, const int list_id)
Definition: util-ja3.c:284