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