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
44static int g_smb_version_list_id = 0;
45
46static void DetectSmbVersionFree(DetectEngineCtx *de_ctx, void *ptr)
47{
48
49 SCLogDebug("smb_version: DetectSmbVersionFree");
50 SCSmbVersionFree(ptr);
51}
52
53/**
54 * \brief Creates a SigMatch for the "smb.version" keyword being sent as argument,
55 * and appends it to the SCSmbVersionMatch 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
65static 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 = SCSmbVersionParse(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
114static 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 = SCSmbVersionMatch(txv, (void *)m);
121
122 if (matchvalue != 1) {
123 SCLogDebug("SCSmbVersionMatch: didn't match");
124 SCReturnInt(0);
125 } else {
126 SCLogDebug("SCSmbVersionMatch: 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}
@ ALPROTO_SMB
uint8_t flags
Definition decode-gre.h:0
@ DETECT_SMB_VERSION
Data structures and function prototypes for keeping state for the detection engine.
int DetectBufferTypeRegister(const char *name)
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
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.
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.
SigMatch * DetectGetLastSMFromLists(const Signature *s,...)
Returns the sm with the largest index (added latest) from the lists passed to us.
SigTableElmt * sigmatch_table
#define BUFFER_NAME
void DetectSmbVersionRegister(void)
Registers the keyword handlers for the "smb_version" keyword.
#define KEYWORD_NAME
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIG_FLAG_TOSERVER
Definition detect.h:271
SCMutex m
Definition flow-hash.h:6
DetectEngineCtx * de_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
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#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