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"
45#include "util-byte.h"
46
47#include "app-layer-nfs-tcp.h"
48#include "rust.h"
49
50
51static int DetectNfsVersionSetup (DetectEngineCtx *, Signature *s, const char *str);
52static void DetectNfsVersionFree(DetectEngineCtx *de_ctx, void *);
53static int g_nfs_request_buffer_id = 0;
54
55static 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";
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 */
94static 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 SCNfsTxGetVersion(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 */
119static DetectU32Data *DetectNfsVersionParse(const char *rawstr)
120{
121 return SCDetectU32ParseInclusive(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 */
137static 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
161error:
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 */
172void DetectNfsVersionFree(DetectEngineCtx *de_ctx, void *ptr)
173{
174 SCDetectU32Free(ptr);
175}
@ ALPROTO_NFS
uint8_t flags
Definition decode-gre.h:0
uint8_t version
Definition decode-gre.h:1
@ DETECT_NFS_VERSION
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
DetectUintData_u32 DetectU32Data
int DetectBufferTypeGetByName(const char *name)
void DetectNfsVersionRegister(void)
Registration function for nfs_procedure keyword.
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
DetectEngineCtx * de_ctx
struct Thresholds ctx
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition detect.h:1424
const char * alias
Definition detect.h:1460
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define str(s)
#define SCEnter(...)
Definition util-debug.h:277
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCReturnInt(x)
Definition util-debug.h:281
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267