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_AL_NFS_VERSION].name = "nfs.version";
66  sigmatch_table[DETECT_AL_NFS_VERSION].desc = "match NFS version";
67  sigmatch_table[DETECT_AL_NFS_VERSION].url = "/rules/nfs-keywords.html#version";
68  sigmatch_table[DETECT_AL_NFS_VERSION].AppLayerTxMatch = DetectNfsVersionMatch;
69  sigmatch_table[DETECT_AL_NFS_VERSION].Setup = DetectNfsVersionSetup;
70  sigmatch_table[DETECT_AL_NFS_VERSION].Free = DetectNfsVersionFree;
71  // unit tests were the same as DetectNfsProcedureRegisterTests
74 
75  g_nfs_request_buffer_id = DetectBufferTypeGetByName("nfs_request");
76 
77  SCLogDebug("g_nfs_request_buffer_id %d", g_nfs_request_buffer_id);
78 }
79 
80 /**
81  * \internal
82  * \brief Function to match version of a TX
83  *
84  * \param t Pointer to thread vars.
85  * \param det_ctx Pointer to the pattern matcher thread.
86  * \param f Pointer to the current flow.
87  * \param flags Flags.
88  * \param state App layer state.
89  * \param s Pointer to the Signature.
90  * \param m Pointer to the sigmatch that we will cast into
91  * DetectU32Data.
92  *
93  * \retval 0 no match.
94  * \retval 1 match.
95  */
96 static int DetectNfsVersionMatch (DetectEngineThreadCtx *det_ctx,
97  Flow *f, uint8_t flags, void *state,
98  void *txv, const Signature *s,
99  const SigMatchCtx *ctx)
100 {
101  SCEnter();
102 
103  const DetectU32Data *dd = (const DetectU32Data *)ctx;
104  uint32_t version;
105  rs_nfs_tx_get_version(txv, &version);
106  SCLogDebug("version %u mode %u lo %u hi %u", version, dd->mode, dd->arg1, dd->arg2);
107  if (DetectU32Match(version, dd))
108  SCReturnInt(1);
109  SCReturnInt(0);
110 }
111 
112 /**
113  * \internal
114  * \brief Function to parse options passed via tls validity keywords.
115  *
116  * \param rawstr Pointer to the user provided options.
117  *
118  * \retval dd pointer to DetectU32Data on success.
119  * \retval NULL on failure.
120  */
121 static DetectU32Data *DetectNfsVersionParse(const char *rawstr)
122 {
123  return rs_detect_u32_parse_inclusive(rawstr);
124 }
125 
126 
127 
128 /**
129  * \brief Function to add the parsed tls validity field into the current signature.
130  *
131  * \param de_ctx Pointer to the Detection Engine Context.
132  * \param s Pointer to the Current Signature.
133  * \param rawstr Pointer to the user provided flags options.
134  * \param type Defines if this is notBefore or notAfter.
135  *
136  * \retval 0 on Success.
137  * \retval -1 on Failure.
138  */
139 static int DetectNfsVersionSetup (DetectEngineCtx *de_ctx, Signature *s,
140  const char *rawstr)
141 {
142  SCLogDebug("\'%s\'", rawstr);
143 
145  return -1;
146 
147  DetectU32Data *dd = DetectNfsVersionParse(rawstr);
148  if (dd == NULL) {
149  SCLogError("Parsing \'%s\' failed", rawstr);
150  return -1;
151  }
152 
153  /* okay so far so good, lets get this into a SigMatch
154  * and put it in the Signature. */
155 
156  SCLogDebug("low %u hi %u", dd->arg1, dd->arg2);
158  g_nfs_request_buffer_id) == NULL) {
159  goto error;
160  }
161  return 0;
162 
163 error:
164  DetectNfsVersionFree(de_ctx, dd);
165  return -1;
166 }
167 
168 /**
169  * \internal
170  * \brief Function to free memory associated with DetectU32Data.
171  *
172  * \param de_ptr Pointer to DetectU32Data.
173  */
174 void DetectNfsVersionFree(DetectEngineCtx *de_ctx, void *ptr)
175 {
176  rs_detect_u32_free(ptr);
177 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
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:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1294
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1304
detect-nfs-version.h
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
DETECT_AL_NFS_VERSION
@ DETECT_AL_NFS_VERSION
Definition: detect-engine-register.h:187
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1275
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
detect-pcre.h
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1093
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
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:344
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1305
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:2111
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:601
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:240
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:436
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
flow-var.h
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:45
app-layer-nfs-tcp.h