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