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  return PrefilterIsPrefilterableById(s, DETECT_ID);
181 }
182 
183 #ifdef UNITTESTS /* UNITTESTS */
184 
185 /**
186  * \test DetectIdTestParse01 is a test to make sure that we parse the "id"
187  * option correctly when given valid id option
188  */
189 static int DetectIdTestParse01 (void)
190 {
191  DetectU16Data *id_d = SCDetectU16UnquoteParse(" 35402 ");
192 
193  FAIL_IF_NULL(id_d);
194  FAIL_IF_NOT(id_d->arg1 == 35402);
195 
196  DetectIdFree(NULL, id_d);
197 
198  PASS;
199 }
200 
201 /**
202  * \test DetectIdTestParse02 is a test to make sure that we parse the "id"
203  * option correctly when given an invalid id option
204  * it should return id_d = NULL
205  */
206 static int DetectIdTestParse02 (void)
207 {
208  DetectU16Data *id_d = SCDetectU16UnquoteParse("65537");
209 
210  FAIL_IF_NOT_NULL(id_d);
211 
212  PASS;
213 }
214 
215 /**
216  * \test DetectIdTestParse03 is a test to make sure that we parse the "id"
217  * option correctly when given an invalid id option
218  * it should return id_d = NULL
219  */
220 static int DetectIdTestParse03 (void)
221 {
222  DetectU16Data *id_d = SCDetectU16UnquoteParse("12what?");
223 
224  FAIL_IF_NOT_NULL(id_d);
225 
226  PASS;
227 }
228 
229 /**
230  * \test DetectIdTestParse04 is a test to make sure that we parse the "id"
231  * option correctly when given valid id option but wrapped with "'s
232  */
233 static int DetectIdTestParse04 (void)
234 {
235  /* yep, look if we trim blank spaces correctly and ignore "'s */
236  DetectU16Data *id_d = SCDetectU16UnquoteParse(" \"35402\" ");
237 
238  FAIL_IF_NULL(id_d);
239  FAIL_IF_NOT(id_d->arg1 == 35402);
240 
241  DetectIdFree(NULL, id_d);
242 
243  PASS;
244 }
245 
246 /**
247  * \test DetectIdTestSig01
248  * \brief Test to check "id" keyword with constructed packets
249  */
250 static int DetectIdTestMatch01(void)
251 {
252  uint8_t *buf = (uint8_t *)"Hi all!";
253  uint16_t buflen = strlen((char *)buf);
254  Packet *p[3];
255  p[0] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
256  p[1] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_UDP);
257  p[2] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_ICMP);
258 
259  FAIL_IF_NULL(p[0]);
260  FAIL_IF_NULL(p[1]);
261  FAIL_IF_NULL(p[2]);
262 
263  /* TCP IP id = 1234 */
264  p[0]->l3.hdrs.ip4h->ip_id = htons(1234);
265 
266  /* UDP IP id = 5678 */
267  p[1]->l3.hdrs.ip4h->ip_id = htons(5678);
268 
269  /* UDP IP id = 91011 */
270  p[2]->l3.hdrs.ip4h->ip_id = htons(5101);
271 
272  const char *sigs[3];
273  sigs[0]= "alert ip any any -> any any (msg:\"Testing id 1\"; id:1234; sid:1;)";
274  sigs[1]= "alert ip any any -> any any (msg:\"Testing id 2\"; id:5678; sid:2;)";
275  sigs[2]= "alert ip any any -> any any (msg:\"Testing id 3\"; id:5101; sid:3;)";
276 
277  uint32_t sid[3] = {1, 2, 3};
278 
279  uint32_t results[3][3] = {
280  /* packet 0 match sid 1 but should not match sid 2 */
281  {1, 0, 0},
282  /* packet 1 should not match */
283  {0, 1, 0},
284  /* packet 2 should not match */
285  {0, 0, 1} };
286 
287  FAIL_IF_NOT(UTHGenericTest(p, 3, sigs, sid, (uint32_t *)results, 3));
288 
289  UTHFreePackets(p, 3);
290 
291  PASS;
292 }
293 
294 /**
295  * \brief this function registers unit tests for DetectId
296  */
297 void DetectIdRegisterTests(void)
298 {
299  UtRegisterTest("DetectIdTestParse01", DetectIdTestParse01);
300  UtRegisterTest("DetectIdTestParse02", DetectIdTestParse02);
301  UtRegisterTest("DetectIdTestParse03", DetectIdTestParse03);
302  UtRegisterTest("DetectIdTestParse04", DetectIdTestParse04);
303  UtRegisterTest("DetectIdTestMatch01", DetectIdTestMatch01);
304 
305 }
306 #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
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:19
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
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
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
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
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