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"
34#include "detect-engine-mpm.h"
36#include "detect-ja4-hash.h"
37
38#include "app-layer-ssl.h"
39
40#ifndef HAVE_JA4
41static int DetectJA4SetupNoSupport(DetectEngineCtx *a, Signature *b, const char *c)
42{
43 SCLogError("no JA4 support built in");
44 return -1;
45}
46#endif /* HAVE_JA4 */
47
48#ifdef HAVE_JA4
49static int DetectJa4HashSetup(DetectEngineCtx *, Signature *, const char *);
50static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
51 const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
52 const int list_id);
53int Ja4IsDisabled(const char *type);
54static InspectionBuffer *Ja4DetectGetHash(DetectEngineThreadCtx *det_ctx,
55 const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
56 const int list_id);
57#ifdef UNITTESTS
58static void DetectJa4RegisterTests(void);
59#endif
60
61static int g_ja4_hash_buffer_id = 0;
62#endif
63
64/**
65 * \brief Registration function for keyword: ja4.hash
66 */
68{
71 sigmatch_table[DETECT_JA4_HASH].desc = "sticky buffer to match the JA4 hash buffer";
72 sigmatch_table[DETECT_JA4_HASH].url = "/rules/ja4-keywords.html#ja4-hash";
73#ifdef HAVE_JA4
74 sigmatch_table[DETECT_JA4_HASH].Setup = DetectJa4HashSetup;
75#ifdef UNITTESTS
76 sigmatch_table[DETECT_JA4_HASH].RegisterTests = DetectJa4RegisterTests;
77#endif
78#else /* HAVE_JA4 */
79 sigmatch_table[DETECT_JA4_HASH].Setup = DetectJA4SetupNoSupport;
80#endif /* HAVE_JA4 */
83
84#ifdef HAVE_JA4
87
90
92 Ja4DetectGetHash, ALPROTO_QUIC, 1);
93
95 DetectEngineInspectBufferGeneric, Ja4DetectGetHash);
96
97 DetectBufferTypeSetDescriptionByName("ja4.hash", "TLS JA4 hash");
98
99 g_ja4_hash_buffer_id = DetectBufferTypeGetByName("ja4.hash");
100#endif /* HAVE_JA4 */
101}
102
103#ifdef HAVE_JA4
104/**
105 * \brief this function setup the ja4.hash modifier keyword used in the rule
106 *
107 * \param de_ctx Pointer to the Detection Engine Context
108 * \param s Pointer to the Signature to which the current keyword belongs
109 * \param str Should hold an empty string always
110 *
111 * \retval 0 On success
112 * \retval -1 On failure
113 */
114static int DetectJa4HashSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
115{
116 if (SCDetectBufferSetActiveList(de_ctx, s, g_ja4_hash_buffer_id) < 0)
117 return -1;
118
120 if (DetectSignatureSetMultiAppProto(s, alprotos) < 0) {
121 SCLogError("rule contains conflicting protocols.");
122 return -1;
123 }
124
125 /* try to enable JA4 */
126 SSLEnableJA4();
127
128 /* check if JA4 enabling had an effect */
129 if (!RunmodeIsUnittests() && !SSLJA4IsEnabled()) {
131 SCLogError("JA4 support is not enabled");
132 }
133 return -2;
134 }
135
136 return 0;
137}
138
139static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
140 const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
141 const int list_id)
142{
143 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
144 if (buffer->inspect == NULL) {
145 const SSLState *ssl_state = (SSLState *)f->alstate;
146
147 if (ssl_state->client_connp.hs == NULL) {
148 return NULL;
149 }
150
151 uint8_t data[JA4_HEX_LEN];
152 SCJA4GetHash(ssl_state->client_connp.hs, (uint8_t(*)[JA4_HEX_LEN])data);
153
154 InspectionBufferSetup(det_ctx, list_id, buffer, data, 0);
155 InspectionBufferCopy(buffer, data, JA4_HEX_LEN);
156 InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
157 }
158
159 return buffer;
160}
161
162static InspectionBuffer *Ja4DetectGetHash(DetectEngineThreadCtx *det_ctx,
163 const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
164 const int list_id)
165{
166 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
167 if (buffer->inspect == NULL) {
168 uint32_t b_len = 0;
169 const uint8_t *b = NULL;
170
171 if (SCQuicTxGetJa4(txv, &b, &b_len) != 1)
172 return NULL;
173 if (b == NULL || b_len == 0)
174 return NULL;
175
176 InspectionBufferSetup(det_ctx, list_id, buffer, NULL, 0);
177 InspectionBufferCopy(buffer, (uint8_t *)b, JA4_HEX_LEN);
178 InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
179 }
180 return buffer;
181}
182
183#ifdef UNITTESTS
184static int DetectJa4TestParse01(void)
185{
187
188 // invalid tests
189 Signature *s =
190 SigInit(de_ctx, "alert ip any any -> any any (sid: 1; file.data; content: \"toto\"; "
191 "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
192 // cannot have file.data with ja4.hash (quic or tls)
194 s = SigInit(de_ctx, "alert ip any any -> any any (sid: 1; "
195 "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\"; file.data; "
196 "content: \"toto\";)");
197 // cannot have file.data with ja4.hash (quic or tls)
199 s = SigInit(de_ctx, "alert smb any any -> any any (sid: 1; "
200 "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
201 // cannot have alproto=smb with ja4.hash (quic or tls)
203 s = SigInit(de_ctx, "alert ip any any -> any any (sid: 1; "
204 "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\"; smb.share; "
205 "content:\"toto\";)");
206 // cannot have a smb keyword with ja4.hash (quic or tls)
208 s = SigInit(de_ctx, "alert ip any any -> any any (sid: 1; "
209 "smb.share; content:\"toto\"; ja4.hash; content: "
210 "\"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
211 // cannot have a smb keyword with ja4.hash (quic or tls)
213
214 // valid tests
216 "alert ip any any -> any any (sid: 1; "
217 "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
218 // just ja4.hash any proto
219 FAIL_IF_NULL(s);
221 "alert quic any any -> any any (sid: 2; "
222 "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
223 // just ja4.hash only quic
224 FAIL_IF_NULL(s);
226 "alert tls any any -> any any (sid: 3; "
227 "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
228 // just ja4.hash only tls
229 FAIL_IF_NULL(s);
231 "alert ip any any -> any any (sid: 4; "
232 "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\"; "
233 "quic.version; content:\"|00|\";)");
234 // ja4.hash and a quic keyword
235 FAIL_IF_NULL(s);
237 PASS;
238}
239
240static void DetectJa4RegisterTests(void)
241{
242 UtRegisterTest("DetectJa4TestParse01", DetectJa4TestParse01);
243}
244#endif
245
246#endif // HAVE_JA4
uint16_t AppProto
@ ALPROTO_TLS
@ ALPROTO_UNKNOWN
@ ALPROTO_QUIC
void SSLEnableJA4(void)
if not explicitly disabled in config, enable ja4 support
bool SSLJA4IsEnabled(void)
return whether ja4 is effectively enabled
@ TLS_STATE_CLIENT_HELLO_DONE
uint16_t type
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
void InspectionBufferApplyTransforms(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
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
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
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
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.
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
void DetectJa4HashRegister(void)
Registration function for keyword: ja4.hash.
int DetectSignatureSetMultiAppProto(Signature *s, const AppProto *alprotos)
this function is used to set multiple possible app-layer protos
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
bool SigMatchSilentErrorEnabled(const DetectEngineCtx *de_ctx, const enum DetectKeywordId id)
SigTableElmt * sigmatch_table
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
void * alstate
Definition flow.h:479
SSLv[2.0|3.[0|1|2|3]] state structure.
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
const char * alias
Definition detect.h:1460
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define str(s)
int RunmodeIsUnittests(void)
Definition suricata.c:270
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267