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-mpm.h"
35 #include "detect-ja4-hash.h"
36 
37 #include "util-ja4.h"
38 
39 #include "app-layer-ssl.h"
40 
41 #ifndef HAVE_JA4
42 static int DetectJA4SetupNoSupport(DetectEngineCtx *a, Signature *b, const char *c)
43 {
44  SCLogError("no JA4 support built in");
45  return -1;
46 }
47 #endif /* HAVE_JA4 */
48 
49 #ifdef HAVE_JA4
50 static int DetectJa4HashSetup(DetectEngineCtx *, Signature *, const char *);
51 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
52  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
53  const int list_id);
54 int Ja4IsDisabled(const char *type);
55 static InspectionBuffer *Ja4DetectGetHash(DetectEngineThreadCtx *det_ctx,
56  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
57  const int list_id);
58 #ifdef UNITTESTS
59 static void DetectJa4RegisterTests(void);
60 #endif
61 
62 static int g_ja4_hash_buffer_id = 0;
63 #endif
64 
65 /**
66  * \brief Registration function for keyword: ja4.hash
67  */
69 {
70  sigmatch_table[DETECT_JA4_HASH].name = "ja4.hash";
71  sigmatch_table[DETECT_JA4_HASH].alias = "ja4_hash";
72  sigmatch_table[DETECT_JA4_HASH].desc = "sticky buffer to match the JA4 hash buffer";
73  sigmatch_table[DETECT_JA4_HASH].url = "/rules/ja4-keywords.html#ja4-hash";
74 #ifdef HAVE_JA4
75  sigmatch_table[DETECT_JA4_HASH].Setup = DetectJa4HashSetup;
76 #ifdef UNITTESTS
77  sigmatch_table[DETECT_JA4_HASH].RegisterTests = DetectJa4RegisterTests;
78 #endif
79 #else /* HAVE_JA4 */
80  sigmatch_table[DETECT_JA4_HASH].Setup = DetectJA4SetupNoSupport;
81 #endif /* HAVE_JA4 */
84 
85 #ifdef HAVE_JA4
88 
91 
93  Ja4DetectGetHash, ALPROTO_QUIC, 1);
94 
96  DetectEngineInspectBufferGeneric, Ja4DetectGetHash);
97 
98  DetectBufferTypeSetDescriptionByName("ja4.hash", "TLS JA4 hash");
99 
100  g_ja4_hash_buffer_id = DetectBufferTypeGetByName("ja4.hash");
101 #endif /* HAVE_JA4 */
102 }
103 
104 #ifdef HAVE_JA4
105 /**
106  * \brief this function setup the ja4.hash modifier keyword used in the rule
107  *
108  * \param de_ctx Pointer to the Detection Engine Context
109  * \param s Pointer to the Signature to which the current keyword belongs
110  * \param str Should hold an empty string always
111  *
112  * \retval 0 On success
113  * \retval -1 On failure
114  */
115 static int DetectJa4HashSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
116 {
117  if (DetectBufferSetActiveList(de_ctx, s, g_ja4_hash_buffer_id) < 0)
118  return -1;
119 
121  if (DetectSignatureSetMultiAppProto(s, alprotos) < 0) {
122  SCLogError("rule contains conflicting protocols.");
123  return -1;
124  }
125 
126  /* try to enable JA4 */
127  SSLEnableJA4();
128 
129  /* check if JA4 enabling had an effect */
130  if (!RunmodeIsUnittests() && !SSLJA4IsEnabled()) {
132  SCLogError("JA4 support is not enabled");
133  }
134  return -2;
135  }
136 
137  return 0;
138 }
139 
140 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
141  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
142  const int list_id)
143 {
144  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
145  if (buffer->inspect == NULL) {
146  const SSLState *ssl_state = (SSLState *)f->alstate;
147 
148  if (ssl_state->client_connp.ja4 == NULL) {
149  return NULL;
150  }
151 
152  uint8_t data[JA4_HEX_LEN];
153  SCJA4GetHash(ssl_state->client_connp.ja4, (uint8_t(*)[JA4_HEX_LEN])data);
154 
155  InspectionBufferSetup(det_ctx, list_id, buffer, data, 0);
156  InspectionBufferCopy(buffer, data, JA4_HEX_LEN);
157  InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
158  }
159 
160  return buffer;
161 }
162 
163 static InspectionBuffer *Ja4DetectGetHash(DetectEngineThreadCtx *det_ctx,
164  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
165  const int list_id)
166 {
167  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
168  if (buffer->inspect == NULL) {
169  uint32_t b_len = 0;
170  const uint8_t *b = NULL;
171 
172  if (rs_quic_tx_get_ja4(txv, &b, &b_len) != 1)
173  return NULL;
174  if (b == NULL || b_len == 0)
175  return NULL;
176 
177  InspectionBufferSetup(det_ctx, list_id, buffer, NULL, 0);
178  InspectionBufferCopy(buffer, (uint8_t *)b, JA4_HEX_LEN);
179  InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
180  }
181  return buffer;
182 }
183 
184 #ifdef UNITTESTS
185 static int DetectJa4TestParse01(void)
186 {
188 
189  // invalid tests
190  Signature *s =
191  SigInit(de_ctx, "alert ip any any -> any any (sid: 1; file.data; content: \"toto\"; "
192  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
193  // cannot have file.data with ja4.hash (quic or tls)
194  FAIL_IF_NOT_NULL(s);
195  s = SigInit(de_ctx, "alert ip any any -> any any (sid: 1; "
196  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\"; file.data; "
197  "content: \"toto\";)");
198  // cannot have file.data with ja4.hash (quic or tls)
199  FAIL_IF_NOT_NULL(s);
200  s = SigInit(de_ctx, "alert smb any any -> any any (sid: 1; "
201  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
202  // cannot have alproto=smb with ja4.hash (quic or tls)
203  FAIL_IF_NOT_NULL(s);
204  s = SigInit(de_ctx, "alert ip any any -> any any (sid: 1; "
205  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\"; smb.share; "
206  "content:\"toto\";)");
207  // cannot have a smb keyword with ja4.hash (quic or tls)
208  FAIL_IF_NOT_NULL(s);
209  s = SigInit(de_ctx, "alert ip any any -> any any (sid: 1; "
210  "smb.share; content:\"toto\"; ja4.hash; content: "
211  "\"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
212  // cannot have a smb keyword with ja4.hash (quic or tls)
213  FAIL_IF_NOT_NULL(s);
214 
215  // valid tests
217  "alert ip any any -> any any (sid: 1; "
218  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
219  // just ja4.hash any proto
220  FAIL_IF_NULL(s);
222  "alert quic any any -> any any (sid: 2; "
223  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
224  // just ja4.hash only quic
225  FAIL_IF_NULL(s);
227  "alert tls any any -> any any (sid: 3; "
228  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\";)");
229  // just ja4.hash only tls
230  FAIL_IF_NULL(s);
232  "alert ip any any -> any any (sid: 4; "
233  "ja4.hash; content: \"q13d0310h3_55b375c5d22e_cd85d2d88918\"; "
234  "quic.version; content:\"|00|\";)");
235  // ja4.hash and a quic keyword
236  FAIL_IF_NULL(s);
238  PASS;
239 }
240 
241 static void DetectJa4RegisterTests(void)
242 {
243  UtRegisterTest("DetectJa4TestParse01", DetectJa4TestParse01);
244 }
245 #endif
246 
247 #endif // HAVE_JA4
SigTableElmt_::url
const char * url
Definition: detect.h:1405
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:1616
SigTableElmt_::desc
const char * desc
Definition: detect.h:1404
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:2252
SigTableElmt_::name
const char * name
Definition: detect.h:1402
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:2140
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
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1422
InspectionBuffer
Definition: detect.h:380
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1396
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:920
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2799
SSLStateConnp_::ja4
JA4 * ja4
Definition: app-layer-ssl.h:285
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3344
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1387
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1578
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:1197
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:3002
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:1768
SigMatchSilentErrorEnabled
bool SigMatchSilentErrorEnabled(const DetectEngineCtx *de_ctx, const enum DetectKeywordId id)
Definition: detect-parse.c:406
SigTableElmt_::alias
const char * alias
Definition: detect.h:1403
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1650
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:381
str
#define str(s)
Definition: suricata-common.h:300
DETECT_JA4_HASH
@ DETECT_JA4_HASH
Definition: detect-engine-register.h:332
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:1712
Flow_::alstate
void * alstate
Definition: flow.h:479
detect-parse.h
Signature_
Signature container.
Definition: detect.h:669
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:2760
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1592
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:68
app-layer-ssl.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1394