suricata
detect-fragoffset.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2021 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 Breno Silva <breno.silva@gmail.com>
22  *
23  * Implements fragoffset keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 #include "decode-ipv4.h"
29 #include "decode-ipv6.h"
30 
31 #include "detect.h"
32 #include "detect-parse.h"
34 #include "detect-engine-build.h"
35 #include "detect-engine-uint.h"
36 
37 #include "detect-fragoffset.h"
38 
39 #include "util-byte.h"
40 #include "util-unittest.h"
41 #include "util-debug.h"
42 
43 static int DetectFragOffsetMatch(DetectEngineThreadCtx *,
44  Packet *, const Signature *, const SigMatchCtx *);
45 static int DetectFragOffsetSetup(DetectEngineCtx *, Signature *, const char *);
46 #ifdef UNITTESTS
48 #endif
50 
51 static int PrefilterSetupFragOffset(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
52 static bool PrefilterFragOffsetIsPrefilterable(const Signature *s);
53 
54 /**
55  * \brief Registration function for fragoffset
56  */
58 {
59  sigmatch_table[DETECT_FRAGOFFSET].name = "fragoffset";
60  sigmatch_table[DETECT_FRAGOFFSET].desc = "match on specific decimal values of the IP fragment offset field";
61  sigmatch_table[DETECT_FRAGOFFSET].url = "/rules/header-keywords.html#fragoffset";
62  sigmatch_table[DETECT_FRAGOFFSET].Match = DetectFragOffsetMatch;
63  sigmatch_table[DETECT_FRAGOFFSET].Setup = DetectFragOffsetSetup;
66 #ifdef UNITTESTS
68 #endif
69  sigmatch_table[DETECT_FRAGOFFSET].SupportsPrefilter = PrefilterFragOffsetIsPrefilterable;
70  sigmatch_table[DETECT_FRAGOFFSET].SetupPrefilter = PrefilterSetupFragOffset;
71 }
72 
73 /**
74  * \brief This function is used to match fragoffset rule option set on a packet
75  *
76  * \param t pointer to thread vars
77  * \param det_ctx pointer to the pattern matcher thread
78  * \param p pointer to the current packet
79  * \param m pointer to the sigmatch that we will cast into DetectFragOffsetData
80  *
81  * \retval 0 no match or frag is not set
82  * \retval 1 match
83  *
84  */
85 static int DetectFragOffsetMatch (DetectEngineThreadCtx *det_ctx,
86  Packet *p, const Signature *s, const SigMatchCtx *ctx)
87 {
88  uint16_t frag = 0;
89  const DetectU16Data *fragoff = (const DetectU16Data *)ctx;
90 
92 
93  if (PacketIsIPv4(p)) {
94  const IPV4Hdr *ip4h = PacketGetIPv4(p);
95  frag = IPV4_GET_RAW_FRAGOFFSET(ip4h);
96  } else if (PacketIsIPv6(p)) {
97  if (IPV6_EXTHDR_ISSET_FH(p)) {
98  frag = IPV6_EXTHDR_GET_FH_OFFSET(p);
99  } else {
100  return 0;
101  }
102  } else {
103  SCLogDebug("No IPv4 or IPv6 packet");
104  return 0;
105  }
106 
107  return DetectU16Match(frag, fragoff);
108 }
109 
110 /**
111  * \brief this function is used to add the parsed fragoffset data into the current signature
112  *
113  * \param de_ctx pointer to the Detection Engine Context
114  * \param s pointer to the Current Signature
115  * \param fragoffsetstr pointer to the user provided fragoffset option
116  *
117  * \retval 0 on Success
118  * \retval -1 on Failure
119  */
120 static int DetectFragOffsetSetup (DetectEngineCtx *de_ctx, Signature *s, const char *fragoffsetstr)
121 {
122  DetectU16Data *fragoff = SCDetectU16Parse(fragoffsetstr);
123  if (fragoff == NULL)
124  return -1;
125 
127  DETECT_SM_LIST_MATCH) == NULL) {
128  goto error;
129  }
131 
132  return 0;
133 
134 error:
135  DetectFragOffsetFree(de_ctx, fragoff);
136  return -1;
137 
138 }
139 
140 /**
141  * \brief this function will free memory associated with DetectFragOffsetData
142  *
143  * \param ptr pointer to DetectFragOffsetData
144  */
146 {
147  SCDetectU16Free(ptr);
148 }
149 
150 static void
151 PrefilterPacketFragOffsetMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
152 {
154 
155  uint16_t frag;
156 
157  if (PacketIsIPv4(p)) {
158  const IPV4Hdr *ip4h = PacketGetIPv4(p);
159  frag = IPV4_GET_RAW_FRAGOFFSET(ip4h);
160  } else if (PacketIsIPv6(p)) {
161  if (IPV6_EXTHDR_ISSET_FH(p)) {
162  frag = IPV6_EXTHDR_GET_FH_OFFSET(p);
163  } else {
164  return;
165  }
166  } else {
167  SCLogDebug("No IPv4 or IPv6 packet");
168  return;
169  }
170 
171  const PrefilterPacketHeaderCtx *ctx = pectx;
172  DetectU16Data du16;
173  du16.mode = ctx->v1.u8[0];
174  du16.arg1 = ctx->v1.u16[1];
175  du16.arg2 = ctx->v1.u16[2];
176 
177  if (DetectU16Match(frag, &du16)) {
178  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
179  }
180 }
181 
182 static int PrefilterSetupFragOffset(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
183 {
185  PrefilterPacketU16Set, PrefilterPacketU16Compare, PrefilterPacketFragOffsetMatch);
186 }
187 
188 static bool PrefilterFragOffsetIsPrefilterable(const Signature *s)
189 {
190  const SigMatch *sm;
191  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
192  switch (sm->type) {
193  case DETECT_FRAGOFFSET:
194  return true;
195  }
196  }
197  return false;
198 }
199 
200 #ifdef UNITTESTS
201 #include "util-unittest-helper.h"
202 #include "detect-engine.h"
203 #include "detect-engine-alert.h"
204 
205 /**
206  * \test DetectFragOffsetParseTest01 is a test for setting a valid fragoffset value
207  */
208 static int DetectFragOffsetParseTest01 (void)
209 {
210  DetectU16Data *fragoff = SCDetectU16Parse("300");
211 
212  FAIL_IF_NULL(fragoff);
213  FAIL_IF_NOT(fragoff->arg1 == 300);
214 
215  DetectFragOffsetFree(NULL, fragoff);
216 
217  PASS;
218 }
219 
220 /**
221  * \test DetectFragOffsetParseTest02 is a test for setting a valid fragoffset value
222  * with spaces all around
223  */
224 static int DetectFragOffsetParseTest02 (void)
225 {
226  DetectU16Data *fragoff = SCDetectU16Parse(">300");
227 
228  FAIL_IF_NULL(fragoff);
229  FAIL_IF_NOT(fragoff->arg1 == 300);
230  FAIL_IF_NOT(fragoff->mode == DetectUintModeGt);
231 
232  DetectFragOffsetFree(NULL, fragoff);
233 
234  PASS;
235 }
236 
237 /**
238  * \test DetectFragOffsetParseTest03 is a test for setting an invalid fragoffset value
239  */
240 static int DetectFragOffsetParseTest03 (void)
241 {
242  DetectU16Data *fragoff = SCDetectU16Parse("badc");
243 
244  FAIL_IF_NOT_NULL(fragoff);
245 
246  PASS;
247 }
248 
249 /**
250  * \test DetectFragOffsetMatchTest01 is a test for checking the working of
251  * fragoffset keyword by creating 2 rules and matching a crafted packet
252  * against them. Only the first one shall trigger.
253  */
254 static int DetectFragOffsetMatchTest01 (void)
255 {
256  Packet *p = PacketGetFromAlloc();
257 
258  FAIL_IF_NULL(p);
259  Signature *s = NULL;
261  ThreadVars th_v;
262  DetectEngineThreadCtx *det_ctx = NULL;
263  IPV4Hdr ip4h;
264 
265  memset(&ip4h, 0, sizeof(IPV4Hdr));
266  memset(&dtv, 0, sizeof(DecodeThreadVars));
267  memset(&th_v, 0, sizeof(ThreadVars));
268 
270 
271  p->src.family = AF_INET;
272  p->dst.family = AF_INET;
273  p->src.addr_data32[0] = 0x01020304;
274  p->dst.addr_data32[0] = 0x04030201;
275 
276  ip4h.s_ip_src.s_addr = p->src.addr_data32[0];
277  ip4h.s_ip_dst.s_addr = p->dst.addr_data32[0];
278  ip4h.ip_off = 0x2222;
279  UTHSetIPV4Hdr(p, &ip4h);
280 
283 
284  de_ctx->flags |= DE_QUIET;
285 
286  s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (fragoffset:546; sid:1;)");
287  FAIL_IF_NULL(s);
288 
289  s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (fragoffset:5000; sid:2;)");
290  FAIL_IF_NULL(s);
291 
293  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
294 
295  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
296 
297  FAIL_IF(PacketAlertCheck(p, 1) == 0);
298  FAIL_IF(PacketAlertCheck(p, 2));
299 
300  PacketFree(p);
301  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
303  FlowShutdown();
304  StatsThreadCleanup(&th_v);
305  PASS;
306 }
307 
309 {
310  UtRegisterTest("DetectFragOffsetParseTest01", DetectFragOffsetParseTest01);
311  UtRegisterTest("DetectFragOffsetParseTest02", DetectFragOffsetParseTest02);
312  UtRegisterTest("DetectFragOffsetParseTest03", DetectFragOffsetParseTest03);
313  UtRegisterTest("DetectFragOffsetMatchTest01", DetectFragOffsetMatchTest01);
314 }
315 #endif /* UNITTESTS */
util-byte.h
SIGMATCH_INFO_UINT16
#define SIGMATCH_INFO_UINT16
Definition: detect.h:1689
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1461
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
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
DetectFragOffsetRegisterTests
void DetectFragOffsetRegisterTests(void)
Definition: detect-fragoffset.c:308
PrefilterPacketU16Set
void PrefilterPacketU16Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:126
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1445
IPV6_EXTHDR_GET_FH_OFFSET
#define IPV6_EXTHDR_GET_FH_OFFSET(p)
Definition: decode-ipv6.h:101
SigTableElmt_::name
const char * name
Definition: detect.h:1458
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1323
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1628
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1449
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:279
IPV6_EXTHDR_ISSET_FH
#define IPV6_EXTHDR_ISSET_FH(p)
Definition: decode-ipv6.h:174
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:142
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1348
UTHSetIPV4Hdr
void UTHSetIPV4Hdr(Packet *p, IPV4Hdr *ip4h)
Definition: util-unittest-helper.c:126
DetectFragOffsetRegister
void DetectFragOffsetRegister(void)
Registration function for fragoffset.
Definition: detect-fragoffset.c:57
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2634
DE_QUIET
#define DE_QUIET
Definition: detect.h:330
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:2416
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3440
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
util-unittest.h
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1443
FlowInitConfig
void FlowInitConfig(bool quiet)
initialize the configuration
Definition: flow.c:549
DetectFragOffsetFree
void DetectFragOffsetFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectFragOffsetData
Definition: detect-fragoffset.c:145
decode-ipv6.h
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:35
decode.h
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1245
IPV4_GET_RAW_FRAGOFFSET
#define IPV4_GET_RAW_FRAGOFFSET(ip4h)
Definition: decode-ipv4.h:101
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
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3364
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:360
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:223
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
Signature_::flags
uint32_t flags
Definition: detect.h:669
Packet_
Definition: decode.h:501
detect-engine-build.h
detect-engine-alert.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:470
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1420
detect-fragoffset.h
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2194
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:33
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
IPV4Hdr_
Definition: decode-ipv4.h:72
decode-ipv4.h
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
DetectU16Match
int DetectU16Match(const uint16_t parg, const DetectUintData_u16 *du16)
Definition: detect-engine-uint.c:107
PrefilterPacketU16Compare
bool PrefilterPacketU16Compare(PrefilterPacketHeaderValue v, void *smctx)
Definition: detect-engine-uint.c:134
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:357
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:693
DETECT_FRAGOFFSET
@ DETECT_FRAGOFFSET
Definition: detect-engine-register.h:44
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3596
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:262
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:963
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1442
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
SigMatch_
a single match condition for a signature
Definition: detect.h:356
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2595
Address_::family
char family
Definition: decode.h:113
Packet_::dst
Address dst
Definition: decode.h:506
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:43
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:935
detect-engine-prefilter-common.h
IPV4Hdr_::ip_off
uint16_t ip_off
Definition: decode-ipv4.h:77
DetectU16Data
DetectUintData_u16 DetectU16Data
Definition: detect-engine-uint.h:42
StatsThreadCleanup
void StatsThreadCleanup(ThreadVars *tv)
Definition: counters.c:1324
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
Packet_::src
Address src
Definition: decode.h:505
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1447
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:254