suricata
detect-nfs-procedure.c
Go to the documentation of this file.
1 /* Copyright (C) 2017-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  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  */
23 
24 #include "suricata-common.h"
25 #include "threads.h"
26 #include "decode.h"
27 #include "detect.h"
28 
29 #include "detect-parse.h"
30 #include "detect-engine.h"
31 #include "detect-engine-mpm.h"
32 #include "detect-content.h"
33 #include "detect-pcre.h"
34 #include "detect-nfs-procedure.h"
35 #include "detect-engine-uint.h"
36 
37 #include "app-layer-parser.h"
38 
39 #include "flow.h"
40 #include "flow-util.h"
41 #include "flow-var.h"
42 
43 #include "util-unittest.h"
44 #include "util-unittest-helper.h"
45 #include "util-byte.h"
46 
47 #include "app-layer-nfs-tcp.h"
48 #include "rust.h"
49 
50 static int DetectNfsProcedureSetup (DetectEngineCtx *, Signature *s, const char *str);
51 static void DetectNfsProcedureFree(DetectEngineCtx *, void *);
52 #ifdef UNITTESTS
53 static void DetectNfsProcedureRegisterTests(void);
54 #endif
55 static int g_nfs_request_buffer_id = 0;
56 
57 static int DetectNfsProcedureMatch (DetectEngineThreadCtx *, Flow *,
58  uint8_t, void *, void *, const Signature *,
59  const SigMatchCtx *);
60 
61 /**
62  * \brief Registration function for nfs_procedure keyword.
63  */
65 {
66  sigmatch_table[DETECT_AL_NFS_PROCEDURE].name = "nfs_procedure";
67  sigmatch_table[DETECT_AL_NFS_PROCEDURE].desc = "match NFS procedure";
68  sigmatch_table[DETECT_AL_NFS_PROCEDURE].url = "/rules/nfs-keywords.html#procedure";
70  sigmatch_table[DETECT_AL_NFS_PROCEDURE].AppLayerTxMatch = DetectNfsProcedureMatch;
71  sigmatch_table[DETECT_AL_NFS_PROCEDURE].Setup = DetectNfsProcedureSetup;
72  sigmatch_table[DETECT_AL_NFS_PROCEDURE].Free = DetectNfsProcedureFree;
73 #ifdef UNITTESTS
74  sigmatch_table[DETECT_AL_NFS_PROCEDURE].RegisterTests = DetectNfsProcedureRegisterTests;
75 #endif
76 
79 
80  g_nfs_request_buffer_id = DetectBufferTypeGetByName("nfs_request");
81 
82  SCLogDebug("g_nfs_request_buffer_id %d", g_nfs_request_buffer_id);
83 }
84 
85 /**
86  * \internal
87  * \brief Function to match procedure of a TX
88  *
89  * For 'file txs'
90  *
91  * \param t Pointer to thread vars.
92  * \param det_ctx Pointer to the pattern matcher thread.
93  * \param f Pointer to the current flow.
94  * \param flags Flags.
95  * \param state App layer state.
96  * \param s Pointer to the Signature.
97  * \param m Pointer to the sigmatch that we will cast into
98  * DetectU32Data.
99  *
100  * \retval 0 no match.
101  * \retval 1 match.
102  */
103 static int DetectNfsProcedureMatch (DetectEngineThreadCtx *det_ctx,
104  Flow *f, uint8_t flags, void *state,
105  void *txv, const Signature *s,
106  const SigMatchCtx *ctx)
107 {
108  SCEnter();
109 
110  const DetectU32Data *dd = (const DetectU32Data *)ctx;
111  uint16_t i;
112  for (i = 0; i < 256; i++) {
113  uint32_t procedure;
114  if (rs_nfs_tx_get_procedures(txv, i, &procedure) == 1) {
115  SCLogDebug("proc %u mode %u lo %u hi %u", procedure, dd->mode, dd->arg1, dd->arg2);
116  if (DetectU32Match(procedure, dd))
117  SCReturnInt(1);
118  continue;
119  }
120  break;
121  }
122  SCReturnInt(0);
123 }
124 
125 /**
126  * \internal
127  * \brief Function to parse options passed via tls validity keywords.
128  *
129  * \param rawstr Pointer to the user provided options.
130  *
131  * \retval dd pointer to DetectU32Data on success.
132  * \retval NULL on failure.
133  */
134 static DetectU32Data *DetectNfsProcedureParse(const char *rawstr)
135 {
136  return rs_detect_u32_parse_inclusive(rawstr);
137 }
138 
139 
140 
141 /**
142  * \brief Function to add the parsed tls validity field into the current signature.
143  *
144  * \param de_ctx Pointer to the Detection Engine Context.
145  * \param s Pointer to the Current Signature.
146  * \param rawstr Pointer to the user provided flags options.
147  * \param type Defines if this is notBefore or notAfter.
148  *
149  * \retval 0 on Success.
150  * \retval -1 on Failure.
151  */
152 static int DetectNfsProcedureSetup (DetectEngineCtx *de_ctx, Signature *s,
153  const char *rawstr)
154 {
155  DetectU32Data *dd = NULL;
156 
157  SCLogDebug("\'%s\'", rawstr);
158 
160  return -1;
161 
162  dd = DetectNfsProcedureParse(rawstr);
163  if (dd == NULL) {
164  SCLogError("Parsing \'%s\' failed", rawstr);
165  goto error;
166  }
167 
168  /* okay so far so good, lets get this into a SigMatch
169  * and put it in the Signature. */
170 
171  SCLogDebug("low %u hi %u", dd->arg1, dd->arg2);
173  g_nfs_request_buffer_id) == NULL) {
174  goto error;
175  }
176  return 0;
177 
178 error:
179  DetectNfsProcedureFree(de_ctx, dd);
180  return -1;
181 }
182 
183 /**
184  * \internal
185  * \brief Function to free memory associated with DetectU32Data.
186  *
187  * \param de_ptr Pointer to DetectU32Data.
188  */
189 void DetectNfsProcedureFree(DetectEngineCtx *de_ctx, void *ptr)
190 {
191  rs_detect_u32_free(ptr);
192 }
193 
194 #ifdef UNITTESTS
195 
196 /**
197  * \test This is a test for a valid value 1430000000.
198  *
199  * \retval 1 on success.
200  * \retval 0 on failure.
201  */
202 static int ValidityTestParse01 (void)
203 {
204  DetectU32Data *dd = NULL;
205  dd = DetectNfsProcedureParse("1430000000");
206  FAIL_IF_NULL(dd);
207  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->mode == DETECT_UINT_EQ);
208  DetectNfsProcedureFree(NULL, dd);
209  PASS;
210 }
211 
212 /**
213  * \test This is a test for a valid value >1430000000.
214  *
215  * \retval 1 on success.
216  * \retval 0 on failure.
217  */
218 static int ValidityTestParse02 (void)
219 {
220  DetectU32Data *dd = NULL;
221  dd = DetectNfsProcedureParse(">1430000000");
222  FAIL_IF_NULL(dd);
223  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->mode == DETECT_UINT_GT);
224  DetectNfsProcedureFree(NULL, dd);
225  PASS;
226 }
227 
228 /**
229  * \test This is a test for a valid value <1430000000.
230  *
231  * \retval 1 on success.
232  * \retval 0 on failure.
233  */
234 static int ValidityTestParse03 (void)
235 {
236  DetectU32Data *dd = NULL;
237  dd = DetectNfsProcedureParse("<1430000000");
238  FAIL_IF_NULL(dd);
239  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->mode == DETECT_UINT_LT);
240  DetectNfsProcedureFree(NULL, dd);
241  PASS;
242 }
243 
244 /**
245  * \test This is a test for a valid value 1430000000<>1470000000.
246  *
247  * \retval 1 on success.
248  * \retval 0 on failure.
249  */
250 static int ValidityTestParse04 (void)
251 {
252  DetectU32Data *dd = NULL;
253  dd = DetectNfsProcedureParse("1430000001<>1470000000");
254  FAIL_IF_NULL(dd);
255  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->arg2 == 1470000001 && dd->mode == DETECT_UINT_RA);
256  DetectNfsProcedureFree(NULL, dd);
257  PASS;
258 }
259 
260 /**
261  * \test This is a test for a invalid value A.
262  *
263  * \retval 1 on success.
264  * \retval 0 on failure.
265  */
266 static int ValidityTestParse05 (void)
267 {
268  DetectU32Data *dd = NULL;
269  dd = DetectNfsProcedureParse("A");
270  FAIL_IF_NOT_NULL(dd);
271  PASS;
272 }
273 
274 /**
275  * \test This is a test for a invalid value >1430000000<>1470000000.
276  *
277  * \retval 1 on success.
278  * \retval 0 on failure.
279  */
280 static int ValidityTestParse06 (void)
281 {
282  DetectU32Data *dd = NULL;
283  dd = DetectNfsProcedureParse(">1430000000<>1470000000");
284  FAIL_IF_NOT_NULL(dd);
285  PASS;
286 }
287 
288 /**
289  * \test This is a test for a invalid value 1430000000<>.
290  *
291  * \retval 1 on success.
292  * \retval 0 on failure.
293  */
294 static int ValidityTestParse07 (void)
295 {
296  DetectU32Data *dd = NULL;
297  dd = DetectNfsProcedureParse("1430000000<>");
298  FAIL_IF_NOT_NULL(dd);
299  PASS;
300 }
301 
302 /**
303  * \test This is a test for a invalid value <>1430000000.
304  *
305  * \retval 1 on success.
306  * \retval 0 on failure.
307  */
308 static int ValidityTestParse08 (void)
309 {
310  DetectU32Data *dd = NULL;
311  dd = DetectNfsProcedureParse("<>1430000000");
312  FAIL_IF_NOT_NULL(dd);
313  PASS;
314 }
315 
316 /**
317  * \test This is a test for a invalid value "".
318  *
319  * \retval 1 on success.
320  * \retval 0 on failure.
321  */
322 static int ValidityTestParse09 (void)
323 {
324  DetectU32Data *dd = NULL;
325  dd = DetectNfsProcedureParse("");
326  FAIL_IF_NOT_NULL(dd);
327  PASS;
328 }
329 
330 /**
331  * \test This is a test for a invalid value " ".
332  *
333  * \retval 1 on success.
334  * \retval 0 on failure.
335  */
336 static int ValidityTestParse10 (void)
337 {
338  DetectU32Data *dd = NULL;
339  dd = DetectNfsProcedureParse(" ");
340  FAIL_IF_NOT_NULL(dd);
341  PASS;
342 }
343 
344 /**
345  * \test This is a test for a invalid value 1490000000<>1430000000.
346  *
347  * \retval 1 on success.
348  * \retval 0 on failure.
349  */
350 static int ValidityTestParse11 (void)
351 {
352  DetectU32Data *dd = NULL;
353  dd = DetectNfsProcedureParse("1490000000<>1430000000");
354  FAIL_IF_NOT_NULL(dd);
355  PASS;
356 }
357 
358 /**
359  * \test This is a test for a valid value 1430000000 <> 1490000000.
360  *
361  * \retval 1 on success.
362  * \retval 0 on failure.
363  */
364 static int ValidityTestParse12 (void)
365 {
366  DetectU32Data *dd = NULL;
367  dd = DetectNfsProcedureParse("1430000001 <> 1490000000");
368  FAIL_IF_NULL(dd);
369  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->arg2 == 1490000001 && dd->mode == DETECT_UINT_RA);
370  DetectNfsProcedureFree(NULL, dd);
371  PASS;
372 }
373 
374 /**
375  * \test This is a test for a valid value > 1430000000.
376  *
377  * \retval 1 on success.
378  * \retval 0 on failure.
379  */
380 static int ValidityTestParse13 (void)
381 {
382  DetectU32Data *dd = NULL;
383  dd = DetectNfsProcedureParse("> 1430000000 ");
384  FAIL_IF_NULL(dd);
385  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->mode == DETECT_UINT_GT);
386  DetectNfsProcedureFree(NULL, dd);
387  PASS;
388 }
389 
390 /**
391  * \test This is a test for a valid value < 1490000000.
392  *
393  * \retval 1 on success.
394  * \retval 0 on failure.
395  */
396 static int ValidityTestParse14 (void)
397 {
398  DetectU32Data *dd = NULL;
399  dd = DetectNfsProcedureParse("< 1490000000 ");
400  FAIL_IF_NULL(dd);
401  FAIL_IF_NOT(dd->arg1 == 1490000000 && dd->mode == DETECT_UINT_LT);
402  DetectNfsProcedureFree(NULL, dd);
403  PASS;
404 }
405 
406 /**
407  * \test This is a test for a valid value 1490000000.
408  *
409  * \retval 1 on success.
410  * \retval 0 on failure.
411  */
412 static int ValidityTestParse15 (void)
413 {
414  DetectU32Data *dd = NULL;
415  dd = DetectNfsProcedureParse(" 1490000000 ");
416  FAIL_IF_NULL(dd);
417  FAIL_IF_NOT(dd->arg1 == 1490000000 && dd->mode == DETECT_UINT_EQ);
418  DetectNfsProcedureFree(NULL, dd);
419  PASS;
420 }
421 
422 /**
423  * \brief Register unit tests for nfs_procedure.
424  */
425 void DetectNfsProcedureRegisterTests(void)
426 {
427  UtRegisterTest("ValidityTestParse01", ValidityTestParse01);
428  UtRegisterTest("ValidityTestParse02", ValidityTestParse02);
429  UtRegisterTest("ValidityTestParse03", ValidityTestParse03);
430  UtRegisterTest("ValidityTestParse04", ValidityTestParse04);
431  UtRegisterTest("ValidityTestParse05", ValidityTestParse05);
432  UtRegisterTest("ValidityTestParse06", ValidityTestParse06);
433  UtRegisterTest("ValidityTestParse07", ValidityTestParse07);
434  UtRegisterTest("ValidityTestParse08", ValidityTestParse08);
435  UtRegisterTest("ValidityTestParse09", ValidityTestParse09);
436  UtRegisterTest("ValidityTestParse10", ValidityTestParse10);
437  UtRegisterTest("ValidityTestParse11", ValidityTestParse11);
438  UtRegisterTest("ValidityTestParse12", ValidityTestParse12);
439  UtRegisterTest("ValidityTestParse13", ValidityTestParse13);
440  UtRegisterTest("ValidityTestParse14", ValidityTestParse14);
441  UtRegisterTest("ValidityTestParse15", ValidityTestParse15);
442 }
443 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1299
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
detect-content.h
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
DetectU32Match
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
Definition: detect-engine-uint.c:31
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1286
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1296
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DETECT_UINT_LT
#define DETECT_UINT_LT
Definition: detect-engine-uint.h:37
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DETECT_AL_NFS_PROCEDURE
@ DETECT_AL_NFS_PROCEDURE
Definition: detect-engine-register.h:172
threads.h
Flow_
Flow data structure.
Definition: flow.h:351
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
DETECT_UINT_EQ
#define DETECT_UINT_EQ
Definition: detect-engine-uint.h:35
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1267
rust.h
DETECT_UINT_GT
#define DETECT_UINT_GT
Definition: detect-engine-uint.h:32
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
detect-pcre.h
util-unittest.h
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
decode.h
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:1095
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
app-layer-parser.h
detect-nfs-procedure.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1264
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:345
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
DetectU32Data
DetectUintData_u32 DetectU32Data
Definition: detect-engine-uint.h:41
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct 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:2079
DetectNfsProcedureRegister
void DetectNfsProcedureRegister(void)
Registration function for nfs_procedure keyword.
Definition: detect-nfs-procedure.c:64
str
#define str(s)
Definition: suricata-common.h:291
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:169
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:447
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
flow-var.h
MQTTUnsubscribeTopicGetDataArgs::txv
void * txv
Definition: detect-mqtt-unsubscribe-topic.c:64
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:45
DETECT_UINT_RA
#define DETECT_UINT_RA
Definition: detect-engine-uint.h:34
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1288
app-layer-nfs-tcp.h