suricata
detect-distance.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 distance keyword
25 */
26
27#include "suricata-common.h"
28
29#include "decode.h"
30
31#include "detect.h"
32#include "detect-parse.h"
33#include "detect-engine.h"
34#include "app-layer.h"
35
36#include "detect-content.h"
37#include "detect-uricontent.h"
38#include "detect-pcre.h"
39#include "detect-byte.h"
40#include "detect-byte-extract.h"
41#include "detect-distance.h"
42
43#include "flow-var.h"
44
45#include "util-byte.h"
46#include "util-debug.h"
47#include "util-unittest.h"
48#include "detect-bytejump.h"
50
51static int DetectDistanceSetup(DetectEngineCtx *, Signature *, const char *);
52#ifdef UNITTESTS
53static void DetectDistanceRegisterTests(void);
54#endif
55
57{
59 sigmatch_table[DETECT_DISTANCE].desc = "indicates a relation between this content keyword and the content preceding it";
60 sigmatch_table[DETECT_DISTANCE].url = "/rules/payload-keywords.html#distance";
62 sigmatch_table[DETECT_DISTANCE].Setup = DetectDistanceSetup;
64#ifdef UNITTESTS
65 sigmatch_table[DETECT_DISTANCE].RegisterTests = DetectDistanceRegisterTests;
66#endif
67}
68
69static int DetectDistanceSetup (DetectEngineCtx *de_ctx, Signature *s,
70 const char *distancestr)
71{
72 const char *str = distancestr;
73
74 /* retrieve the sm to apply the distance against */
76 if (pm == NULL) {
77 SCLogError("distance needs "
78 "preceding content, uricontent option, http_client_body, "
79 "http_server_body, http_header option, http_raw_header option, "
80 "http_method option, http_cookie, http_raw_uri, "
81 "http_stat_msg, http_stat_code, http_user_agent or "
82 "file_data/dce_stub_data sticky buffer option");
83 return -1;
84 }
85
86 /* verify other conditions */
88 if (cd->flags & DETECT_CONTENT_DISTANCE) {
89 SCLogError("can't use multiple distances for the same content.");
90 return -1;
91 }
92 if ((cd->flags & DETECT_CONTENT_DEPTH) || (cd->flags & DETECT_CONTENT_OFFSET)) {
93 SCLogError("can't use a relative "
94 "keyword like within/distance with a absolute "
95 "relative keyword like depth/offset for the same "
96 "content.");
97 return -1;
98 }
99 if (cd->flags & DETECT_CONTENT_NEGATED && cd->flags & DETECT_CONTENT_FAST_PATTERN) {
100 SCLogError("can't have a relative "
101 "negated keyword set along with a fast_pattern");
102 return -1;
103 }
104 if (cd->flags & DETECT_CONTENT_FAST_PATTERN_ONLY) {
105 SCLogError("can't have a relative "
106 "keyword set along with a fast_pattern:only;");
107 return -1;
108 }
109 if (str[0] != '-' && isalpha((unsigned char)str[0])) {
111 if (!DetectByteRetrieveSMVar(str, s, -1, &index)) {
112 SCLogError("unknown byte_ keyword var "
113 "seen in distance - %s",
114 str);
115 return -1;
116 }
117 cd->distance = index;
118 cd->flags |= DETECT_CONTENT_DISTANCE_VAR;
119 } else {
120 if ((StringParseI32RangeCheck(&cd->distance, 0, 0, str, -DETECT_CONTENT_VALUE_MAX,
122 SCLogError("invalid value for distance: %s", str);
123 return -1;
124 }
125 }
126 cd->flags |= DETECT_CONTENT_DISTANCE;
127
128 SigMatch *prev_pm = DetectGetLastSMByListPtr(s, pm->prev,
130 if (prev_pm == NULL) {
131 return 0;
132 }
133
134 if (prev_pm->type == DETECT_CONTENT) {
135 DetectContentData *prev_cd = (DetectContentData *)prev_pm->ctx;
136 if (prev_cd->flags & DETECT_CONTENT_FAST_PATTERN_ONLY) {
137 SCLogError("previous keyword "
138 "has a fast_pattern:only; set. Can't "
139 "have relative keywords around a fast_pattern "
140 "only content");
141 return -1;
142 }
143 if ((cd->flags & DETECT_CONTENT_NEGATED) == 0) {
144 prev_cd->flags |= DETECT_CONTENT_DISTANCE_NEXT;
145 } else {
146 prev_cd->flags |= DETECT_CONTENT_RELATIVE_NEXT;
147 }
148 } else if (prev_pm->type == DETECT_PCRE) {
149 DetectPcreData *pd = (DetectPcreData *)prev_pm->ctx;
150 pd->flags |= DETECT_PCRE_RELATIVE_NEXT;
151 }
152
153 return 0;
154}
155
156#ifdef UNITTESTS
157
158static int DetectDistanceTest01(void)
159{
163
165 "alert tcp any any -> any any (content:\"|AA BB|\"; content:\"|CC DD EE FF 00 11 22 33 "
166 "44 55 66 77 88 99 AA BB CC DD EE|\"; distance: 4; within: 19; sid:1; rev:1;)");
167 FAIL_IF_NULL(s);
168
170 FAIL_IF_NULL(sm);
171
172 sm = sm->next;
173 FAIL_IF_NULL(sm);
174
176 FAIL_IF_NULL(co);
177
178 FAIL_IF_NOT(co->distance = 4);
179
180 /* within needs to be 23: distance + content_len as Snort auto fixes this */
181 FAIL_IF_NOT(co->within = 19);
182
184
185 PASS;
186}
187
188/**
189 * \test DetectDistanceTestPacket01 is a test to check matches of
190 * distance works, if the previous keyword is byte_jump and content
191 * (bug 163)
192 */
193static int DetectDistanceTestPacket01 (void)
194{
195 uint8_t buf[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
196 uint16_t buflen = sizeof(buf);
197 Packet *p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
198
199 FAIL_IF_NULL(p);
200 char sig[] = "alert tcp any any -> any any (msg:\"suricata test\"; "
201 "byte_jump:1,2; content:\"|00|\"; "
202 "within:1; distance:2; sid:98711212; rev:1;)";
203
206
207 UTHFreePacket(p);
208
209 PASS;
210}
211
212static void DetectDistanceRegisterTests(void)
213{
214 UtRegisterTest("DetectDistanceTest01 -- distance / within mix",
215 DetectDistanceTest01);
216 UtRegisterTest("DetectDistanceTestPacket01", DetectDistanceTestPacket01);
217}
218#endif /* UNITTESTS */
bool DetectByteRetrieveSMVar(const char *arg, const Signature *s, int sm_list, DetectByteIndexType *index)
Used to retrieve args from BM.
Definition detect-byte.c:41
uint8_t DetectByteIndexType
Definition detect-byte.h:28
#define DETECT_CONTENT_DEPTH
#define DETECT_CONTENT_VALUE_MAX
#define DETECT_CONTENT_DISTANCE_NEXT
#define DETECT_CONTENT_RELATIVE_NEXT
#define DETECT_CONTENT_FAST_PATTERN_ONLY
#define DETECT_CONTENT_FAST_PATTERN
#define DETECT_CONTENT_DISTANCE
#define DETECT_CONTENT_OFFSET
#define DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_DISTANCE_VAR
void DetectDistanceRegister(void)
@ DETECT_DISTANCE
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
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.
SigMatch * DetectGetLastSMFromLists(const Signature *s,...)
Returns the sm with the largest index (added latest) from the lists passed to us.
SigTableElmt * sigmatch_table
#define DETECT_PCRE_RELATIVE_NEXT
Definition detect-pcre.h:34
#define DE_QUIET
Definition detect.h:330
@ DETECT_SM_LIST_PMATCH
Definition detect.h:119
#define FLOW_PKT_ESTABLISHED
Definition flow.h:235
#define FLOW_PKT_TOCLIENT
Definition flow.h:234
DetectEngineCtx * de_ctx
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
#define PASS
Pass the test.
main detection engine ctx
Definition detect.h:932
uint8_t flags
Definition detect.h:934
Signature * sig_list
Definition detect.h:941
uint8_t flowflags
Definition decode.h:532
a single match condition for a signature
Definition detect.h:356
uint16_t type
Definition detect.h:357
struct SigMatch_ * prev
Definition detect.h:361
struct SigMatch_ * next
Definition detect.h:360
SigMatchCtx * ctx
Definition detect.h:359
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * name
Definition detect.h:1459
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition detect.h:642
Signature container.
Definition detect.h:668
SignatureInitData * init_data
Definition detect.h:747
#define str(s)
int StringParseI32RangeCheck(int32_t *res, int base, size_t len, const char *str, int32_t min, int32_t max)
Definition util-byte.c:716
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
int UTHPacketMatchSig(Packet *p, const char *sig)
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.
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.