suricata
detect-tls-alpn.c
Go to the documentation of this file.
1 /* Copyright (C) 2024 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 Victor Julien <vjulien@oisf.net>
22  *
23  * Implements support for tls.alpn 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"
37 #include "detect-content.h"
38 #include "detect-tls-alpn.h"
39 #include "detect-engine-uint.h"
40 
41 #include "flow.h"
42 #include "flow-util.h"
43 #include "flow-var.h"
44 
45 #include "util-debug.h"
46 #include "util-spm.h"
47 #include "util-print.h"
48 
49 #include "stream-tcp.h"
50 
51 #include "app-layer.h"
52 #include "app-layer-ssl.h"
53 #include "util-profiling.h"
54 
55 static int DetectTlsAlpnSetup(DetectEngineCtx *, Signature *, const char *);
56 static int g_tls_alpn_buffer_id = 0;
57 
58 static bool TlsAlpnGetData(DetectEngineThreadCtx *det_ctx, const void *txv, const uint8_t flags,
59  uint32_t idx, const uint8_t **buf, uint32_t *buf_len)
60 {
61  SCEnter();
62 
63  const SSLState *ssl_state = (SSLState *)txv;
64  const SSLStateConnp *connp;
65 
66  if (flags & STREAM_TOSERVER) {
67  connp = &ssl_state->client_connp;
68  } else {
69  connp = &ssl_state->server_connp;
70  }
71 
72  if (TAILQ_EMPTY(&connp->alpns)) {
73  return false;
74  }
75 
76  SSLAlpns *a;
77  if (idx == 0) {
78  a = TAILQ_FIRST(&connp->alpns);
79  } else {
80  // TODO optimize ?
81  a = TAILQ_FIRST(&connp->alpns);
82  for (uint32_t i = 0; i < idx; i++) {
83  a = TAILQ_NEXT(a, next);
84  }
85  }
86  if (a == NULL) {
87  return false;
88  }
89 
90  *buf = a->alpn;
91  *buf_len = a->size;
92  return true;
93 }
94 
95 /**
96  * \brief Registration function for keyword: tls.alpn
97  */
99 {
100  sigmatch_table[DETECT_TLS_ALPN].name = "tls.alpn";
101  sigmatch_table[DETECT_TLS_ALPN].desc = "sticky buffer to match the TLS ALPN buffer";
102  sigmatch_table[DETECT_TLS_ALPN].url = "/rules/tls-keywords.html#tls-alpn";
103  sigmatch_table[DETECT_TLS_ALPN].Setup = DetectTlsAlpnSetup;
106 
108  TLS_STATE_CLIENT_HELLO_DONE, TlsAlpnGetData, 2);
110  "tls.alpn", ALPROTO_TLS, SIG_FLAG_TOCLIENT, TLS_STATE_SERVER_HELLO, TlsAlpnGetData, 2);
111 
112  DetectBufferTypeSetDescriptionByName("tls.alpn", "TLS APLN");
113 
115 
116  g_tls_alpn_buffer_id = DetectBufferTypeGetByName("tls.alpn");
117 }
118 
119 /**
120  * \brief This function setup the tls.alpn sticky buffer keyword
121  *
122  * \param de_ctx Pointer to the Detect Engine Context
123  * \param s Pointer to the Signature to which the keyword belongs
124  * \param str Should hold an empty string always
125  *
126  * \retval 0 On success
127  * \retval -1 On failure
128  */
129 static int DetectTlsAlpnSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
130 {
131  if (SCDetectBufferSetActiveList(de_ctx, s, g_tls_alpn_buffer_id) < 0)
132  return -1;
133 
135  return -1;
136 
137  return 0;
138 }
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1431
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2313
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:301
detect-content.h
detect-engine.h
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
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1428
stream-tcp.h
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:322
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:39
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:323
SSLStateConnp_
Definition: app-layer-ssl.h:240
threads.h
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1422
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:931
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:248
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1107
SSLAlpns_::size
uint32_t size
Definition: app-layer-ssl.h:236
SSLAlpns_
Definition: app-layer-ssl.h:234
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1413
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1157
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
decode.h
util-debug.h
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:250
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
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
util-profiling.h
detect-engine-content-inspection.h
DETECT_TLS_ALPN
@ DETECT_TLS_ALPN
Definition: detect-engine-register.h:148
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
DetectTlsAlpnRegister
void DetectTlsAlpnRegister(void)
Registration function for keyword: tls.alpn.
Definition: detect-tls-alpn.c:98
SSLAlpns_::alpn
uint8_t alpn[]
Definition: app-layer-ssl.h:237
TAILQ_NEXT
#define TAILQ_NEXT(elm, field)
Definition: queue.h:307
util-spm.h
detect-engine-buffer.h
detect-tls-alpn.h
str
#define str(s)
Definition: suricata-common.h:308
detect-parse.h
Signature_
Signature container.
Definition: detect.h:670
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1620
TLS_STATE_SERVER_HELLO
@ TLS_STATE_SERVER_HELLO
Definition: app-layer-ssl.h:87
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1254
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority)
Definition: detect-engine.c:2153
flow.h
flow-var.h
app-layer-ssl.h
app-layer.h