suricata
detect-quic-sni.c
Go to the documentation of this file.
1 /* Copyright (C) 2022 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  *
20  * Implements the quic.sni
21  */
22 
23 #include "suricata-common.h"
24 #include "conf.h"
25 #include "detect.h"
26 #include "detect-parse.h"
27 #include "detect-engine.h"
29 #include "detect-engine-mpm.h"
31 #include "detect-engine-uint.h"
32 #include "detect-quic-sni.h"
33 #include "util-byte.h"
34 #include "util-unittest.h"
35 #include "rust.h"
36 
37 #ifdef UNITTESTS
38 static void DetectQuicSniRegisterTests(void);
39 #endif
40 
41 #define BUFFER_NAME "quic_sni"
42 #define KEYWORD_NAME "quic.sni"
43 #define KEYWORD_ID DETECT_AL_QUIC_SNI
44 
45 static int quic_sni_id = 0;
46 
47 static int DetectQuicSniSetup(DetectEngineCtx *, Signature *, const char *);
48 
49 static InspectionBuffer *GetSniData(DetectEngineThreadCtx *det_ctx,
50  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
51  const int list_id)
52 {
53  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
54  if (buffer->inspect == NULL) {
55  uint32_t b_len = 0;
56  const uint8_t *b = NULL;
57 
58  if (rs_quic_tx_get_sni(txv, &b, &b_len) != 1)
59  return NULL;
60  if (b == NULL || b_len == 0)
61  return NULL;
62 
63  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
64  InspectionBufferApplyTransforms(buffer, transforms);
65  }
66  return buffer;
67 }
68 
69 /**
70  * \brief Registration function for quic.sni: keyword
71  */
73 {
75  sigmatch_table[DETECT_AL_QUIC_SNI].desc = "match Quic sni";
76  sigmatch_table[DETECT_AL_QUIC_SNI].url = "/rules/quic-keywords.html#quic-sni";
77  sigmatch_table[DETECT_AL_QUIC_SNI].Setup = DetectQuicSniSetup;
79 #ifdef UNITTESTS
80  sigmatch_table[DETECT_AL_QUIC_SNI].RegisterTests = DetectQuicSniRegisterTests;
81 #endif
82 
84  GetSniData, ALPROTO_QUIC, 1);
85 
88 
90 }
91 
92 /**
93  * \internal
94  * \brief this function is used to add the parsed sigmatch into the current signature
95  *
96  * \param de_ctx pointer to the Detection Engine Context
97  * \param s pointer to the Current Signature
98  * \param rawstr pointer to the user provided options
99  *
100  * \retval 0 on Success
101  * \retval -1 on Failure
102  */
103 static int DetectQuicSniSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
104 {
105  if (DetectBufferSetActiveList(s, quic_sni_id) < 0)
106  return -1;
107 
109  return -1;
110 
111  return 0;
112 }
113 
114 #ifdef UNITTESTS
115 
116 /**
117  * \test QuicSniTestParse01 is a test for a valid value
118  *
119  * \retval 1 on success
120  * \retval 0 on failure
121  */
122 static int QuicSniTestParse01(void)
123 {
126 
128  de_ctx, "alert ip any any -> any any (quic.sni; content:\"googe.com\"; sid:1; rev:1;)");
129  FAIL_IF_NULL(sig);
130 
131  sig = DetectEngineAppendSig(
132  de_ctx, "alert ip any any -> any any (quic.sni; content:\"|00|\"; sid:2; rev:1;)");
133  FAIL_IF_NULL(sig);
134 
136 
137  PASS;
138 }
139 
140 /**
141  * \test QuicSniTestParse03 is a test for an invalid value
142  *
143  * \retval 1 on success
144  * \retval 0 on failure
145  */
146 static int QuicSniTestParse03(void)
147 {
150 
151  Signature *sig =
152  DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (quic.sni:; sid:1; rev:1;)");
153  FAIL_IF_NOT_NULL(sig);
154 
156 
157  PASS;
158 }
159 
160 /**
161  * \brief this function registers unit tests for QuicSni
162  */
163 void DetectQuicSniRegisterTests(void)
164 {
165  UtRegisterTest("QuicSniTestParse01", QuicSniTestParse01);
166  UtRegisterTest("QuicSniTestParse03", QuicSniTestParse03);
167 }
168 
169 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1244
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1491
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:1451
SigTableElmt_::desc
const char * desc
Definition: detect.h:1243
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:1995
SigTableElmt_::name
const char * name
Definition: detect.h:1241
DetectEngineTransforms
Definition: detect.h:372
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
ALPROTO_QUIC
@ ALPROTO_QUIC
Definition: app-layer-protos.h:51
InspectionBuffer
Definition: detect.h:338
Flow_
Flow data structure.
Definition: flow.h:353
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-quic-sni.c:42
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:754
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1235
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2446
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1226
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1370
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1087
QuicStringGetDataArgs::txv
void * txv
Definition: detect-quic-cyu-string.c:46
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:230
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:17
DetectEngineThreadCtx_
Definition: detect.h:1034
detect-engine-mpm.h
detect.h
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:228
conf.h
DETECT_AL_QUIC_SNI
@ DETECT_AL_QUIC_SNI
Definition: detect-engine-register.h:292
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-quic-sni.c:41
DetectQuicSniRegister
void DetectQuicSniRegister(void)
Registration function for quic.sni: keyword.
Definition: detect-quic-sni.c:72
detect-engine-content-inspection.h
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2422
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1553
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:74
detect-quic-sni.h
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:339
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:1451
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2407
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1427
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1301
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1233