suricata
detect-icmp-seq.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 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 the icmp_seq keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 
29 #include "detect.h"
30 #include "detect-parse.h"
32 #include "detect-engine-build.h"
33 #include "detect-engine-uint.h"
34 
35 #include "detect-icmp-seq.h"
36 
37 #include "util-byte.h"
38 #include "util-unittest.h"
39 #include "util-unittest-helper.h"
40 #include "util-debug.h"
41 
42 static int DetectIcmpSeqMatch(DetectEngineThreadCtx *, Packet *,
43  const Signature *, const SigMatchCtx *);
44 static int DetectIcmpSeqSetup(DetectEngineCtx *, Signature *, const char *);
45 #ifdef UNITTESTS
46 static void DetectIcmpSeqRegisterTests(void);
47 #endif
48 void DetectIcmpSeqFree(DetectEngineCtx *, void *);
49 static int PrefilterSetupIcmpSeq(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
50 static bool PrefilterIcmpSeqIsPrefilterable(const Signature *s);
51 
52 /**
53  * \brief Registration function for icmp_seq
54  */
56 {
57  sigmatch_table[DETECT_ICMP_SEQ].name = "icmp_seq";
58  sigmatch_table[DETECT_ICMP_SEQ].desc = "check for a ICMP sequence number";
59  sigmatch_table[DETECT_ICMP_SEQ].url = "/rules/header-keywords.html#icmp-seq";
60  sigmatch_table[DETECT_ICMP_SEQ].Match = DetectIcmpSeqMatch;
61  sigmatch_table[DETECT_ICMP_SEQ].Setup = DetectIcmpSeqSetup;
64 #ifdef UNITTESTS
65  sigmatch_table[DETECT_ICMP_SEQ].RegisterTests = DetectIcmpSeqRegisterTests;
66 #endif
67  sigmatch_table[DETECT_ICMP_SEQ].SupportsPrefilter = PrefilterIcmpSeqIsPrefilterable;
68  sigmatch_table[DETECT_ICMP_SEQ].SetupPrefilter = PrefilterSetupIcmpSeq;
69 }
70 
71 static inline bool GetIcmpSeq(Packet *p, uint16_t *seq)
72 {
73  uint16_t seqn;
74 
75  if (PacketIsICMPv4(p)) {
76  switch (p->icmp_s.type) {
77  case ICMP_ECHOREPLY:
78  case ICMP_ECHO:
79  case ICMP_TIMESTAMP:
81  case ICMP_INFO_REQUEST:
82  case ICMP_INFO_REPLY:
83  case ICMP_ADDRESS:
84  case ICMP_ADDRESSREPLY:
85  SCLogDebug("ICMPV4_GET_SEQ(p) %"PRIu16" (network byte order), "
86  "%"PRIu16" (host byte order)", ICMPV4_GET_SEQ(p),
88 
89  seqn = ICMPV4_GET_SEQ(p);
90  break;
91  default:
92  SCLogDebug("Packet has no seq field");
93  return false;
94  }
95  } else if (PacketIsICMPv6(p)) {
96  switch (ICMPV6_GET_TYPE(PacketGetICMPv6(p))) {
97  case ICMP6_ECHO_REQUEST:
98  case ICMP6_ECHO_REPLY:
99  SCLogDebug("ICMPV6_GET_SEQ(p) %"PRIu16" (network byte order), "
100  "%"PRIu16" (host byte order)", ICMPV6_GET_SEQ(p),
101  SCNtohs(ICMPV6_GET_SEQ(p)));
102 
103  seqn = ICMPV6_GET_SEQ(p);
104  break;
105  default:
106  SCLogDebug("Packet has no seq field");
107  return false;
108  }
109  } else {
110  SCLogDebug("Packet not ICMPV4 nor ICMPV6");
111  return false;
112  }
113 
114  *seq = SCNtohs(seqn);
115  return true;
116 }
117 
118 /**
119  * \brief This function is used to match icmp_seq rule option set on a packet
120  *
121  * \param t pointer to thread vars
122  * \param det_ctx pointer to the pattern matcher thread
123  * \param p pointer to the current packet
124  * \param m pointer to the sigmatch that we will cast into DetectU16Data
125  *
126  * \retval 0 no match
127  * \retval 1 match
128  */
129 static int DetectIcmpSeqMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
130  const Signature *s, const SigMatchCtx *ctx)
131 {
133  uint16_t seqn;
134 
135  if (!GetIcmpSeq(p, &seqn))
136  return 0;
137 
138  const DetectU16Data *iseq = (const DetectU16Data *)ctx;
139  return DetectU16Match(seqn, iseq);
140 }
141 
142 /**
143  * \brief this function is used to add the parsed icmp_seq data into the current signature
144  *
145  * \param de_ctx pointer to the Detection Engine Context
146  * \param s pointer to the Current Signature
147  * \param icmpseqstr pointer to the user provided icmp_seq option
148  *
149  * \retval 0 on Success
150  * \retval -1 on Failure
151  */
152 static int DetectIcmpSeqSetup (DetectEngineCtx *de_ctx, Signature *s, const char *icmpseqstr)
153 {
154  DetectU16Data *iseq = SCDetectU16UnquoteParse(icmpseqstr);
155  if (iseq == NULL)
156  return -1;
157 
159  de_ctx, s, DETECT_ICMP_SEQ, (SigMatchCtx *)iseq, DETECT_SM_LIST_MATCH) == NULL) {
160  goto error;
161  }
163 
164  return 0;
165 
166 error:
167  DetectIcmpSeqFree(de_ctx, iseq);
168  return -1;
169 
170 }
171 
172 /**
173  * \brief this function will free memory associated with DetectU16Data
174  *
175  * \param ptr pointer to DetectU16Data
176  */
178 {
179  SCDetectU16Free(ptr);
180 }
181 
182 /* prefilter code */
183 
184 static void
185 PrefilterPacketIcmpSeqMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
186 {
188 
189  const PrefilterPacketHeaderCtx *ctx = pectx;
190  uint16_t seqn;
191 
192  if (!GetIcmpSeq(p, &seqn))
193  return;
194 
195  DetectU16Data du16;
196  du16.mode = ctx->v1.u8[0];
197  du16.arg1 = ctx->v1.u16[1];
198  du16.arg2 = ctx->v1.u16[2];
199  if (DetectU16Match(seqn, &du16)) {
200  SCLogDebug("packet matches ICMP SEQ %u", ctx->v1.u16[0]);
201  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
202  }
203 }
204 
205 static int PrefilterSetupIcmpSeq(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
206 {
208  PrefilterPacketU16Set, PrefilterPacketU16Compare, PrefilterPacketIcmpSeqMatch);
209 }
210 
211 static bool PrefilterIcmpSeqIsPrefilterable(const Signature *s)
212 {
213  const SigMatch *sm;
214  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
215  switch (sm->type) {
216  case DETECT_ICMP_SEQ:
217  return true;
218  }
219  }
220  return false;
221 }
222 
223 #ifdef UNITTESTS
224 #include "detect-engine.h"
225 #include "detect-engine-mpm.h"
226 #include "detect-engine-alert.h"
227 
228 /**
229  * \test DetectIcmpSeqParseTest01 is a test for setting a valid icmp_seq value
230  */
231 static int DetectIcmpSeqParseTest01 (void)
232 {
233  DetectU16Data *iseq = NULL;
234  iseq = SCDetectU16UnquoteParse("300");
235  FAIL_IF_NULL(iseq);
236  FAIL_IF_NOT(iseq->arg1 == 300);
237  DetectIcmpSeqFree(NULL, iseq);
238  PASS;
239 }
240 
241 /**
242  * \test DetectIcmpSeqParseTest02 is a test for setting a valid icmp_seq value
243  * with spaces all around
244  */
245 static int DetectIcmpSeqParseTest02 (void)
246 {
247  DetectU16Data *iseq = NULL;
248  iseq = SCDetectU16UnquoteParse(" 300 ");
249  FAIL_IF_NULL(iseq);
250  FAIL_IF_NOT(iseq->arg1 == 300);
251  DetectIcmpSeqFree(NULL, iseq);
252  PASS;
253 }
254 
255 /**
256  * \test DetectIcmpSeqParseTest03 is a test for setting an invalid icmp_seq value
257  */
258 static int DetectIcmpSeqParseTest03 (void)
259 {
260  DetectU16Data *iseq = SCDetectU16UnquoteParse("badc");
261  FAIL_IF_NOT_NULL(iseq);
262  PASS;
263 }
264 
265 static void DetectIcmpSeqRegisterTests (void)
266 {
267  UtRegisterTest("DetectIcmpSeqParseTest01", DetectIcmpSeqParseTest01);
268  UtRegisterTest("DetectIcmpSeqParseTest02", DetectIcmpSeqParseTest02);
269  UtRegisterTest("DetectIcmpSeqParseTest03", DetectIcmpSeqParseTest03);
270 }
271 #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
PrefilterPacketU16Set
void PrefilterPacketU16Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:126
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
ICMP_INFO_REQUEST
#define ICMP_INFO_REQUEST
Definition: decode-icmpv4.h:66
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1445
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
seq
uint32_t seq
Definition: stream-tcp-private.h:2
ICMPV6_GET_SEQ
#define ICMPV6_GET_SEQ(p)
Definition: decode-icmpv6.h:109
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1348
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
Packet_::icmp_s
struct Packet_::@33::@40 icmp_s
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
ICMP6_ECHO_REQUEST
#define ICMP6_ECHO_REQUEST
Definition: decode-icmpv6.h:42
ICMP_ECHO
#define ICMP_ECHO
Definition: decode-icmpv4.h:45
ICMPV4_GET_SEQ
#define ICMPV4_GET_SEQ(p)
Definition: decode-icmpv4.h:238
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1443
ICMP_ADDRESSREPLY
#define ICMP_ADDRESSREPLY
Definition: decode-icmpv4.h:75
ICMP_ADDRESS
#define ICMP_ADDRESS
Definition: decode-icmpv4.h:72
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
ICMP6_ECHO_REPLY
#define ICMP6_ECHO_REPLY
Definition: decode-icmpv6.h:43
DetectEngineThreadCtx_
Definition: detect.h:1245
detect-engine-mpm.h
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
DetectIcmpSeqRegister
void DetectIcmpSeqRegister(void)
Registration function for icmp_seq.
Definition: detect-icmp-seq.c:55
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
ICMP_INFO_REPLY
#define ICMP_INFO_REPLY
Definition: decode-icmpv4.h:69
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
ICMP_ECHOREPLY
#define ICMP_ECHOREPLY
Definition: decode-icmpv4.h:33
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
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
SCNtohs
#define SCNtohs(x)
Definition: suricata-common.h:431
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:357
DETECT_ICMP_SEQ
@ DETECT_ICMP_SEQ
Definition: detect-engine-register.h:50
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
ICMP_TIMESTAMPREPLY
#define ICMP_TIMESTAMPREPLY
Definition: decode-icmpv4.h:63
ICMPV6_GET_TYPE
#define ICMPV6_GET_TYPE(icmp6h)
Definition: decode-icmpv6.h:101
ICMP_TIMESTAMP
#define ICMP_TIMESTAMP
Definition: decode-icmpv4.h:60
detect-engine-prefilter-common.h
DetectU16Data
DetectUintData_u16 DetectU16Data
Definition: detect-engine-uint.h:42
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
DetectIcmpSeqFree
void DetectIcmpSeqFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectU16Data
Definition: detect-icmp-seq.c:177
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1447
detect-icmp-seq.h
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:254