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 "debug.h"
27 #include "decode.h"
28 #include "detect.h"
29 
30 #include "detect-parse.h"
31 #include "detect-engine.h"
32 #include "detect-engine-mpm.h"
33 #include "detect-content.h"
34 #include "detect-pcre.h"
35 #include "detect-nfs-version.h"
36 #include "detect-engine-uint.h"
37 
38 #include "app-layer-parser.h"
39 
40 #include "flow.h"
41 #include "flow-util.h"
42 #include "flow-var.h"
43 
44 #include "util-unittest.h"
45 #include "util-unittest-helper.h"
46 #include "util-byte.h"
47 
48 #include "app-layer-nfs-tcp.h"
49 #include "rust.h"
50 
51 
52 static int DetectNfsVersionSetup (DetectEngineCtx *, Signature *s, const char *str);
53 static void DetectNfsVersionFree(DetectEngineCtx *de_ctx, void *);
54 static int g_nfs_request_buffer_id = 0;
55 
56 static uint8_t DetectEngineInspectNfsRequestGeneric(DetectEngineCtx *de_ctx,
57  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
58  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
59 
60 static int DetectNfsVersionMatch (DetectEngineThreadCtx *, Flow *,
61  uint8_t, void *, void *, const Signature *,
62  const SigMatchCtx *);
63 
64 /**
65  * \brief Registration function for nfs_procedure keyword.
66  */
68 {
69  sigmatch_table[DETECT_AL_NFS_VERSION].name = "nfs.version";
71  sigmatch_table[DETECT_AL_NFS_VERSION].desc = "match NFS version";
72  sigmatch_table[DETECT_AL_NFS_VERSION].url = "/rules/nfs-keywords.html#version";
73  sigmatch_table[DETECT_AL_NFS_VERSION].AppLayerTxMatch = DetectNfsVersionMatch;
74  sigmatch_table[DETECT_AL_NFS_VERSION].Setup = DetectNfsVersionSetup;
75  sigmatch_table[DETECT_AL_NFS_VERSION].Free = DetectNfsVersionFree;
76  // unit tests were the same as DetectNfsProcedureRegisterTests
78  DetectEngineInspectNfsRequestGeneric, NULL);
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 static uint8_t DetectEngineInspectNfsRequestGeneric(DetectEngineCtx *de_ctx,
86  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
87  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
88 {
90  de_ctx, det_ctx, s, engine->smd, f, flags, alstate, txv, tx_id);
91 }
92 
93 /**
94  * \internal
95  * \brief Function to match version of a TX
96  *
97  * \param t Pointer to thread vars.
98  * \param det_ctx Pointer to the pattern matcher thread.
99  * \param f Pointer to the current flow.
100  * \param flags Flags.
101  * \param state App layer state.
102  * \param s Pointer to the Signature.
103  * \param m Pointer to the sigmatch that we will cast into
104  * DetectU32Data.
105  *
106  * \retval 0 no match.
107  * \retval 1 match.
108  */
109 static int DetectNfsVersionMatch (DetectEngineThreadCtx *det_ctx,
110  Flow *f, uint8_t flags, void *state,
111  void *txv, const Signature *s,
112  const SigMatchCtx *ctx)
113 {
114  SCEnter();
115 
116  const DetectU32Data *dd = (const DetectU32Data *)ctx;
117  uint32_t version;
118  rs_nfs_tx_get_version(txv, &version);
119  SCLogDebug("version %u mode %u lo %u hi %u", version, dd->mode, dd->arg1, dd->arg2);
120  if (DetectU32Match(version, dd))
121  SCReturnInt(1);
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 *DetectNfsVersionParse(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 DetectNfsVersionSetup (DetectEngineCtx *de_ctx, Signature *s,
153  const char *rawstr)
154 {
155  SCLogDebug("\'%s\'", rawstr);
156 
158  return -1;
159 
160  DetectU32Data *dd = DetectNfsVersionParse(rawstr);
161  if (dd == NULL) {
162  SCLogError(SC_ERR_INVALID_ARGUMENT,"Parsing \'%s\' failed", rawstr);
163  return -1;
164  }
165 
166  /* okay so far so good, lets get this into a SigMatch
167  * and put it in the Signature. */
168  SigMatch *sm = SigMatchAlloc();
169  if (sm == NULL)
170  goto error;
171 
173  sm->ctx = (void *)dd;
174 
175  SCLogDebug("low %u hi %u", dd->arg1, dd->arg2);
176  SigMatchAppendSMToList(s, sm, g_nfs_request_buffer_id);
177  return 0;
178 
179 error:
180  DetectNfsVersionFree(de_ctx, dd);
181  return -1;
182 }
183 
184 /**
185  * \internal
186  * \brief Function to free memory associated with DetectU32Data.
187  *
188  * \param de_ptr Pointer to DetectU32Data.
189  */
190 void DetectNfsVersionFree(DetectEngineCtx *de_ctx, void *ptr)
191 {
192  rs_detect_u32_free(ptr);
193 }
util-byte.h
detect-engine-uint.h
DetectEngineAppInspectionEngine_
Definition: detect.h:399
SigTableElmt_::url
const char * url
Definition: detect.h:1253
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
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:67
SigTableElmt_::desc
const char * desc
Definition: detect.h:1252
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1240
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1250
detect-nfs-version.h
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:794
DETECT_AL_NFS_VERSION
@ DETECT_AL_NFS_VERSION
Definition: detect-engine-register.h:162
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1221
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1235
detect-pcre.h
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1084
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:239
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1043
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
detect.h
SC_ERR_INVALID_ARGUMENT
@ SC_ERR_INVALID_ARGUMENT
Definition: util-error.h:43
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:326
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:225
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:318
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:416
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, const uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1949
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1251
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:324
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectU32Data
DetectUintData_u32 DetectU32Data
Definition: detect-engine-uint.h:41
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
version
uint8_t version
Definition: decode-gre.h:1
str
#define str(s)
Definition: suricata-common.h:272
detect-parse.h
Signature_
Signature container.
Definition: detect.h:549
SigMatch_
a single match condition for a signature
Definition: detect.h:323
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
flow-var.h
MQTTUnsubscribeTopicGetDataArgs::txv
void * txv
Definition: detect-mqtt-unsubscribe-topic.c:65
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:45
debug.h
app-layer-nfs-tcp.h