suricata
detect-quic-ua.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.ua
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-ua.h"
33 #include "util-byte.h"
34 #include "util-unittest.h"
35 #include "rust.h"
36 
37 #ifdef UNITTESTS
38 static void DetectQuicUaRegisterTests(void);
39 #endif
40 
41 #define BUFFER_NAME "quic_ua"
42 #define KEYWORD_NAME "quic.ua"
43 
44 static int quic_ua_id = 0;
45 
46 static int DetectQuicUaSetup(DetectEngineCtx *, Signature *, const char *);
47 
48 static InspectionBuffer *GetUaData(DetectEngineThreadCtx *det_ctx,
49  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
50  const int list_id)
51 {
52  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
53  if (buffer->inspect == NULL) {
54  uint32_t b_len = 0;
55  const uint8_t *b = NULL;
56 
57  if (rs_quic_tx_get_ua(txv, &b, &b_len) != 1)
58  return NULL;
59  if (b == NULL || b_len == 0)
60  return NULL;
61 
62  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
63  InspectionBufferApplyTransforms(buffer, transforms);
64  }
65  return buffer;
66 }
67 
68 /**
69  * \brief Registration function for quic.ua: keyword
70  */
72 {
74  sigmatch_table[DETECT_AL_QUIC_UA].desc = "match Quic ua";
75  sigmatch_table[DETECT_AL_QUIC_UA].url = "/rules/quic-keywords.html#quic-ua";
76  sigmatch_table[DETECT_AL_QUIC_UA].Setup = DetectQuicUaSetup;
78 #ifdef UNITTESTS
79  sigmatch_table[DETECT_AL_QUIC_UA].RegisterTests = DetectQuicUaRegisterTests;
80 #endif
81 
83  GetUaData, ALPROTO_QUIC, 1);
84 
87 
89 }
90 
91 /**
92  * \internal
93  * \brief this function is used to add the parsed sigmatch into the current signature
94  *
95  * \param de_ctx pointer to the Detection Engine Context
96  * \param s pointer to the Current Signature
97  * \param rawstr pointer to the user provided options
98  *
99  * \retval 0 on Success
100  * \retval -1 on Failure
101  */
102 static int DetectQuicUaSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
103 {
104  if (DetectBufferSetActiveList(de_ctx, s, quic_ua_id) < 0)
105  return -1;
106 
108  return -1;
109 
110  return 0;
111 }
112 
113 #ifdef UNITTESTS
114 
115 /**
116  * \test QuicUaTestParse01 is a test for a valid value
117  *
118  * \retval 1 on success
119  * \retval 0 on failure
120  */
121 static int QuicUaTestParse01(void)
122 {
125 
127  de_ctx, "alert ip any any -> any any (quic.ua; content:\"googe.com\"; sid:1; rev:1;)");
128  FAIL_IF_NULL(sig);
129 
130  sig = DetectEngineAppendSig(
131  de_ctx, "alert ip any any -> any any (quic.ua; content:\"|00|\"; sid:2; rev:1;)");
132  FAIL_IF_NULL(sig);
133 
135 
136  PASS;
137 }
138 
139 /**
140  * \test QuicUaTestParse03 is a test for an invalid value
141  *
142  * \retval 1 on success
143  * \retval 0 on failure
144  */
145 static int QuicUaTestParse03(void)
146 {
149 
150  Signature *sig =
151  DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (quic.ua:; sid:1; rev:1;)");
152  FAIL_IF_NOT_NULL(sig);
153 
155 
156  PASS;
157 }
158 
159 /**
160  * \brief this function registers unit tests for QuicUa
161  */
162 void DetectQuicUaRegisterTests(void)
163 {
164  UtRegisterTest("QuicUaTestParse01", QuicUaTestParse01);
165  UtRegisterTest("QuicUaTestParse03", QuicUaTestParse03);
166 }
167 
168 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
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: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
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DetectQuicUaRegister
void DetectQuicUaRegister(void)
Registration function for quic.ua: keyword.
Definition: detect-quic-ua.c:71
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
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
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2611
rust.h
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-quic-ua.c:42
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2587
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1500
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
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:1093
detect-engine-mpm.h
detect.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
conf.h
detect-engine-content-inspection.h
DETECT_AL_QUIC_UA
@ DETECT_AL_QUIC_UA
Definition: detect-engine-register.h:296
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1711
detect-quic-ua.h
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
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
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2572
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
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-quic-ua.c:41
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1296