suricata
detect-ja4-hash.c
Go to the documentation of this file.
1 /* Copyright (C) 2023 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 Sascha Steinbiss <sascha@steinbiss.name>
22  *
23  * Implements support for ja4.hash keyword.
24  */
25 
26 #include "suricata-common.h"
27 #include "threads.h"
28 #include "decode.h"
29 #include "detect.h"
30 
31 #include "detect-parse.h"
32 #include "detect-engine.h"
33 #include "detect-engine-buffer.h"
34 #include "detect-engine-mpm.h"
36 #include "detect-ja4-hash.h"
37 
38 #include "util-ja4.h"
39 
40 #include "app-layer-ssl.h"
41 
42 #ifndef HAVE_JA4
43 static int DetectJA4SetupNoSupport(DetectEngineCtx *a, Signature *b, const char *c)
44 {
45  SCLogError("no JA4 support built in");
46  return -1;
47 }
48 #endif /* HAVE_JA4 */
49 
50 #ifdef HAVE_JA4
51 static int DetectJa4HashSetup(DetectEngineCtx *, Signature *, const char *);
52 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
53  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
54  const int list_id);
55 int Ja4IsDisabled(const char *type);
56 static InspectionBuffer *Ja4DetectGetHash(DetectEngineThreadCtx *det_ctx,
57  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
58  const int list_id);
59 #ifdef UNITTESTS
60 static void DetectJa4RegisterTests(void);
61 #endif
62 
63 static int g_ja4_hash_buffer_id = 0;
64 #endif
65 
66 /**
67  * \brief Registration function for keyword: ja4.hash
68  */
70 {
71  sigmatch_table[DETECT_JA4_HASH].name = "ja4.hash";
72  sigmatch_table[DETECT_JA4_HASH].alias = "ja4_hash";
73  sigmatch_table[DETECT_JA4_HASH].desc = "sticky buffer to match the JA4 hash buffer";
74  sigmatch_table[DETECT_JA4_HASH].url = "/rules/ja4-keywords.html#ja4-hash";
75 #ifdef HAVE_JA4
76  sigmatch_table[DETECT_JA4_HASH].Setup = DetectJa4HashSetup;
77 #ifdef UNITTESTS
78  sigmatch_table[DETECT_JA4_HASH].RegisterTests = DetectJa4RegisterTests;
79 #endif
80 #else /* HAVE_JA4 */
81  sigmatch_table[DETECT_JA4_HASH].Setup = DetectJA4SetupNoSupport;
82 #endif /* HAVE_JA4 */
85 
86 #ifdef HAVE_JA4
89 
92 
94  Ja4DetectGetHash, ALPROTO_QUIC, 1);
95 
97  DetectEngineInspectBufferGeneric, Ja4DetectGetHash);
98 
99  DetectBufferTypeSetDescriptionByName("ja4.hash", "TLS JA4 hash");
100 
101  g_ja4_hash_buffer_id = DetectBufferTypeGetByName("ja4.hash");
102 #endif /* HAVE_JA4 */
103 }
104 
105 #ifdef HAVE_JA4
106 /**
107  * \brief this function setup the ja4.hash modifier keyword used in the rule
108  *
109  * \param de_ctx Pointer to the Detection Engine Context
110  * \param s Pointer to the Signature to which the current keyword belongs
111  * \param str Should hold an empty string always
112  *
113  * \retval 0 On success
114  * \retval -1 On failure
115  */
116 static int DetectJa4HashSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
117 {
118  if (SCDetectBufferSetActiveList(de_ctx, s, g_ja4_hash_buffer_id) < 0)
119  return -1;
120 
122  if (DetectSignatureSetMultiAppProto(s, alprotos) < 0) {
123  SCLogError("rule contains conflicting protocols.");
124  return -1;
125  }
126 
127  /* try to enable JA4 */
128  SSLEnableJA4();
129 
130  /* check if JA4 enabling had an effect */
131  if (!RunmodeIsUnittests() && !SSLJA4IsEnabled()) {
133  SCLogError("JA4 support is not enabled");
134  }
135  return -2;
136  }
137 
138  return 0;
139 }
140 
141 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
142  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
143  const int list_id)
144 {
145  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
146  if (buffer->inspect == NULL) {
147  const SSLState *ssl_state = (SSLState *)f->alstate;
148 
149  if (ssl_state->client_connp.ja4 == NULL) {
150  return NULL;
151  }
152 
153  uint8_t data[JA4_HEX_LEN];
154  SCJA4GetHash(ssl_state->client_connp.ja4, (uint8_t(*)[JA4_HEX_LEN])data);
155 
156  InspectionBufferSetup(det_ctx, list_id, buffer, data, 0);
157  InspectionBufferCopy(buffer, data, JA4_HEX_LEN);
158  InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
159  }
160 
161  return buffer;
162 }
163 
164 static InspectionBuffer *Ja4DetectGetHash(DetectEngineThreadCtx *det_ctx,
165  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
166  const int list_id)
167 {
168  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
169  if (buffer->inspect == NULL) {
170  uint32_t b_len = 0;
171  const uint8_t *b = NULL;
172 
173  if (SCQuicTxGetJa4(txv, &b, &b_len) != 1)
174  return NULL;
175  if (b == NULL || b_len == 0)
176  return NULL;
177 
178  InspectionBufferSetup(det_ctx, list_id, buffer, NULL, 0);
179  InspectionBufferCopy(buffer, (uint8_t *)b, JA4_HEX_LEN);
180  InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
181  }
182  return buffer;
183 }
184 
185 #ifdef UNITTESTS
186 static int DetectJa4TestParse01(void)
187 {
189 
190  // invalid tests
191  Signature *s =
192  SigInit(de_ctx, "alert ip any any -> any any (sid: 1; file.data; content: \"toto\"; "
193  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
194  // cannot have file.data with ja4.hash (quic or tls)
195  FAIL_IF_NOT_NULL(s);
196  s = SigInit(de_ctx, "alert ip any any -> any any (sid: 1; "
197  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\"; file.data; "
198  "content: \"toto\";)");
199  // cannot have file.data with ja4.hash (quic or tls)
200  FAIL_IF_NOT_NULL(s);
201  s = SigInit(de_ctx, "alert smb any any -> any any (sid: 1; "
202  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
203  // cannot have alproto=smb with ja4.hash (quic or tls)
204  FAIL_IF_NOT_NULL(s);
205  s = SigInit(de_ctx, "alert ip any any -> any any (sid: 1; "
206  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\"; smb.share; "
207  "content:\"toto\";)");
208  // cannot have a smb keyword with ja4.hash (quic or tls)
209  FAIL_IF_NOT_NULL(s);
210  s = SigInit(de_ctx, "alert ip any any -> any any (sid: 1; "
211  "smb.share; content:\"toto\"; ja4.hash; content: "
212  "\"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
213  // cannot have a smb keyword with ja4.hash (quic or tls)
214  FAIL_IF_NOT_NULL(s);
215 
216  // valid tests
218  "alert ip any any -> any any (sid: 1; "
219  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
220  // just ja4.hash any proto
221  FAIL_IF_NULL(s);
223  "alert quic any any -> any any (sid: 2; "
224  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
225  // just ja4.hash only quic
226  FAIL_IF_NULL(s);
228  "alert tls any any -> any any (sid: 3; "
229  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
230  // just ja4.hash only tls
231  FAIL_IF_NULL(s);
233  "alert ip any any -> any any (sid: 4; "
234  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\"; "
235  "quic.version; content:\"|00|\";)");
236  // ja4.hash and a quic keyword
237  FAIL_IF_NULL(s);
239  PASS;
240 }
241 
242 static void DetectJa4RegisterTests(void)
243 {
244  UtRegisterTest("DetectJa4TestParse01", DetectJa4TestParse01);
245 }
246 #endif
247 
248 #endif // HAVE_JA4
SigTableElmt_::url
const char * url
Definition: detect.h:1431
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:301
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1645
SigTableElmt_::desc
const char * desc
Definition: detect.h:1430
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
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:2103
SigTableElmt_::name
const char * name
Definition: detect.h:1428
DetectEngineTransforms
Definition: detect.h:415
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:322
DetectSignatureSetMultiAppProto
int DetectSignatureSetMultiAppProto(Signature *s, const AppProto *alprotos)
this function is used to set multiple possible app-layer protos
Definition: detect-parse.c:2235
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:39
SSLEnableJA4
void SSLEnableJA4(void)
if not explicitly disabled in config, enable ja4 support
Definition: app-layer-ssl.c:3506
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:85
ALPROTO_QUIC
@ ALPROTO_QUIC
Definition: app-layer-protos.h:57
InspectionBuffer
Definition: detect.h:380
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1422
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:931
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2674
SSLStateConnp_::ja4
JA4 * ja4
Definition: app-layer-ssl.h:285
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3439
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1413
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1429
JA4_HEX_LEN
#define JA4_HEX_LEN
Definition: util-ja4.h:27
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1157
util-ja4.h
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
decode.h
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1223
TLS_STATE_CLIENT_HELLO_DONE
@ TLS_STATE_CLIENT_HELLO_DONE
Definition: app-layer-ssl.h:79
detect-engine-mpm.h
detect.h
detect-ja4-hash.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1547
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:3097
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
type
uint16_t type
Definition: decode-vlan.c:106
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:257
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine.c:1619
SigMatchSilentErrorEnabled
bool SigMatchSilentErrorEnabled(const DetectEngineCtx *de_ctx, const enum DetectKeywordId id)
Definition: detect-parse.c:406
SigTableElmt_::alias
const char * alias
Definition: detect.h:1429
suricata-common.h
detect-engine-buffer.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1501
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:381
str
#define str(s)
Definition: suricata-common.h:308
DETECT_JA4_HASH
@ DETECT_JA4_HASH
Definition: detect-engine-register.h:329
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:1563
Flow_::alstate
void * alstate
Definition: flow.h:479
detect-parse.h
Signature_
Signature container.
Definition: detect.h:670
SSLJA4IsEnabled
bool SSLJA4IsEnabled(void)
return whether ja4 is effectively enabled
Definition: app-layer-ssl.c:3538
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2635
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1620
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:245
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1254
DetectJa4HashRegister
void DetectJa4HashRegister(void)
Registration function for keyword: ja4.hash.
Definition: detect-ja4-hash.c:69
app-layer-ssl.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1420