suricata
detect-smb-version.c
Go to the documentation of this file.
1 /* Copyright (C) 2022-2023 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 Eloy PĂ©rez
22  * \author Jason Taylor
23  *
24  * Implements the smb.version keyword
25  */
26 
27 #include "suricata-common.h"
28 
29 #include "detect.h"
30 #include "detect-parse.h"
31 
32 #include "detect-engine.h"
33 #include "detect-engine-mpm.h"
34 #include "detect-engine-state.h"
37 
38 #include "detect-smb-version.h"
39 #include "rust.h"
40 
41 #define BUFFER_NAME "smb_version"
42 #define KEYWORD_NAME "smb.version"
43 
44 static int g_smb_version_list_id = 0;
45 
46 static void DetectSmbVersionFree(DetectEngineCtx *de_ctx, void *ptr)
47 {
48 
49  SCLogDebug("smb_version: DetectSmbVersionFree");
50  rs_smb_version_free(ptr);
51 }
52 
53 /**
54  * \brief Creates a SigMatch for the "smb.version" keyword being sent as argument,
55  * and appends it to the rs_smb_version_match Signature(s).
56  *
57  * \param de_ctx Pointer to the detection engine context.
58  * \param s Pointer to signature for the current Signature being parsed
59  * from the rules.
60  * \param arg Pointer to the string holding the keyword value.
61  *
62  * \retval 0 on success, -1 on failure
63  */
64 
65 static int DetectSmbVersionSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
66 {
67  SCLogDebug("smb_version: DetectSmbVersionSetup");
68 
70  return -1;
71 
72  if (arg == NULL) {
73  SCLogError("Error parsing smb.version option in signature, it needs a value");
74  return -1;
75  }
76 
78  SCLogError("Can't use 2 or more smb.version declarations in "
79  "the same sig. Invalidating signature.");
80  return -1;
81  }
82 
83  void *dod = rs_smb_version_parse(arg);
84 
85  if (dod == NULL) {
86  SCLogError("Error parsing smb.version option in signature");
87  return -1;
88  }
89 
91  de_ctx, s, DETECT_SMB_VERSION, (SigMatchCtx *)dod, g_smb_version_list_id) == NULL) {
92  DetectSmbVersionFree(de_ctx, dod);
93  return -1;
94  }
95 
96  return 0;
97 }
98 
99 /**
100  * \brief App layer match function for the "smb.version" keyword.
101  *
102  * \param t Pointer to the ThreadVars instance.
103  * \param det_ctx Pointer to the DetectEngineThreadCtx.
104  * \param f Pointer to the flow.
105  * \param flags Pointer to the flags indicating the flow direction.
106  * \param state Pointer to the app layer state data.
107  * \param s Pointer to the Signature instance.
108  * \param m Pointer to the SigMatch.
109  *
110  * \retval 1 On Match.
111  * \retval 0 On no match.
112  */
113 
114 static int DetectSmbVersionMatchRust(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
115  void *state, void *txv, const Signature *s, const SigMatchCtx *m)
116 {
117 
118  SCLogDebug("smb_version: DetectSmbVersionMatchRust");
119 
120  int matchvalue = rs_smb_version_match(txv, (void *)m);
121 
122  if (matchvalue != 1) {
123  SCLogDebug("rs_smb_version_match: didn't match");
124  SCReturnInt(0);
125  } else {
126  SCLogDebug("rs_smb_version_match: matched!");
127  return matchvalue;
128  }
129 }
130 
131 /**
132  * \brief Registers the keyword handlers for the "smb_version" keyword.
133  */
134 
136 {
138  sigmatch_table[DETECT_SMB_VERSION].Setup = DetectSmbVersionSetup;
140  sigmatch_table[DETECT_SMB_VERSION].AppLayerTxMatch = DetectSmbVersionMatchRust;
141  sigmatch_table[DETECT_SMB_VERSION].Free = DetectSmbVersionFree;
142  sigmatch_table[DETECT_SMB_VERSION].desc = "smb keyword to match on SMB version";
143  sigmatch_table[DETECT_SMB_VERSION].url = "/rules/smb-keywords.html#smb-version";
144 
147 
150 
151  g_smb_version_list_id = DetectBufferTypeRegister(BUFFER_NAME);
152 
153  SCLogDebug("registering " BUFFER_NAME " rule option");
154 }
SigTableElmt_::url
const char * url
Definition: detect.h:1312
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1738
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1311
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:128
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1299
SigTableElmt_::name
const char * name
Definition: detect.h:1309
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:843
DetectSmbVersionRegister
void DetectSmbVersionRegister(void)
Registers the keyword handlers for the "smb_version" keyword.
Definition: detect-smb-version.c:135
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1280
rust.h
m
SCMutex m
Definition: flow-hash.h:6
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1294
detect-engine-prefilter.h
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1098
detect-engine-mpm.h
detect.h
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1277
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-smb-version.c:41
detect-engine-content-inspection.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:344
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1030
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
detect-smb-version.h
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:2112
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
detect-parse.h
Signature_
Signature container.
Definition: detect.h:603
DETECT_SMB_VERSION
@ DETECT_SMB_VERSION
Definition: detect-engine-register.h:222
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
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
DetectGetLastSMFromLists
SigMatch * DetectGetLastSMFromLists(const Signature *s,...)
Returns the sm with the largest index (added latest) from the lists passed to us.
Definition: detect-parse.c:607
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:437
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-smb-version.c:42
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275