/src/suricata7/src/detect-ike-chosen-sa.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2020 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 | | * \author Frank Honza <frank.honza@dcso.de> |
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-content-inspection.h" |
29 | | #include "detect-ike-chosen-sa.h" |
30 | | #include "app-layer-parser.h" |
31 | | #include "util-byte.h" |
32 | | #include "util-unittest.h" |
33 | | |
34 | | #include "rust.h" |
35 | | |
36 | | /** |
37 | | * [ike.chosen_sa_attribute]:<sa_attribute>=<type>; |
38 | | */ |
39 | | |
40 | | // support the basic attributes, which are parsed as integer and life_duration, if variable length |
41 | | // is 4 it is stored as integer too |
42 | | #define PARSE_REGEX \ |
43 | 73 | "^\\s*(alg_enc|alg_hash|alg_auth|alg_dh|\ |
44 | 73 | sa_group_type|sa_life_type|sa_life_duration|alg_prf|sa_key_length|sa_field_size)\ |
45 | 73 | \\s*=\\s*([0-9]+)\\s*$" |
46 | | |
47 | | static DetectParseRegex parse_regex; |
48 | | |
49 | | typedef struct { |
50 | | char *sa_type; |
51 | | uint32_t sa_value; |
52 | | } DetectIkeChosenSaData; |
53 | | |
54 | | static DetectIkeChosenSaData *DetectIkeChosenSaParse(const char *); |
55 | | static int DetectIkeChosenSaSetup(DetectEngineCtx *, Signature *s, const char *str); |
56 | | static void DetectIkeChosenSaFree(DetectEngineCtx *, void *); |
57 | | static int g_ike_chosen_sa_buffer_id = 0; |
58 | | |
59 | | static int DetectIkeChosenSaMatch(DetectEngineThreadCtx *, Flow *, uint8_t, void *, void *, |
60 | | const Signature *, const SigMatchCtx *); |
61 | | void IKEChosenSaRegisterTests(void); |
62 | | |
63 | | /** |
64 | | * \brief Registration function for ike.ChosenSa keyword. |
65 | | */ |
66 | | void DetectIkeChosenSaRegister(void) |
67 | 73 | { |
68 | 73 | sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].name = "ike.chosen_sa_attribute"; |
69 | 73 | sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].desc = "match IKE chosen SA Attribute"; |
70 | 73 | sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].url = |
71 | 73 | "/rules/ike-keywords.html#ike-chosen_sa_attribute"; |
72 | 73 | sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].AppLayerTxMatch = DetectIkeChosenSaMatch; |
73 | 73 | sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].Setup = DetectIkeChosenSaSetup; |
74 | 73 | sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].Free = DetectIkeChosenSaFree; |
75 | | #ifdef UNITTESTS |
76 | | sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].RegisterTests = IKEChosenSaRegisterTests; |
77 | | #endif |
78 | 73 | DetectSetupParseRegexes(PARSE_REGEX, &parse_regex); |
79 | | |
80 | 73 | DetectAppLayerInspectEngineRegister2("ike.chosen_sa_attribute", ALPROTO_IKE, SIG_FLAG_TOCLIENT, |
81 | 73 | 1, DetectEngineInspectGenericList, NULL); |
82 | | |
83 | 73 | g_ike_chosen_sa_buffer_id = DetectBufferTypeGetByName("ike.chosen_sa_attribute"); |
84 | 73 | } |
85 | | |
86 | | /** |
87 | | * \internal |
88 | | * \brief Function to match SA attributes of a IKE state |
89 | | * |
90 | | * \param det_ctx Pointer to the pattern matcher thread. |
91 | | * \param f Pointer to the current flow. |
92 | | * \param flags Flags. |
93 | | * \param state App layer state. |
94 | | * \param txv Pointer to the Ike Transaction. |
95 | | * \param s Pointer to the Signature. |
96 | | * \param ctx Pointer to the sigmatch that we will cast into DetectIkeChosenSaData. |
97 | | * |
98 | | * \retval 0 no match. |
99 | | * \retval 1 match. |
100 | | */ |
101 | | static int DetectIkeChosenSaMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, |
102 | | void *state, void *txv, const Signature *s, const SigMatchCtx *ctx) |
103 | 149 | { |
104 | 149 | SCEnter(); |
105 | | |
106 | 149 | const DetectIkeChosenSaData *dd = (const DetectIkeChosenSaData *)ctx; |
107 | | |
108 | 149 | uint32_t value; |
109 | 149 | if (!rs_ike_state_get_sa_attribute(txv, dd->sa_type, &value)) |
110 | 112 | SCReturnInt(0); |
111 | 37 | if (value == dd->sa_value) |
112 | 33 | SCReturnInt(1); |
113 | 37 | SCReturnInt(0); |
114 | 37 | } |
115 | | |
116 | | /** |
117 | | * \internal |
118 | | * \brief Function to parse options passed via ike.chosen_sa_attribute keywords. |
119 | | * |
120 | | * \param rawstr Pointer to the user provided options. |
121 | | * |
122 | | * \retval dd pointer to DetectIkeChosenSaData on success. |
123 | | * \retval NULL on failure. |
124 | | */ |
125 | | static DetectIkeChosenSaData *DetectIkeChosenSaParse(const char *rawstr) |
126 | 2.86k | { |
127 | | /* |
128 | | * idea: do not implement one c file per type, invent an own syntax: |
129 | | * ike.chosen_sa_attribute:"encryption_algorithm=4" |
130 | | * ike.chosen_sa_attribute:"hash_algorithm=8" |
131 | | */ |
132 | 2.86k | DetectIkeChosenSaData *dd = NULL; |
133 | 2.86k | int res = 0; |
134 | 2.86k | size_t pcre2len; |
135 | 2.86k | char attribute[100]; |
136 | 2.86k | char value[100]; |
137 | | |
138 | 2.86k | pcre2_match_data *match = NULL; |
139 | 2.86k | int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0); |
140 | 2.86k | if (ret < 3 || ret > 5) { |
141 | 615 | SCLogError( |
142 | 615 | "pcre match for ike.chosen_sa_attribute failed, should be: <sa_attribute>=<type>, " |
143 | 615 | "but was: %s; error code %d", |
144 | 615 | rawstr, ret); |
145 | 615 | goto error; |
146 | 615 | } |
147 | | |
148 | 2.25k | pcre2len = sizeof(attribute); |
149 | 2.25k | res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)attribute, &pcre2len); |
150 | 2.25k | if (res < 0) { |
151 | 0 | SCLogError("pcre2_substring_copy_bynumber failed"); |
152 | 0 | goto error; |
153 | 0 | } |
154 | | |
155 | 2.25k | pcre2len = sizeof(value); |
156 | 2.25k | res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)value, &pcre2len); |
157 | 2.25k | if (res < 0) { |
158 | 0 | SCLogError("pcre2_substring_copy_bynumber failed"); |
159 | 0 | goto error; |
160 | 0 | } |
161 | | |
162 | 2.25k | dd = SCCalloc(1, sizeof(DetectIkeChosenSaData)); |
163 | 2.25k | if (unlikely(dd == NULL)) |
164 | 0 | goto error; |
165 | | |
166 | 2.25k | dd->sa_type = SCStrdup(attribute); |
167 | 2.25k | if (dd->sa_type == NULL) |
168 | 0 | goto error; |
169 | | |
170 | 2.25k | if (ByteExtractStringUint32(&dd->sa_value, 10, strlen(value), value) <= 0) { |
171 | 0 | SCLogError("invalid input as arg " |
172 | 0 | "to ike.chosen_sa_attribute keyword"); |
173 | 0 | goto error; |
174 | 0 | } |
175 | | |
176 | 2.25k | pcre2_match_data_free(match); |
177 | 2.25k | return dd; |
178 | | |
179 | 615 | error: |
180 | 615 | if (match) { |
181 | 615 | pcre2_match_data_free(match); |
182 | 615 | } |
183 | 615 | if (dd) { |
184 | 0 | if (dd->sa_type != NULL) |
185 | 0 | SCFree(dd->sa_type); |
186 | 0 | SCFree(dd); |
187 | 0 | } |
188 | 615 | return NULL; |
189 | 2.25k | } |
190 | | |
191 | | /** |
192 | | * \brief Function to add the parsed IKE SA attribute query into the current signature. |
193 | | * |
194 | | * \param de_ctx Pointer to the Detection Engine Context. |
195 | | * \param s Pointer to the Current Signature. |
196 | | * \param rawstr Pointer to the user provided flags options. |
197 | | * |
198 | | * \retval 0 on Success. |
199 | | * \retval -1 on Failure. |
200 | | */ |
201 | | static int DetectIkeChosenSaSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr) |
202 | 2.87k | { |
203 | 2.87k | if (DetectSignatureSetAppProto(s, ALPROTO_IKE) != 0) |
204 | 12 | return -1; |
205 | | |
206 | 2.86k | DetectIkeChosenSaData *dd = DetectIkeChosenSaParse(rawstr); |
207 | 2.86k | if (dd == NULL) { |
208 | 615 | SCLogError("Parsing \'%s\' failed", rawstr); |
209 | 615 | goto error; |
210 | 615 | } |
211 | | |
212 | | /* okay so far so good, lets get this into a SigMatch |
213 | | * and put it in the Signature. */ |
214 | 2.25k | SigMatch *sm = SigMatchAlloc(); |
215 | 2.25k | if (sm == NULL) |
216 | 0 | goto error; |
217 | | |
218 | 2.25k | sm->type = DETECT_AL_IKE_CHOSEN_SA; |
219 | 2.25k | sm->ctx = (void *)dd; |
220 | | |
221 | 2.25k | SigMatchAppendSMToList(s, sm, g_ike_chosen_sa_buffer_id); |
222 | 2.25k | return 0; |
223 | | |
224 | 615 | error: |
225 | 615 | DetectIkeChosenSaFree(de_ctx, dd); |
226 | 615 | return -1; |
227 | 2.25k | } |
228 | | |
229 | | /** |
230 | | * \internal |
231 | | * \brief Function to free memory associated with DetectIkeChosenSaData. |
232 | | * |
233 | | * \param de_ptr Pointer to DetectIkeChosenSaData. |
234 | | */ |
235 | | static void DetectIkeChosenSaFree(DetectEngineCtx *de_ctx, void *ptr) |
236 | 2.86k | { |
237 | 2.86k | DetectIkeChosenSaData *dd = (DetectIkeChosenSaData *)ptr; |
238 | 2.86k | if (dd == NULL) |
239 | 615 | return; |
240 | 2.25k | if (dd->sa_type != NULL) |
241 | 2.25k | SCFree(dd->sa_type); |
242 | | |
243 | 2.25k | SCFree(ptr); |
244 | 2.25k | } |
245 | | |
246 | | /* |
247 | | * ONLY TESTS BELOW THIS COMMENT |
248 | | */ |
249 | | |
250 | | #ifdef UNITTESTS |
251 | | |
252 | | /** |
253 | | * \test IKEChosenSaParserTest is a test for valid values |
254 | | * |
255 | | * \retval 1 on success |
256 | | * \retval 0 on failure |
257 | | */ |
258 | | static int IKEChosenSaParserTest(void) |
259 | | { |
260 | | DetectIkeChosenSaData *de = NULL; |
261 | | de = DetectIkeChosenSaParse("alg_hash=2"); |
262 | | |
263 | | FAIL_IF_NULL(de); |
264 | | FAIL_IF(de->sa_value != 2); |
265 | | FAIL_IF(strcmp(de->sa_type, "alg_hash") != 0); |
266 | | |
267 | | DetectIkeChosenSaFree(NULL, de); |
268 | | PASS; |
269 | | } |
270 | | |
271 | | #endif /* UNITTESTS */ |
272 | | |
273 | | void IKEChosenSaRegisterTests(void) |
274 | 0 | { |
275 | | #ifdef UNITTESTS |
276 | | UtRegisterTest("IKEChosenSaParserTest", IKEChosenSaParserTest); |
277 | | #endif /* UNITTESTS */ |
278 | 0 | } |