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  SigMatch *sm = NULL;
157 
158  SCLogDebug("\'%s\'", rawstr);
159 
161  return -1;
162 
163  dd = DetectNfsProcedureParse(rawstr);
164  if (dd == NULL) {
165  SCLogError("Parsing \'%s\' failed", rawstr);
166  goto error;
167  }
168 
169  /* okay so far so good, lets get this into a SigMatch
170  * and put it in the Signature. */
171  sm = SigMatchAlloc();
172  if (sm == NULL)
173  goto error;
174 
176  sm->ctx = (void *)dd;
177 
178  SCLogDebug("low %u hi %u", dd->arg1, dd->arg2);
179  SigMatchAppendSMToList(s, sm, g_nfs_request_buffer_id);
180  return 0;
181 
182 error:
183  DetectNfsProcedureFree(de_ctx, dd);
184  return -1;
185 }
186 
187 /**
188  * \internal
189  * \brief Function to free memory associated with DetectU32Data.
190  *
191  * \param de_ptr Pointer to DetectU32Data.
192  */
193 void DetectNfsProcedureFree(DetectEngineCtx *de_ctx, void *ptr)
194 {
195  rs_detect_u32_free(ptr);
196 }
197 
198 #ifdef UNITTESTS
199 
200 /**
201  * \test This is a test for a valid value 1430000000.
202  *
203  * \retval 1 on success.
204  * \retval 0 on failure.
205  */
206 static int ValidityTestParse01 (void)
207 {
208  DetectU32Data *dd = NULL;
209  dd = DetectNfsProcedureParse("1430000000");
210  FAIL_IF_NULL(dd);
211  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->mode == DETECT_UINT_EQ);
212  DetectNfsProcedureFree(NULL, dd);
213  PASS;
214 }
215 
216 /**
217  * \test This is a test for a valid value >1430000000.
218  *
219  * \retval 1 on success.
220  * \retval 0 on failure.
221  */
222 static int ValidityTestParse02 (void)
223 {
224  DetectU32Data *dd = NULL;
225  dd = DetectNfsProcedureParse(">1430000000");
226  FAIL_IF_NULL(dd);
227  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->mode == DETECT_UINT_GT);
228  DetectNfsProcedureFree(NULL, dd);
229  PASS;
230 }
231 
232 /**
233  * \test This is a test for a valid value <1430000000.
234  *
235  * \retval 1 on success.
236  * \retval 0 on failure.
237  */
238 static int ValidityTestParse03 (void)
239 {
240  DetectU32Data *dd = NULL;
241  dd = DetectNfsProcedureParse("<1430000000");
242  FAIL_IF_NULL(dd);
243  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->mode == DETECT_UINT_LT);
244  DetectNfsProcedureFree(NULL, dd);
245  PASS;
246 }
247 
248 /**
249  * \test This is a test for a valid value 1430000000<>1470000000.
250  *
251  * \retval 1 on success.
252  * \retval 0 on failure.
253  */
254 static int ValidityTestParse04 (void)
255 {
256  DetectU32Data *dd = NULL;
257  dd = DetectNfsProcedureParse("1430000001<>1470000000");
258  FAIL_IF_NULL(dd);
259  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->arg2 == 1470000001 && dd->mode == DETECT_UINT_RA);
260  DetectNfsProcedureFree(NULL, dd);
261  PASS;
262 }
263 
264 /**
265  * \test This is a test for a invalid value A.
266  *
267  * \retval 1 on success.
268  * \retval 0 on failure.
269  */
270 static int ValidityTestParse05 (void)
271 {
272  DetectU32Data *dd = NULL;
273  dd = DetectNfsProcedureParse("A");
274  FAIL_IF_NOT_NULL(dd);
275  PASS;
276 }
277 
278 /**
279  * \test This is a test for a invalid value >1430000000<>1470000000.
280  *
281  * \retval 1 on success.
282  * \retval 0 on failure.
283  */
284 static int ValidityTestParse06 (void)
285 {
286  DetectU32Data *dd = NULL;
287  dd = DetectNfsProcedureParse(">1430000000<>1470000000");
288  FAIL_IF_NOT_NULL(dd);
289  PASS;
290 }
291 
292 /**
293  * \test This is a test for a invalid value 1430000000<>.
294  *
295  * \retval 1 on success.
296  * \retval 0 on failure.
297  */
298 static int ValidityTestParse07 (void)
299 {
300  DetectU32Data *dd = NULL;
301  dd = DetectNfsProcedureParse("1430000000<>");
302  FAIL_IF_NOT_NULL(dd);
303  PASS;
304 }
305 
306 /**
307  * \test This is a test for a invalid value <>1430000000.
308  *
309  * \retval 1 on success.
310  * \retval 0 on failure.
311  */
312 static int ValidityTestParse08 (void)
313 {
314  DetectU32Data *dd = NULL;
315  dd = DetectNfsProcedureParse("<>1430000000");
316  FAIL_IF_NOT_NULL(dd);
317  PASS;
318 }
319 
320 /**
321  * \test This is a test for a invalid value "".
322  *
323  * \retval 1 on success.
324  * \retval 0 on failure.
325  */
326 static int ValidityTestParse09 (void)
327 {
328  DetectU32Data *dd = NULL;
329  dd = DetectNfsProcedureParse("");
330  FAIL_IF_NOT_NULL(dd);
331  PASS;
332 }
333 
334 /**
335  * \test This is a test for a invalid value " ".
336  *
337  * \retval 1 on success.
338  * \retval 0 on failure.
339  */
340 static int ValidityTestParse10 (void)
341 {
342  DetectU32Data *dd = NULL;
343  dd = DetectNfsProcedureParse(" ");
344  FAIL_IF_NOT_NULL(dd);
345  PASS;
346 }
347 
348 /**
349  * \test This is a test for a invalid value 1490000000<>1430000000.
350  *
351  * \retval 1 on success.
352  * \retval 0 on failure.
353  */
354 static int ValidityTestParse11 (void)
355 {
356  DetectU32Data *dd = NULL;
357  dd = DetectNfsProcedureParse("1490000000<>1430000000");
358  FAIL_IF_NOT_NULL(dd);
359  PASS;
360 }
361 
362 /**
363  * \test This is a test for a valid value 1430000000 <> 1490000000.
364  *
365  * \retval 1 on success.
366  * \retval 0 on failure.
367  */
368 static int ValidityTestParse12 (void)
369 {
370  DetectU32Data *dd = NULL;
371  dd = DetectNfsProcedureParse("1430000001 <> 1490000000");
372  FAIL_IF_NULL(dd);
373  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->arg2 == 1490000001 && dd->mode == DETECT_UINT_RA);
374  DetectNfsProcedureFree(NULL, dd);
375  PASS;
376 }
377 
378 /**
379  * \test This is a test for a valid value > 1430000000.
380  *
381  * \retval 1 on success.
382  * \retval 0 on failure.
383  */
384 static int ValidityTestParse13 (void)
385 {
386  DetectU32Data *dd = NULL;
387  dd = DetectNfsProcedureParse("> 1430000000 ");
388  FAIL_IF_NULL(dd);
389  FAIL_IF_NOT(dd->arg1 == 1430000000 && dd->mode == DETECT_UINT_GT);
390  DetectNfsProcedureFree(NULL, dd);
391  PASS;
392 }
393 
394 /**
395  * \test This is a test for a valid value < 1490000000.
396  *
397  * \retval 1 on success.
398  * \retval 0 on failure.
399  */
400 static int ValidityTestParse14 (void)
401 {
402  DetectU32Data *dd = NULL;
403  dd = DetectNfsProcedureParse("< 1490000000 ");
404  FAIL_IF_NULL(dd);
405  FAIL_IF_NOT(dd->arg1 == 1490000000 && dd->mode == DETECT_UINT_LT);
406  DetectNfsProcedureFree(NULL, dd);
407  PASS;
408 }
409 
410 /**
411  * \test This is a test for a valid value 1490000000.
412  *
413  * \retval 1 on success.
414  * \retval 0 on failure.
415  */
416 static int ValidityTestParse15 (void)
417 {
418  DetectU32Data *dd = NULL;
419  dd = DetectNfsProcedureParse(" 1490000000 ");
420  FAIL_IF_NULL(dd);
421  FAIL_IF_NOT(dd->arg1 == 1490000000 && dd->mode == DETECT_UINT_EQ);
422  DetectNfsProcedureFree(NULL, dd);
423  PASS;
424 }
425 
426 /**
427  * \brief Register unit tests for nfs_procedure.
428  */
429 void DetectNfsProcedureRegisterTests(void)
430 {
431  UtRegisterTest("ValidityTestParse01", ValidityTestParse01);
432  UtRegisterTest("ValidityTestParse02", ValidityTestParse02);
433  UtRegisterTest("ValidityTestParse03", ValidityTestParse03);
434  UtRegisterTest("ValidityTestParse04", ValidityTestParse04);
435  UtRegisterTest("ValidityTestParse05", ValidityTestParse05);
436  UtRegisterTest("ValidityTestParse06", ValidityTestParse06);
437  UtRegisterTest("ValidityTestParse07", ValidityTestParse07);
438  UtRegisterTest("ValidityTestParse08", ValidityTestParse08);
439  UtRegisterTest("ValidityTestParse09", ValidityTestParse09);
440  UtRegisterTest("ValidityTestParse10", ValidityTestParse10);
441  UtRegisterTest("ValidityTestParse11", ValidityTestParse11);
442  UtRegisterTest("ValidityTestParse12", ValidityTestParse12);
443  UtRegisterTest("ValidityTestParse13", ValidityTestParse13);
444  UtRegisterTest("ValidityTestParse14", ValidityTestParse14);
445  UtRegisterTest("ValidityTestParse15", ValidityTestParse15);
446 }
447 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1287
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1703
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
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:437
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:1286
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1274
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1284
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:166
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:826
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:1255
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:1269
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:1124
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:255
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:1074
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:343
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:216
detect-nfs-procedure.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1252
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:322
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:335
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:341
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
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:2122
DetectNfsProcedureRegister
void DetectNfsProcedureRegister(void)
Registration function for nfs_procedure keyword.
Definition: detect-nfs-procedure.c:64
str
#define str(s)
Definition: suricata-common.h:286
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
detect-parse.h
Signature_
Signature container.
Definition: detect.h:581
SigMatch_
a single match condition for a signature
Definition: detect.h:340
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:1276
app-layer-nfs-tcp.h