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  return PrefilterIsPrefilterableById(s, DETECT_FRAGOFFSET);
191 }
192 
193 #ifdef UNITTESTS
194 #include "util-unittest-helper.h"
195 #include "detect-engine.h"
196 #include "detect-engine-alert.h"
197 
198 /**
199  * \test DetectFragOffsetParseTest01 is a test for setting a valid fragoffset value
200  */
201 static int DetectFragOffsetParseTest01 (void)
202 {
203  DetectU16Data *fragoff = SCDetectU16Parse("300");
204 
205  FAIL_IF_NULL(fragoff);
206  FAIL_IF_NOT(fragoff->arg1 == 300);
207 
208  DetectFragOffsetFree(NULL, fragoff);
209 
210  PASS;
211 }
212 
213 /**
214  * \test DetectFragOffsetParseTest02 is a test for setting a valid fragoffset value
215  * with spaces all around
216  */
217 static int DetectFragOffsetParseTest02 (void)
218 {
219  DetectU16Data *fragoff = SCDetectU16Parse(">300");
220 
221  FAIL_IF_NULL(fragoff);
222  FAIL_IF_NOT(fragoff->arg1 == 300);
223  FAIL_IF_NOT(fragoff->mode == DetectUintModeGt);
224 
225  DetectFragOffsetFree(NULL, fragoff);
226 
227  PASS;
228 }
229 
230 /**
231  * \test DetectFragOffsetParseTest03 is a test for setting an invalid fragoffset value
232  */
233 static int DetectFragOffsetParseTest03 (void)
234 {
235  DetectU16Data *fragoff = SCDetectU16Parse("badc");
236 
237  FAIL_IF_NOT_NULL(fragoff);
238 
239  PASS;
240 }
241 
242 /**
243  * \test DetectFragOffsetMatchTest01 is a test for checking the working of
244  * fragoffset keyword by creating 2 rules and matching a crafted packet
245  * against them. Only the first one shall trigger.
246  */
247 static int DetectFragOffsetMatchTest01 (void)
248 {
249  Packet *p = PacketGetFromAlloc();
250 
251  FAIL_IF_NULL(p);
252  Signature *s = NULL;
254  ThreadVars th_v;
255  DetectEngineThreadCtx *det_ctx = NULL;
256  IPV4Hdr ip4h;
257 
258  memset(&ip4h, 0, sizeof(IPV4Hdr));
259  memset(&dtv, 0, sizeof(DecodeThreadVars));
260  memset(&th_v, 0, sizeof(ThreadVars));
261  StatsThreadInit(&th_v.stats);
262 
264 
265  p->src.family = AF_INET;
266  p->dst.family = AF_INET;
267  p->src.addr_data32[0] = 0x01020304;
268  p->dst.addr_data32[0] = 0x04030201;
269 
270  ip4h.s_ip_src.s_addr = p->src.addr_data32[0];
271  ip4h.s_ip_dst.s_addr = p->dst.addr_data32[0];
272  ip4h.ip_off = 0x2222;
273  UTHSetIPV4Hdr(p, &ip4h);
274 
277 
278  de_ctx->flags |= DE_QUIET;
279 
280  s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (fragoffset:546; sid:1;)");
281  FAIL_IF_NULL(s);
282 
283  s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (fragoffset:5000; sid:2;)");
284  FAIL_IF_NULL(s);
285 
287  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
288 
289  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
290 
291  FAIL_IF(PacketAlertCheck(p, 1) == 0);
292  FAIL_IF(PacketAlertCheck(p, 2));
293 
294  PacketFree(p);
295  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
297  FlowShutdown();
298  StatsThreadCleanup(&th_v.stats);
299  PASS;
300 }
301 
303 {
304  UtRegisterTest("DetectFragOffsetParseTest01", DetectFragOffsetParseTest01);
305  UtRegisterTest("DetectFragOffsetParseTest02", DetectFragOffsetParseTest02);
306  UtRegisterTest("DetectFragOffsetParseTest03", DetectFragOffsetParseTest03);
307  UtRegisterTest("DetectFragOffsetMatchTest01", DetectFragOffsetMatchTest01);
308 }
309 #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
DetectFragOffsetRegisterTests
void DetectFragOffsetRegisterTests(void)
Definition: detect-fragoffset.c:302
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:2418
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3447
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:19
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
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
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:34
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1258
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
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:3601
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
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
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1354
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