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 static int DetectJa4HashSetup(DetectEngineCtx *, Signature *, const char *);
50 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
51  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
52  const int list_id);
53 int Ja4IsDisabled(const char *type);
54 static InspectionBuffer *Ja4DetectGetHash(DetectEngineThreadCtx *det_ctx,
55  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
56  const int list_id);
57 
58 static int g_ja4_hash_buffer_id = 0;
59 
60 /**
61  * \brief Registration function for keyword: ja4.hash
62  */
64 {
67  sigmatch_table[DETECT_AL_JA4_HASH].desc = "sticky buffer to match the JA4 hash buffer";
68  sigmatch_table[DETECT_AL_JA4_HASH].url = "/rules/ja4-keywords.html#ja4-hash";
69 #ifdef HAVE_JA4
70  sigmatch_table[DETECT_AL_JA4_HASH].Setup = DetectJa4HashSetup;
71 #else /* HAVE_JA4 */
72  sigmatch_table[DETECT_AL_JA4_HASH].Setup = DetectJA4SetupNoSupport;
73 #endif /* HAVE_JA4 */
76 
77 #ifdef HAVE_JA4
80 
82  "ja4.hash", SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, GetData, ALPROTO_TLS, 0);
83 
85  Ja4DetectGetHash, ALPROTO_QUIC, 1);
86 
88  DetectEngineInspectBufferGeneric, Ja4DetectGetHash);
89 
90  DetectBufferTypeSetDescriptionByName("ja4.hash", "TLS JA4 hash");
91 
92  g_ja4_hash_buffer_id = DetectBufferTypeGetByName("ja4.hash");
93 #endif /* HAVE_JA4 */
94 }
95 
96 /**
97  * \brief this function setup the ja4.hash modifier keyword used in the rule
98  *
99  * \param de_ctx Pointer to the Detection Engine Context
100  * \param s Pointer to the Signature to which the current keyword belongs
101  * \param str Should hold an empty string always
102  *
103  * \retval 0 On success
104  * \retval -1 On failure
105  */
106 static int DetectJa4HashSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
107 {
108  if (DetectBufferSetActiveList(de_ctx, s, g_ja4_hash_buffer_id) < 0)
109  return -1;
110 
111  if (s->alproto != ALPROTO_UNKNOWN && s->alproto != ALPROTO_TLS && s->alproto != ALPROTO_QUIC) {
112  SCLogError("rule contains conflicting protocols.");
113  return -1;
114  }
115 
116  /* try to enable JA4 */
117  SSLEnableJA4();
118 
119  /* check if JA4 enabling had an effect */
120  if (!RunmodeIsUnittests() && !SSLJA4IsEnabled()) {
122  SCLogError("JA4 support is not enabled");
123  }
124  return -2;
125  }
127 
128  return 0;
129 }
130 
131 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
132  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
133  const int list_id)
134 {
135  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
136  if (buffer->inspect == NULL) {
137  const SSLState *ssl_state = (SSLState *)f->alstate;
138 
139  if (ssl_state->client_connp.ja4 == NULL) {
140  return NULL;
141  }
142 
143  uint8_t data[JA4_HEX_LEN];
144  SCJA4GetHash(ssl_state->client_connp.ja4, (uint8_t(*)[JA4_HEX_LEN])data);
145 
146  InspectionBufferSetup(det_ctx, list_id, buffer, data, 0);
147  InspectionBufferCopy(buffer, data, JA4_HEX_LEN);
148  InspectionBufferApplyTransforms(buffer, transforms);
149  }
150 
151  return buffer;
152 }
153 
154 static InspectionBuffer *Ja4DetectGetHash(DetectEngineThreadCtx *det_ctx,
155  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
156  const int list_id)
157 {
158  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
159  if (buffer->inspect == NULL) {
160  uint32_t b_len = 0;
161  const uint8_t *b = NULL;
162 
163  if (rs_quic_tx_get_ja4(txv, &b, &b_len) != 1)
164  return NULL;
165  if (b == NULL || b_len == 0)
166  return NULL;
167 
168  InspectionBufferSetup(det_ctx, list_id, buffer, NULL, 0);
169  InspectionBufferCopy(buffer, (uint8_t *)b, JA4_HEX_LEN);
170  InspectionBufferApplyTransforms(buffer, transforms);
171  }
172  return buffer;
173 }
SigTableElmt_::url
const char * url
Definition: detect.h:1299
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:288
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1500
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
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:2122
SigTableElmt_::name
const char * name
Definition: detect.h:1296
DetectEngineTransforms
Definition: detect.h:409
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:306
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:3332
Signature_::alproto
AppProto alproto
Definition: detect.h:601
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:1335
InspectionBuffer
Definition: detect.h:374
threads.h
Flow_
Flow data structure.
Definition: flow.h:355
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1290
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
Ja4IsDisabled
int Ja4IsDisabled(const char *type)
SSLStateConnp_::ja4
JA4 * ja4
Definition: app-layer-ssl.h:272
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:548
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1479
JA4_HEX_LEN
#define JA4_HEX_LEN
Definition: util-ja4.h:27
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
util-ja4.h
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1095
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:745
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
type
uint16_t type
Definition: decode-vlan.c:107
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:665
DETECT_AL_JA4_HASH
@ DETECT_AL_JA4_HASH
Definition: detect-engine-register.h:355
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:254
SIG_FLAG_INIT_JA
#define SIG_FLAG_INIT_JA
Definition: detect.h:296
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine.c:1622
SigMatchSilentErrorEnabled
bool SigMatchSilentErrorEnabled(const DetectEngineCtx *de_ctx, const enum DetectKeywordId id)
Definition: detect-parse.c:389
SigTableElmt_::alias
const char * alias
Definition: detect.h:1297
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1678
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:375
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:1574
Flow_::alstate
void * alstate
Definition: flow.h:480
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
SSLJA4IsEnabled
bool SSLJA4IsEnabled(void)
return whether ja4 is effectively enabled
Definition: app-layer-ssl.c:3364
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1476
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:169
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1169
DetectJa4HashRegister
void DetectJa4HashRegister(void)
Registration function for keyword: ja4.hash.
Definition: detect-ja4-hash.c:63
app-layer-ssl.h