suricata
detect-nfs-version.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-version.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 
51 static int DetectNfsVersionSetup (DetectEngineCtx *, Signature *s, const char *str);
52 static void DetectNfsVersionFree(DetectEngineCtx *de_ctx, void *);
53 static int g_nfs_request_buffer_id = 0;
54 
55 static int DetectNfsVersionMatch (DetectEngineThreadCtx *, Flow *,
56  uint8_t, void *, void *, const Signature *,
57  const SigMatchCtx *);
58 
59 /**
60  * \brief Registration function for nfs_procedure keyword.
61  */
63 {
64  sigmatch_table[DETECT_NFS_VERSION].name = "nfs.version";
65  sigmatch_table[DETECT_NFS_VERSION].alias = "nfs_version";
66  sigmatch_table[DETECT_NFS_VERSION].desc = "match NFS version";
67  // TODO write doc sigmatch_table[DETECT_NFS_VERSION].url =
68  // "/rules/nfs-keywords.html#nfs-version";
69  sigmatch_table[DETECT_NFS_VERSION].AppLayerTxMatch = DetectNfsVersionMatch;
70  sigmatch_table[DETECT_NFS_VERSION].Setup = DetectNfsVersionSetup;
71  sigmatch_table[DETECT_NFS_VERSION].Free = DetectNfsVersionFree;
73  // unit tests were the same as DetectNfsProcedureRegisterTests
74 
77  g_nfs_request_buffer_id = DetectBufferTypeGetByName("nfs_request");
78 
79  SCLogDebug("g_nfs_request_buffer_id %d", g_nfs_request_buffer_id);
80 }
81 
82 /**
83  * \internal
84  * \brief Function to match version of a TX
85  *
86  * \param t Pointer to thread vars.
87  * \param det_ctx Pointer to the pattern matcher thread.
88  * \param f Pointer to the current flow.
89  * \param flags Flags.
90  * \param state App layer state.
91  * \param s Pointer to the Signature.
92  * \param m Pointer to the sigmatch that we will cast into
93  * DetectU32Data.
94  *
95  * \retval 0 no match.
96  * \retval 1 match.
97  */
98 static int DetectNfsVersionMatch (DetectEngineThreadCtx *det_ctx,
99  Flow *f, uint8_t flags, void *state,
100  void *txv, const Signature *s,
101  const SigMatchCtx *ctx)
102 {
103  SCEnter();
104 
105  const DetectU32Data *dd = (const DetectU32Data *)ctx;
106  uint32_t version;
107  SCNfsTxGetVersion(txv, &version);
108  SCLogDebug("version %u mode %u lo %u hi %u", version, dd->mode, dd->arg1, dd->arg2);
109  if (DetectU32Match(version, dd))
110  SCReturnInt(1);
111  SCReturnInt(0);
112 }
113 
114 /**
115  * \internal
116  * \brief Function to parse options passed via tls validity keywords.
117  *
118  * \param rawstr Pointer to the user provided options.
119  *
120  * \retval dd pointer to DetectU32Data on success.
121  * \retval NULL on failure.
122  */
123 static DetectU32Data *DetectNfsVersionParse(const char *rawstr)
124 {
125  return SCDetectU32ParseInclusive(rawstr);
126 }
127 
128 
129 
130 /**
131  * \brief Function to add the parsed tls validity field into the current signature.
132  *
133  * \param de_ctx Pointer to the Detection Engine Context.
134  * \param s Pointer to the Current Signature.
135  * \param rawstr Pointer to the user provided flags options.
136  * \param type Defines if this is notBefore or notAfter.
137  *
138  * \retval 0 on Success.
139  * \retval -1 on Failure.
140  */
141 static int DetectNfsVersionSetup (DetectEngineCtx *de_ctx, Signature *s,
142  const char *rawstr)
143 {
144  SCLogDebug("\'%s\'", rawstr);
145 
147  return -1;
148 
149  DetectU32Data *dd = DetectNfsVersionParse(rawstr);
150  if (dd == NULL) {
151  SCLogError("Parsing \'%s\' failed", rawstr);
152  return -1;
153  }
154 
155  /* okay so far so good, lets get this into a SigMatch
156  * and put it in the Signature. */
157 
158  SCLogDebug("low %u hi %u", dd->arg1, dd->arg2);
160  g_nfs_request_buffer_id) == NULL) {
161  goto error;
162  }
163  return 0;
164 
165 error:
166  DetectNfsVersionFree(de_ctx, dd);
167  return -1;
168 }
169 
170 /**
171  * \internal
172  * \brief Function to free memory associated with DetectU32Data.
173  *
174  * \param de_ptr Pointer to DetectU32Data.
175  */
176 void DetectNfsVersionFree(DetectEngineCtx *de_ctx, void *ptr)
177 {
178  SCDetectU32Free(ptr);
179 }
util-byte.h
detect-engine-uint.h
detect-content.h
detect-engine.h
DetectU32Match
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
Definition: detect-engine-uint.c:31
DetectNfsVersionRegister
void DetectNfsVersionRegister(void)
Registration function for nfs_procedure keyword.
Definition: detect-nfs-version.c:62
SigTableElmt_::desc
const char * desc
Definition: detect.h:1502
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1487
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1500
detect-nfs-version.h
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1491
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:966
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1465
rust.h
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2277
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1482
detect-pcre.h
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1278
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
DetectEngineThreadCtx_
Definition: detect.h:1284
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect-engine-mpm.h
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:387
detect.h
app-layer-parser.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1501
suricata-common.h
DetectU32Data
DetectUintData_u32 DetectU32Data
Definition: detect-engine-uint.h:41
version
uint8_t version
Definition: decode-gre.h:1
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:1946
str
#define str(s)
Definition: suricata-common.h:308
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SIGMATCH_INFO_UINT32
#define SIGMATCH_INFO_UINT32
Definition: detect-engine-register.h:344
detect-parse.h
Signature_
Signature container.
Definition: detect.h:672
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:273
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
flow-var.h
DETECT_NFS_VERSION
@ DETECT_NFS_VERSION
Definition: detect-engine-register.h:194
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:51
app-layer-nfs-tcp.h