/src/suricata8/src/detect-quic-ua.c
Line | Count | Source |
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" |
28 | | #include "detect-engine-buffer.h" |
29 | | #include "detect-engine-prefilter.h" |
30 | | #include "detect-engine-mpm.h" |
31 | | #include "detect-engine-content-inspection.h" |
32 | | #include "detect-engine-uint.h" |
33 | | #include "detect-quic-ua.h" |
34 | | #include "util-byte.h" |
35 | | #include "util-unittest.h" |
36 | | #include "rust.h" |
37 | | |
38 | | #ifdef UNITTESTS |
39 | | static void DetectQuicUaRegisterTests(void); |
40 | | #endif |
41 | | |
42 | 15 | #define BUFFER_NAME "quic_ua" |
43 | 5 | #define KEYWORD_NAME "quic.ua" |
44 | | |
45 | | static int quic_ua_id = 0; |
46 | | |
47 | | static int DetectQuicUaSetup(DetectEngineCtx *, Signature *, const char *); |
48 | | |
49 | | static InspectionBuffer *GetUaData(DetectEngineThreadCtx *det_ctx, |
50 | | const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv, |
51 | | const int list_id) |
52 | 0 | { |
53 | 0 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
54 | 0 | if (buffer->inspect == NULL) { |
55 | 0 | uint32_t b_len = 0; |
56 | 0 | const uint8_t *b = NULL; |
57 | |
|
58 | 0 | if (SCQuicTxGetUa(txv, &b, &b_len) != 1) |
59 | 0 | return NULL; |
60 | 0 | if (b == NULL || b_len == 0) |
61 | 0 | return NULL; |
62 | | |
63 | 0 | InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms); |
64 | 0 | } |
65 | 0 | return buffer; |
66 | 0 | } |
67 | | |
68 | | /** |
69 | | * \brief Registration function for quic.ua: keyword |
70 | | */ |
71 | | void DetectQuicUaRegister(void) |
72 | 5 | { |
73 | 5 | sigmatch_table[DETECT_QUIC_UA].name = KEYWORD_NAME; |
74 | 5 | sigmatch_table[DETECT_QUIC_UA].desc = "match Quic ua"; |
75 | 5 | sigmatch_table[DETECT_QUIC_UA].url = "/rules/quic-keywords.html#quic-ua"; |
76 | 5 | sigmatch_table[DETECT_QUIC_UA].Setup = DetectQuicUaSetup; |
77 | 5 | sigmatch_table[DETECT_QUIC_UA].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER; |
78 | | #ifdef UNITTESTS |
79 | | sigmatch_table[DETECT_QUIC_UA].RegisterTests = DetectQuicUaRegisterTests; |
80 | | #endif |
81 | | |
82 | 5 | DetectAppLayerMpmRegister(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, |
83 | 5 | GetUaData, ALPROTO_QUIC, 1); |
84 | | |
85 | 5 | DetectAppLayerInspectEngineRegister(BUFFER_NAME, ALPROTO_QUIC, SIG_FLAG_TOSERVER, 1, |
86 | 5 | DetectEngineInspectBufferGeneric, GetUaData); |
87 | | |
88 | 5 | quic_ua_id = DetectBufferTypeGetByName(BUFFER_NAME); |
89 | 5 | } |
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 | 11 | { |
104 | 11 | if (SCDetectBufferSetActiveList(de_ctx, s, quic_ua_id) < 0) |
105 | 1 | return -1; |
106 | | |
107 | 10 | if (SCDetectSignatureSetAppProto(s, ALPROTO_QUIC) < 0) |
108 | 4 | return -1; |
109 | | |
110 | 6 | return 0; |
111 | 10 | } |
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 | | { |
123 | | DetectEngineCtx *de_ctx = DetectEngineCtxInit(); |
124 | | FAIL_IF_NULL(de_ctx); |
125 | | |
126 | | Signature *sig = DetectEngineAppendSig( |
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 | | |
134 | | DetectEngineCtxFree(de_ctx); |
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 | | { |
147 | | DetectEngineCtx *de_ctx = DetectEngineCtxInit(); |
148 | | FAIL_IF_NULL(de_ctx); |
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 | | |
154 | | DetectEngineCtxFree(de_ctx); |
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 */ |