suricata
detect-quic-version.c
Go to the documentation of this file.
1/* Copyright (C) 2021 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.version
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"
30#include "detect-engine-mpm.h"
32#include "detect-engine-uint.h"
33#include "detect-quic-version.h"
34#include "util-byte.h"
35#include "util-unittest.h"
36#include "rust.h"
37
38#ifdef UNITTESTS
39static void DetectQuicVersionRegisterTests(void);
40#endif
41
42#define BUFFER_NAME "quic_version"
43#define KEYWORD_NAME "quic.version"
44
45static int quic_version_id = 0;
46
47static int DetectQuicVersionSetup(DetectEngineCtx *, Signature *, const char *);
48
49static InspectionBuffer *GetVersionData(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 (SCQuicTxGetVersion(txv, &b, &b_len) != 1)
59 return NULL;
60 if (b == NULL || b_len == 0)
61 return NULL;
62
63 InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
64 }
65 return buffer;
66}
67
68/**
69 * \brief Registration function for quic.version: keyword
70 */
94
95/**
96 * \internal
97 * \brief this function is used to add the parsed sigmatch into the current signature
98 *
99 * \param de_ctx pointer to the Detection Engine Context
100 * \param s pointer to the Current Signature
101 * \param rawstr pointer to the user provided options
102 *
103 * \retval 0 on Success
104 * \retval -1 on Failure
105 */
106static int DetectQuicVersionSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
107{
108 if (SCDetectBufferSetActiveList(de_ctx, s, quic_version_id) < 0)
109 return -1;
110
112 return -1;
113
114 return 0;
115}
116
117#ifdef UNITTESTS
118
119/**
120 * \test QuicVersionTestParse01 is a test for a valid value
121 *
122 * \retval 1 on success
123 * \retval 0 on failure
124 */
125static int QuicVersionTestParse01(void)
126{
129
131 de_ctx, "alert ip any any -> any any (quic.version; content:\"Q046\"; sid:1; rev:1;)");
132 FAIL_IF_NULL(sig);
133
135 de_ctx, "alert ip any any -> any any (quic.version; content:\"|00|\"; sid:2; rev:1;)");
136 FAIL_IF_NULL(sig);
137
139
140 PASS;
141}
142
143/**
144 * \test QuicVersionTestParse03 is a test for an invalid value
145 *
146 * \retval 1 on success
147 * \retval 0 on failure
148 */
149static int QuicVersionTestParse03(void)
150{
153
155 de_ctx, "alert ip any any -> any any (quic.version:; sid:1; rev:1;)");
156 FAIL_IF_NOT_NULL(sig);
157
159
160 PASS;
161}
162
163/**
164 * \brief this function registers unit tests for QuicVersion
165 */
166void DetectQuicVersionRegisterTests(void)
167{
168 UtRegisterTest("QuicVersionTestParse01", QuicVersionTestParse01);
169 UtRegisterTest("QuicVersionTestParse03", QuicVersionTestParse03);
170}
171
172#endif /* UNITTESTS */
@ ALPROTO_QUIC
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
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
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
@ DETECT_QUIC_VERSION
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
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.
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigTableElmt * sigmatch_table
void DetectQuicVersionRegister(void)
Registration function for quic.version: keyword.
#define BUFFER_NAME
#define KEYWORD_NAME
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668