suricata
detect-within.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2023 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 Victor Julien <victor@inliniac.net>
22  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
23  *
24  * Implements the within keyword
25  */
26 
27 #include "suricata-common.h"
28 
29 #include "decode.h"
30 
31 #include "detect.h"
32 #include "detect-engine.h"
33 #include "detect-parse.h"
34 #include "detect-content.h"
35 #include "detect-uricontent.h"
36 #include "detect-byte.h"
37 #include "app-layer.h"
38 
39 #include "flow-var.h"
40 
41 #include "util-byte.h"
42 #include "util-debug.h"
43 #include "detect-pcre.h"
44 #include "detect-within.h"
45 
46 static int DetectWithinSetup(DetectEngineCtx *, Signature *, const char *);
47 #ifdef UNITTESTS
48 static void DetectWithinRegisterTests(void);
49 #endif
50 
52 {
53  sigmatch_table[DETECT_WITHIN].name = "within";
54  sigmatch_table[DETECT_WITHIN].desc = "indicate that this content match has to be within a certain distance of the previous content keyword match";
55  sigmatch_table[DETECT_WITHIN].url = "/rules/payload-keywords.html#within";
57  sigmatch_table[DETECT_WITHIN].Setup = DetectWithinSetup;
58 #ifdef UNITTESTS
59  sigmatch_table[DETECT_WITHIN].RegisterTests = DetectWithinRegisterTests;
60 #endif
61 }
62 
63 /** \brief Setup within pattern (content/uricontent) modifier.
64  *
65  * \todo apply to uricontent
66  *
67  * \retval 0 ok
68  * \retval -1 error, sig needs to be invalidated
69  */
70 static int DetectWithinSetup(DetectEngineCtx *de_ctx, Signature *s, const char *withinstr)
71 {
72  const char *str = withinstr;
73 
74  /* retrieve the sm to apply the within against */
76  if (pm == NULL) {
77  SCLogError("within needs preceding content option");
78  return -1;
79  }
80 
81  /* verify other conditions */
83  if (cd->flags & DETECT_CONTENT_WITHIN) {
84  SCLogError("can't use multiple withins for the same content.");
85  return -1;
86  }
87  if ((cd->flags & DETECT_CONTENT_DEPTH) || (cd->flags & DETECT_CONTENT_OFFSET)) {
88  SCLogError("can't use a relative "
89  "keyword like within/distance with a absolute "
90  "relative keyword like depth/offset for the same "
91  "content.");
92  return -1;
93  }
95  SCLogError("can't have a relative "
96  "negated keyword set along with a fast_pattern");
97  return -1;
98  }
100  SCLogError("can't have a relative "
101  "keyword set along with a fast_pattern:only;");
102  return -1;
103  }
104  if (str[0] != '-' && isalpha((unsigned char)str[0])) {
105  DetectByteIndexType index;
106  if (!DetectByteRetrieveSMVar(str, s, -1, &index)) {
107  SCLogError("unknown byte_ keyword var "
108  "seen in within - %s",
109  str);
110  return -1;
111  }
112  cd->within = index;
114  } else {
116  DETECT_CONTENT_VALUE_MAX) < 0)) {
117  SCLogError("invalid value for within: %s", str);
118  return -1;
119  }
120 
121  if (cd->within < (int32_t)cd->content_len) {
122  SCLogError("within argument \"%" PRIi32 "\" is "
123  "less than the content length \"%" PRIu32 "\" which is invalid, since "
124  "this will never match. Invalidating signature",
125  cd->within, cd->content_len);
126  return -1;
127  }
128  }
130 
131  /* these are the only ones against which we set a flag. We have other
132  * relative keywords like byttest, isdataat, bytejump, but we don't
133  * set a flag against them */
134  SigMatch *prev_pm = DetectGetLastSMByListPtr(s, pm->prev,
136  if (prev_pm == NULL) {
137  return 0;
138  }
139  if (prev_pm->type == DETECT_CONTENT) {
140  DetectContentData *prev_cd = (DetectContentData *)prev_pm->ctx;
141  if (prev_cd->flags & DETECT_CONTENT_FAST_PATTERN_ONLY) {
142  SCLogError("previous keyword "
143  "has a fast_pattern:only; set. Can't "
144  "have relative keywords around a fast_pattern "
145  "only content");
146  return -1;
147  }
148  prev_cd->flags |= DETECT_CONTENT_WITHIN_NEXT;
149  } else if (prev_pm->type == DETECT_PCRE) {
150  DetectPcreData *pd = (DetectPcreData *)prev_pm->ctx;
152  }
153  return 0;
154 }
155 
156 /***********************************Unittests**********************************/
157 
158 #ifdef UNITTESTS
159 #include "util-unittest-helper.h"
160  /**
161  * \test DetectWithinTestPacket01 is a test to check matches of
162  * within, if the previous keyword is pcre (bug 145)
163  */
164 static int DetectWithinTestPacket01 (void)
165 {
166  uint8_t *buf = (uint8_t *)"GET /AllWorkAndNoPlayMakesWillADullBoy HTTP/1.0"
167  "User-Agent: Wget/1.11.4"
168  "Accept: */*"
169  "Host: www.google.com"
170  "Connection: Keep-Alive"
171  "Date: Mon, 04 Jan 2010 17:29:39 GMT";
172  uint16_t buflen = strlen((char *)buf);
173 
174  Packet *p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
175  FAIL_IF_NULL(p);
176 
177  char sig[] = "alert tcp any any -> any any (msg:\"pcre with within "
178  "modifier\"; pcre:\"/AllWorkAndNoPlayMakesWillADullBoy/\";"
179  " content:\"HTTP\"; within:5; sid:49; rev:1;)";
180  int result = UTHPacketMatchSig(p, sig);
181  FAIL_IF_NOT(result == 1);
182 
183  UTHFreePacket(p);
184  PASS;
185 }
186 
187 
188 static int DetectWithinTestPacket02 (void)
189 {
190  uint8_t *buf = (uint8_t *)"Zero Five Ten Fourteen";
191  uint16_t buflen = strlen((char *)buf);
192 
193  Packet *p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
194  FAIL_IF_NULL(p);
195 
196  char sig[] = "alert tcp any any -> any any (msg:\"pcre with within "
197  "modifier\"; content:\"Five\"; content:\"Ten\"; within:3; distance:1; sid:1;)";
198 
199  int result = UTHPacketMatchSig(p, sig);
200  FAIL_IF_NOT(result == 1);
201 
202  UTHFreePacket(p);
203  PASS;
204 }
205 
206 static int DetectWithinTestVarSetup(void)
207 {
208  char sig[] = "alert tcp any any -> any any ( "
209  "msg:\"test rule\"; "
210  "content:\"abc\"; "
211  "http_client_body; "
212  "byte_extract:2,0,somevar,relative; "
213  "content:\"def\"; "
214  "within:somevar; "
215  "http_client_body; "
216  "sid:4; rev:1;)";
217 
220 
222  FAIL_IF_NULL(s);
223 
225  PASS;
226 }
227 
228 void DetectWithinRegisterTests(void)
229 {
230  UtRegisterTest("DetectWithinTestPacket01", DetectWithinTestPacket01);
231  UtRegisterTest("DetectWithinTestPacket02", DetectWithinTestPacket02);
232  UtRegisterTest("DetectWithinTestVarSetup", DetectWithinTestVarSetup);
233 }
234 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
SigMatch_::prev
struct SigMatch_ * prev
Definition: detect.h:365
detect-content.h
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
SigTableElmt_::name
const char * name
Definition: detect.h:1542
DetectContentData_::within
int32_t within
Definition: detect-content.h:109
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:78
SCDetectGetLastSMFromLists
SigMatch * SCDetectGetLastSMFromLists(const Signature *s,...)
Returns the sm with the largest index (added latest) from the lists passed to us.
Definition: detect-parse.c:600
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2912
UTHPacketMatchSig
int UTHPacketMatchSig(Packet *p, const char *sig)
Definition: util-unittest-helper.c:833
DetectGetLastSMByListPtr
SigMatch * DetectGetLastSMByListPtr(const Signature *s, SigMatch *sm_list,...)
Returns the sm with the largest index (added last) from the list passed to us as a pointer.
Definition: detect-parse.c:662
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
DETECT_CONTENT_VALUE_MAX
#define DETECT_CONTENT_VALUE_MAX
Definition: detect-content.h:89
DetectContentData_
Definition: detect-content.h:93
DetectPcreData_::flags
uint16_t flags
Definition: detect-pcre.h:52
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3971
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
detect-pcre.h
DetectByteIndexType
uint8_t DetectByteIndexType
Definition: detect-byte.h:28
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
decode.h
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
StringParseI32RangeCheck
int StringParseI32RangeCheck(int32_t *res, int base, size_t len, const char *str, int32_t min, int32_t max)
Definition: util-byte.c:680
DetectWithinRegister
void DetectWithinRegister(void)
Definition: detect-within.c:51
DETECT_CONTENT_DEPTH
#define DETECT_CONTENT_DEPTH
Definition: detect-content.h:33
detect.h
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:363
Packet_
Definition: decode.h:516
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1504
detect-byte.h
DETECT_PCRE
@ DETECT_PCRE
Definition: detect-engine-register.h:80
DETECT_CONTENT_WITHIN_VAR
#define DETECT_CONTENT_WITHIN_VAR
Definition: detect-content.h:48
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:361
DETECT_CONTENT_WITHIN_NEXT
#define DETECT_CONTENT_WITHIN_NEXT
Definition: detect-content.h:57
str
#define str(s)
Definition: suricata-common.h:313
detect-within.h
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:470
DETECT_CONTENT_OFFSET
#define DETECT_CONTENT_OFFSET
Definition: detect-content.h:32
DETECT_CONTENT_FAST_PATTERN_ONLY
#define DETECT_CONTENT_FAST_PATTERN_ONLY
Definition: detect-content.h:35
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
SigMatch_
a single match condition for a signature
Definition: detect.h:360
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2873
DETECT_PCRE_RELATIVE_NEXT
#define DETECT_PCRE_RELATIVE_NEXT
Definition: detect-pcre.h:34
DetectPcreData_
Definition: detect-pcre.h:48
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
detect-uricontent.h
DetectByteRetrieveSMVar
bool DetectByteRetrieveSMVar(const char *arg, const Signature *s, int sm_list, DetectByteIndexType *index)
Used to retrieve args from BM.
Definition: detect-byte.c:41
DETECT_CONTENT_FAST_PATTERN
#define DETECT_CONTENT_FAST_PATTERN
Definition: detect-content.h:34
DETECT_WITHIN
@ DETECT_WITHIN
Definition: detect-engine-register.h:85
flow-var.h
DETECT_CONTENT_WITHIN
#define DETECT_CONTENT_WITHIN
Definition: detect-content.h:31
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531
app-layer.h