suricata
detect-app-layer-event.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 Anoop Saldanha <anoopsaldanha@gmail.com>
22 */
23
24#include "suricata-common.h"
25#include "threads.h"
26#include "decode.h"
27
28#include "app-layer.h"
29#include "app-layer-protos.h"
30#include "app-layer-parser.h"
31#include "app-layer-events.h"
32#include "app-layer-smtp.h"
33#include "detect.h"
34#include "detect-parse.h"
35#include "detect-engine.h"
36#include "detect-engine-state.h"
37#include "detect-engine-build.h"
39
40#include "flow.h"
41#include "flow-var.h"
42#include "flow-util.h"
43
44#include "decode-events.h"
45#include "util-byte.h"
46#include "util-debug.h"
47#include "util-enum.h"
48#include "util-profiling.h"
49#include "util-unittest.h"
51#include "stream-tcp-util.h"
52
53#define MAX_ALPROTO_NAME 50
54
59
60static int DetectAppLayerEventPktMatch(DetectEngineThreadCtx *det_ctx,
61 Packet *p, const Signature *s, const SigMatchCtx *ctx);
62static int DetectAppLayerEventSetup(DetectEngineCtx *, Signature *, const char *);
63static void DetectAppLayerEventFree(DetectEngineCtx *, void *);
64static uint8_t DetectEngineAptEventInspect(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
65 const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
66 uint8_t flags, void *alstate, void *tx, uint64_t tx_id);
67static int g_applayer_events_list_id = 0;
68
69/**
70 * \brief Registers the keyword handlers for the "app-layer-event" keyword.
71 */
73{
74 sigmatch_table[DETECT_APP_LAYER_EVENT].name = "app-layer-event";
76 "match on events generated by the App Layer Parsers and the protocol detection engine";
77 sigmatch_table[DETECT_APP_LAYER_EVENT].url = "/rules/app-layer.html#app-layer-event";
78 sigmatch_table[DETECT_APP_LAYER_EVENT].Match = DetectAppLayerEventPktMatch;
79 sigmatch_table[DETECT_APP_LAYER_EVENT].Setup = DetectAppLayerEventSetup;
80 sigmatch_table[DETECT_APP_LAYER_EVENT].Free = DetectAppLayerEventFree;
81
83 DetectEngineAptEventInspect, NULL);
85 DetectEngineAptEventInspect, NULL);
86
87 g_applayer_events_list_id = DetectBufferTypeGetByName("app-layer-events");
88}
89
90static uint8_t DetectEngineAptEventInspect(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
91 const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
92 uint8_t flags, void *alstate, void *tx, uint64_t tx_id)
93{
94 int r = 0;
95 const AppProto alproto = f->alproto;
96 const AppLayerDecoderEvents *decoder_events =
97 AppLayerParserGetEventsByTx(f->proto, alproto, tx);
98 if (decoder_events == NULL) {
99 goto end;
100 }
101 const SigMatchData *smd = engine->smd;
102 while (1) {
103 const DetectAppLayerEventData *aled = (const DetectAppLayerEventData *)smd->ctx;
105
106 if (AppLayerDecoderEventsIsEventSet(decoder_events, aled->event_id)) {
107 KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
108
109 if (smd->is_last)
110 break;
111 smd++;
112 continue;
113 }
114
115 KEYWORD_PROFILING_END(det_ctx, smd->type, 0);
116 goto end;
117 }
118
119 r = 1;
120
121 end:
122 if (r == 1) {
124 } else {
125 if (AppLayerParserGetStateProgress(f->proto, alproto, tx, flags) ==
127 {
129 } else {
131 }
132 }
133}
134
135
136static int DetectAppLayerEventPktMatch(DetectEngineThreadCtx *det_ctx,
137 Packet *p, const Signature *s, const SigMatchCtx *ctx)
138{
140
141 return AppLayerDecoderEventsIsEventSet(p->app_layer_events,
142 aled->event_id);
143}
144
145static DetectAppLayerEventData *DetectAppLayerEventParsePkt(const char *arg,
146 AppLayerEventType *event_type)
147{
148 uint8_t event_id = 0;
149 if (AppLayerGetPktEventInfo(arg, &event_id) != 0) {
150 SCLogError("app-layer-event keyword "
151 "supplied with packet based event - \"%s\" that isn't "
152 "supported yet.",
153 arg);
154 return NULL;
155 }
156
158 if (unlikely(aled == NULL))
159 return NULL;
160 aled->event_id = (uint8_t)event_id;
161 *event_type = APP_LAYER_EVENT_TYPE_PACKET;
162
163 return aled;
164}
165
166static bool OutdatedEvent(const char *raw)
167{
168 if (strcmp(raw, "tls.certificate_missing_element") == 0 ||
169 strcmp(raw, "tls.certificate_unknown_element") == 0 ||
170 strcmp(raw, "tls.certificate_invalid_string") == 0) {
171 return true;
172 }
173 return false;
174}
175
176static AppProto AppLayerEventGetProtoByName(char *alproto_name)
177{
178 AppProto alproto = AppLayerGetProtoByName(alproto_name);
179 if (alproto == ALPROTO_HTTP) {
180 // app-layer events http refer to http1
181 alproto = ALPROTO_HTTP1;
182 }
183 return alproto;
184}
185
186static int DetectAppLayerEventSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
187{
188 if (arg == NULL) {
189 SCLogError("app-layer-event keyword supplied "
190 "with no arguments. This keyword needs an argument.");
191 return -1;
192 }
193
194 while (*arg != '\0' && isspace((unsigned char)*arg))
195 arg++;
196
197 AppLayerEventType event_type;
198 DetectAppLayerEventData *data = NULL;
199
200 if (strchr(arg, '.') == NULL) {
201 data = DetectAppLayerEventParsePkt(arg, &event_type);
202 if (data == NULL)
203 return -1;
204 } else {
205 SCLogDebug("parsing %s", arg);
206 char alproto_name[MAX_ALPROTO_NAME];
207 bool needs_detctx = false;
208
209 const char *p_idx = strchr(arg, '.');
210 if (strlen(arg) > MAX_ALPROTO_NAME) {
211 SCLogError("app-layer-event keyword is too long or malformed");
212 return -1;
213 }
214 const char *event_name = p_idx + 1; // skip .
215 /* + 1 for trailing \0 */
216 strlcpy(alproto_name, arg, p_idx - arg + 1);
217
218 const AppProto alproto = AppLayerEventGetProtoByName(alproto_name);
219 if (alproto == ALPROTO_UNKNOWN) {
220 if (!strcmp(alproto_name, "file")) {
221 needs_detctx = true;
222 } else {
223 SCLogError("app-layer-event keyword "
224 "supplied with unknown protocol \"%s\"",
225 alproto_name);
226 return -1;
227 }
228 }
229 if (OutdatedEvent(arg)) {
231 SCLogError("app-layer-event keyword no longer supports event \"%s\"", arg);
232 return -1;
233 } else {
234 SCLogWarning("app-layer-event keyword no longer supports event \"%s\"", arg);
235 return -3;
236 }
237 }
238
239 uint8_t ipproto = 0;
240 if (s->proto.proto[IPPROTO_TCP / 8] & 1 << (IPPROTO_TCP % 8)) {
241 ipproto = IPPROTO_TCP;
242 } else if (s->proto.proto[IPPROTO_UDP / 8] & 1 << (IPPROTO_UDP % 8)) {
243 ipproto = IPPROTO_UDP;
244 } else {
245 SCLogError("protocol %s is disabled", alproto_name);
246 return -1;
247 }
248
249 int r;
250 uint8_t event_id = 0;
251 if (!needs_detctx) {
252 r = AppLayerParserGetEventInfo(ipproto, alproto, event_name, &event_id, &event_type);
253 } else {
254 r = DetectEngineGetEventInfo(event_name, &event_id, &event_type);
255 }
256 if (r < 0) {
258 SCLogError("app-layer-event keyword's "
259 "protocol \"%s\" doesn't have event \"%s\" registered",
260 alproto_name, event_name);
261 return -1;
262 } else {
263 SCLogWarning("app-layer-event keyword's "
264 "protocol \"%s\" doesn't have event \"%s\" registered",
265 alproto_name, event_name);
266 return -3;
267 }
268 }
269 data = SCCalloc(1, sizeof(*data));
270 if (unlikely(data == NULL))
271 return -1;
272 data->alproto = alproto;
273 data->event_id = (uint8_t)event_id;
274 }
275 SCLogDebug("data->event_id %u", data->event_id);
276
277 if (event_type == APP_LAYER_EVENT_TYPE_PACKET) {
279 DETECT_SM_LIST_MATCH) == NULL) {
280 goto error;
281 }
282 } else {
283 if (SCDetectSignatureSetAppProto(s, data->alproto) != 0)
284 goto error;
285
287 g_applayer_events_list_id) == NULL) {
288 goto error;
289 }
291 }
292
293 return 0;
294
295error:
296 if (data) {
297 DetectAppLayerEventFree(de_ctx, data);
298 }
299 return -1;
300}
301
302static void DetectAppLayerEventFree(DetectEngineCtx *de_ctx, void *ptr)
303{
304 SCFree(ptr);
305}
int DetectEngineGetEventInfo(const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
int AppLayerGetPktEventInfo(const char *event_name, uint8_t *event_id)
int AppLayerParserGetEventInfo(uint8_t ipproto, AppProto alproto, const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
int AppLayerParserGetStateProgressCompletionStatus(AppProto alproto, uint8_t direction)
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
AppLayerDecoderEvents * AppLayerParserGetEventsByTx(uint8_t ipproto, AppProto alproto, void *tx)
enum AppLayerEventType AppLayerEventType
uint16_t AppProto
@ ALPROTO_HTTP
@ ALPROTO_UNKNOWN
@ ALPROTO_HTTP1
AppProto AppLayerGetProtoByName(const char *alproto_name)
Given a protocol string, returns the corresponding internal protocol id.
Definition app-layer.c:1002
uint8_t flags
Definition decode-gre.h:0
#define MAX_ALPROTO_NAME
struct DetectAppLayerEventData_ DetectAppLayerEventData
void DetectAppLayerEventRegister(void)
Registers the keyword handlers for the "app-layer-event" keyword.
@ DETECT_APP_LAYER_EVENT
Data structures and function prototypes for keeping state for the detection engine.
#define DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
bool SigMatchStrictEnabled(const enum DetectKeywordId id)
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
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
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIG_FLAG_TOSERVER
Definition detect.h:271
@ DETECT_SM_LIST_MATCH
Definition detect.h:117
#define SIG_FLAG_APPLAYER
Definition detect.h:249
DetectEngineCtx * de_ctx
struct Thresholds ctx
Data structure to store app layer decoder events.
main detection engine ctx
Definition detect.h:932
uint8_t proto[256/8]
Flow data structure.
Definition flow.h:356
uint8_t proto
Definition flow.h:378
AppProto alproto
application level protocol
Definition flow.h:450
AppLayerDecoderEvents * app_layer_events
Definition decode.h:632
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
Data needed for Match()
Definition detect.h:365
bool is_last
Definition detect.h:367
SigMatchCtx * ctx
Definition detect.h:368
uint16_t type
Definition detect.h:366
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
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
uint32_t flags
Definition detect.h:669
DetectProto proto
Definition detect.h:687
size_t strlcpy(char *dst, const char *src, size_t siz)
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition util-debug.h:255
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define unlikely(expr)
#define KEYWORD_PROFILING_END(ctx, type, m)
#define KEYWORD_PROFILING_START