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 */
37static int DetectTemplate2Match (DetectEngineThreadCtx *, Packet *,
38 const Signature *, const SigMatchCtx *);
39static int DetectTemplate2Setup (DetectEngineCtx *, Signature *, const char *);
41#ifdef UNITTESTS
43#endif
44static int PrefilterSetupTemplate2(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
45static bool PrefilterTemplate2IsPrefilterable(const Signature *s);
46
47/**
48 * \brief Registration function for template2: keyword
49 */
50
52{
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
63/**
64 * \brief This function is used to match TEMPLATE2 rule option on a packet with those passed via
65 * template2:
66 *
67 * \param t pointer to thread vars
68 * \param det_ctx pointer to the pattern matcher thread
69 * \param p pointer to the current packet
70 * \param m pointer to the sigmatch that we will cast into DetectU8Data
71 *
72 * \retval 0 no match
73 * \retval 1 match
74 */
75static int DetectTemplate2Match (DetectEngineThreadCtx *det_ctx, Packet *p,
76 const Signature *s, const SigMatchCtx *ctx)
77{
79
80 /* TODO replace this */
81 uint8_t ptemplate2;
82 if (PacketIsIPv4(p)) {
83 const IPV4Hdr *ip4h = PacketGetIPv4(p);
84 ptemplate2 = IPV4_GET_RAW_IPTTL(ip4h);
85 } else if (PacketIsIPv6(p)) {
86 const IPV6Hdr *ip6h = PacketGetIPv6(p);
87 ptemplate2 = IPV6_GET_RAW_HLIM(ip6h);
88 } else {
89 SCLogDebug("Packet is of not IPv4 or IPv6");
90 return 0;
91 }
92
93 const DetectU8Data *template2d = (const DetectU8Data *)ctx;
94 return DetectU8Match(ptemplate2, template2d);
95}
96
97/**
98 * \brief this function is used to add the parsed template2 data into the current signature
99 *
100 * \param de_ctx pointer to the Detection Engine Context
101 * \param s pointer to the Current Signature
102 * \param template2str pointer to the user provided template2 options
103 *
104 * \retval 0 on Success
105 * \retval -1 on Failure
106 */
107static int DetectTemplate2Setup (DetectEngineCtx *de_ctx, Signature *s, const char *template2str)
108{
109 DetectU8Data *template2d = DetectU8Parse(template2str);
110 if (template2d == NULL)
111 return -1;
112
114 DETECT_SM_LIST_MATCH) == NULL) {
115 DetectTemplate2Free(de_ctx, template2d);
116 return -1;
117 }
119
120 return 0;
121}
122
123/**
124 * \brief this function will free memory associated with DetectU8Data
125 *
126 * \param ptr pointer to DetectU8Data
127 */
129{
130 SCDetectU8Free(ptr);
131}
132
133/* prefilter code */
134
135static void
136PrefilterPacketTemplate2Match(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
137{
139
140 uint8_t ptemplate2;
141/* TODO update */
142 if (PacketIsIPv4(p)) {
143 const IPV4Hdr *ip4h = PacketGetIPv4(p);
144 ptemplate2 = IPV4_GET_RAW_IPTTL(ip4h);
145 } else if (PacketIsIPv6(p)) {
146 const IPV6Hdr *ip6h = PacketGetIPv6(p);
147 ptemplate2 = IPV6_GET_RAW_HLIM(ip6h);
148 } else {
149 SCLogDebug("Packet is of not IPv4 or IPv6");
150 return;
151 }
152
153 /* during setup Suricata will automatically see if there is another
154 * check that can be added: alproto, sport or dport */
155 const PrefilterPacketHeaderCtx *ctx = pectx;
156 if (!PrefilterPacketHeaderExtraMatch(ctx, p))
157 return;
158
159 DetectU8Data du8;
160 du8.mode = ctx->v1.u8[0];
161 du8.arg1 = ctx->v1.u8[1];
162 du8.arg2 = ctx->v1.u8[2];
163 /* if we match, add all the sigs that use this prefilter. This means
164 * that these will be inspected further */
165 if (DetectU8Match(ptemplate2, &du8)) {
166 SCLogDebug("packet matches template2/hl %u", ptemplate2);
167 PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
168 }
169}
170
171static int PrefilterSetupTemplate2(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
172{
174 PrefilterPacketU8Set, PrefilterPacketU8Compare, PrefilterPacketTemplate2Match);
175}
176
177static bool PrefilterTemplate2IsPrefilterable(const Signature *s)
178{
179 const SigMatch *sm;
180 for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
181 switch (sm->type) {
182 case DETECT_TEMPLATE2:
183 return true;
184 }
185 }
186 return false;
187}
#define IPV4_GET_RAW_IPTTL(ip4h)
#define IPV6_GET_RAW_HLIM(ip6h)
Definition decode-ipv6.h:67
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition decode.h:1321
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))
DetectUintData_u8 * DetectU8Parse(const char *u8str)
This function is used to parse u8 options passed via some u8 keyword.
void PrefilterPacketU8Set(PrefilterPacketHeaderValue *v, void *smctx)
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
bool PrefilterPacketU8Compare(PrefilterPacketHeaderValue v, void *smctx)
DetectUintData_u8 DetectU8Data
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
void DetectTemplate2Free(DetectEngineCtx *, void *)
this function will free memory associated with DetectU8Data
void DetectTemplate2Register(void)
Registration function for template2: keyword.
void DetectTemplate2RegisterTests(void)
#define SIG_FLAG_REQUIRE_PACKET
Definition detect.h:254
#define SIG_MASK_REQUIRE_REAL_PKT
Definition detect.h:316
@ DETECT_SM_LIST_MATCH
Definition detect.h:117
DetectEngineCtx * de_ctx
struct Thresholds ctx
main detection engine ctx
Definition detect.h:932
PrefilterRuleStore pmq
Definition detect.h:1349
Container for matching data for a signature group.
Definition detect.h:1629
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
a single match condition for a signature
Definition detect.h:356
uint16_t type
Definition detect.h:357
struct SigMatch_ * next
Definition detect.h:360
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition detect.h:1444
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * name
Definition detect.h:1459
bool(* SupportsPrefilter)(const Signature *s)
Definition detect.h:1443
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition detect.h:642
Signature container.
Definition detect.h:668
uint32_t flags
Definition detect.h:669
SignatureInitData * init_data
Definition detect.h:747
#define SCLogDebug(...)
Definition util-debug.h:275
#define DEBUG_VALIDATE_BUG_ON(exp)