suricata
detect-template2.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 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 XXX
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "util-byte.h"
27 
28 #include "detect.h"
29 #include "detect-parse.h"
31 #include "detect-engine-uint.h"
32 
33 #include "detect-template2.h"
34 
35 
36 /* prototypes */
37 static int DetectTemplate2Match (DetectEngineThreadCtx *, Packet *,
38  const Signature *, const SigMatchCtx *);
39 static int DetectTemplate2Setup (DetectEngineCtx *, Signature *, const char *);
40 void DetectTemplate2Free (DetectEngineCtx *, void *);
41 #ifdef UNITTESTS
43 #endif
44 static int PrefilterSetupTemplate2(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
45 static bool PrefilterTemplate2IsPrefilterable(const Signature *s);
46 
47 /**
48  * \brief Registration function for template2: keyword
49  */
50 
52 {
53  sigmatch_table[DETECT_TEMPLATE2].name = "template2";
54  sigmatch_table[DETECT_TEMPLATE2].desc = "TODO describe the keyword";
55  sigmatch_table[DETECT_TEMPLATE2].url = "/rules/header-keywords.html#template2";
56  sigmatch_table[DETECT_TEMPLATE2].Match = DetectTemplate2Match;
57  sigmatch_table[DETECT_TEMPLATE2].Setup = DetectTemplate2Setup;
59  sigmatch_table[DETECT_TEMPLATE2].SupportsPrefilter = PrefilterTemplate2IsPrefilterable;
60  sigmatch_table[DETECT_TEMPLATE2].SetupPrefilter = PrefilterSetupTemplate2;
61 
62  return;
63 }
64 
65 /**
66  * \brief This function is used to match TEMPLATE2 rule option on a packet with those passed via
67  * template2:
68  *
69  * \param t pointer to thread vars
70  * \param det_ctx pointer to the pattern matcher thread
71  * \param p pointer to the current packet
72  * \param m pointer to the sigmatch that we will cast into DetectU8Data
73  *
74  * \retval 0 no match
75  * \retval 1 match
76  */
77 static int DetectTemplate2Match (DetectEngineThreadCtx *det_ctx, Packet *p,
78  const Signature *s, const SigMatchCtx *ctx)
79 {
80 
81  if (PKT_IS_PSEUDOPKT(p))
82  return 0;
83 
84  /* TODO replace this */
85  uint8_t ptemplate2;
86  if (PKT_IS_IPV4(p)) {
87  ptemplate2 = IPV4_GET_IPTTL(p);
88  } else if (PKT_IS_IPV6(p)) {
89  ptemplate2 = IPV6_GET_HLIM(p);
90  } else {
91  SCLogDebug("Packet is of not IPv4 or IPv6");
92  return 0;
93  }
94 
95  const DetectU8Data *template2d = (const DetectU8Data *)ctx;
96  return DetectU8Match(ptemplate2, template2d);
97 }
98 
99 /**
100  * \brief this function is used to add the parsed template2 data into the current signature
101  *
102  * \param de_ctx pointer to the Detection Engine Context
103  * \param s pointer to the Current Signature
104  * \param template2str pointer to the user provided template2 options
105  *
106  * \retval 0 on Success
107  * \retval -1 on Failure
108  */
109 static int DetectTemplate2Setup (DetectEngineCtx *de_ctx, Signature *s, const char *template2str)
110 {
111  DetectU8Data *template2d = DetectU8Parse(template2str);
112  if (template2d == NULL)
113  return -1;
114 
115  SigMatch *sm = SigMatchAlloc();
116  if (sm == NULL) {
117  DetectTemplate2Free(de_ctx, template2d);
118  return -1;
119  }
120 
121  sm->type = DETECT_TEMPLATE2;
122  sm->ctx = (SigMatchCtx *)template2d;
123 
126 
127  return 0;
128 }
129 
130 /**
131  * \brief this function will free memory associated with DetectU8Data
132  *
133  * \param ptr pointer to DetectU8Data
134  */
136 {
137  rs_detect_u8_free(ptr);
138 }
139 
140 /* prefilter code */
141 
142 static void
143 PrefilterPacketTemplate2Match(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
144 {
145  if (PKT_IS_PSEUDOPKT(p)) {
146  SCReturn;
147  }
148 
149  uint8_t ptemplate2;
150 /* TODO update */
151  if (PKT_IS_IPV4(p)) {
152  ptemplate2 = IPV4_GET_IPTTL(p);
153  } else if (PKT_IS_IPV6(p)) {
154  ptemplate2 = IPV6_GET_HLIM(p);
155  } else {
156  SCLogDebug("Packet is of not IPv4 or IPv6");
157  return;
158  }
159 
160  /* during setup Suricata will automatically see if there is another
161  * check that can be added: alproto, sport or dport */
162  const PrefilterPacketHeaderCtx *ctx = pectx;
163  if (!PrefilterPacketHeaderExtraMatch(ctx, p))
164  return;
165 
166  DetectU8Data du8;
167  du8.mode = ctx->v1.u8[0];
168  du8.arg1 = ctx->v1.u8[1];
169  du8.arg2 = ctx->v1.u8[2];
170  /* if we match, add all the sigs that use this prefilter. This means
171  * that these will be inspected further */
172  if (DetectU8Match(ptemplate2, &du8)) {
173  SCLogDebug("packet matches template2/hl %u", ptemplate2);
174  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
175  }
176 }
177 
178 static int PrefilterSetupTemplate2(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
179 {
181  PrefilterPacketU8Compare, PrefilterPacketTemplate2Match);
182 }
183 
184 static bool PrefilterTemplate2IsPrefilterable(const Signature *s)
185 {
186  const SigMatch *sm;
187  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
188  switch (sm->type) {
189  case DETECT_TEMPLATE2:
190  return true;
191  }
192  }
193  return false;
194 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1238
SigTableElmt_::desc
const char * desc
Definition: detect.h:1237
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1225
SigTableElmt_::name
const char * name
Definition: detect.h:1235
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1052
PKT_IS_IPV6
#define PKT_IS_IPV6(p)
Definition: decode.h:243
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1393
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1131
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:784
detect-template2.h
PrefilterPacketHeaderCtx_::sigs_array
SigIntId * sigs_array
Definition: detect-engine-prefilter-common.h:43
PrefilterPacketHeaderValue::u8
uint8_t u8[16]
Definition: detect-engine-prefilter-common.h:24
DetectTemplate2Free
void DetectTemplate2Free(DetectEngineCtx *, void *)
this function will free memory associated with DetectU8Data
Definition: detect-template2.c:135
PrefilterPacketHeaderCtx_::sigs_cnt
uint32_t sigs_cnt
Definition: detect-engine-prefilter-common.h:42
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1220
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1223
DetectU8Parse
DetectUintData_u8 * DetectU8Parse(const char *u8str)
This function is used to parse u8 options passed via some u8 keyword.
Definition: detect-engine-uint.c:85
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:35
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1024
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:317
DetectU8Data
DetectUintData_u8 DetectU8Data
Definition: detect-engine-uint.h:43
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:78
PrefilterPacketU8Set
void PrefilterPacketU8Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:90
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:316
DetectTemplate2RegisterTests
void DetectTemplate2RegisterTests(void)
SCReturn
#define SCReturn
Definition: util-debug.h:300
Signature_::flags
uint32_t flags
Definition: detect.h:540
Packet_
Definition: decode.h:425
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:610
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1203
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:533
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:238
PrefilterPacketHeaderCtx_::v1
PrefilterPacketHeaderValue v1
Definition: detect-engine-prefilter-common.h:36
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:308
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
Definition: detect-engine-uint.c:71
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:314
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
PrefilterSetupPacketHeader
int PrefilterSetupPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, 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:417
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1222
detect-parse.h
Signature_
Signature container.
Definition: detect.h:539
SigMatch_
a single match condition for a signature
Definition: detect.h:313
DETECT_TEMPLATE2
@ DETECT_TEMPLATE2
Definition: detect-engine-register.h:270
IPV6_GET_HLIM
#define IPV6_GET_HLIM(p)
Definition: decode-ipv6.h:90
PrefilterPacketU8Compare
bool PrefilterPacketU8Compare(PrefilterPacketHeaderValue v, void *smctx)
Definition: detect-engine-uint.c:98
detect-engine-prefilter-common.h
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:242
DetectTemplate2Register
void DetectTemplate2Register(void)
Registration function for template2: keyword.
Definition: detect-template2.c:51
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:352
IPV4_GET_IPTTL
#define IPV4_GET_IPTTL(p)
Definition: decode-ipv4.h:146
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:215