suricata
detect-id.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 Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
22  *
23  * Implements the id keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 #include "detect.h"
29 
30 #include "detect-parse.h"
31 #include "detect-engine.h"
32 #include "detect-engine-mpm.h"
34 #include "detect-engine-uint.h"
35 
36 #include "detect-id.h"
37 #include "flow.h"
38 #include "flow-var.h"
39 
40 #include "util-debug.h"
41 #include "util-byte.h"
42 #include "util-unittest.h"
43 #include "util-unittest-helper.h"
44 
45 /**
46  * \brief Regex for parsing "id" option, matching number or "number"
47  */
48 
49 static int DetectIdMatch (DetectEngineThreadCtx *, Packet *,
50  const Signature *, const SigMatchCtx *);
51 static int DetectIdSetup (DetectEngineCtx *, Signature *, const char *);
52 #ifdef UNITTESTS
53 static void DetectIdRegisterTests(void);
54 #endif
55 void DetectIdFree(DetectEngineCtx *, void *);
56 
57 static int PrefilterSetupId(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
58 static bool PrefilterIdIsPrefilterable(const Signature *s);
59 
60 /**
61  * \brief Registration function for keyword: id
62  */
63 void DetectIdRegister (void)
64 {
66  sigmatch_table[DETECT_ID].desc = "match on a specific IP ID value";
67  sigmatch_table[DETECT_ID].url = "/rules/header-keywords.html#id";
68  sigmatch_table[DETECT_ID].Match = DetectIdMatch;
69  sigmatch_table[DETECT_ID].Setup = DetectIdSetup;
72 #ifdef UNITTESTS
73  sigmatch_table[DETECT_ID].RegisterTests = DetectIdRegisterTests;
74 #endif
75  sigmatch_table[DETECT_ID].SupportsPrefilter = PrefilterIdIsPrefilterable;
76  sigmatch_table[DETECT_ID].SetupPrefilter = PrefilterSetupId;
77 }
78 
79 /**
80  * \brief This function is used to match the specified id on a packet
81  *
82  * \param t pointer to thread vars
83  * \param det_ctx pointer to the pattern matcher thread
84  * \param p pointer to the current packet
85  * \param m pointer to the sigmatch that we will cast into DetectIdData
86  *
87  * \retval 0 no match
88  * \retval 1 match
89  */
90 static int DetectIdMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
91  const Signature *s, const SigMatchCtx *ctx)
92 {
94  const DetectU16Data *id_d = (const DetectU16Data *)ctx;
95 
96  /**
97  * To match a ipv4 packet with a "id" rule
98  */
99  if (!PacketIsIPv4(p)) {
100  return 0;
101  }
102 
103  const IPV4Hdr *ip4h = PacketGetIPv4(p);
104  return DetectU16Match(IPV4_GET_RAW_IPID(ip4h), id_d);
105 }
106 
107 /**
108  * \brief this function is used to add the parsed "id" option
109  * \brief into the current signature
110  *
111  * \param de_ctx pointer to the Detection Engine Context
112  * \param s pointer to the Current Signature
113  * \param idstr pointer to the user provided "id" option
114  *
115  * \retval 0 on Success
116  * \retval -1 on Failure
117  */
118 int DetectIdSetup (DetectEngineCtx *de_ctx, Signature *s, const char *idstr)
119 {
120  DetectU16Data *id_d = SCDetectU16UnquoteParse(idstr);
121  if (id_d == NULL)
122  return -1;
123 
124  /* Okay so far so good, lets get this into a SigMatch
125  * and put it in the Signature. */
127  NULL) {
128  DetectIdFree(de_ctx, id_d);
129  return -1;
130  }
132  return 0;
133 }
134 
135 /**
136  * \brief this function will free memory associated with DetectIdData
137  *
138  * \param id_d pointer to DetectIdData
139  */
141 {
142  SCDetectU16Free(ptr);
143 }
144 
145 /* prefilter code */
146 
147 static void
148 PrefilterPacketIdMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
149 {
151 
152  const PrefilterPacketHeaderCtx *ctx = pectx;
153 
154  if (!PacketIsIPv4(p)) {
155  return;
156  }
157 
158  if (!PrefilterPacketHeaderExtraMatch(ctx, p))
159  return;
160 
161  const IPV4Hdr *ip4h = PacketGetIPv4(p);
162  DetectU16Data du16;
163  du16.mode = ctx->v1.u8[0];
164  du16.arg1 = ctx->v1.u16[1];
165  du16.arg2 = ctx->v1.u16[2];
166  if (DetectU16Match(IPV4_GET_RAW_IPID(ip4h), &du16)) {
167  SCLogDebug("packet matches IP id %u", ctx->v1.u16[0]);
168  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
169  }
170 }
171 
172 static int PrefilterSetupId(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
173 {
175  PrefilterPacketU16Set, PrefilterPacketU16Compare, PrefilterPacketIdMatch);
176 }
177 
178 static bool PrefilterIdIsPrefilterable(const Signature *s)
179 {
180  const SigMatch *sm;
181  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
182  switch (sm->type) {
183  case DETECT_ID:
184  return true;
185  }
186  }
187  return false;
188 }
189 
190 #ifdef UNITTESTS /* UNITTESTS */
191 
192 /**
193  * \test DetectIdTestParse01 is a test to make sure that we parse the "id"
194  * option correctly when given valid id option
195  */
196 static int DetectIdTestParse01 (void)
197 {
198  DetectU16Data *id_d = SCDetectU16UnquoteParse(" 35402 ");
199 
200  FAIL_IF_NULL(id_d);
201  FAIL_IF_NOT(id_d->arg1 == 35402);
202 
203  DetectIdFree(NULL, id_d);
204 
205  PASS;
206 }
207 
208 /**
209  * \test DetectIdTestParse02 is a test to make sure that we parse the "id"
210  * option correctly when given an invalid id option
211  * it should return id_d = NULL
212  */
213 static int DetectIdTestParse02 (void)
214 {
215  DetectU16Data *id_d = SCDetectU16UnquoteParse("65537");
216 
217  FAIL_IF_NOT_NULL(id_d);
218 
219  PASS;
220 }
221 
222 /**
223  * \test DetectIdTestParse03 is a test to make sure that we parse the "id"
224  * option correctly when given an invalid id option
225  * it should return id_d = NULL
226  */
227 static int DetectIdTestParse03 (void)
228 {
229  DetectU16Data *id_d = SCDetectU16UnquoteParse("12what?");
230 
231  FAIL_IF_NOT_NULL(id_d);
232 
233  PASS;
234 }
235 
236 /**
237  * \test DetectIdTestParse04 is a test to make sure that we parse the "id"
238  * option correctly when given valid id option but wrapped with "'s
239  */
240 static int DetectIdTestParse04 (void)
241 {
242  /* yep, look if we trim blank spaces correctly and ignore "'s */
243  DetectU16Data *id_d = SCDetectU16UnquoteParse(" \"35402\" ");
244 
245  FAIL_IF_NULL(id_d);
246  FAIL_IF_NOT(id_d->arg1 == 35402);
247 
248  DetectIdFree(NULL, id_d);
249 
250  PASS;
251 }
252 
253 /**
254  * \test DetectIdTestSig01
255  * \brief Test to check "id" keyword with constructed packets
256  */
257 static int DetectIdTestMatch01(void)
258 {
259  uint8_t *buf = (uint8_t *)"Hi all!";
260  uint16_t buflen = strlen((char *)buf);
261  Packet *p[3];
262  p[0] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
263  p[1] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_UDP);
264  p[2] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_ICMP);
265 
266  FAIL_IF_NULL(p[0]);
267  FAIL_IF_NULL(p[1]);
268  FAIL_IF_NULL(p[2]);
269 
270  /* TCP IP id = 1234 */
271  p[0]->l3.hdrs.ip4h->ip_id = htons(1234);
272 
273  /* UDP IP id = 5678 */
274  p[1]->l3.hdrs.ip4h->ip_id = htons(5678);
275 
276  /* UDP IP id = 91011 */
277  p[2]->l3.hdrs.ip4h->ip_id = htons(5101);
278 
279  const char *sigs[3];
280  sigs[0]= "alert ip any any -> any any (msg:\"Testing id 1\"; id:1234; sid:1;)";
281  sigs[1]= "alert ip any any -> any any (msg:\"Testing id 2\"; id:5678; sid:2;)";
282  sigs[2]= "alert ip any any -> any any (msg:\"Testing id 3\"; id:5101; sid:3;)";
283 
284  uint32_t sid[3] = {1, 2, 3};
285 
286  uint32_t results[3][3] = {
287  /* packet 0 match sid 1 but should not match sid 2 */
288  {1, 0, 0},
289  /* packet 1 should not match */
290  {0, 1, 0},
291  /* packet 2 should not match */
292  {0, 0, 1} };
293 
294  FAIL_IF_NOT(UTHGenericTest(p, 3, sigs, sid, (uint32_t *)results, 3));
295 
296  UTHFreePackets(p, 3);
297 
298  PASS;
299 }
300 
301 /**
302  * \brief this function registers unit tests for DetectId
303  */
304 void DetectIdRegisterTests(void)
305 {
306  UtRegisterTest("DetectIdTestParse01", DetectIdTestParse01);
307  UtRegisterTest("DetectIdTestParse02", DetectIdTestParse02);
308  UtRegisterTest("DetectIdTestParse03", DetectIdTestParse03);
309  UtRegisterTest("DetectIdTestParse04", DetectIdTestParse04);
310  UtRegisterTest("DetectIdTestMatch01", DetectIdTestMatch01);
311 
312 }
313 #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
IPV4_GET_RAW_IPID
#define IPV4_GET_RAW_IPID(ip4h)
Definition: decode-ipv4.h:99
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
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
DETECT_ID
@ DETECT_ID
Definition: detect-engine-register.h:108
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1348
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
IPV4Hdr_::ip_id
uint16_t ip_id
Definition: decode-ipv4.h:76
PacketL3::hdrs
union PacketL3::Hdrs hdrs
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:365
PacketL3::Hdrs::ip4h
IPV4Hdr * ip4h
Definition: decode.h:439
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
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
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
DetectIdRegister
void DetectIdRegister(void)
Registration function for keyword: id.
Definition: detect-id.c:63
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-id.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
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
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
UTHGenericTest
int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
UTHGenericTest: function that perform a generic check taking care of as maximum common unittest eleme...
Definition: util-unittest-helper.c:581
Packet_::l3
struct PacketL3 l3
Definition: decode.h:600
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
DetectIdFree
void DetectIdFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectIdData
Definition: detect-id.c:140
detect-engine-prefilter-common.h
DetectU16Data
DetectUintData_u16 DetectU16Data
Definition: detect-engine-uint.h:42
flow.h
flow-var.h
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1447
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:254
UTHFreePackets
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:456