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-debug.h"
41 
42 static int DetectFragOffsetMatch(DetectEngineThreadCtx *,
43  Packet *, const Signature *, const SigMatchCtx *);
44 static int DetectFragOffsetSetup(DetectEngineCtx *, Signature *, const char *);
45 #ifdef UNITTESTS
47 #endif
49 
50 static int PrefilterSetupFragOffset(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
51 static bool PrefilterFragOffsetIsPrefilterable(const Signature *s);
52 
53 /**
54  * \brief Registration function for fragoffset
55  */
57 {
58  sigmatch_table[DETECT_FRAGOFFSET].name = "fragoffset";
59  sigmatch_table[DETECT_FRAGOFFSET].desc = "match on specific decimal values of the IP fragment offset field";
60  sigmatch_table[DETECT_FRAGOFFSET].url = "/rules/header-keywords.html#fragoffset";
61  sigmatch_table[DETECT_FRAGOFFSET].Match = DetectFragOffsetMatch;
62  sigmatch_table[DETECT_FRAGOFFSET].Setup = DetectFragOffsetSetup;
65 #ifdef UNITTESTS
67 #endif
68  sigmatch_table[DETECT_FRAGOFFSET].SupportsPrefilter = PrefilterFragOffsetIsPrefilterable;
69  sigmatch_table[DETECT_FRAGOFFSET].SetupPrefilter = PrefilterSetupFragOffset;
70 }
71 
72 /**
73  * \brief This function is used to match fragoffset rule option set on a packet
74  *
75  * \param t pointer to thread vars
76  * \param det_ctx pointer to the pattern matcher thread
77  * \param p pointer to the current packet
78  * \param m pointer to the sigmatch that we will cast into DetectFragOffsetData
79  *
80  * \retval 0 no match or frag is not set
81  * \retval 1 match
82  *
83  */
84 static int DetectFragOffsetMatch (DetectEngineThreadCtx *det_ctx,
85  Packet *p, const Signature *s, const SigMatchCtx *ctx)
86 {
87  uint16_t frag = 0;
88  const DetectU16Data *fragoff = (const DetectU16Data *)ctx;
89 
91 
92  if (PacketIsIPv4(p)) {
93  const IPV4Hdr *ip4h = PacketGetIPv4(p);
94  frag = IPV4_GET_RAW_FRAGOFFSET(ip4h);
95  } else if (PacketIsIPv6(p)) {
96  if (IPV6_EXTHDR_ISSET_FH(p)) {
98  } else {
99  return 0;
100  }
101  } else {
102  SCLogDebug("No IPv4 or IPv6 packet");
103  return 0;
104  }
105 
106  return DetectU16Match(frag, fragoff);
107 }
108 
109 /**
110  * \brief this function is used to add the parsed fragoffset data into the current signature
111  *
112  * \param de_ctx pointer to the Detection Engine Context
113  * \param s pointer to the Current Signature
114  * \param fragoffsetstr pointer to the user provided fragoffset option
115  *
116  * \retval 0 on Success
117  * \retval -1 on Failure
118  */
119 static int DetectFragOffsetSetup (DetectEngineCtx *de_ctx, Signature *s, const char *fragoffsetstr)
120 {
121  DetectU16Data *fragoff = SCDetectU16Parse(fragoffsetstr);
122  if (fragoff == NULL)
123  return -1;
124 
126  DETECT_SM_LIST_MATCH) == NULL) {
127  goto error;
128  }
130 
131  return 0;
132 
133 error:
134  DetectFragOffsetFree(de_ctx, fragoff);
135  return -1;
136 
137 }
138 
139 /**
140  * \brief this function will free memory associated with DetectFragOffsetData
141  *
142  * \param ptr pointer to DetectFragOffsetData
143  */
145 {
146  SCDetectU16Free(ptr);
147 }
148 
149 static void
150 PrefilterPacketFragOffsetMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
151 {
153 
154  uint16_t frag;
155 
156  if (PacketIsIPv4(p)) {
157  const IPV4Hdr *ip4h = PacketGetIPv4(p);
158  frag = IPV4_GET_RAW_FRAGOFFSET(ip4h);
159  } else if (PacketIsIPv6(p)) {
160  if (IPV6_EXTHDR_ISSET_FH(p)) {
162  } else {
163  return;
164  }
165  } else {
166  SCLogDebug("No IPv4 or IPv6 packet");
167  return;
168  }
169 
170  const PrefilterPacketHeaderCtx *ctx = pectx;
171  DetectU16Data du16;
172  du16.mode = ctx->v1.u8[0];
173  du16.arg1 = ctx->v1.u16[1];
174  du16.arg2 = ctx->v1.u16[2];
175 
176  if (DetectU16Match(frag, &du16)) {
177  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
178  }
179 }
180 
181 static int PrefilterSetupFragOffset(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
182 {
184  PrefilterPacketU16Set, PrefilterPacketU16Compare, PrefilterPacketFragOffsetMatch);
185 }
186 
187 static bool PrefilterFragOffsetIsPrefilterable(const Signature *s)
188 {
189  return PrefilterIsPrefilterableById(s, DETECT_FRAGOFFSET);
190 }
191 
192 #ifdef UNITTESTS
193 #include "util-unittest-helper.h"
194 #include "detect-engine.h"
195 #include "detect-engine-alert.h"
196 
197 /**
198  * \test DetectFragOffsetParseTest01 is a test for setting a valid fragoffset value
199  */
200 static int DetectFragOffsetParseTest01 (void)
201 {
202  DetectU16Data *fragoff = SCDetectU16Parse("300");
203 
204  FAIL_IF_NULL(fragoff);
205  FAIL_IF_NOT(fragoff->arg1 == 300);
206 
207  DetectFragOffsetFree(NULL, fragoff);
208 
209  PASS;
210 }
211 
212 /**
213  * \test DetectFragOffsetParseTest02 is a test for setting a valid fragoffset value
214  * with spaces all around
215  */
216 static int DetectFragOffsetParseTest02 (void)
217 {
218  DetectU16Data *fragoff = SCDetectU16Parse(">300");
219 
220  FAIL_IF_NULL(fragoff);
221  FAIL_IF_NOT(fragoff->arg1 == 300);
222  FAIL_IF_NOT(fragoff->mode == DetectUintModeGt);
223 
224  DetectFragOffsetFree(NULL, fragoff);
225 
226  PASS;
227 }
228 
229 /**
230  * \test DetectFragOffsetParseTest03 is a test for setting an invalid fragoffset value
231  */
232 static int DetectFragOffsetParseTest03 (void)
233 {
234  DetectU16Data *fragoff = SCDetectU16Parse("badc");
235 
236  FAIL_IF_NOT_NULL(fragoff);
237 
238  PASS;
239 }
240 
241 /**
242  * \test DetectFragOffsetMatchTest01 is a test for checking the working of
243  * fragoffset keyword by creating 2 rules and matching a crafted packet
244  * against them. Only the first one shall trigger.
245  */
246 static int DetectFragOffsetMatchTest01 (void)
247 {
249 
250  FAIL_IF_NULL(p);
251  Signature *s = NULL;
254  DetectEngineThreadCtx *det_ctx = NULL;
255  IPV4Hdr ip4h;
256 
257  memset(&ip4h, 0, sizeof(IPV4Hdr));
258  memset(&dtv, 0, sizeof(DecodeThreadVars));
259  memset(&th_v, 0, sizeof(ThreadVars));
261 
263 
264  p->src.family = AF_INET;
265  p->dst.family = AF_INET;
266  p->src.addr_data32[0] = 0x01020304;
267  p->dst.addr_data32[0] = 0x04030201;
268 
269  ip4h.s_ip_src.s_addr = p->src.addr_data32[0];
270  ip4h.s_ip_dst.s_addr = p->dst.addr_data32[0];
271  ip4h.ip_off = 0x2222;
272  UTHSetIPV4Hdr(p, &ip4h);
273 
276 
277  de_ctx->flags |= DE_QUIET;
278 
279  s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (fragoffset:546; sid:1;)");
280  FAIL_IF_NULL(s);
281 
282  s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (fragoffset:5000; sid:2;)");
283  FAIL_IF_NULL(s);
284 
286  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
287 
288  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
289 
290  FAIL_IF(PacketAlertCheck(p, 1) == 0);
292 
293  PacketFree(p);
294  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
296  FlowShutdown();
298  PASS;
299 }
300 
302 {
303  UtRegisterTest("DetectFragOffsetParseTest01", DetectFragOffsetParseTest01);
304  UtRegisterTest("DetectFragOffsetParseTest02", DetectFragOffsetParseTest02);
305  UtRegisterTest("DetectFragOffsetParseTest03", DetectFragOffsetParseTest03);
306  UtRegisterTest("DetectFragOffsetMatchTest01", DetectFragOffsetMatchTest01);
307 }
308 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
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:320
DetectFragOffsetRegisterTests
void DetectFragOffsetRegisterTests(void)
Definition: detect-fragoffset.c:301
PrefilterPacketU16Set
void PrefilterPacketU16Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:124
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1529
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:1542
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1364
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1730
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
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:144
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1429
UTHSetIPV4Hdr
void UTHSetIPV4Hdr(Packet *p, IPV4Hdr *ip4h)
Definition: util-unittest-helper.c:251
DetectFragOffsetRegister
void DetectFragOffsetRegister(void)
Registration function for fragoffset.
Definition: detect-fragoffset.c:56
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2912
DE_QUIET
#define DE_QUIET
Definition: detect.h:334
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:3305
p
Packet * p
Definition: fuzz_dataset.c:30
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3965
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
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:1527
FlowInitConfig
void FlowInitConfig(bool quiet)
initialize the configuration
Definition: flow.c:574
DetectFragOffsetFree
void DetectFragOffsetFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectFragOffsetData
Definition: detect-fragoffset.c:144
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
SIGMATCH_INFO_UINT16
#define SIGMATCH_INFO_UINT16
Definition: detect-engine-register.h:344
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1316
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:424
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3660
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:221
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:118
Signature_::flags
uint32_t flags
Definition: detect.h:693
Packet_
Definition: decode.h:516
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:1504
detect-fragoffset.h
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2300
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:35
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1332
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:355
IPV4Hdr_
Definition: decode-ipv4.h:72
decode-ipv4.h
th_v
ThreadVars * th_v
Definition: fuzz_dataset.c:29
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:105
PrefilterPacketU16Compare
bool PrefilterPacketU16Compare(PrefilterPacketHeaderValue v, void *smctx)
Definition: detect-engine-uint.c:132
suricata-common.h
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:718
DETECT_FRAGOFFSET
@ DETECT_FRAGOFFSET
Definition: detect-engine-register.h:44
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3905
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:260
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:995
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1526
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2873
Address_::family
char family
Definition: decode.h:114
Packet_::dst
Address dst
Definition: decode.h:521
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:43
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:997
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:120
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1428
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
Packet_::src
Address src
Definition: decode.h:520
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:257