suricata
detect-tcp-wscale.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2025 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 <vjulien@oisf.net>
22  *
23  */
24 
25 #include "suricata-common.h"
26 
27 #include "detect.h"
28 #include "detect-parse.h"
30 #include "detect-engine-uint.h"
31 #include "util-byte.h"
32 
33 #include "detect-tcp-wscale.h"
34 
35 /* prototypes */
36 static int DetectTcpWscaleMatch(
37  DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *);
38 static int DetectTcpWscaleSetup(DetectEngineCtx *, Signature *, const char *);
39 void DetectTcpWscaleFree(DetectEngineCtx *, void *);
40 static int PrefilterSetupTcpWscale(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
41 static bool PrefilterTcpWscaleIsPrefilterable(const Signature *s);
42 
43 /**
44  * \brief Registration function for tcp.wscale keyword
45  */
46 
48 {
49  sigmatch_table[DETECT_TCP_WSCALE].name = "tcp.wscale";
50  sigmatch_table[DETECT_TCP_WSCALE].desc = "match on TCP WSCALE option field";
51  sigmatch_table[DETECT_TCP_WSCALE].url = "/rules/header-keywords.html#tcpwscale";
52  sigmatch_table[DETECT_TCP_WSCALE].Match = DetectTcpWscaleMatch;
53  sigmatch_table[DETECT_TCP_WSCALE].Setup = DetectTcpWscaleSetup;
55  sigmatch_table[DETECT_TCP_WSCALE].SupportsPrefilter = PrefilterTcpWscaleIsPrefilterable;
56  sigmatch_table[DETECT_TCP_WSCALE].SetupPrefilter = PrefilterSetupTcpWscale;
61 }
62 
63 /**
64  * \brief This function is used to match WSCALE rule option on a packet with those passed via
65  * tcp.wscale:
66  *
67  * \param det_ctx pointer to the pattern matcher thread
68  * \param p pointer to the current packet
69  * \param ctx pointer to the sigmatch that we will cast into DetectU8Data
70  *
71  * \retval 0 no match
72  * \retval 1 match
73  */
74 static int DetectTcpWscaleMatch(
75  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
76 {
78 
79  if (!(PacketIsTCP(p)))
80  return 0;
81 
82  if (!(TCP_HAS_WSCALE(p)))
83  return 0;
84 
85  uint8_t v = TCP_GET_WSCALE(p);
86 
87  const DetectU8Data *ws = (const DetectU8Data *)ctx;
88  return DetectU8Match(v, ws);
89 }
90 
91 /**
92  * \brief this function is used to attach the parsed tcp.wscale data into the current signature
93  *
94  * \param de_ctx pointer to the Detection Engine Context
95  * \param s pointer to the Current Signature
96  * \param str pointer to the user provided options
97  *
98  * \retval 0 on Success
99  * \retval -1 on Failure
100  */
101 static int DetectTcpWscaleSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
102 {
104  if (ws == NULL)
105  return -1;
106 
110  return -1;
111  }
113 
114  return 0;
115 }
116 
117 /**
118  * \brief this function will free memory associated with DetectU8Data
119  *
120  * \param ptr pointer to DetectU8Data
121  */
123 {
124  SCDetectU8Free(ptr);
125 }
126 
127 /* prefilter code */
128 
129 static void PrefilterPacketTcpWscaleMatch(
130  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
131 {
133  if (!(PacketIsTCP(p)))
134  return;
135 
136  if (!(TCP_HAS_WSCALE(p)))
137  return;
138 
139  const uint8_t v = TCP_GET_WSCALE(p);
140 
141  /* during setup Suricata will automatically see if there is another
142  * check that can be added: alproto, sport or dport */
143  const PrefilterPacketHeaderCtx *ctx = pectx;
144  if (!PrefilterPacketHeaderExtraMatch(ctx, p))
145  return;
146 
147  DetectU8Data du8;
148  du8.mode = ctx->v1.u8[0];
149  du8.arg1 = ctx->v1.u8[1];
150  du8.arg2 = ctx->v1.u8[2];
151  /* if we match, add all the sigs that use this prefilter. This means
152  * that these will be inspected further */
153  if (DetectU8Match(v, &du8)) {
154  SCLogDebug("packet matches wscale %u", v);
155  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
156  }
157 }
158 
159 static int PrefilterSetupTcpWscale(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
160 {
162  PrefilterPacketU8Set, PrefilterPacketU8Compare, PrefilterPacketTcpWscaleMatch);
163 }
164 
165 static bool PrefilterTcpWscaleIsPrefilterable(const Signature *s)
166 {
167  const SigMatch *sm;
168  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH]; sm != NULL; sm = sm->next) {
169  switch (sm->type) {
170  case DETECT_TCP_WSCALE:
171  return true;
172  }
173  }
174  return false;
175 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1462
SIG_MASK_REQUIRE_REAL_PKT
#define SIG_MASK_REQUIRE_REAL_PKT
Definition: detect.h:316
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:642
SigTableElmt_::desc
const char * desc
Definition: detect.h:1461
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1446
SigTableElmt_::name
const char * name
Definition: detect.h:1459
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1319
DETECT_TABLE_PACKET_FILTER_FLAG
#define DETECT_TABLE_PACKET_FILTER_FLAG
Definition: detect.h:563
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1629
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:275
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1349
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1450
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:932
SIGMATCH_SUPPORT_FIREWALL
#define SIGMATCH_SUPPORT_FIREWALL
Definition: detect.h:1682
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1441
TCP_GET_WSCALE
#define TCP_GET_WSCALE(p)
Definition: decode-tcp.h:100
DETECT_TABLE_PACKET_TD_FLAG
#define DETECT_TABLE_PACKET_TD_FLAG
Definition: detect.h:564
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1444
DetectU8Parse
DetectUintData_u8 * DetectU8Parse(const char *u8str)
This function is used to parse u8 options passed via some u8 keyword.
Definition: detect-engine-uint.c:85
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:35
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1244
SigTableElmt_::tables
uint8_t tables
Definition: detect.h:1454
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
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:360
DetectU8Data
DetectUintData_u8 DetectU8Data
Definition: detect-engine-uint.h:43
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
PrefilterPacketU8Set
void PrefilterPacketU8Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:90
DetectTcpWscaleRegister
void DetectTcpWscaleRegister(void)
Registration function for tcp.wscale keyword.
Definition: detect-tcp-wscale.c:47
Signature_::flags
uint32_t flags
Definition: detect.h:669
Packet_
Definition: decode.h:501
detect-tcp-wscale.h
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:747
PrefilterSetupPacketHeader
int PrefilterSetupPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, SignatureMask mask, void(*Set)(PrefilterPacketHeaderValue *v, void *), bool(*Compare)(PrefilterPacketHeaderValue v, void *), void(*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
Definition: detect-engine-prefilter-common.c:406
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1421
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
Definition: detect-engine-uint.c:71
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:357
str
#define str(s)
Definition: suricata-common.h:308
DETECT_TABLE_PACKET_PRE_FLOW_FLAG
#define DETECT_TABLE_PACKET_PRE_FLOW_FLAG
Definition: detect.h:561
DETECT_TABLE_PACKET_PRE_STREAM_FLAG
#define DETECT_TABLE_PACKET_PRE_STREAM_FLAG
Definition: detect.h:562
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1443
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
SigMatch_
a single match condition for a signature
Definition: detect.h:356
DETECT_TCP_WSCALE
@ DETECT_TCP_WSCALE
Definition: detect-engine-register.h:292
PrefilterPacketU8Compare
bool PrefilterPacketU8Compare(PrefilterPacketHeaderValue v, void *smctx)
Definition: detect-engine-uint.c:98
TCP_HAS_WSCALE
#define TCP_HAS_WSCALE(p)
Definition: decode-tcp.h:93
detect-engine-prefilter-common.h
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
DetectTcpWscaleFree
void DetectTcpWscaleFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectU8Data
Definition: detect-tcp-wscale.c:122
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:254