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 
59 static int g_ja4_hash_buffer_id = 0;
60 #endif
61 
62 /**
63  * \brief Registration function for keyword: ja4.hash
64  */
66 {
69  sigmatch_table[DETECT_AL_JA4_HASH].desc = "sticky buffer to match the JA4 hash buffer";
70  sigmatch_table[DETECT_AL_JA4_HASH].url = "/rules/ja4-keywords.html#ja4-hash";
71 #ifdef HAVE_JA4
72  sigmatch_table[DETECT_AL_JA4_HASH].Setup = DetectJa4HashSetup;
73 #else /* HAVE_JA4 */
74  sigmatch_table[DETECT_AL_JA4_HASH].Setup = DetectJA4SetupNoSupport;
75 #endif /* HAVE_JA4 */
78 
79 #ifdef HAVE_JA4
82 
84  "ja4.hash", SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, GetData, ALPROTO_TLS, 0);
85 
87  Ja4DetectGetHash, ALPROTO_QUIC, 1);
88 
90  DetectEngineInspectBufferGeneric, Ja4DetectGetHash);
91 
92  DetectBufferTypeSetDescriptionByName("ja4.hash", "TLS JA4 hash");
93 
94  g_ja4_hash_buffer_id = DetectBufferTypeGetByName("ja4.hash");
95 #endif /* HAVE_JA4 */
96 }
97 
98 #ifdef HAVE_JA4
99 /**
100  * \brief this function setup the ja4.hash modifier keyword used in the rule
101  *
102  * \param de_ctx Pointer to the Detection Engine Context
103  * \param s Pointer to the Signature to which the current keyword belongs
104  * \param str Should hold an empty string always
105  *
106  * \retval 0 On success
107  * \retval -1 On failure
108  */
109 static int DetectJa4HashSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
110 {
111  if (DetectBufferSetActiveList(de_ctx, s, g_ja4_hash_buffer_id) < 0)
112  return -1;
113 
114  if (s->alproto != ALPROTO_UNKNOWN && s->alproto != ALPROTO_TLS && s->alproto != ALPROTO_QUIC) {
115  SCLogError("rule contains conflicting protocols.");
116  return -1;
117  }
118 
119  /* try to enable JA4 */
120  SSLEnableJA4();
121 
122  /* check if JA4 enabling had an effect */
123  if (!RunmodeIsUnittests() && !SSLJA4IsEnabled()) {
125  SCLogError("JA4 support is not enabled");
126  }
127  return -2;
128  }
130 
131  return 0;
132 }
133 
134 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
135  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
136  const int list_id)
137 {
138  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
139  if (buffer->inspect == NULL) {
140  const SSLState *ssl_state = (SSLState *)f->alstate;
141 
142  if (ssl_state->client_connp.ja4 == NULL) {
143  return NULL;
144  }
145 
146  uint8_t data[JA4_HEX_LEN];
147  SCJA4GetHash(ssl_state->client_connp.ja4, (uint8_t(*)[JA4_HEX_LEN])data);
148 
149  InspectionBufferSetup(det_ctx, list_id, buffer, data, 0);
150  InspectionBufferCopy(buffer, data, JA4_HEX_LEN);
151  InspectionBufferApplyTransforms(buffer, transforms);
152  }
153 
154  return buffer;
155 }
156 
157 static InspectionBuffer *Ja4DetectGetHash(DetectEngineThreadCtx *det_ctx,
158  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
159  const int list_id)
160 {
161  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
162  if (buffer->inspect == NULL) {
163  uint32_t b_len = 0;
164  const uint8_t *b = NULL;
165 
166  if (rs_quic_tx_get_ja4(txv, &b, &b_len) != 1)
167  return NULL;
168  if (b == NULL || b_len == 0)
169  return NULL;
170 
171  InspectionBufferSetup(det_ctx, list_id, buffer, NULL, 0);
172  InspectionBufferCopy(buffer, (uint8_t *)b, JA4_HEX_LEN);
173  InspectionBufferApplyTransforms(buffer, transforms);
174  }
175  return buffer;
176 }
177 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1307
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:296
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1512
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
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:2154
SigTableElmt_::name
const char * name
Definition: detect.h:1304
DetectEngineTransforms
Definition: detect.h:408
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:314
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:33
SSLEnableJA4
void SSLEnableJA4(void)
if not explicitly disabled in config, enable ja4 support
Definition: app-layer-ssl.c:3357
Signature_::alproto
AppProto alproto
Definition: detect.h:606
ALPROTO_QUIC
@ ALPROTO_QUIC
Definition: app-layer-protos.h:51
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1356
InspectionBuffer
Definition: detect.h:373
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1298
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
SSLStateConnp_::ja4
JA4 * ja4
Definition: app-layer-ssl.h:280
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:553
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1500
JA4_HEX_LEN
#define JA4_HEX_LEN
Definition: util-ja4.h:27
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
util-ja4.h
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1093
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:750
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:107
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:670
DETECT_AL_JA4_HASH
@ DETECT_AL_JA4_HASH
Definition: detect-engine-register.h:329
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:251
SIG_FLAG_INIT_JA
#define SIG_FLAG_INIT_JA
Definition: detect.h:297
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine.c:1655
SigMatchSilentErrorEnabled
bool SigMatchSilentErrorEnabled(const DetectEngineCtx *de_ctx, const enum DetectKeywordId id)
Definition: detect-parse.c:378
SigTableElmt_::alias
const char * alias
Definition: detect.h:1305
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1711
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
str
#define str(s)
Definition: suricata-common.h:291
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:1595
Flow_::alstate
void * alstate
Definition: flow.h:481
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
SSLJA4IsEnabled
bool SSLJA4IsEnabled(void)
return whether ja4 is effectively enabled
Definition: app-layer-ssl.c:3389
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1488
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:240
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1190
DetectJa4HashRegister
void DetectJa4HashRegister(void)
Registration function for keyword: ja4.hash.
Definition: detect-ja4-hash.c:65
app-layer-ssl.h