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